Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down
41 changes: 37 additions & 4 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')({
_,
Expand Down Expand Up @@ -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);
Expand All @@ -571,31 +587,48 @@ 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),
foreignTable: getTableName(foreignTable, dbData.databaseName),
name,
foreignKey: foreignKeysToString(foreignKey),
primaryKey: foreignKeysToString(primaryKey),
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
}),
isActivated:
!isAllPrimaryKeysDeactivated &&
Expand Down
7 changes: 7 additions & 0 deletions forward_engineering/helpers/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -331,5 +337,6 @@ module.exports = (_, wrap) => {
getCharacteristics,
escapeQuotes,
wrapInTicks,
additionalPropertiesForForeignKey,
};
};
21 changes: 17 additions & 4 deletions properties_pane/model_level/modelLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ making sure that you maintain a proper JSON format.
}
]
},
{
"lowerTab": "Relationships",
"structure": []
},
{
"lowerTab": "Tablespaces",
"structure": [
Expand Down Expand Up @@ -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"]
}
]
}
]
Loading