Skip to content
Merged
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
13 changes: 7 additions & 6 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ const {
const { generateConstraint } = require('./helpers/constraintHelper');
const getFormatTypeOptions = require('./helpers/getFormatTypeOptions');
const { getStageCopyOptions } = require('./helpers/getStageCopyOptions');
const {
getTagStatement,
getTagAllowedValues,
getTagKeyValues,
prepareObjectTagsData,
isEmptyTags,
} = require('./helpers/tagHelper');

const DEFAULT_SNOWFLAKE_SEQUENCE_START = 1;
const DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT = 1;
Expand Down Expand Up @@ -99,12 +106,6 @@ module.exports = (baseProvider, options, app) => {
tab,
});

const { getTagStatement, getTagAllowedValues, getTagKeyValues, prepareObjectTagsData, isEmptyTags } =
require('./helpers/tagHelper')({
getName,
toString,
});

const getOutOfLineConstraints = (
isParentActivated,
foreignKeyConstraints = [],
Expand Down
2 changes: 1 addition & 1 deletion forward_engineering/helpers/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const getAlterCollectionsScripts = ({ collection, ddlProvider, app, scriptFormat
);
const modifiedColumnScripts = getColumnScripts(
getItems(collection, 'entities', 'modified', 'values'),
getModifyColumnScript,
getModifyColumnScript({ scriptFormat }),
);

return {
Expand Down
117 changes: 74 additions & 43 deletions forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const _ = require('lodash');
const { checkFieldPropertiesChanged, getNames, getBaseAndContainerNames } = require('./common');
const { createColumnDefinitionBySchema } = require('./createColumnDefinition');
const { commentIfDeactivated } = require('../commentHelpers/commentDeactivatedHelper');
const { getEntityName, getFullName, getName, toString } = require('../general');
const { getEntityName, getFullName, getName } = require('../general');
const { getSetTagValue, getUnsetTagValue } = require('../../helpers/tagHelper');
const assignTemplates = require('../../utils/assignTemplates');
const templates = require('../../configs/templates');
const { escapeString } = require('../../utils/escapeString');

const getAddCollectionScript =
({ ddlProvider, scriptFormat }) =>
Expand Down Expand Up @@ -96,52 +100,79 @@ const getDeleteColumnScript = collection => {
.map(([name]) => `ALTER TABLE IF EXISTS ${fullName} DROP COLUMN ${name};`);
};

const getModifyColumnScript = collection => {
const { getSetTagValue, getUnsetTagValue } = require('../../helpers/tagHelper')({ getName, toString });

const collectionSchema = {
...collection,
...(_.omit(collection?.role, 'properties') || {}),
};
const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName);
const fullName = getFullName(databaseName, getFullName(schemaName, tableName));

const renameColumnScripts = _.values(collection.properties)
.filter(jsonSchema => checkFieldPropertiesChanged(jsonSchema.compMod, ['name']))
.map(
jsonSchema =>
`ALTER TABLE IF EXISTS ${fullName} RENAME COLUMN ${jsonSchema.compMod.oldField.name} TO ${jsonSchema.compMod.newField.name};`,
);

const changeTypeScripts = _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
.map(
([name, jsonSchema]) =>
`ALTER TABLE IF EXISTS ${fullName} ALTER COLUMN ${name} SET DATA TYPE ${
jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type
};`,
);

const changeTagScripts = _.toPairs(collection.properties).reduce((result, [name, jsonSchema]) => {
const tags = jsonSchema.columnTags;
const oldTags = collection.role?.properties?.[name]?.columnTags;
const isCaseSensitive = collection.role?.isCaseSensitive;
const tagsToSet = getSetTagValue({ tags, oldTags, isCaseSensitive });
const tagsToUnset = getUnsetTagValue({ tags, oldTags, isCaseSensitive });
const getModifyColumnScript =
({ scriptFormat }) =>
collection => {
const collectionSchema = {
...collection,
..._.omit(collection?.role, 'properties'),
};
const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName);
const fullName = getFullName(databaseName, getFullName(schemaName, tableName));

if (tagsToSet) {
result.push(`ALTER TABLE IF EXISTS ${fullName} MODIFY COLUMN ${name} SET ${tagsToSet};`);
}
const renameColumnScripts = _.values(collection.properties)
.filter(jsonSchema => checkFieldPropertiesChanged(jsonSchema.compMod, ['name']))
.map(
jsonSchema =>
`ALTER TABLE IF EXISTS ${fullName} RENAME COLUMN ${jsonSchema.compMod.oldField.name} TO ${jsonSchema.compMod.newField.name};`,
);

if (tagsToUnset) {
result.push(`ALTER TABLE IF EXISTS ${fullName} MODIFY COLUMN ${name} UNSET ${tagsToUnset};`);
}
const nameToJsonSchemaPairs = _.toPairs(collection.properties);

return result;
}, []);
const changeTypeScripts = nameToJsonSchemaPairs
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
.map(
([name, jsonSchema]) =>
`ALTER TABLE IF EXISTS ${fullName} ALTER COLUMN ${name} SET DATA TYPE ${
jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type
};`,
);

return [...renameColumnScripts, ...changeTypeScripts, ...changeTagScripts];
};
const changeTagScripts = nameToJsonSchemaPairs.reduce((result, [name, jsonSchema]) => {
const tags = jsonSchema.columnTags;
const oldTags = collection.role?.properties?.[name]?.columnTags;
const isCaseSensitive = collection.role?.isCaseSensitive;
const tagsToSet = getSetTagValue({ tags, oldTags, isCaseSensitive });
const tagsToUnset = getUnsetTagValue({ tags, oldTags, isCaseSensitive });

if (tagsToSet) {
result.push(`ALTER TABLE IF EXISTS ${fullName} MODIFY COLUMN ${name} SET ${tagsToSet};`);
}

if (tagsToUnset) {
result.push(`ALTER TABLE IF EXISTS ${fullName} MODIFY COLUMN ${name} UNSET ${tagsToUnset};`);
}

return result;
}, []);

const modifyCommentScripts = nameToJsonSchemaPairs
.map(([name, jsonSchema]) => {
const columnName = getName(collectionSchema.isCaseSensitive, name);

const comment = jsonSchema.description;
const oldComment = collection.role?.properties?.[name]?.description;

// comment was removed
if (oldComment && !comment) {
return assignTemplates(templates.alterTable, {
name: fullName,
action: `MODIFY COLUMN ${columnName} UNSET COMMENT`,
});
}

// new or modified comment
if (oldComment !== comment) {
return assignTemplates(templates.alterTable, {
name: fullName,
action: `MODIFY COLUMN ${columnName} COMMENT = ${escapeString(scriptFormat, comment)}`,
});
}
})
.filter(Boolean);

return [...renameColumnScripts, ...changeTypeScripts, ...changeTagScripts, ...modifyCommentScripts];
};

module.exports = {
getAddCollectionScript,
Expand Down
Loading