Skip to content

Commit 2c4fd3c

Browse files
author
DrMyroslav
committed
add additional properties for FK
1 parent c57509e commit 2c4fd3c

File tree

6 files changed

+109
-3
lines changed

6 files changed

+109
-3
lines changed

forward_engineering/configs/templates.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ module.exports = {
2020

2121
checkConstraint: '${name} CHECK (${expression})${noInherit}',
2222

23-
createForeignKeyConstraint: '${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey})',
23+
createForeignKeyConstraint: '${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey})${match}${onDelete}${onUpdate}',
2424

2525
createKeyConstraint: '${constraintName}${keyType}${columns}${includeNonKey}${storageParameters}${tablespace}',
2626

2727
createForeignKey:
28-
'ALTER TABLE IF EXISTS ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey});',
28+
'ALTER TABLE IF EXISTS ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey})${match}${onDelete}${onUpdate};',
2929

3030
index:
3131
'CREATE${unique} INDEX${concurrently}${ifNotExist} ${name}\n' +

forward_engineering/ddlProvider.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ module.exports = (baseProvider, options, app) => {
2626
divideIntoActivatedAndDeactivated,
2727
commentIfDeactivated,
2828
});
29-
const { generateConstraintsString, foreignKeysToString, foreignActiveKeysToString, createKeyConstraint } =
29+
const {
30+
generateConstraintsString,
31+
foreignKeysToString,
32+
foreignActiveKeysToString,
33+
createKeyConstraint,
34+
additionalPropertiesForForeignKey
35+
} =
3036
require('./helpers/constraintsHelper')({
3137
_,
3238
commentIfDeactivated,
@@ -309,6 +315,7 @@ module.exports = (baseProvider, options, app) => {
309315
foreignTableActivated,
310316
foreignSchemaName,
311317
primarySchemaName,
318+
customProperties
312319
},
313320
dbData,
314321
schemaData,
@@ -321,11 +328,16 @@ module.exports = (baseProvider, options, app) => {
321328
primaryTableActivated &&
322329
foreignTableActivated;
323330

331+
const { foreignOnDelete, foreignOnUpdate, foreignMatch } = additionalPropertiesForForeignKey(customProperties);
332+
324333
const foreignKeyStatement = assignTemplates(templates.createForeignKeyConstraint, {
325334
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
326335
name: name ? `CONSTRAINT ${wrapInQuotes(name)}` : '',
327336
foreignKey: isActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
328337
primaryKey: isActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
338+
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
339+
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
340+
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
329341
});
330342

331343
return {
@@ -345,6 +357,7 @@ module.exports = (baseProvider, options, app) => {
345357
foreignTableActivated,
346358
foreignSchemaName,
347359
primarySchemaName,
360+
customProperties,
348361
},
349362
dbData,
350363
schemaData,
@@ -357,12 +370,17 @@ module.exports = (baseProvider, options, app) => {
357370
primaryTableActivated &&
358371
foreignTableActivated;
359372

373+
const { foreignOnDelete, foreignOnUpdate, foreignMatch } = additionalPropertiesForForeignKey(customProperties);
374+
360375
const foreignKeyStatement = assignTemplates(templates.createForeignKey, {
361376
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
362377
foreignTable: getNamePrefixedWithSchemaName(foreignTable, foreignSchemaName || schemaData.schemaName),
363378
name: name ? wrapInQuotes(name) : '',
364379
foreignKey: isActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
365380
primaryKey: isActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
381+
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
382+
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
383+
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
366384
});
367385

368386
return {

forward_engineering/helpers/constraintsHelper.js

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

72+
const additionalPropertiesForForeignKey = relationship => {
73+
const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', '');
74+
const foreignOnUpdate = _.get(relationship, 'relationshipOnUpdate', '');
75+
const foreignMatch = _.get(relationship, 'relationshipMatch', '');
76+
return { foreignOnDelete, foreignOnUpdate, foreignMatch }
77+
};
78+
7279
return {
7380
generateConstraintsString,
7481
foreignKeysToString,
7582
foreignActiveKeysToString,
7683
createKeyConstraint,
84+
additionalPropertiesForForeignKey,
7785
};
7886
};

properties_pane/model_level/modelLevelConfig.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,47 @@ making sure that you maintain a proper JSON format.
205205
"template": "textarea"
206206
}
207207
]
208+
},
209+
{
210+
"lowerTab": "Relationships",
211+
"structure": [
212+
{
213+
"propertyName": "On Delete",
214+
"propertyKeyword": "relationshipOnDelete",
215+
"propertyType": "select",
216+
"options": [
217+
"",
218+
"NO ACTION",
219+
"RESTRICT",
220+
"CASCADE",
221+
"SET NULL",
222+
"SET DEFAULT"
223+
]
224+
},
225+
{
226+
"propertyName": "On Update",
227+
"propertyKeyword": "relationshipOnUpdate",
228+
"propertyType": "select",
229+
"options": [
230+
"",
231+
"NO ACTION",
232+
"RESTRICT",
233+
"CASCADE",
234+
"SET NULL",
235+
"SET DEFAULT"
236+
]
237+
},
238+
{
239+
"propertyName": "Match",
240+
"propertyKeyword": "relationshipMatch",
241+
"propertyType": "select",
242+
"options": [
243+
"",
244+
"SIMPLE",
245+
"PARTIAL",
246+
"FULL"
247+
]
248+
}
249+
]
208250
}
209251
]

reverse_engineering/helpers/postgresHelpers/foreignKeysHelper.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@ const setDependencies = app => {
66
_ = app.require('lodash');
77
};
88

9+
const prepareDeleteAndUpdate = value => {
10+
switch (value) {
11+
case 'a':
12+
return 'NO ACTION';
13+
case 'r':
14+
return 'RESTRICT';
15+
case 'c':
16+
return 'CASCADE';
17+
case 'n':
18+
return 'SET NULL';
19+
case 'd':
20+
return 'SET DEFAULT';
21+
default:
22+
return '';
23+
}
24+
};
25+
26+
const prepareMatch = value => {
27+
switch (value) {
28+
case 'f':
29+
return 'FULL';
30+
case 's':
31+
return 'SIMPLE';
32+
case 'p':
33+
return 'PARTIAL';
34+
default:
35+
return '';
36+
}
37+
};
38+
939
const prepareForeignKeys = (tableForeignKeys, tableName, schemaName, columns) => {
1040
return _.map(tableForeignKeys, foreignKeyData => {
1141
return {
@@ -17,6 +47,11 @@ const prepareForeignKeys = (tableForeignKeys, tableName, schemaName, columns) =>
1747
childCollection: tableName,
1848
childField: _.map(foreignKeyData.table_columns_positions, getColumnNameByPosition(columns)),
1949
relationshipType: 'Foreign Key',
50+
customProperties: {
51+
relationshipOnDelete: prepareDeleteAndUpdate(foreignKeyData.relationship_on_delete),
52+
relationshipOnUpdate: prepareDeleteAndUpdate(foreignKeyData.relationship_on_update),
53+
relationshipMatch: prepareMatch(foreignKeyData.relationship_match),
54+
}
2055
};
2156
});
2257
};

reverse_engineering/helpers/queryConstants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ const queryConstants = {
176176
GET_TABLE_FOREIGN_KEYS: `
177177
SELECT pcon.conname AS relationship_name,
178178
pcon.conkey AS table_columns_positions,
179+
pcon.confdeltype AS relationship_on_delete,
180+
pcon.confupdtype AS relationship_on_update,
181+
pcon.confmatchtype AS relationship_match,
179182
pc_foreign_table.relname AS foreign_table_name,
180183
ARRAY(
181184
SELECT column_name::text FROM unnest(pcon.confkey) AS column_position

0 commit comments

Comments
 (0)