Skip to content

Commit 8cdcc1d

Browse files
author
Sasha
authored
Merge pull request #76 from ArzamastsevVladyslav/fix/HCK-4261-missing-deferrable-clause-on-constraints
Fix/hck 4261 missing deferrable clause on constraints
2 parents 5a51e94 + 5b077ed commit 8cdcc1d

File tree

7 files changed

+469
-14
lines changed

7 files changed

+469
-14
lines changed

forward_engineering/ddlProvider/ddlHelpers/constraintsHelper.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ module.exports = ({
8080
: '';
8181
const storageParameters = keyData.storageParameters ? ` WITH (${keyData.storageParameters})` : '';
8282
const tablespace = keyData.tablespace ? ` USING INDEX TABLESPACE ${wrapInQuotes(keyData.tablespace)}` : '';
83+
const deferrable = keyData?.deferrable ? ` ${keyData.deferrable}` : '';
84+
const deferrableConstraintCheckTime = keyData?.deferrable === 'DEFERRABLE' && keyData?.deferrableConstraintCheckTime
85+
? ` ${keyData?.deferrableConstraintCheckTime}` : '';
8386

8487
return {
8588
statement: assignTemplates(templates.createKeyConstraint, {
@@ -89,6 +92,8 @@ module.exports = ({
8992
includeNonKey,
9093
storageParameters,
9194
tablespace,
95+
deferrable,
96+
deferrableConstraintCheckTime,
9297
}),
9398
isActivated: !isAllColumnsDeactivated,
9499
};
@@ -111,11 +116,29 @@ module.exports = ({
111116
);
112117
};
113118

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};
119+
/**
120+
* @param relationshipCustomProperties {{
121+
* relationshipOnDelete?: string,
122+
* relationshipOnUpdate?: string,
123+
* relationshipMatch?: string,
124+
* deferrable?: "" | "DEFERRABLE" | "NOT DEFERRABLE",
125+
* deferrableConstraintCheckTime?: "" | "INITIALLY IMMEDIATE" | "INITIALLY DEFERRED",
126+
* }}
127+
* @return {{
128+
* relationshipOnDelete: string,
129+
* relationshipOnUpdate: string,
130+
* relationshipMatch: string,
131+
* deferrable: string,
132+
* deferrableConstraintCheckTime: string,
133+
* }}
134+
* */
135+
const additionalPropertiesForForeignKey = relationshipCustomProperties => {
136+
const foreignOnDelete = _.get(relationshipCustomProperties, 'relationshipOnDelete', '');
137+
const foreignOnUpdate = _.get(relationshipCustomProperties, 'relationshipOnUpdate', '');
138+
const foreignMatch = _.get(relationshipCustomProperties, 'relationshipMatch', '');
139+
const deferrable = _.get(relationshipCustomProperties, 'deferrable', '');
140+
const deferrableConstraintCheckTime = _.get(relationshipCustomProperties, 'deferrableConstraintCheckTime', '');
141+
return {foreignOnDelete, foreignOnUpdate, foreignMatch, deferrable, deferrableConstraintCheckTime};
119142
};
120143

121144
return {

forward_engineering/ddlProvider/ddlHelpers/keyHelper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ module.exports = (_, clean) => {
5959
comment: options['indexComment'],
6060
tablespace: options['indexTablespace'],
6161
nullsDistinct: options['nullsDistinct'],
62+
deferrable: options['deferrable'],
63+
deferrableConstraintCheckTime: options['deferrableConstraintCheckTime'],
6264
});
6365

6466
const hydratePrimaryKeyOptions = (options, columnName, isActivated, jsonSchema) =>
@@ -75,6 +77,8 @@ module.exports = (_, clean) => {
7577
storageParameters: options['indexStorageParameters'],
7678
comment: options['indexComment'],
7779
tablespace: options['indexTablespace'],
80+
deferrable: options['deferrable'],
81+
deferrableConstraintCheckTime: options['deferrableConstraintCheckTime'],
7882
});
7983

8084
const findName = (keyId, properties) => {

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ 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

25-
createKeyConstraint: '${constraintName}${keyType}${columns}${includeNonKey}${storageParameters}${tablespace}',
25+
createKeyConstraint: '${constraintName}${keyType}${columns}${includeNonKey}${storageParameters}${tablespace}${deferrable}${deferrableConstraintCheckTime}',
2626

2727
alterColumnType: 'ALTER TABLE IF EXISTS ${tableName} ALTER COLUMN ${columnName} SET DATA TYPE ${dataType};',
2828

@@ -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

properties_pane/entity_level/entityLevelConfig.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,26 @@ making sure that you maintain a proper JSON format.
709709
"addTimestampButton": false,
710710
"propertyType": "details",
711711
"template": "textarea"
712+
},
713+
{
714+
"propertyName": "Deferrable",
715+
"propertyKeyword": "deferrable",
716+
"propertyTooltip": "\nA constraint that is not deferrable will be checked immediately after every command. Checking of constraints that are deferrable can be postponed until the end of the transaction",
717+
"propertyType": "select",
718+
"defaultValue": "",
719+
"options": ["", "DEFERRABLE", "NOT DEFERRABLE"]
720+
},
721+
{
722+
"propertyName": "Check time",
723+
"propertyKeyword": "deferrableConstraintCheckTime",
724+
"propertyTooltip": "\nIf the constraint is INITIALLY IMMEDIATE, it is checked after each statement. This is the default. If the constraint is INITIALLY DEFERRED, it is checked only at the end of the transaction",
725+
"propertyType": "select",
726+
"defaultValue": "",
727+
"options": ["", "INITIALLY IMMEDIATE", "INITIALLY DEFERRED"],
728+
"dependency": {
729+
"key": "deferrable",
730+
"value": "DEFERRABLE"
731+
}
712732
}
713733
],
714734
"disabledOnCondition": [
@@ -824,6 +844,26 @@ making sure that you maintain a proper JSON format.
824844
}
825845
]
826846
}
847+
},
848+
{
849+
"propertyName": "Deferrable",
850+
"propertyKeyword": "deferrable",
851+
"propertyTooltip": "\nA constraint that is not deferrable will be checked immediately after every command. Checking of constraints that are deferrable can be postponed until the end of the transaction",
852+
"propertyType": "select",
853+
"defaultValue": "",
854+
"options": ["", "DEFERRABLE", "NOT DEFERRABLE"]
855+
},
856+
{
857+
"propertyName": "Check time",
858+
"propertyKeyword": "deferrableConstraintCheckTime",
859+
"propertyTooltip": "\nIf the constraint is INITIALLY IMMEDIATE, it is checked after each statement. This is the default. If the constraint is INITIALLY DEFERRED, it is checked only at the end of the transaction",
860+
"propertyType": "select",
861+
"defaultValue": "",
862+
"options": ["", "INITIALLY IMMEDIATE", "INITIALLY DEFERRED"],
863+
"dependency": {
864+
"key": "deferrable",
865+
"value": "DEFERRABLE"
866+
}
827867
}
828868
]
829869
}

0 commit comments

Comments
 (0)