Skip to content

Commit 40e7f20

Browse files
author
unknown
committed
Relationships that exist between deactivated entities are now also deactivated. Deactivated columns are now commented out
1 parent bffd112 commit 40e7f20

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

forward_engineering/alterScript/alterScriptHelpers/alterRelationshipsHelper.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ const getFullChildTableName = (_) => (relationship) => {
2525
}
2626

2727
/**
28-
* @return {(relationship: AlterRelationshipDto) => string}
28+
* @return {(relationship: AlterRelationshipDto) => {
29+
* isActivated: boolean,
30+
* statement: string,
31+
* }}
2932
* */
30-
const getAddSingleForeignKeyScript = (ddlProvider, _) => (relationship) => {
33+
const getAddSingleForeignKeyStatementDto = (ddlProvider, _) => (relationship) => {
3134
const compMod = relationship.role.compMod;
3235

3336
const relationshipName = compMod.name?.new || getRelationshipName(relationship) || '';
@@ -43,7 +46,8 @@ const getAddSingleForeignKeyScript = (ddlProvider, _) => (relationship) => {
4346
primaryTable: compMod.parent.collection.name,
4447
primarySchemaName: compMod.parent.bucket.name,
4548
primaryTableActivated: compMod.parent.collection.isActivated,
46-
}).statement;
49+
isActivated: Boolean(relationship.role?.compMod?.isActivated?.new),
50+
});
4751
}
4852

4953
/**
@@ -73,26 +77,35 @@ const getAddForeignKeyScriptDtos = (ddlProvider, _) => (addedRelationships) => {
7377
return addedRelationships
7478
.filter((relationship) => canRelationshipBeAdded(relationship))
7579
.map(relationship => {
76-
const script = getAddSingleForeignKeyScript(ddlProvider, _)(relationship);
77-
const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new);
78-
return AlterScriptDto.getInstance([script], isActivated, false);
80+
const scriptDto = getAddSingleForeignKeyStatementDto(ddlProvider, _)(relationship);
81+
return AlterScriptDto.getInstance([scriptDto.statement], scriptDto.isActivated, false);
7982
})
8083
.filter(Boolean)
8184
.filter(res => res.scripts.some(scriptDto => Boolean(scriptDto.script)));
8285
}
8386

8487
/**
85-
* @return {(relationship: AlterRelationshipDto) => string}
88+
* @return {(relationship: AlterRelationshipDto) => {
89+
* isActivated: boolean,
90+
* statement: string,
91+
* }}
8692
* */
87-
const getDeleteSingleForeignKeyScript = (ddlProvider, _) => (relationship) => {
93+
const getDeleteSingleForeignKeyStatementDto = (ddlProvider, _) => (relationship) => {
8894
const {wrapInQuotes} = require('../../utils/general')(_);
8995
const compMod = relationship.role.compMod;
9096

9197
const ddlChildEntityName = getFullChildTableName(_)(relationship);
9298

9399
const relationshipName = compMod.name?.old || getRelationshipName(relationship) || '';
94100
const ddlRelationshipName = wrapInQuotes(relationshipName);
95-
return ddlProvider.dropForeignKey(ddlChildEntityName, ddlRelationshipName);
101+
const statement = ddlProvider.dropForeignKey(ddlChildEntityName, ddlRelationshipName);
102+
103+
const isRelationshipActivated = Boolean(relationship.role?.compMod?.isActivated?.new);
104+
const isChildTableActivated = compMod.child.collection.isActivated;
105+
return {
106+
statement,
107+
isActivated: isRelationshipActivated && isChildTableActivated,
108+
}
96109
}
97110

98111
/**
@@ -118,9 +131,8 @@ const getDeleteForeignKeyScriptDtos = (ddlProvider, _) => (deletedRelationships)
118131
return deletedRelationships
119132
.filter((relationship) => canRelationshipBeDeleted(relationship))
120133
.map(relationship => {
121-
const script = getDeleteSingleForeignKeyScript(ddlProvider, _)(relationship);
122-
const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new);
123-
return AlterScriptDto.getInstance([script], isActivated, true);
134+
const scriptDto = getDeleteSingleForeignKeyStatementDto(ddlProvider, _)(relationship);
135+
return AlterScriptDto.getInstance([scriptDto.statement], scriptDto.isActivated, true);
124136
})
125137
.filter(Boolean)
126138
.filter(res => res.scripts.some(scriptDto => Boolean(scriptDto.script)));
@@ -133,10 +145,10 @@ const getModifyForeignKeyScriptDtos = (ddlProvider, _) => (modifiedRelationships
133145
return modifiedRelationships
134146
.filter(relationship => canRelationshipBeAdded(relationship) && canRelationshipBeDeleted(relationship))
135147
.map(relationship => {
136-
const deleteScript = getDeleteSingleForeignKeyScript(ddlProvider, _)(relationship);
137-
const addScript = getAddSingleForeignKeyScript(ddlProvider, _)(relationship);
138-
const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new);
139-
return AlterScriptDto.getDropAndRecreateInstance(deleteScript, addScript, isActivated);
148+
const deleteScriptDto = getDeleteSingleForeignKeyStatementDto(ddlProvider, _)(relationship);
149+
const addScriptDto = getAddSingleForeignKeyStatementDto(ddlProvider, _)(relationship);
150+
const isActivated = addScriptDto.isActivated && deleteScriptDto.isActivated;
151+
return AlterScriptDto.getDropAndRecreateInstance(deleteScriptDto.statement, addScriptDto.statement, isActivated);
140152
})
141153
.filter(Boolean)
142154
.filter(res => res.scripts.some(scriptDto => Boolean(scriptDto.script)));

forward_engineering/ddlProvider/ddlProvider.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,29 +368,28 @@ module.exports = (baseProvider, options, app) => {
368368
) {
369369
const isAllPrimaryKeysDeactivated = checkAllKeysDeactivated(primaryKey);
370370
const isAllForeignKeysDeactivated = checkAllKeysDeactivated(foreignKey);
371-
const isRelationshipActivated =
371+
const areKeysActivated =
372372
!isAllPrimaryKeysDeactivated &&
373373
!isAllForeignKeysDeactivated &&
374374
primaryTableActivated &&
375-
foreignTableActivated &&
376-
isActivated;
375+
foreignTableActivated;
377376

378377
const { foreignOnDelete, foreignOnUpdate, foreignMatch } =
379378
additionalPropertiesForForeignKey(customProperties);
380379

381380
const foreignKeyStatement = assignTemplates(templates.createForeignKeyConstraint, {
382381
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
383382
name: name ? `CONSTRAINT ${wrapInQuotes(name)}` : '',
384-
foreignKey: isRelationshipActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
385-
primaryKey: isRelationshipActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
383+
foreignKey: areKeysActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
384+
primaryKey: areKeysActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
386385
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
387386
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
388387
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
389388
});
390389

391390
return {
392391
statement: _.trim(foreignKeyStatement),
393-
isActivated: isRelationshipActivated,
392+
isActivated: areKeysActivated && isActivated,
394393
};
395394
},
396395

@@ -444,12 +443,11 @@ module.exports = (baseProvider, options, app) => {
444443
) {
445444
const isAllPrimaryKeysDeactivated = checkAllKeysDeactivated(primaryKey);
446445
const isAllForeignKeysDeactivated = checkAllKeysDeactivated(foreignKey);
447-
const isRelationshipActivated =
446+
const areKeysActivated =
448447
!isAllPrimaryKeysDeactivated &&
449448
!isAllForeignKeysDeactivated &&
450449
primaryTableActivated &&
451-
foreignTableActivated &&
452-
isActivated;
450+
foreignTableActivated;
453451

454452
const { foreignOnDelete, foreignOnUpdate, foreignMatch } =
455453
additionalPropertiesForForeignKey(customProperties);
@@ -458,16 +456,16 @@ module.exports = (baseProvider, options, app) => {
458456
primaryTable: getNamePrefixedWithSchemaName(primaryTable, primarySchemaName || schemaData.schemaName),
459457
foreignTable: getNamePrefixedWithSchemaName(foreignTable, foreignSchemaName || schemaData.schemaName),
460458
name: name ? wrapInQuotes(name) : '',
461-
foreignKey: isRelationshipActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
462-
primaryKey: isRelationshipActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
459+
foreignKey: areKeysActivated ? foreignKeysToString(foreignKey) : foreignActiveKeysToString(foreignKey),
460+
primaryKey: areKeysActivated ? foreignKeysToString(primaryKey) : foreignActiveKeysToString(primaryKey),
463461
onDelete: foreignOnDelete ? ` ON DELETE ${foreignOnDelete}` : '',
464462
onUpdate: foreignOnUpdate ? ` ON UPDATE ${foreignOnUpdate}` : '',
465463
match: foreignMatch ? ` MATCH ${foreignMatch}` : '',
466464
});
467465

468466
return {
469467
statement: _.trim(foreignKeyStatement),
470-
isActivated: isRelationshipActivated,
468+
isActivated: areKeysActivated && isActivated,
471469
};
472470
},
473471

0 commit comments

Comments
 (0)