Skip to content

Commit da59574

Browse files
author
unknown
committed
Added ability to model deferrable FK constraints
1 parent e7a841b commit da59574

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

forward_engineering/ddlProvider/ddlHelpers/constraintsHelper.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,29 @@ module.exports = ({
111111
);
112112
};
113113

114-
const additionalPropertiesForForeignKey = relationship => {
115-
const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', '');
116-
const foreignOnUpdate = _.get(relationship, 'relationshipOnUpdate', '');
117-
const foreignMatch = _.get(relationship, 'relationshipMatch', '');
118-
return {foreignOnDelete, foreignOnUpdate, foreignMatch};
114+
/**
115+
* @param relationshipCustomProperties {{
116+
* relationshipOnDelete?: string,
117+
* relationshipOnUpdate?: string,
118+
* relationshipMatch?: string,
119+
* deferrable?: "" | "DEFERRABLE" | "NOT DEFERRABLE",
120+
* deferrableConstraintCheckTime?: "" | "INITIALLY IMMEDIATE" | "INITIALLY DEFERRED",
121+
* }}
122+
* @return {{
123+
* relationshipOnDelete: string,
124+
* relationshipOnUpdate: string,
125+
* relationshipMatch: string,
126+
* deferrable: string,
127+
* deferrableConstraintCheckTime: string,
128+
* }}
129+
* */
130+
const additionalPropertiesForForeignKey = relationshipCustomProperties => {
131+
const foreignOnDelete = _.get(relationshipCustomProperties, 'relationshipOnDelete', '');
132+
const foreignOnUpdate = _.get(relationshipCustomProperties, 'relationshipOnUpdate', '');
133+
const foreignMatch = _.get(relationshipCustomProperties, 'relationshipMatch', '');
134+
const deferrable = _.get(relationshipCustomProperties, 'deferrable', '');
135+
const deferrableConstraintCheckTime = _.get(relationshipCustomProperties, 'deferrableConstraintCheckTime', '');
136+
return {foreignOnDelete, foreignOnUpdate, foreignMatch, deferrable, deferrableConstraintCheckTime};
119137
};
120138

121139
return {

forward_engineering/ddlProvider/ddlProvider.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ module.exports = (baseProvider, options, app) => {
329329
* relationshipOnDelete?: string,
330330
* relationshipOnUpdate?: string,
331331
* relationshipMatch?: string,
332+
* deferrable?: "" | "DEFERRABLE" | "NOT DEFERRABLE",
333+
* deferrableConstraintCheckTime?: "" | "INITIALLY IMMEDIATE" | "INITIALLY DEFERRED",
332334
* }}
333335
* @param primaryTableActivated {boolean}
334336
* @param foreignTableActivated {boolean}
@@ -376,7 +378,7 @@ module.exports = (baseProvider, options, app) => {
376378
primaryTableActivated &&
377379
foreignTableActivated;
378380

379-
const { foreignOnDelete, foreignOnUpdate, foreignMatch } =
381+
const { foreignOnDelete, foreignOnUpdate, foreignMatch, deferrable, deferrableConstraintCheckTime } =
380382
additionalPropertiesForForeignKey(customProperties);
381383

382384
const foreignKeyStatement = assignTemplates(templates.createForeignKeyConstraint, {
@@ -387,6 +389,9 @@ module.exports = (baseProvider, options, app) => {
387389
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
388390
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
389391
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
392+
deferrable: deferrable ? ` ${deferrable}` : '',
393+
deferrableConstraintCheckTime: deferrable === 'DEFERRABLE' && deferrableConstraintCheckTime
394+
? ` ${deferrableConstraintCheckTime}` : '',
390395
});
391396

392397
return {
@@ -451,7 +456,7 @@ module.exports = (baseProvider, options, app) => {
451456
primaryTableActivated &&
452457
foreignTableActivated;
453458

454-
const { foreignOnDelete, foreignOnUpdate, foreignMatch } =
459+
const { foreignOnDelete, foreignOnUpdate, foreignMatch, deferrable, deferrableConstraintCheckTime } =
455460
additionalPropertiesForForeignKey(customProperties);
456461

457462
const foreignKeyStatement = assignTemplates(templates.createForeignKey, {
@@ -463,6 +468,9 @@ module.exports = (baseProvider, options, app) => {
463468
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
464469
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
465470
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
471+
deferrable: deferrable ? ` ${deferrable}` : '',
472+
deferrableConstraintCheckTime: deferrable === 'DEFERRABLE' && deferrableConstraintCheckTime
473+
? ` ${deferrableConstraintCheckTime}` : '',
466474
});
467475

468476
return {

forward_engineering/ddlProvider/templates.js

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

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

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

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

@@ -37,7 +37,7 @@ module.exports = {
3737
dropConstraint: 'ALTER TABLE IF EXISTS ${tableName} DROP CONSTRAINT IF EXISTS ${constraintName};',
3838

3939
createForeignKey:
40-
'ALTER TABLE IF EXISTS ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey})${match}${onDelete}${onUpdate};',
40+
'ALTER TABLE IF EXISTS ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey})${match}${onDelete}${onUpdate}${deferrable}${deferrableConstraintCheckTime};',
4141

4242
dropForeignKey: 'ALTER TABLE ${tableName} DROP CONSTRAINT ${fkConstraintName};',
4343

0 commit comments

Comments
 (0)