Skip to content

Commit e84f0b1

Browse files
feat: added alter script for column comment (#217)
1 parent 9d10258 commit e84f0b1

File tree

4 files changed

+192
-161
lines changed

4 files changed

+192
-161
lines changed

forward_engineering/ddlProvider.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ const {
4949
const { generateConstraint } = require('./helpers/constraintHelper');
5050
const getFormatTypeOptions = require('./helpers/getFormatTypeOptions');
5151
const { getStageCopyOptions } = require('./helpers/getStageCopyOptions');
52+
const {
53+
getTagStatement,
54+
getTagAllowedValues,
55+
getTagKeyValues,
56+
prepareObjectTagsData,
57+
isEmptyTags,
58+
} = require('./helpers/tagHelper');
5259

5360
const DEFAULT_SNOWFLAKE_SEQUENCE_START = 1;
5461
const DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT = 1;
@@ -99,12 +106,6 @@ module.exports = (baseProvider, options, app) => {
99106
tab,
100107
});
101108

102-
const { getTagStatement, getTagAllowedValues, getTagKeyValues, prepareObjectTagsData, isEmptyTags } =
103-
require('./helpers/tagHelper')({
104-
getName,
105-
toString,
106-
});
107-
108109
const getOutOfLineConstraints = (
109110
isParentActivated,
110111
foreignKeyConstraints = [],

forward_engineering/helpers/alterScriptFromDeltaHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const getAlterCollectionsScripts = ({ collection, ddlProvider, app, scriptFormat
6666
);
6767
const modifiedColumnScripts = getColumnScripts(
6868
getItems(collection, 'entities', 'modified', 'values'),
69-
getModifyColumnScript,
69+
getModifyColumnScript({ scriptFormat }),
7070
);
7171

7272
return {

forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ const _ = require('lodash');
22
const { checkFieldPropertiesChanged, getNames, getBaseAndContainerNames } = require('./common');
33
const { createColumnDefinitionBySchema } = require('./createColumnDefinition');
44
const { commentIfDeactivated } = require('../commentHelpers/commentDeactivatedHelper');
5-
const { getEntityName, getFullName, getName, toString } = require('../general');
5+
const { getEntityName, getFullName, getName } = require('../general');
6+
const { getSetTagValue, getUnsetTagValue } = require('../../helpers/tagHelper');
7+
const assignTemplates = require('../../utils/assignTemplates');
8+
const templates = require('../../configs/templates');
9+
const { escapeString } = require('../../utils/escapeString');
610

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

99-
const getModifyColumnScript = collection => {
100-
const { getSetTagValue, getUnsetTagValue } = require('../../helpers/tagHelper')({ getName, toString });
101-
102-
const collectionSchema = {
103-
...collection,
104-
...(_.omit(collection?.role, 'properties') || {}),
105-
};
106-
const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName);
107-
const fullName = getFullName(databaseName, getFullName(schemaName, tableName));
108-
109-
const renameColumnScripts = _.values(collection.properties)
110-
.filter(jsonSchema => checkFieldPropertiesChanged(jsonSchema.compMod, ['name']))
111-
.map(
112-
jsonSchema =>
113-
`ALTER TABLE IF EXISTS ${fullName} RENAME COLUMN ${jsonSchema.compMod.oldField.name} TO ${jsonSchema.compMod.newField.name};`,
114-
);
115-
116-
const changeTypeScripts = _.toPairs(collection.properties)
117-
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
118-
.map(
119-
([name, jsonSchema]) =>
120-
`ALTER TABLE IF EXISTS ${fullName} ALTER COLUMN ${name} SET DATA TYPE ${
121-
jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type
122-
};`,
123-
);
124-
125-
const changeTagScripts = _.toPairs(collection.properties).reduce((result, [name, jsonSchema]) => {
126-
const tags = jsonSchema.columnTags;
127-
const oldTags = collection.role?.properties?.[name]?.columnTags;
128-
const isCaseSensitive = collection.role?.isCaseSensitive;
129-
const tagsToSet = getSetTagValue({ tags, oldTags, isCaseSensitive });
130-
const tagsToUnset = getUnsetTagValue({ tags, oldTags, isCaseSensitive });
103+
const getModifyColumnScript =
104+
({ scriptFormat }) =>
105+
collection => {
106+
const collectionSchema = {
107+
...collection,
108+
..._.omit(collection?.role, 'properties'),
109+
};
110+
const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName);
111+
const fullName = getFullName(databaseName, getFullName(schemaName, tableName));
131112

132-
if (tagsToSet) {
133-
result.push(`ALTER TABLE IF EXISTS ${fullName} MODIFY COLUMN ${name} SET ${tagsToSet};`);
134-
}
113+
const renameColumnScripts = _.values(collection.properties)
114+
.filter(jsonSchema => checkFieldPropertiesChanged(jsonSchema.compMod, ['name']))
115+
.map(
116+
jsonSchema =>
117+
`ALTER TABLE IF EXISTS ${fullName} RENAME COLUMN ${jsonSchema.compMod.oldField.name} TO ${jsonSchema.compMod.newField.name};`,
118+
);
135119

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

140-
return result;
141-
}, []);
122+
const changeTypeScripts = nameToJsonSchemaPairs
123+
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
124+
.map(
125+
([name, jsonSchema]) =>
126+
`ALTER TABLE IF EXISTS ${fullName} ALTER COLUMN ${name} SET DATA TYPE ${
127+
jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type
128+
};`,
129+
);
142130

143-
return [...renameColumnScripts, ...changeTypeScripts, ...changeTagScripts];
144-
};
131+
const changeTagScripts = nameToJsonSchemaPairs.reduce((result, [name, jsonSchema]) => {
132+
const tags = jsonSchema.columnTags;
133+
const oldTags = collection.role?.properties?.[name]?.columnTags;
134+
const isCaseSensitive = collection.role?.isCaseSensitive;
135+
const tagsToSet = getSetTagValue({ tags, oldTags, isCaseSensitive });
136+
const tagsToUnset = getUnsetTagValue({ tags, oldTags, isCaseSensitive });
137+
138+
if (tagsToSet) {
139+
result.push(`ALTER TABLE IF EXISTS ${fullName} MODIFY COLUMN ${name} SET ${tagsToSet};`);
140+
}
141+
142+
if (tagsToUnset) {
143+
result.push(`ALTER TABLE IF EXISTS ${fullName} MODIFY COLUMN ${name} UNSET ${tagsToUnset};`);
144+
}
145+
146+
return result;
147+
}, []);
148+
149+
const modifyCommentScripts = nameToJsonSchemaPairs
150+
.map(([name, jsonSchema]) => {
151+
const columnName = getName(collectionSchema.isCaseSensitive, name);
152+
153+
const comment = jsonSchema.description;
154+
const oldComment = collection.role?.properties?.[name]?.description;
155+
156+
// comment was removed
157+
if (oldComment && !comment) {
158+
return assignTemplates(templates.alterTable, {
159+
name: fullName,
160+
action: `MODIFY COLUMN ${columnName} UNSET COMMENT`,
161+
});
162+
}
163+
164+
// new or modified comment
165+
if (oldComment !== comment) {
166+
return assignTemplates(templates.alterTable, {
167+
name: fullName,
168+
action: `MODIFY COLUMN ${columnName} COMMENT = ${escapeString(scriptFormat, comment)}`,
169+
});
170+
}
171+
})
172+
.filter(Boolean);
173+
174+
return [...renameColumnScripts, ...changeTypeScripts, ...changeTagScripts, ...modifyCommentScripts];
175+
};
145176

146177
module.exports = {
147178
getAddCollectionScript,

0 commit comments

Comments
 (0)