Skip to content

Commit 10b8ae2

Browse files
author
DrMyroslav
committed
fix conflict
2 parents 9408794 + 1561517 commit 10b8ae2

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = (baseProvider, options, app) => {
3232
foreignActiveKeysToString,
3333
createKeyConstraint,
3434
getConstraintsWarnings,
35+
additionalPropertiesForForeignKey,
3536
} = require('./helpers/constraintsHelper')({
3637
_,
3738
commentIfDeactivated,
@@ -322,6 +323,7 @@ module.exports = (baseProvider, options, app) => {
322323
foreignTableActivated,
323324
foreignSchemaName,
324325
primarySchemaName,
326+
customProperties
325327
},
326328
dbData,
327329
schemaData,
@@ -334,11 +336,16 @@ module.exports = (baseProvider, options, app) => {
334336
primaryTableActivated &&
335337
foreignTableActivated;
336338

339+
const { foreignOnDelete, foreignOnUpdate, foreignMatch } = additionalPropertiesForForeignKey(customProperties);
340+
337341
const foreignKeyStatement = assignTemplates(templates.createForeignKeyConstraint, {
338342
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
339343
name: name ? `CONSTRAINT ${wrapInQuotes(name)}` : '',
340344
foreignKey: isActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
341345
primaryKey: isActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
346+
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
347+
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
348+
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
342349
});
343350

344351
return {
@@ -358,6 +365,7 @@ module.exports = (baseProvider, options, app) => {
358365
foreignTableActivated,
359366
foreignSchemaName,
360367
primarySchemaName,
368+
customProperties,
361369
},
362370
dbData,
363371
schemaData,
@@ -370,12 +378,17 @@ module.exports = (baseProvider, options, app) => {
370378
primaryTableActivated &&
371379
foreignTableActivated;
372380

381+
const { foreignOnDelete, foreignOnUpdate, foreignMatch } = additionalPropertiesForForeignKey(customProperties);
382+
373383
const foreignKeyStatement = assignTemplates(templates.createForeignKey, {
374384
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
375385
foreignTable: getNamePrefixedWithSchemaName(foreignTable, foreignSchemaName || schemaData.schemaName),
376386
name: name ? wrapInQuotes(name) : '',
377387
foreignKey: isActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
378388
primaryKey: isActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
389+
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
390+
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
391+
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
379392
});
380393

381394
return {

forward_engineering/helpers/constraintsHelper.js

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

72+
7273
const getConstraintsWarnings = (invalidConstraints = []) => {
7374
if (_.isEmpty(invalidConstraints)) {
7475
return '';
@@ -86,11 +87,19 @@ module.exports = ({
8687
);
8788
};
8889

90+
const additionalPropertiesForForeignKey = relationship => {
91+
const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', '');
92+
const foreignOnUpdate = _.get(relationship, 'relationshipOnUpdate', '');
93+
const foreignMatch = _.get(relationship, 'relationshipMatch', '');
94+
return { foreignOnDelete, foreignOnUpdate, foreignMatch }
95+
};
96+
8997
return {
9098
generateConstraintsString,
9199
foreignKeysToString,
92100
foreignActiveKeysToString,
93101
createKeyConstraint,
94102
getConstraintsWarnings,
103+
additionalPropertiesForForeignKey,
95104
};
96105
};

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+
relationshipInfo: {
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)