Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 });
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 1 addition & 1 deletion forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);',
Expand Down
36 changes: 0 additions & 36 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down