Skip to content

Commit 5cb8f8f

Browse files
committed
Added validation of primary/unique keys. Added warnings in FE script when prmary/unique constraints have no keys
1 parent c57509e commit 5cb8f8f

File tree

4 files changed

+70
-26
lines changed

4 files changed

+70
-26
lines changed

forward_engineering/ddlProvider.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ module.exports = (baseProvider, options, app) => {
2626
divideIntoActivatedAndDeactivated,
2727
commentIfDeactivated,
2828
});
29-
const { generateConstraintsString, foreignKeysToString, foreignActiveKeysToString, createKeyConstraint } =
30-
require('./helpers/constraintsHelper')({
31-
_,
32-
commentIfDeactivated,
33-
checkAllKeysDeactivated,
34-
assignTemplates,
35-
getColumnsList,
36-
wrapInQuotes,
37-
});
29+
const {
30+
generateConstraintsString,
31+
foreignKeysToString,
32+
foreignActiveKeysToString,
33+
createKeyConstraint,
34+
getConstraintsWarnings,
35+
} = require('./helpers/constraintsHelper')({
36+
_,
37+
commentIfDeactivated,
38+
checkAllKeysDeactivated,
39+
assignTemplates,
40+
getColumnsList,
41+
wrapInQuotes,
42+
});
3843
const keyHelper = require('./helpers/keyHelper')(_, clean);
3944

4045
const { getFunctionsScript } = require('./helpers/functionHelper')({
@@ -175,10 +180,18 @@ module.exports = (baseProvider, options, app) => {
175180
});
176181

177182
const dividedKeysConstraints = divideIntoActivatedAndDeactivated(
178-
keyConstraints.map(createKeyConstraint(templates, isActivated)),
183+
keyConstraints
184+
.filter(({ errorMessage }) => !errorMessage)
185+
.map(createKeyConstraint(templates, isActivated)),
179186
key => key.statement,
180187
);
181-
const keyConstraintsString = generateConstraintsString(dividedKeysConstraints, isActivated);
188+
const constraintWarnings = getConstraintsWarnings(
189+
keyConstraints.filter(({ errorMessage }) => errorMessage),
190+
);
191+
const keyConstraintsString = `${generateConstraintsString(
192+
dividedKeysConstraints,
193+
isActivated,
194+
)}\n\t${constraintWarnings}`;
182195
const keyConstraintsValue = partitionOf ? keyConstraintsString?.slice(1) : keyConstraintsString;
183196

184197
const dividedForeignKeys = divideIntoActivatedAndDeactivated(foreignKeyConstraints, key => key.statement);
@@ -531,7 +544,7 @@ module.exports = (baseProvider, options, app) => {
531544
}
532545

533546
jsonSchema = _.omit(jsonSchema, '$ref');
534-
return { ...definitionJsonSchema, ...jsonSchema };
547+
return { ...definitionJsonSchema, ...jsonSchema };
535548
},
536549

537550
hydrateIndex(indexData, tableData, schemaData) {

forward_engineering/helpers/constraintsHelper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,21 @@ module.exports = ({
6969
};
7070
};
7171

72+
const getConstraintsWarnings = (invalidConstraints = []) => {
73+
return invalidConstraints
74+
.map(keyData => {
75+
const constraintName = keyData.name ? ` [constraint name: ${keyData.name}]` : '';
76+
77+
return `-- ${keyData.errorMessage}${constraintName}`;
78+
})
79+
.join('\n\t');
80+
};
81+
7282
return {
7383
generateConstraintsString,
7484
foreignKeysToString,
7585
foreignActiveKeysToString,
7686
createKeyConstraint,
87+
getConstraintsWarnings,
7788
};
7889
};

forward_engineering/helpers/keyHelper.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,35 @@ module.exports = (_, clean) => {
9191
return [];
9292
}
9393

94-
return jsonSchema.primaryKey
95-
.filter(primaryKey => !_.isEmpty(primaryKey.compositePrimaryKey))
96-
.map(primaryKey => ({
97-
...hydratePrimaryKeyOptions(primaryKey, null, null, jsonSchema),
98-
columns: getKeys(primaryKey.compositePrimaryKey, jsonSchema),
99-
}));
94+
return jsonSchema.primaryKey.map(primaryKey =>
95+
!_.isEmpty(primaryKey.compositePrimaryKey)
96+
? {
97+
...hydratePrimaryKeyOptions(primaryKey, null, null, jsonSchema),
98+
columns: getKeys(primaryKey.compositePrimaryKey, jsonSchema),
99+
}
100+
: {
101+
name: primaryKey.constraintName,
102+
errorMessage: 'A primary key constraint cannot be created without any primary key selected',
103+
},
104+
);
100105
};
101106

102107
const getCompositeUniqueKeys = jsonSchema => {
103108
if (!Array.isArray(jsonSchema.uniqueKey)) {
104109
return [];
105110
}
106111

107-
return jsonSchema.uniqueKey
108-
.filter(uniqueKey => !_.isEmpty(uniqueKey.compositeUniqueKey))
109-
.map(uniqueKey => ({
110-
...hydrateUniqueOptions(uniqueKey, null, null, jsonSchema),
111-
columns: getKeys(uniqueKey.compositeUniqueKey, jsonSchema),
112-
}));
112+
return jsonSchema.uniqueKey.map(uniqueKey =>
113+
!_.isEmpty(uniqueKey.compositeUniqueKey)
114+
? {
115+
...hydrateUniqueOptions(uniqueKey, null, null, jsonSchema),
116+
columns: getKeys(uniqueKey.compositeUniqueKey, jsonSchema),
117+
}
118+
: {
119+
name: uniqueKey.constraintName,
120+
errorMessage: 'A unique key constraint cannot be created without any unique key selected',
121+
},
122+
);
113123
};
114124

115125
const getTableKeyConstraints = jsonSchema => {

properties_pane/entity_level/entityLevelConfig.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,12 +675,17 @@ making sure that you maintain a proper JSON format.
675675
"propertyName": "Constraint name",
676676
"propertyKeyword": "constraintName",
677677
"propertyTooltip": "",
678-
"propertyType": "text"
678+
"propertyType": "text",
679+
"validation": {
680+
"indexKey": "compositePrimaryKey",
681+
"message": "A primary key constraint cannot be created without any primary key selected"
682+
}
679683
},
680684
{
681685
"propertyName": "Key",
682686
"propertyKeyword": "compositePrimaryKey",
683687
"propertyType": "primaryKeySetter",
688+
"requiredProperty": true,
684689
"abbr": "pk"
685690
},
686691
{
@@ -764,12 +769,17 @@ making sure that you maintain a proper JSON format.
764769
"propertyName": "Constraint name",
765770
"propertyKeyword": "constraintName",
766771
"propertyTooltip": "",
767-
"propertyType": "text"
772+
"propertyType": "text",
773+
"validation": {
774+
"indexKey": "compositeUniqueKey",
775+
"message": "A unique key constraint cannot be created without any unique key selected"
776+
}
768777
},
769778
{
770779
"propertyName": "Key",
771780
"propertyKeyword": "compositeUniqueKey",
772781
"propertyType": "primaryKeySetter",
782+
"requiredProperty": true,
773783
"abbr": "uk"
774784
},
775785
{

0 commit comments

Comments
 (0)