diff --git a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js index 5519deb..7329110 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js @@ -7,6 +7,7 @@ const { getModifiedColumnTypeScripts } = require('./columnHelpers/typeHelper'); const { getModifyCollectionNameScript } = require('./entityHelpers/nameHelper'); const { getModifyPkConstraintsScriptDtos } = require('./entityHelpers/primaryKeyHelper'); const { getCompMod, checkCompModEqual, setEntityKeys } = require('./common'); +const { getModifyCollectionOptionsScript } = require('./entityHelpers/optionsHelper'); module.exports = (app, options) => { const _ = app.require('lodash'); @@ -84,7 +85,7 @@ module.exports = (app, options) => { }; const modifyEntityNameScript = getModifyCollectionNameScript({ app, collection, dbData }); - const modifyTableOptionsScript = getModifyTableOptions({ jsonSchema, tableData }); + const modifyTableOptionsScript = getModifyCollectionOptionsScript({ app, jsonSchema, tableData }); const modifyColumnNamesScript = getModifyColumnNameScript({ app, collection, tableData }); const modifyColumnScripts = getModifyColumnScripts({ tableData, dbData, collection }); const modifyPkScripts = getModifyPkConstraintsScriptDtos({ app, collection, tableData }); @@ -100,35 +101,6 @@ module.exports = (app, options) => { .join('\n\n'); }; - const getModifyTableOptions = ({ jsonSchema, tableData }) => { - const compMod = getCompMod(jsonSchema); - const optionsProperties = [ - 'description', - 'partitioning', - 'partitioningFilterRequired', - 'expiration', - 'tableType', - 'customerEncryptionKey', - 'encryption', - 'labels', - 'title', - ]; - - const isAnyOptionChanged = _.some(optionsProperties, property => !checkCompModEqual(compMod[property])); - - if (!isAnyOptionChanged) { - return ''; - } - - const hydratedTable = ddlProvider.hydrateTable({ - entityData: [jsonSchema], - tableData, - jsonSchema, - }); - - return ddlProvider.alterTableOptions(hydratedTable); - }; - const getAddColumnScript = modelData => collection => { const collectionSchema = { ...collection, ..._.omit(collection?.role, 'properties') }; const tableName = getName(collectionSchema); diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/optionsHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/optionsHelper.js new file mode 100644 index 0000000..7f976f5 --- /dev/null +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/optionsHelper.js @@ -0,0 +1,59 @@ +const _ = require('lodash'); +const { wrapByBackticks, getFullName, escapeQuotes, getTimestamp } = require('../../../helpers/utils'); +const templates = require('../../../configs/templates'); + +const columnOptions = { + 'description': 'description', + 'partitioningFilterRequired': 'require_partition_filter', + 'expiration': 'expiration_timestamp', + 'customerEncryptionKey': 'kms_key_name', + 'labels': 'labels', +}; + +const getModifyCollectionOptionsScript = ({ jsonSchema, tableData, app }) => { + const { assignTemplates } = app.require('@hackolade/ddl-fe-utils'); + const { tab } = app.require('@hackolade/ddl-fe-utils').general; + const { getLabels } = require('../../../helpers/general')(app); + const optionsToUpdate = []; + + Object.entries(columnOptions).forEach(([customOptionName, columnOptionName]) => { + const { new: newOptionValue, old: oldOptionValue } = jsonSchema.compMod[customOptionName] || {}; + + if (!_.isEqual(newOptionValue, oldOptionValue)) { + switch (customOptionName) { + case 'description': { + const value = newOptionValue ? `"${escapeQuotes(newOptionValue)}"` : 'NULL'; + optionsToUpdate.push(`description=${value}`); + break; + } + case 'expiration': { + const value = newOptionValue ? `TIMESTAMP "${getTimestamp(newOptionValue)}"` : 'NULL'; + optionsToUpdate.push(`${columnOptionName}=${value}`); + break; + } + case 'labels': { + const value = newOptionValue.length ? `[\n${tab(getLabels(newOptionValue))}\n]` : 'NULL'; + optionsToUpdate.push(`labels=${value}`); + break; + } + default: { + const value = newOptionValue === undefined || newOptionValue === '' ? 'NULL' : newOptionValue; + optionsToUpdate.push(`${columnOptionName}=${value}`); + } + } + } + }); + + if (!optionsToUpdate.length) { + return ''; + } + + return assignTemplates(templates.alterTableSetOptions, { + tableName: tableData.name, + options: tab(optionsToUpdate.join(',\n')), + }); +}; + +module.exports = { + getModifyCollectionOptionsScript, +}; diff --git a/forward_engineering/configs/templates.js b/forward_engineering/configs/templates.js index 46c4443..f22e711 100644 --- a/forward_engineering/configs/templates.js +++ b/forward_engineering/configs/templates.js @@ -18,7 +18,7 @@ module.exports = { dropTable: 'DROP TABLE IF EXISTS ${name};', - alterTable: 'ALTER TABLE IF EXISTS ${name} SET ${options};', + alterTableSetOptions: 'ALTER TABLE IF EXISTS ${tableName}\nSET OPTIONS (\n${options}\n);', alterColumnOptions: 'ALTER TABLE IF EXISTS ${tableName}\nALTER COLUMN IF EXISTS ${columnName}\nSET OPTIONS (\n${options}\n);', diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index 3334a0f..b21b85a 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -509,42 +509,6 @@ module.exports = (baseProvider, options, app) => { }); }, - alterTableOptions({ - name, - dbData, - description, - partitioning, - partitioningFilterRequired, - expiration, - tableType, - customerEncryptionKey, - labels, - friendlyName, - }) { - const tableName = getFullName(dbData.projectId, dbData.databaseName, name); - const isExternal = tableType === 'External'; - - const options = getTableOptions( - tab, - getLabels, - )({ - partitioningFilterRequired: isExternal ? false : partitioningFilterRequired, - customerEncryptionKey, - partitioning, - friendlyName, - description, - expiration, - labels, - }); - - return options?.trim() - ? assignTemplates(templates.alterTable, { - name: tableName, - options, - }) - : ''; - }, - addColumn({ column }, tableName, dbData) { const fullTableName = getFullName(dbData.projectId, dbData.databaseName, tableName);