diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index ccbc0e6..01a8bad 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -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')({ @@ -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); @@ -531,7 +544,7 @@ module.exports = (baseProvider, options, app) => { } jsonSchema = _.omit(jsonSchema, '$ref'); - return { ...definitionJsonSchema, ...jsonSchema }; + return { ...definitionJsonSchema, ...jsonSchema }; }, hydrateIndex(indexData, tableData, schemaData) { diff --git a/forward_engineering/helpers/constraintsHelper.js b/forward_engineering/helpers/constraintsHelper.js index 5a3d5f7..636e3f9 100644 --- a/forward_engineering/helpers/constraintsHelper.js +++ b/forward_engineering/helpers/constraintsHelper.js @@ -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, }; }; diff --git a/forward_engineering/helpers/keyHelper.js b/forward_engineering/helpers/keyHelper.js index fe3e623..e5a4199 100644 --- a/forward_engineering/helpers/keyHelper.js +++ b/forward_engineering/helpers/keyHelper.js @@ -91,12 +91,17 @@ 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 => { @@ -104,12 +109,17 @@ module.exports = (_, clean) => { 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 => { diff --git a/properties_pane/entity_level/entityLevelConfig.json b/properties_pane/entity_level/entityLevelConfig.json index 2b68438..4b220a0 100644 --- a/properties_pane/entity_level/entityLevelConfig.json +++ b/properties_pane/entity_level/entityLevelConfig.json @@ -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" }, { @@ -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" }, {