Skip to content

Commit 700616c

Browse files
HCK-13902: constraints as a separate statement (#140)
* fix: properly combine and format all column constraints * fix: correct notation for column name in constraint
1 parent 39ff020 commit 700616c

13 files changed

Lines changed: 174 additions & 96 deletions

forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ const getAddColumnsScripts = (definitions, provider) => entity => {
245245
const entityData = { ...entity, ..._.omit(entity.role, ['properties']) };
246246
const { columns } = getColumns(entityData, true, definitions);
247247
const properties = getEntityProperties(entity);
248-
const columnStatement = getColumnsStatement(columns, null, entityData.disableNoValidate);
248+
const columnStatement = getColumnsStatement({ columns });
249249
const fullCollectionName = generateFullEntityName(entity);
250250
const { hydratedAddIndexes, hydratedDropIndexes } = hydrateIndex(entity, properties, definitions);
251251
const modifyScript = generateModifyCollectionScript(entity, definitions, provider);

forward_engineering/helpers/alterScriptHelpers/alterForeignKeyHelper.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
const {
2-
getFullEntityName,
3-
generateFullEntityName,
4-
getEntityProperties,
5-
getContainerName,
6-
getEntityData,
7-
getEntityName,
8-
prepareScript,
9-
hydrateProperty,
10-
} = require('./generalHelper');
1+
const { getFullEntityName } = require('./generalHelper');
112
const { prepareName, commentDeactivatedStatements } = require('../generalHelper');
123

134
const templates = require('./config/templates');
@@ -69,7 +60,7 @@ const canRelationshipBeAdded = relationship => {
6960
compMod.child?.bucket,
7061
compMod.child?.collection,
7162
compMod.child?.collection?.fkFields?.length,
72-
].every(property => Boolean(property));
63+
].every(Boolean);
7364
};
7465

7566
const getAddForeignKeyScript = provider => relationship => {
@@ -100,7 +91,7 @@ const canRelationshipBeDeleted = relationship => {
10091
compMod.code?.old || compMod.name?.old || getRelationshipName(relationship),
10192
compMod.child?.bucket,
10293
compMod.child?.collection,
103-
].every(property => Boolean(property));
94+
].every(Boolean);
10495
};
10596

10697
const getDeleteForeignKeyScripts = provider => deletedRelationships => {

forward_engineering/helpers/alterScriptHelpers/checkConstraintHelper.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const templates = require('./config/templates');
33
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
44
const { getTypeByProperty } = require('../columnHelper');
55
const { commentDeactivatedStatements } = require('../generalHelper');
6-
7-
const postfix = 'check';
6+
const { CONSTRAINT_POSTFIX } = require('../constants');
87

98
const didCompositeCheckConstraintsChange = collection => {
109
const checkConstraintsDto = collection?.role?.compMod?.chkConstr || {};
@@ -33,7 +32,9 @@ const getDropCompositeCheckConstraintsScripts = ({ collection, provider }) => {
3332
const oldCheckConstraints = checkConstraintsDto.old || [];
3433

3534
return oldCheckConstraints.map(oldCheckConstraint => {
36-
const constraintName = oldCheckConstraint.constraintName || getDefaultConstraintName(collection, postfix);
35+
const constraintName =
36+
oldCheckConstraint.constraintName ||
37+
getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.check });
3738

3839
return provider.assignTemplates(templates.dropConstraint, {
3940
tableName,
@@ -54,7 +55,9 @@ const getAddCompositeCheckConstraintsScripts = ({ collection, provider }) => {
5455
const newCheckConstraints = checkConstraintsDto.new || [];
5556

5657
return newCheckConstraints.map(newCheckConstraint => {
57-
const constraintName = newCheckConstraint.constraintName || getDefaultConstraintName(collection, postfix);
58+
const constraintName =
59+
newCheckConstraint.constraintName ||
60+
getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.check });
5861
const expression = newCheckConstraint.checkExpression || '';
5962
const enable = newCheckConstraint.enableSpecification ? ` ${newCheckConstraint.enableSpecification}` : '';
6063
const noValidate = newCheckConstraint.noValidateSpecification
@@ -82,10 +85,10 @@ const getModifyCompositeCheckConstraintsScripts = ({ collection, provider }) =>
8285

8386
const getModifyColumnCheckConstraintsScripts = ({ collection, provider, definitions }) => {
8487
const tableName = generateFullEntityName(collection);
85-
const constraintName = getDefaultConstraintName(collection, postfix);
88+
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.check });
8689
const isActivated = collection.role.isActivated;
8790

88-
const addCheckConstraintsScript = _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
91+
return _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
8992
const oldName = jsonSchema.compMod.oldField.name;
9093
const newField = jsonSchema.compMod.newField;
9194

@@ -120,8 +123,6 @@ const getModifyColumnCheckConstraintsScripts = ({ collection, provider, definiti
120123

121124
return scripts.map(statement => commentDeactivatedStatements(statement, isActivated));
122125
});
123-
124-
return addCheckConstraintsScript;
125126
};
126127

127128
module.exports = {

forward_engineering/helpers/alterScriptHelpers/defaultConstraintHelper.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ const templates = require('./config/templates');
33
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
44
const { getTypeByProperty } = require('../columnHelper');
55
const { commentDeactivatedStatements } = require('../generalHelper');
6-
7-
const postfix = 'default';
6+
const { CONSTRAINT_POSTFIX } = require('../constants');
87

98
const getModifyDefaultValueConstraintsScripts = ({ collection, provider, definitions }) => {
109
const tableName = generateFullEntityName(collection);
11-
const constraintName = getDefaultConstraintName(collection, postfix);
10+
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.default });
1211
const isActivated = collection.role.isActivated;
1312

14-
const addDefaultConstraintsScript = _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
13+
return _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
1514
const oldName = jsonSchema.compMod.oldField.name;
1615
const newField = jsonSchema.compMod.newField;
1716

@@ -46,8 +45,6 @@ const getModifyDefaultValueConstraintsScripts = ({ collection, provider, definit
4645

4746
return scripts.map(statement => commentDeactivatedStatements(statement, isActivated));
4847
});
49-
50-
return addDefaultConstraintsScript;
5148
};
5249

5350
module.exports = {

forward_engineering/helpers/alterScriptHelpers/generalHelper.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const generateFullEntityName = entity => {
2525
const getEntityProperties = entity => {
2626
const propertiesInRole = _.get(entity, 'role.properties', {});
2727
const propertiesInEntity = _.get(entity, 'properties', {});
28-
return { ...(propertiesInEntity || {}), ...propertiesInRole };
28+
return { ...propertiesInEntity, ...propertiesInRole };
2929
};
3030

3131
const getEntityName = (compMod = {}, type = 'collectionName') => {
@@ -43,13 +43,14 @@ const isEqualProperty = (compMod, nameProperty) => {
4343
};
4444

4545
const hydrateProperty = (entity, compMod, nameProperty) => {
46-
return !isEqualProperty(compMod, nameProperty) ? entity?.role?.[nameProperty] : null;
46+
return isEqualProperty(compMod, nameProperty) ? null : entity?.role?.[nameProperty];
4747
};
4848

49-
const getDefaultConstraintName = (collection, postfix) => {
49+
const getDefaultConstraintName = ({ collection, column = {}, postfix }) => {
5050
const entityData = collection?.role || {};
5151
const entityName = prepareName(getName(entityData));
52-
return `${entityName}_${postfix}`;
52+
const columnName = prepareName(getName(column));
53+
return [entityName, columnName, postfix].filter(Boolean).join('_');
5354
};
5455

5556
module.exports = {

forward_engineering/helpers/alterScriptHelpers/nonNullConstraintHelper.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ const templates = require('./config/templates');
33
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
44
const { getTypeByProperty } = require('../columnHelper');
55
const { commentDeactivatedStatements } = require('../generalHelper');
6-
7-
const postfix = 'nn';
6+
const { CONSTRAINT_POSTFIX } = require('../constants');
87

98
const getModifyNonNullColumnsScripts = ({ collection, provider, definitions }) => {
109
const tableName = generateFullEntityName(collection);
11-
const constraintName = getDefaultConstraintName(collection, postfix);
10+
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.notNull });
1211
const isActivated = collection.role.isActivated;
1312

1413
const currentRequiredColumnNames = collection.required || [];
1514
const previousRequiredColumnNames = collection.role.required || [];
1615

17-
const addNotNullConstraintsScript = _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
16+
return _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
1817
const oldName = jsonSchema.compMod.oldField.name;
1918
const newField = jsonSchema.compMod.newField;
2019

@@ -44,8 +43,6 @@ const getModifyNonNullColumnsScripts = ({ collection, provider, definitions }) =
4443

4544
return scripts.map(statement => commentDeactivatedStatements(statement, isActivated));
4645
});
47-
48-
return addNotNullConstraintsScript;
4946
};
5047

5148
module.exports = {

forward_engineering/helpers/alterScriptHelpers/primaryKeyHelper.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const _ = require('lodash');
2-
const { getName, prepareName, commentDeactivatedStatements } = require('../generalHelper');
2+
const { prepareName, commentDeactivatedStatements } = require('../generalHelper');
33
const templates = require('./config/templates');
44
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
5-
6-
const postfix = 'pk';
5+
const { CONSTRAINT_POSTFIX } = require('../constants');
76

87
const getPropertyNameByGuid = (collection, guid) => {
9-
const property = _.toPairs(collection?.role?.properties).find(([name, jsonSchema]) => jsonSchema.GUID === guid);
8+
const property = _.toPairs(collection?.role?.properties).find(([, jsonSchema]) => jsonSchema.GUID === guid);
109
return property?.[0] && prepareName(property[0]);
1110
};
1211

@@ -40,7 +39,8 @@ const getDropCompositePkScripts = ({ collection, provider }) => {
4039
const oldPrimaryKeys = pkDto.old || [];
4140

4241
return oldPrimaryKeys.map(oldPk => {
43-
const pkConstraintName = oldPk.constraintName || getDefaultConstraintName(collection, postfix);
42+
const pkConstraintName =
43+
oldPk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
4444
const constraintName = prepareName(pkConstraintName);
4545

4646
return provider.assignTemplates(templates.dropConstraint, {
@@ -65,7 +65,8 @@ const getAddCompositePkScripts = ({ collection, provider }) => {
6565
const compositePrimaryKey = newPk.compositePrimaryKey || [];
6666
const guidsOfColumnsInPk = compositePrimaryKey.map(compositePkEntry => compositePkEntry.keyId);
6767
const columnNames = getPropertiesNamesByGUIDs(collection, guidsOfColumnsInPk);
68-
const pkConstraintName = newPk.constraintName || getDefaultConstraintName(collection, postfix);
68+
const pkConstraintName =
69+
newPk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
6970
const constraintName = prepareName(pkConstraintName);
7071
const noValidate = newPk.noValidateSpecification ? ` ${newPk.noValidateSpecification}` : '';
7172
const rely = newPk.rely ? ` ${newPk.rely}` : '';
@@ -89,18 +90,18 @@ const getModifyCompositePkScripts = ({ collection, provider }) => {
8990

9091
const getDropPkScripts = ({ collection, provider }) => {
9192
const tableName = generateFullEntityName(collection);
92-
const constraintName = getDefaultConstraintName(collection, postfix);
93+
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
9394

9495
return _.toPairs(collection.properties)
95-
.filter(([name, jsonSchema]) => {
96+
.filter(([, jsonSchema]) => {
9697
const oldName = jsonSchema.compMod.oldField.name;
9798
const oldJsonSchema = collection.role.properties[oldName];
9899
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
99100

100101
const isNotAPrimaryKey = !jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
101102
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
102103
})
103-
.map(([name, jsonSchema]) => {
104+
.map(() => {
104105
return provider.assignTemplates(templates.dropConstraint, {
105106
tableName,
106107
constraintName,
@@ -110,10 +111,10 @@ const getDropPkScripts = ({ collection, provider }) => {
110111

111112
const getAddPkScripts = ({ collection, provider }) => {
112113
const tableName = generateFullEntityName(collection);
113-
const constraintName = getDefaultConstraintName(collection, postfix);
114+
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
114115

115116
return _.toPairs(collection.properties)
116-
.filter(([name, jsonSchema]) => {
117+
.filter(([, jsonSchema]) => {
117118
const isRegularPrimaryKey = jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
118119
const oldName = jsonSchema.compMod.oldField.name;
119120
const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey);

forward_engineering/helpers/alterScriptHelpers/uniqueKeyHelper.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const _ = require('lodash');
2-
const { getName, prepareName, commentDeactivatedStatements } = require('../generalHelper');
2+
const { prepareName, commentDeactivatedStatements } = require('../generalHelper');
33
const templates = require('./config/templates');
44
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
5-
6-
const postfix = 'uk';
5+
const { CONSTRAINT_POSTFIX } = require('../constants');
76

87
const getPropertyNameByGuid = (collection, guid) => {
98
const property = _.toPairs(collection?.role?.properties).find(([name, jsonSchema]) => jsonSchema.GUID === guid);
@@ -40,7 +39,8 @@ const getDropCompositeUkScripts = ({ collection, provider }) => {
4039
const oldUniqueKeys = pkDto.old || [];
4140

4241
return oldUniqueKeys.map(oldUk => {
43-
const pkConstraintName = oldUk.constraintName || getDefaultConstraintName(collection, postfix);
42+
const pkConstraintName =
43+
oldUk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });
4444
const constraintName = prepareName(pkConstraintName);
4545

4646
return provider.assignTemplates(templates.dropConstraint, {
@@ -65,7 +65,8 @@ const getAddCompositeUkScripts = ({ collection, provider }) => {
6565
const compositeUniqueKey = newUk.compositeUniqueKey || [];
6666
const guidsOfColumnsInUk = compositeUniqueKey.map(compositeUkEntry => compositeUkEntry.keyId);
6767
const columnNames = getPropertiesNamesByGUIDs(collection, guidsOfColumnsInUk);
68-
const pkConstraintName = newUk.constraintName || getDefaultConstraintName(collection, postfix);
68+
const pkConstraintName =
69+
newUk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });
6970
const constraintName = prepareName(pkConstraintName);
7071
const noValidate = newUk.noValidateSpecification ? ` ${newUk.noValidateSpecification}` : '';
7172
const rely = newUk.rely ? ` ${newUk.rely}` : '';
@@ -89,18 +90,18 @@ const getModifyCompositeUkScripts = ({ collection, provider }) => {
8990

9091
const getDropUkScripts = ({ collection, provider }) => {
9192
const tableName = generateFullEntityName(collection);
92-
const constraintName = getDefaultConstraintName(collection, postfix);
93+
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });
9394

9495
return _.toPairs(collection.properties)
95-
.filter(([name, jsonSchema]) => {
96+
.filter(([, jsonSchema]) => {
9697
const oldName = jsonSchema.compMod.oldField.name;
9798
const oldJsonSchema = collection.role.properties[oldName];
9899
const wasTheFieldARegularUniqueKey = oldJsonSchema?.unique && !oldJsonSchema?.compositeUniqueKey;
99100

100101
const isNotAUniqueKey = !jsonSchema.unique && !jsonSchema.compositeUniqueKey;
101102
return wasTheFieldARegularUniqueKey && isNotAUniqueKey;
102103
})
103-
.map(([name, jsonSchema]) => {
104+
.map(() => {
104105
return provider.assignTemplates(templates.dropConstraint, {
105106
tableName,
106107
constraintName,
@@ -110,10 +111,10 @@ const getDropUkScripts = ({ collection, provider }) => {
110111

111112
const getAddUkScripts = ({ collection, provider }) => {
112113
const tableName = generateFullEntityName(collection);
113-
const constraintName = getDefaultConstraintName(collection, postfix);
114+
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.uniqueKey });
114115

115116
return _.toPairs(collection.properties)
116-
.filter(([name, jsonSchema]) => {
117+
.filter(([, jsonSchema]) => {
117118
const isRegularUniqueKey = jsonSchema.unique && !jsonSchema.compositeUniqueKey;
118119
const oldName = jsonSchema.compMod.oldField.name;
119120
const wasTheFieldAUniqueKey = Boolean(collection.role.properties[oldName]?.unique);

0 commit comments

Comments
 (0)