Skip to content

Commit fd26c16

Browse files
author
unknown
committed
Started to work on pk constraints
1 parent 40e7f20 commit fd26c16

File tree

6 files changed

+598
-15
lines changed

6 files changed

+598
-15
lines changed

forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const {getModifyNonNullColumnsScriptDtos} = require("./columnHelpers/nonNullCons
55
const {getModifiedCommentOnColumnScriptDtos} = require("./columnHelpers/commentsHelper");
66
const {getRenameColumnScriptDtos} = require("./columnHelpers/renameColumnHelper");
77
const {AlterScriptDto} = require("../types/AlterScriptDto");
8+
const {AlterCollectionDto} = require('../types/AlterCollectionDto');
9+
const {getModifyPkConstraintsScriptDtos} = require("./entityHelpers/primaryKeyHelper");
810

911

1012
/**
11-
* @return {(collection: Object) => AlterScriptDto | undefined}
13+
* @return {(collection: AlterCollectionDto) => AlterScriptDto | undefined}
1214
* */
1315
const getAddCollectionScriptDto =
1416
({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) =>
@@ -58,7 +60,7 @@ const getAddCollectionScriptDto =
5860
};
5961

6062
/**
61-
* @return {(collection: Object) => AlterScriptDto | undefined}
63+
* @return {(collection: AlterCollectionDto) => AlterScriptDto | undefined}
6264
* */
6365
const getDeleteCollectionScriptDto = app => collection => {
6466
const _ = app.require('lodash');
@@ -71,14 +73,15 @@ const getDeleteCollectionScriptDto = app => collection => {
7173
};
7274

7375
/**
74-
* @return {(collection: Object) => AlterScriptDto[]}
76+
* @return {(collection: AlterCollectionDto) => AlterScriptDto[]}
7577
* */
7678
const getModifyCollectionScriptDtos = (app) => (collection) => {
7779
const _ = app.require('lodash');
7880
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
7981

8082
const modifyCheckConstraintScriptDtos = getModifyCheckConstraintScriptDtos(_, ddlProvider)(collection);
8183
const modifyCommentScriptDtos = getModifyEntityCommentsScriptDtos(_, ddlProvider)(collection);
84+
const modifyPKConstraintDtos = getModifyPkConstraintsScriptDtos(_, ddlProvider)(collection);
8285
return [
8386
...modifyCheckConstraintScriptDtos,
8487
...modifyCommentScriptDtos

forward_engineering/alterScript/alterScriptHelpers/entityHelpers/checkConstraintHelper.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const {AlterCollectionDto} = require('../../types/AlterCollectionDto');
2+
const {AlterScriptDto} = require("../../types/AlterScriptDto");
3+
14
/**
25
* @typedef {{
36
* id: string,
@@ -11,11 +14,8 @@
1114
* }} CheckConstraintHistoryEntry
1215
* */
1316

14-
15-
const {AlterScriptDto} = require("../../types/AlterScriptDto");
16-
1717
/**
18-
* @return {(collection: Object) => Array<CheckConstraintHistoryEntry>}
18+
* @return {(collection: AlterCollectionDto) => Array<CheckConstraintHistoryEntry>}
1919
* */
2020
const mapCheckConstraintNamesToChangeHistory = (_) => (collection) => {
2121
const checkConstraintHistory = collection?.compMod?.chkConstr;
@@ -98,7 +98,7 @@ const getUpdateCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistor
9898
}
9999

100100
/**
101-
* @return {(collection: Object) => Array<AlterScriptDto>}
101+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
102102
* */
103103
const getModifyCheckConstraintScriptDtos = (_, ddlProvider) => (collection) => {
104104
const {getFullTableName} = require('../../../utils/general')(_);

forward_engineering/alterScript/alterScriptHelpers/entityHelpers/commentsHelper.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const {AlterScriptDto} = require("../../types/AlterScriptDto");
2+
const {AlterCollectionDto} = require('../../types/AlterCollectionDto');
23

34
/**
4-
* @return {(collection: Object) => AlterScriptDto}
5+
* @return {(collection: AlterCollectionDto) => AlterScriptDto}
56
*/
67
const getUpdatedCommentOnCollectionScriptDto = (_, ddlProvider) => (collection) => {
78
const {getFullTableName, wrapComment} = require('../../../utils/general')(_);
@@ -24,7 +25,7 @@ const getUpdatedCommentOnCollectionScriptDto = (_, ddlProvider) => (collection)
2425
}
2526

2627
/**
27-
* @return {(collection: Object) => AlterScriptDto}
28+
* @return {(collection: AlterCollectionDto) => AlterScriptDto}
2829
*/
2930
const getDeletedCommentOnCollectionScriptDto = (_, ddlProvider) => (collection) => {
3031
const {getFullTableName} = require('../../../utils/general')(_);
@@ -46,7 +47,7 @@ const getDeletedCommentOnCollectionScriptDto = (_, ddlProvider) => (collection)
4647
}
4748

4849
/**
49-
* @return {(collection: Object) => Array<AlterScriptDto>}
50+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
5051
* */
5152
const getModifyEntityCommentsScriptDtos = (_, ddlProvider) => collection => {
5253
const updatedCommentScript = getUpdatedCommentOnCollectionScriptDto(_, ddlProvider)(collection);
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
const { AlterScriptDto } = require('../../types/AlterScriptDto');
2+
const {AlterCollectionDto, AlterCollectionColumnDto,AlterCollectionRoleCompModPKDto} = require('../../types/AlterCollectionDto');
3+
4+
5+
/**
6+
* @return {(collection: AlterCollectionDto, guid: string) => AlterCollectionColumnDto | undefined}
7+
* */
8+
const getPropertyByGuid = (_) => (collection, guid) => {
9+
const propertyInArray = _.toPairs(collection?.role?.properties)
10+
.filter(([name, jsonSchema]) => jsonSchema.GUID === guid)
11+
.map(([name, jsonSchema]) => jsonSchema);
12+
if (!propertyInArray.length) {
13+
return undefined;
14+
}
15+
return propertyInArray[0];
16+
}
17+
18+
/**
19+
* @return {(collection: AlterCollectionDto, guids: string[]) => Array<AlterCollectionColumnDto>}
20+
* */
21+
const getPropertiesByGuids = (_) => (collection, guids) => {
22+
return guids
23+
.map(guid => getPropertyByGuid(_)(collection, guid))
24+
.filter(Boolean);
25+
}
26+
27+
/**
28+
* @return {(collection: AlterCollectionDto) => boolean}
29+
* */
30+
const didCompositePkChange = (_) => (collection) => {
31+
const pkDto = collection?.role?.compMod?.primaryKey || {};
32+
const newPrimaryKeys = pkDto.new || [];
33+
const oldPrimaryKeys = pkDto.old || [];
34+
if (newPrimaryKeys.length !== oldPrimaryKeys.length) {
35+
return true;
36+
}
37+
if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) {
38+
return false;
39+
}
40+
const areKeyArraysEqual = _(oldPrimaryKeys).differenceWith(newPrimaryKeys, _.isEqual).isEmpty();
41+
return !areKeyArraysEqual;
42+
}
43+
44+
/**
45+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
46+
* */
47+
const getAddCompositePkScripts = (_, ddlProvider) => (collection) => {
48+
const didPkChange = didCompositePkChange(_)(collection);
49+
if (!didPkChange) {
50+
return []
51+
}
52+
const fullTableName = generateFullEntityName(collection);
53+
const constraintName = getEntityNameFromCollection(collection) + '_pk';
54+
const pkDto = collection?.role?.compMod?.primaryKey || {};
55+
const newPrimaryKeys = pkDto.new || [];
56+
57+
return newPrimaryKeys
58+
.map((newPk) => {
59+
/**
60+
* @type {Array<AlterCollectionRoleCompModPKDto>}
61+
* */
62+
const compositePrimaryKey = newPk.compositePrimaryKey || [];
63+
const guidsOfColumnsInPk = compositePrimaryKey.map((compositePkEntry) => compositePkEntry.keyId);
64+
const columnsInPk = getPropertiesByGuids(_)(collection, guidsOfColumnsInPk);
65+
const columnNamesForDDL = columnsInPk.map(column => prepareName(column.compMod.newField.name));
66+
if (!columnNamesForDDL.length) {
67+
return undefined;
68+
}
69+
return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL);
70+
})
71+
.filter(Boolean)
72+
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false))
73+
.filter(Boolean);
74+
}
75+
76+
/**
77+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
78+
* */
79+
const getDropCompositePkScripts = (_, ddlProvider) => (collection) => {
80+
const didPkChange = didCompositePkChange(_)(collection);
81+
if (!didPkChange) {
82+
return []
83+
}
84+
const fullTableName = generateFullEntityName(collection);
85+
const pkDto = collection?.role?.compMod?.primaryKey || {};
86+
const oldPrimaryKeys = pkDto.old || [];
87+
return oldPrimaryKeys
88+
.map(oldPk => ddlProvider.dropPkConstraint(fullTableName))
89+
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true))
90+
.filter(Boolean);
91+
}
92+
93+
/**
94+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
95+
* */
96+
const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => {
97+
const dropCompositePkScripts = getDropCompositePkScripts(_, ddlProvider)(collection);
98+
const addCompositePkScripts = getAddCompositePkScripts(_, ddlProvider)(collection);
99+
100+
return [
101+
...dropCompositePkScripts,
102+
...addCompositePkScripts,
103+
].filter(Boolean);
104+
}
105+
106+
/**
107+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
108+
* */
109+
const getAddPkScripts = (_, ddlProvider) => (collection) => {
110+
const { getFullCollectionName, getSchemaOfAlterCollection } = require('../../../utils/general')(_);
111+
const collectionSchema = getSchemaOfAlterCollection(collection);
112+
113+
const fullTableName = getFullCollectionName(collectionSchema);
114+
const constraintName = getEntityNameFromCollection(collection) + '_pk';
115+
116+
return _.toPairs(collection.properties)
117+
.filter(([name, jsonSchema]) => {
118+
const isRegularPrimaryKey = jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
119+
const oldName = jsonSchema.compMod.oldField.name;
120+
const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey);
121+
return isRegularPrimaryKey && !wasTheFieldAPrimaryKey;
122+
})
123+
.map(([name, jsonSchema]) => {
124+
const nameForDDl = prepareName(name);
125+
const columnNamesForDDL = [nameForDDl];
126+
return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL);
127+
})
128+
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false))
129+
.filter(Boolean);
130+
}
131+
132+
/**
133+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
134+
* */
135+
const getDropPkScripts = (_, ddlProvider) => (collection) => {
136+
const fullTableName = generateFullEntityName(collection);
137+
138+
return _.toPairs(collection.properties)
139+
.filter(([name, jsonSchema]) => {
140+
const oldName = jsonSchema.compMod.oldField.name;
141+
const oldJsonSchema = collection.role.properties[oldName];
142+
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
143+
144+
const isNotAPrimaryKey = !jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
145+
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
146+
})
147+
.map(([name, jsonSchema]) => {
148+
return ddlProvider.dropConstraint(fullTableName, );
149+
})
150+
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true))
151+
.filter(Boolean);
152+
}
153+
154+
/**
155+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
156+
* */
157+
const getModifyPkScripts = (_, ddlProvider) => (collection) => {
158+
const dropPkScripts = getDropPkScripts(_, ddlProvider)(collection);
159+
const addPkScripts = getAddPkScripts(_, ddlProvider)(collection);
160+
161+
return [
162+
...dropPkScripts,
163+
...addPkScripts,
164+
].filter(Boolean);
165+
}
166+
167+
/**
168+
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
169+
* */
170+
const getModifyPkConstraintsScriptDtos = (_, ddlProvider) => (collection) => {
171+
const modifyCompositePkScripts = getModifyCompositePkScripts(_, ddlProvider)(collection);
172+
const modifyPkScripts = getModifyPkScripts(_, ddlProvider)(collection);
173+
174+
return [
175+
...modifyCompositePkScripts,
176+
...modifyPkScripts,
177+
].filter(Boolean);
178+
}
179+
180+
module.exports = {
181+
getModifyPkConstraintsScriptDtos,
182+
}

0 commit comments

Comments
 (0)