Skip to content

Commit 6166651

Browse files
committed
HCK-12142: Add support of alter default value
1 parent 454a80b commit 6166651

File tree

5 files changed

+121
-38
lines changed

5 files changed

+121
-38
lines changed

forward_engineering/configs/templates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
')${options}${terminator}\n${comment}${columnComments}',
1212

1313
columnDefinition:
14-
'[${name}] ${type}${primary_key}${temporalTableTime}${sparse}${maskedWithFunction}${identity}${default}${collation}${not_null}${encryptedWith}',
14+
'[${name}] ${type}${primary_key}${temporalTableTime}${sparse}${maskedWithFunction}${identity}${collation}${not_null}${default}${encryptedWith}',
1515
computedColumnDefinition: '[${name}] AS ${expression}${persisted}${key}${not_null}',
1616

1717
index:

forward_engineering/ddlProvider.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ module.exports = (baseProvider, options, app) => {
2525
getTableName,
2626
getTableOptions,
2727
hasType,
28-
getDefaultConstraints,
2928
foreignKeysToString,
3029
checkIndexActivated,
3130
getDefaultValue,
@@ -99,6 +98,10 @@ module.exports = (baseProvider, options, app) => {
9998
return databaseStatement + '\n\n' + useStatement + '\n\n' + schemaStatement;
10099
},
101100

101+
createDefaultConstraint(data, tableName, terminator = ';') {
102+
return createDefaultConstraint(templates, terminator)(data, tableName);
103+
},
104+
102105
createTable(
103106
{
104107
name,
@@ -161,20 +164,15 @@ module.exports = (baseProvider, options, app) => {
161164
comment: tableComment ? `\n${tableComment}` : '',
162165
columnComments: columnComments ? `${tableAndColumnCommentsSeparator}${columnComments}\n` : '',
163166
});
164-
const defaultConstraintsStatements = defaultConstraints
165-
.map(data => createDefaultConstraint(templates, tableTerminator)(data, tableName))
166-
.join('\n');
167-
168-
const fullTableStatement = [tableStatement, defaultConstraintsStatements].filter(Boolean).join('\n\n');
169167

170168
return ifNotExist
171169
? wrapIfNotExistTable({
172-
tableStatement: fullTableStatement,
170+
tableStatement,
173171
templates,
174172
tableName: getTableName(name, schemaData.schemaName, false),
175173
terminator,
176174
})
177-
: fullTableStatement;
175+
: tableStatement;
178176
},
179177

180178
createComputedColumn({ name, computedExpression, persisted, primaryKey, unique, notNull }) {
@@ -201,11 +199,7 @@ module.exports = (baseProvider, options, app) => {
201199
const primaryKey = columnDefinition.primaryKey
202200
? ' ' + createPKConstraint(templates, terminator, true)(columnDefinition.primaryKeyOptions).statement
203201
: '';
204-
const defaultValue = getDefaultValue(
205-
columnDefinition.default,
206-
columnDefinition.defaultConstraint?.name,
207-
type,
208-
);
202+
const defaultValue = getDefaultValue(columnDefinition.defaultConstraint, type);
209203
const sparse = columnDefinition.sparse ? ' SPARSE' : '';
210204
const maskedWithFunction = columnDefinition.maskedWithFunction
211205
? ` MASKED WITH (FUNCTION='${columnDefinition.maskedWithFunction}')`
@@ -464,7 +458,6 @@ module.exports = (baseProvider, options, app) => {
464458
_.get(parentJsonSchema, 'periodForSystemTime[0].startTime[0].type', '') === 'hidden';
465459

466460
return Object.assign({}, columnDefinition, {
467-
default: jsonSchema.defaultConstraintName ? '' : columnDefinition.default,
468461
defaultConstraint: {
469462
name: jsonSchema.defaultConstraintName,
470463
value: columnDefinition.default,
@@ -547,7 +540,6 @@ module.exports = (baseProvider, options, app) => {
547540
return Object.assign({}, tableData, {
548541
foreignKeyConstraints: tableData.foreignKeyConstraints || [],
549542
keyConstraints: keyHelper.getTableKeyConstraints({ jsonSchema }),
550-
defaultConstraints: getDefaultConstraints(tableData.columnDefinitions),
551543
ifNotExist: jsonSchema.ifNotExist,
552544
comment: jsonSchema.description,
553545
columnDefinitions: tableData.columnDefinitions,

forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = (app, options) => {
99
const { generateIdToNameHashTable, generateIdToActivatedHashTable } = app.require('@hackolade/ddl-fe-utils');
1010
const { setIndexKeys, modifyGroupItems } = require('./common')(app);
1111
const { getRenameColumnScriptsDto } = require('./columnHelpers/renameColumnHelpers')(app, ddlProvider);
12+
const { getDefaultValueChangeDto } = require('./columnHelpers/defaultValueColumnHelper')(app, ddlProvider);
1213
const { getChangedComputedColumnsScriptsDto } = require('./columnHelpers/alterComputedColumnHelpr')(
1314
app,
1415
ddlProvider,
@@ -199,6 +200,7 @@ module.exports = (app, options) => {
199200
collectionSchema,
200201
schemaName,
201202
);
203+
const modifiedDefaultValues = getDefaultValueChangeDto(collection, fullName);
202204
const changedComputedScriptsDtos = getChangedComputedColumnsScriptsDto({
203205
collection,
204206
fullName,
@@ -210,6 +212,7 @@ module.exports = (app, options) => {
210212
...renameColumnScriptsDtos,
211213
...changeTypeScriptsDtos,
212214
...modifyNotNullScriptDtos,
215+
...modifiedDefaultValues,
213216
...changedComputedScriptsDtos,
214217
].filter(Boolean);
215218
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module.exports = (app, ddlProvider) => {
2+
const { AlterScriptDto } = require('../types/AlterScriptDto');
3+
const { sanitizeConstraintName } = require('../../../helpers/general')(app);
4+
5+
const getDefaultValueChangeDto = (collection, fullName) => {
6+
const scripts = [];
7+
8+
const getDefaultConstraintName = columnName => sanitizeConstraintName(`DF_${fullName}_${columnName}`);
9+
10+
Object.entries(collection.properties).forEach(([columnName, collectionSchema]) => {
11+
const newDefaultValue = collectionSchema.default;
12+
const newConstraintName = collectionSchema.defaultConstraintName;
13+
const oldDefaultValue = collection.role.properties[columnName]?.default;
14+
const oldConstraintName = collection.role.properties[columnName]?.defaultConstraintName;
15+
16+
const defaultValueWasRemoved = !!oldDefaultValue && !newDefaultValue;
17+
const defaultValueWasAdded = !oldDefaultValue && !!newDefaultValue;
18+
const defaultValueWasChanged =
19+
!!oldDefaultValue && !!newDefaultValue && oldDefaultValue !== newDefaultValue;
20+
const constraintNameChanged =
21+
!!oldDefaultValue &&
22+
!!newDefaultValue &&
23+
oldDefaultValue === newDefaultValue &&
24+
!!oldConstraintName &&
25+
!!newConstraintName &&
26+
oldConstraintName !== newConstraintName;
27+
28+
switch (true) {
29+
case defaultValueWasRemoved: {
30+
if (oldConstraintName) {
31+
const dropScript = ddlProvider.dropConstraint(fullName, oldConstraintName);
32+
scripts.push(AlterScriptDto.getInstance([dropScript], true, true));
33+
}
34+
break;
35+
}
36+
case defaultValueWasAdded: {
37+
const constraintName = newConstraintName || getDefaultConstraintName(columnName);
38+
39+
const createScript = ddlProvider.createDefaultConstraint(
40+
{
41+
constraintName,
42+
columnName,
43+
value: newDefaultValue,
44+
},
45+
fullName,
46+
);
47+
scripts.push(AlterScriptDto.getInstance([createScript], true, false));
48+
break;
49+
}
50+
case defaultValueWasChanged: {
51+
if (oldConstraintName) {
52+
const dropScript = ddlProvider.dropConstraint(fullName, oldConstraintName);
53+
scripts.push(AlterScriptDto.getInstance([dropScript], true, true));
54+
}
55+
const constraintName = newConstraintName || getDefaultConstraintName(columnName);
56+
57+
const createScript = ddlProvider.createDefaultConstraint(
58+
{
59+
constraintName,
60+
columnName,
61+
value: newDefaultValue,
62+
},
63+
fullName,
64+
);
65+
scripts.push(AlterScriptDto.getInstance([createScript], true, false));
66+
break;
67+
}
68+
case constraintNameChanged: {
69+
const dropScript = ddlProvider.dropConstraint(fullName, oldConstraintName);
70+
71+
const createScript = ddlProvider.createDefaultConstraint(
72+
{
73+
constraintName: newConstraintName,
74+
columnName,
75+
value: newDefaultValue,
76+
},
77+
fullName,
78+
);
79+
scripts.push(
80+
AlterScriptDto.getInstance([dropScript], true, true),
81+
AlterScriptDto.getInstance([createScript], true, false),
82+
);
83+
break;
84+
}
85+
default:
86+
break;
87+
}
88+
});
89+
90+
return scripts;
91+
};
92+
93+
return {
94+
getDefaultValueChangeDto,
95+
};
96+
};

forward_engineering/helpers/general.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ module.exports = app => {
1717
return withBrackets(tableName);
1818
};
1919

20-
const getDefaultValue = (defaultValue, defaultConstraintName, type) => {
21-
if (_.isUndefined(defaultValue)) {
20+
const getDefaultValue = (defaultConstraint, type) => {
21+
if (_.isUndefined(defaultConstraint.value)) {
2222
return '';
2323
}
24-
if (!_.isUndefined(defaultConstraintName)) {
25-
return '';
24+
25+
const value = decorateDefault(type, defaultConstraint.value);
26+
27+
if (!_.isUndefined(defaultConstraint.name)) {
28+
return ` CONSTRAINT ${defaultConstraint.name} DEFAULT ${value}`;
2629
}
27-
return ` DEFAULT ${decorateDefault(type, defaultValue)}`;
30+
31+
return ` DEFAULT ${value}`;
32+
};
33+
34+
const sanitizeConstraintName = str => {
35+
return str ? str.replace(/\[|\]/g, '').replace(/\./g, '_') : str;
2836
};
2937

3038
const getKeyWithAlias = key => {
@@ -189,22 +197,6 @@ module.exports = app => {
189197
}, {});
190198
};
191199

192-
const getDefaultConstraints = columnDefinitions => {
193-
if (!Array.isArray(columnDefinitions)) {
194-
return [];
195-
}
196-
197-
return columnDefinitions
198-
.filter(
199-
column => _.get(column, 'defaultConstraint.name') && !_.isNil(_.get(column, 'defaultConstraint.value')),
200-
)
201-
.map(column => ({
202-
columnName: column.name,
203-
constraintName: column.defaultConstraint.name,
204-
value: decorateDefault(column.type, column.defaultConstraint.value),
205-
}));
206-
};
207-
208200
const foreignKeysToString = keys => {
209201
if (Array.isArray(keys)) {
210202
const activatedKeys = keys
@@ -266,10 +258,10 @@ module.exports = app => {
266258
getTableOptions,
267259
hasType,
268260
getViewData,
269-
getDefaultConstraints,
270261
foreignKeysToString,
271262
additionalPropertiesForForeignKey,
272263
trimBraces,
264+
sanitizeConstraintName,
273265
checkIndexActivated,
274266
foreignActiveKeysToString,
275267
getDefaultValue,

0 commit comments

Comments
 (0)