Skip to content

Commit

Permalink
Added validation of primary/unique keys. Added warnings in FE script …
Browse files Browse the repository at this point in the history
…when prmary/unique constraints have no keys
  • Loading branch information
Vitalii4as committed Jun 16, 2022
1 parent c57509e commit 5cb8f8f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 26 deletions.
37 changes: 25 additions & 12 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ module.exports = (baseProvider, options, app) => {
divideIntoActivatedAndDeactivated,
commentIfDeactivated,
});
const { generateConstraintsString, foreignKeysToString, foreignActiveKeysToString, createKeyConstraint } =
require('./helpers/constraintsHelper')({
_,
commentIfDeactivated,
checkAllKeysDeactivated,
assignTemplates,
getColumnsList,
wrapInQuotes,
});
const {
generateConstraintsString,
foreignKeysToString,
foreignActiveKeysToString,
createKeyConstraint,
getConstraintsWarnings,
} = require('./helpers/constraintsHelper')({
_,
commentIfDeactivated,
checkAllKeysDeactivated,
assignTemplates,
getColumnsList,
wrapInQuotes,
});
const keyHelper = require('./helpers/keyHelper')(_, clean);

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

const dividedKeysConstraints = divideIntoActivatedAndDeactivated(
keyConstraints.map(createKeyConstraint(templates, isActivated)),
keyConstraints
.filter(({ errorMessage }) => !errorMessage)
.map(createKeyConstraint(templates, isActivated)),
key => key.statement,
);
const keyConstraintsString = generateConstraintsString(dividedKeysConstraints, isActivated);
const constraintWarnings = getConstraintsWarnings(
keyConstraints.filter(({ errorMessage }) => errorMessage),
);
const keyConstraintsString = `${generateConstraintsString(
dividedKeysConstraints,
isActivated,
)}\n\t${constraintWarnings}`;
const keyConstraintsValue = partitionOf ? keyConstraintsString?.slice(1) : keyConstraintsString;

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

jsonSchema = _.omit(jsonSchema, '$ref');
return { ...definitionJsonSchema, ...jsonSchema };
return { ...definitionJsonSchema, ...jsonSchema };
},

hydrateIndex(indexData, tableData, schemaData) {
Expand Down
11 changes: 11 additions & 0 deletions forward_engineering/helpers/constraintsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,21 @@ module.exports = ({
};
};

const getConstraintsWarnings = (invalidConstraints = []) => {
return invalidConstraints
.map(keyData => {
const constraintName = keyData.name ? ` [constraint name: ${keyData.name}]` : '';

return `-- ${keyData.errorMessage}${constraintName}`;
})
.join('\n\t');
};

return {
generateConstraintsString,
foreignKeysToString,
foreignActiveKeysToString,
createKeyConstraint,
getConstraintsWarnings,
};
};
34 changes: 22 additions & 12 deletions forward_engineering/helpers/keyHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,35 @@ module.exports = (_, clean) => {
return [];
}

return jsonSchema.primaryKey
.filter(primaryKey => !_.isEmpty(primaryKey.compositePrimaryKey))
.map(primaryKey => ({
...hydratePrimaryKeyOptions(primaryKey, null, null, jsonSchema),
columns: getKeys(primaryKey.compositePrimaryKey, jsonSchema),
}));
return jsonSchema.primaryKey.map(primaryKey =>
!_.isEmpty(primaryKey.compositePrimaryKey)
? {
...hydratePrimaryKeyOptions(primaryKey, null, null, jsonSchema),
columns: getKeys(primaryKey.compositePrimaryKey, jsonSchema),
}
: {
name: primaryKey.constraintName,
errorMessage: 'A primary key constraint cannot be created without any primary key selected',
},
);
};

const getCompositeUniqueKeys = jsonSchema => {
if (!Array.isArray(jsonSchema.uniqueKey)) {
return [];
}

return jsonSchema.uniqueKey
.filter(uniqueKey => !_.isEmpty(uniqueKey.compositeUniqueKey))
.map(uniqueKey => ({
...hydrateUniqueOptions(uniqueKey, null, null, jsonSchema),
columns: getKeys(uniqueKey.compositeUniqueKey, jsonSchema),
}));
return jsonSchema.uniqueKey.map(uniqueKey =>
!_.isEmpty(uniqueKey.compositeUniqueKey)
? {
...hydrateUniqueOptions(uniqueKey, null, null, jsonSchema),
columns: getKeys(uniqueKey.compositeUniqueKey, jsonSchema),
}
: {
name: uniqueKey.constraintName,
errorMessage: 'A unique key constraint cannot be created without any unique key selected',
},
);
};

const getTableKeyConstraints = jsonSchema => {
Expand Down
14 changes: 12 additions & 2 deletions properties_pane/entity_level/entityLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -675,12 +675,17 @@ making sure that you maintain a proper JSON format.
"propertyName": "Constraint name",
"propertyKeyword": "constraintName",
"propertyTooltip": "",
"propertyType": "text"
"propertyType": "text",
"validation": {
"indexKey": "compositePrimaryKey",
"message": "A primary key constraint cannot be created without any primary key selected"
}
},
{
"propertyName": "Key",
"propertyKeyword": "compositePrimaryKey",
"propertyType": "primaryKeySetter",
"requiredProperty": true,
"abbr": "pk"
},
{
Expand Down Expand Up @@ -764,12 +769,17 @@ making sure that you maintain a proper JSON format.
"propertyName": "Constraint name",
"propertyKeyword": "constraintName",
"propertyTooltip": "",
"propertyType": "text"
"propertyType": "text",
"validation": {
"indexKey": "compositeUniqueKey",
"message": "A unique key constraint cannot be created without any unique key selected"
}
},
{
"propertyName": "Key",
"propertyKeyword": "compositeUniqueKey",
"propertyType": "primaryKeySetter",
"requiredProperty": true,
"abbr": "uk"
},
{
Expand Down

0 comments on commit 5cb8f8f

Please sign in to comment.