diff --git a/forward_engineering/configs/templates.js b/forward_engineering/configs/templates.js index 3954fe6..ad0bb3f 100644 --- a/forward_engineering/configs/templates.js +++ b/forward_engineering/configs/templates.js @@ -14,12 +14,12 @@ module.exports = { checkConstraint: 'CONSTRAINT ${name}CHECK (${expression})${enforcement}', createForeignKeyConstraint: - 'CONSTRAINT `${name}` FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey})', + 'CONSTRAINT `${name}` FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey})${onDelete}${onUpdate}', createKeyConstraint: '${constraintName}${keyType}${columns}${using}${blockSize}${comment}${ignore}', createForeignKey: - 'ALTER TABLE ${foreignTable} ADD CONSTRAINT `${name}` FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey});', + 'ALTER TABLE ${foreignTable} ADD CONSTRAINT `${name}` FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey})${onDelete}${onUpdate};', index: 'CREATE ${indexType}INDEX ${name}${indexCategory}\n' + '\tON ${table} ( ${keys} )${indexOptions};\n', diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index a4b6a48..001c14b 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -20,8 +20,16 @@ module.exports = (baseProvider, options, app) => { const { assignTemplates, compareGroupItems } = app.require('@hackolade/ddl-fe-utils'); const { decorateDefault, decorateType, canBeNational, getSign, createGeneratedColumn, canHaveAutoIncrement } = require('./helpers/columnDefinitionHelper')(wrap); - const { getTableName, getTableOptions, getPartitions, getViewData, getCharacteristics, escapeQuotes, wrapInTicks } = - require('./helpers/general')(_, wrap); + const { + getTableName, + getTableOptions, + getPartitions, + getViewData, + getCharacteristics, + escapeQuotes, + wrapInTicks, + additionalPropertiesForForeignKey, + } = require('./helpers/general')(_, wrap); const { generateConstraintsString, foreignKeysToString, foreignActiveKeysToString, createKeyConstraint } = require('./helpers/constraintsHelper')({ _, @@ -560,7 +568,15 @@ module.exports = (baseProvider, options, app) => { }, createForeignKeyConstraint( - { name, foreignKey, primaryTable, primaryKey, primaryTableActivated, foreignTableActivated }, + { + name, + foreignKey, + primaryTable, + primaryKey, + primaryTableActivated, + foreignTableActivated, + customProperties, + }, dbData, ) { const isAllPrimaryKeysDeactivated = checkAllKeysDeactivated(primaryKey); @@ -571,24 +587,39 @@ module.exports = (baseProvider, options, app) => { primaryTableActivated && foreignTableActivated; + const { foreignOnDelete, foreignOnUpdate } = additionalPropertiesForForeignKey(customProperties); + return { statement: assignTemplates(templates.createForeignKeyConstraint, { primaryTable: getTableName(primaryTable, dbData.databaseName), name, foreignKey: isActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey), primaryKey: isActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey), + onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '', + onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '', }), isActivated, }; }, createForeignKey( - { name, foreignTable, foreignKey, primaryTable, primaryKey, primaryTableActivated, foreignTableActivated }, + { + name, + foreignTable, + foreignKey, + primaryTable, + primaryKey, + primaryTableActivated, + foreignTableActivated, + customProperties, + }, dbData, ) { const isAllPrimaryKeysDeactivated = checkAllKeysDeactivated(primaryKey); const isAllForeignKeysDeactivated = checkAllKeysDeactivated(foreignKey); + const { foreignOnDelete, foreignOnUpdate } = additionalPropertiesForForeignKey(customProperties); + return { statement: assignTemplates(templates.createForeignKey, { primaryTable: getTableName(primaryTable, dbData.databaseName), @@ -596,6 +627,8 @@ module.exports = (baseProvider, options, app) => { name, foreignKey: foreignKeysToString(foreignKey), primaryKey: foreignKeysToString(primaryKey), + onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '', + onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '', }), isActivated: !isAllPrimaryKeysDeactivated && diff --git a/forward_engineering/helpers/general.js b/forward_engineering/helpers/general.js index aad7603..da81839 100644 --- a/forward_engineering/helpers/general.js +++ b/forward_engineering/helpers/general.js @@ -323,6 +323,12 @@ module.exports = (_, wrap) => { return str.replace(/(')/gi, "'$1").replace(/\n/gi, '\\n'); }; + const additionalPropertiesForForeignKey = customProperties => { + const foreignOnDelete = customProperties?.relationshipOnDelete ?? ''; + const foreignOnUpdate = customProperties?.relationshipOnUpdate ?? ''; + return { foreignOnDelete, foreignOnUpdate }; + }; + return { getTableName, getTableOptions, @@ -331,5 +337,6 @@ module.exports = (_, wrap) => { getCharacteristics, escapeQuotes, wrapInTicks, + additionalPropertiesForForeignKey, }; }; diff --git a/properties_pane/model_level/modelLevelConfig.json b/properties_pane/model_level/modelLevelConfig.json index eb5395a..a2b6046 100644 --- a/properties_pane/model_level/modelLevelConfig.json +++ b/properties_pane/model_level/modelLevelConfig.json @@ -181,10 +181,6 @@ making sure that you maintain a proper JSON format. } ] }, - { - "lowerTab": "Relationships", - "structure": [] - }, { "lowerTab": "Tablespaces", "structure": [ @@ -310,5 +306,22 @@ making sure that you maintain a proper JSON format. ] } ] + }, + { + "lowerTab": "Relationships", + "structure": [ + { + "propertyName": "On Delete", + "propertyKeyword": "relationshipOnDelete", + "propertyType": "select", + "options": ["", "NO ACTION", "CASCADE", "SET NULL", "SET DEFAULT", "RESTRICT"] + }, + { + "propertyName": "On Update", + "propertyKeyword": "relationshipOnUpdate", + "propertyType": "select", + "options": ["", "NO ACTION", "CASCADE", "SET NULL", "SET DEFAULT", "RESTRICT"] + } + ] } ]