-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimaryKeyHelper.js
More file actions
462 lines (396 loc) · 18.1 KB
/
primaryKeyHelper.js
File metadata and controls
462 lines (396 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
const _ = require('lodash');
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const { PrimaryKeyTransitionDto, KeyScriptModificationDto } = require('../../types/AlterKeyDto');
const {
getFullCollectionName,
getSchemaOfAlterCollection,
getEntityName,
wrapInQuotes,
isParentContainerActivated,
isObjectInDeltaModelActivated,
} = require('../../../utils/general');
const { alterPkConstraint, dropPK } = require('../../../ddlProvider/ddlHelpers/key/constraintsHelper');
const { KEY_TYPE } = require('../../../ddlProvider/ddlHelpers/key/keyHelper');
const { CONSTRAINT_POSTFIX } = require('../../../../constants/constants');
const amountOfColumnsInRegularPk = 1;
const getDefaultConstraintName = entityName => {
return [entityName, CONSTRAINT_POSTFIX.primaryKey].join('_');
};
const extractOptionsForComparisonWithRegularPkOptions = (optionHolder = {}) => {
return {
constraintName: optionHolder.constraintName,
deferClause: optionHolder.deferClause,
exceptionClause: optionHolder.exceptionClause,
id: optionHolder.id,
indexClause: optionHolder.indexClause,
rely: optionHolder.rely,
validate: optionHolder.validate,
};
};
const getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions = columnJsonSchema => {
return extractOptionsForComparisonWithRegularPkOptions(columnJsonSchema.primaryKeyOptions || {});
};
const getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions = compositePk => {
const optionsForComparison = extractOptionsForComparisonWithRegularPkOptions(compositePk);
return optionsForComparison;
};
const wasCompositePkChangedInTransitionFromCompositeToRegular = collection => {
const pkDto = collection?.role?.compMod?.primaryKey || {};
const oldPrimaryKeys = pkDto.old || [];
const idsOfColumns = oldPrimaryKeys.flatMap(pk => pk.compositePrimaryKey?.map(dto => dto.keyId) || []);
if (idsOfColumns.length !== amountOfColumnsInRegularPk) {
// We return false, because it wouldn't count as transition between regular PK and composite PK
// if composite PK did not constraint exactly 1 column
return PrimaryKeyTransitionDto.noTransition();
}
const idOfPkColumn = idsOfColumns[0];
const newColumnJsonSchema = Object.values(collection.properties).find(
columnJsonSchema => columnJsonSchema.GUID === idOfPkColumn,
);
if (!newColumnJsonSchema) {
return PrimaryKeyTransitionDto.noTransition();
}
const isNewColumnARegularPrimaryKey = newColumnJsonSchema?.primaryKey && !newColumnJsonSchema?.compositePrimaryKey;
if (!isNewColumnARegularPrimaryKey) {
return PrimaryKeyTransitionDto.noTransition();
}
const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(newColumnJsonSchema);
const areOptionsEqual = oldPrimaryKeys.some(compositePk => {
if (compositePk.compositePrimaryKey?.length !== amountOfColumnsInRegularPk) {
return false;
}
const oldCompositePkAsRegularPkOptions =
getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(compositePk);
return _.isEqual(oldCompositePkAsRegularPkOptions, constraintOptions);
});
return PrimaryKeyTransitionDto.transition(!areOptionsEqual);
};
const wasCompositePkChangedInTransitionFromRegularToComposite = collection => {
/**
* @type {AlterCollectionRoleCompModPrimaryKey}
*/
const pkDto = collection?.role?.compMod?.primaryKey || {};
/**
* @type {AlterCollectionRoleCompModPKDto[]}
*/
const newPrimaryKeys = pkDto.new || [];
const idsOfColumns = newPrimaryKeys.flatMap(pk => pk.compositePrimaryKey?.map(dto => dto.keyId) || []);
if (idsOfColumns.length !== amountOfColumnsInRegularPk) {
// We return false, because it wouldn't count as transition between regular PK and composite PK
// if composite PK does not constraint exactly 1 column
return PrimaryKeyTransitionDto.noTransition();
}
const idOfPkColumn = idsOfColumns[0];
const oldColumnJsonSchema = Object.values(collection.role.properties).find(
columnJsonSchema => columnJsonSchema.GUID === idOfPkColumn,
);
if (!oldColumnJsonSchema) {
return PrimaryKeyTransitionDto.noTransition();
}
const isOldColumnARegularPrimaryKey = oldColumnJsonSchema?.primaryKey && !oldColumnJsonSchema?.compositePrimaryKey;
if (!isOldColumnARegularPrimaryKey) {
return PrimaryKeyTransitionDto.noTransition();
}
const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(oldColumnJsonSchema);
const areOptionsEqual = newPrimaryKeys.some(compositePk => {
if (compositePk.compositePrimaryKey?.length !== amountOfColumnsInRegularPk) {
return false;
}
const oldCompositePkAsRegularPkOptions =
getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(compositePk);
return _.isEqual(oldCompositePkAsRegularPkOptions, constraintOptions);
});
return PrimaryKeyTransitionDto.transition(!areOptionsEqual);
};
const getConstraintNameForCompositePk = (primaryKey, entityName) => {
if (primaryKey.constraintName) {
return primaryKey.constraintName;
}
return getDefaultConstraintName(entityName);
};
const getCreateCompositePKDDLProviderConfig = (primaryKey, entityName, entity) => {
const constraintName = getConstraintNameForCompositePk(primaryKey, entityName);
const pkColumns = _.toPairs(entity.role.properties)
.filter(([name, jsonSchema]) =>
Boolean(primaryKey.compositePrimaryKey?.find(keyDto => keyDto.keyId === jsonSchema.GUID)),
)
.map(([name, jsonSchema]) => ({
name,
isActivated: jsonSchema.isActivated,
}));
return {
keyType: KEY_TYPE.primaryKey,
name: constraintName,
columns: pkColumns,
};
};
const getAddCompositePkScriptDtos = collection => {
/**
* @type {AlterCollectionRoleCompModPrimaryKey}
*/
const pkDto = collection?.role?.compMod?.primaryKey || {};
const newPrimaryKeys = pkDto.new || [];
const oldPrimaryKeys = pkDto.old || [];
if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) {
return [];
}
const transitionToCompositeDto = wasCompositePkChangedInTransitionFromRegularToComposite(collection);
if (transitionToCompositeDto.didTransitionHappen && !transitionToCompositeDto.wasPkChangedInTransition) {
return [];
}
if (newPrimaryKeys.length === oldPrimaryKeys.length) {
const areKeyArraysEqual = _(oldPrimaryKeys).differenceWith(newPrimaryKeys, _.isEqual).isEmpty();
if (areKeyArraysEqual) {
return [];
}
}
const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName({ collectionSchema });
const entityName = getEntityName(collectionSchema);
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isContainerActivated && isObjectInDeltaModelActivated(collection);
return newPrimaryKeys
.map(newPk => {
const ddlConfig = getCreateCompositePKDDLProviderConfig(newPk, entityName, collection);
if (_.isEmpty(ddlConfig.columns)) {
return null;
}
const statementDto = alterPkConstraint(fullTableName, isCollectionActivated, ddlConfig);
return new KeyScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated);
})
.filter(scriptDto => Boolean(scriptDto?.script));
};
const getDropCompositePkScriptDtos = collection => {
const pkDto = collection?.role?.compMod?.primaryKey || {};
const newPrimaryKeys = pkDto.new || [];
const oldPrimaryKeys = pkDto.old || [];
if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) {
return [];
}
const transitionToCompositeDto = wasCompositePkChangedInTransitionFromCompositeToRegular(collection);
if (transitionToCompositeDto.didTransitionHappen && !transitionToCompositeDto.wasPkChangedInTransition) {
return [];
}
if (newPrimaryKeys.length === oldPrimaryKeys.length) {
const areKeyArraysEqual = _(oldPrimaryKeys).differenceWith(newPrimaryKeys, _.isEqual).isEmpty();
if (areKeyArraysEqual) {
return [];
}
}
const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName({ collectionSchema });
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isContainerActivated && isObjectInDeltaModelActivated(collection);
return oldPrimaryKeys
.map(oldPk => {
const script = dropPK(fullTableName);
return new KeyScriptModificationDto(script, fullTableName, true, isCollectionActivated);
})
.filter(scriptDto => Boolean(scriptDto.script));
};
const getModifyCompositePkScriptDtos = collection => {
const dropCompositePkScriptDtos = getDropCompositePkScriptDtos(collection);
const addCompositePkScriptDtos = getAddCompositePkScriptDtos(collection);
return [...dropCompositePkScriptDtos, ...addCompositePkScriptDtos].filter(Boolean);
};
const getConstraintNameForRegularPk = (columnJsonSchema, entityName) => {
const constraintOptions = columnJsonSchema.primaryKeyOptions;
if (constraintOptions?.constraintName?.trim()) {
return constraintOptions.constraintName;
}
return getDefaultConstraintName(entityName);
};
const getCreateRegularPKDDLProviderConfig = (columnName, columnJsonSchema, entityName, entity) => {
const constraintName = getConstraintNameForRegularPk(columnJsonSchema, entityName);
const pkColumns = [
{
name: columnName,
isActivated: columnJsonSchema.isActivated,
},
];
return {
keyType: KEY_TYPE.primaryKey,
name: constraintName,
columns: pkColumns,
options: columnJsonSchema.primaryKeyOptions,
};
};
const wasFieldChangedToBeARegularPk = (columnJsonSchema, collection) => {
const oldName = columnJsonSchema.compMod.oldField.name;
const oldColumnJsonSchema = collection.role.properties[oldName];
const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey;
const wasTheFieldAnyPrimaryKey = Boolean(oldColumnJsonSchema?.primaryKey);
return isRegularPrimaryKey && !wasTheFieldAnyPrimaryKey;
};
const wasRegularPkChangedInTransitionFromCompositeToRegular = (columnJsonSchema, collection) => {
const oldName = columnJsonSchema.compMod.oldField.name;
const oldColumnJsonSchema = collection.role.properties[oldName];
const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey;
const wasTheFieldAnyPrimaryKey = Boolean(oldColumnJsonSchema?.primaryKey);
if (!(isRegularPrimaryKey && wasTheFieldAnyPrimaryKey)) {
return PrimaryKeyTransitionDto.noTransition();
}
const pkDto = collection?.role?.compMod?.primaryKey || {};
const newPrimaryKeys = pkDto.new || [];
const oldPrimaryKeys = pkDto.old || [];
const wasTheFieldACompositePrimaryKey = oldPrimaryKeys.some(compPk =>
compPk.compositePrimaryKey?.some(pk => pk.keyId === oldColumnJsonSchema.GUID),
);
const isTheFieldACompositePrimaryKey = newPrimaryKeys.some(compPk =>
compPk.compositePrimaryKey?.some(pk => pk.keyId === columnJsonSchema.GUID),
);
const wasCompositePkRemoved = wasTheFieldACompositePrimaryKey && !isTheFieldACompositePrimaryKey;
if (isRegularPrimaryKey && wasCompositePkRemoved) {
// return compare custom properties and amount of columns.
// If there was a transition and amount of composite PK columns is not equal
// to amount of regular pk columns, we must recreate PK
const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(columnJsonSchema);
const areOptionsEqual = oldPrimaryKeys.some(oldCompositePk => {
if (oldCompositePk.compositePrimaryKey?.length !== amountOfColumnsInRegularPk) {
return false;
}
const oldCompositePkAsRegularPkOptions =
getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(oldCompositePk);
return _.isEqual(oldCompositePkAsRegularPkOptions, constraintOptions);
});
return PrimaryKeyTransitionDto.transition(!areOptionsEqual);
}
return PrimaryKeyTransitionDto.noTransition();
};
const wasRegularPkChangedInTransitionFromRegularToComposite = (columnJsonSchema, collection) => {
const oldName = columnJsonSchema.compMod.oldField.name;
const oldColumnJsonSchema = collection.role.properties[oldName];
const wasRegularPrimaryKey = oldColumnJsonSchema.primaryKey && !oldColumnJsonSchema.compositePrimaryKey;
const isTheFieldAnyPrimaryKey = Boolean(columnJsonSchema?.primaryKey);
if (!(wasRegularPrimaryKey && isTheFieldAnyPrimaryKey)) {
return PrimaryKeyTransitionDto.noTransition();
}
const pkDto = collection?.role?.compMod?.primaryKey || {};
const newPrimaryKeys = pkDto.new || [];
const oldPrimaryKeys = pkDto.old || [];
const wasTheFieldACompositePrimaryKey = oldPrimaryKeys.some(compPk =>
compPk.compositePrimaryKey?.some(pk => pk.keyId === oldColumnJsonSchema.GUID),
);
const isTheFieldACompositePrimaryKey = newPrimaryKeys.some(compPk =>
compPk.compositePrimaryKey?.some(pk => pk.keyId === columnJsonSchema.GUID),
);
const wasCompositePkAdded = isTheFieldACompositePrimaryKey && !wasTheFieldACompositePrimaryKey;
if (wasRegularPrimaryKey && wasCompositePkAdded) {
// return compare custom properties and amount of columns.
// If there was a transition and amount of composite PK columns is not equal
// to amount of regular pk columns, we must recreate PK
const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(oldColumnJsonSchema);
const areOptionsEqual = newPrimaryKeys.some(oldCompositePk => {
if (oldCompositePk.compositePrimaryKey?.length !== amountOfColumnsInRegularPk) {
return false;
}
const oldCompositePkAsRegularPkOptions =
getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(oldCompositePk);
return _.isEqual(oldCompositePkAsRegularPkOptions, constraintOptions);
});
return PrimaryKeyTransitionDto.transition(!areOptionsEqual);
}
return PrimaryKeyTransitionDto.noTransition();
};
const isFieldNoLongerARegularPk = (columnJsonSchema, collection) => {
const oldName = columnJsonSchema.compMod.oldField.name;
const oldJsonSchema = collection.role.properties[oldName];
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
const isNotAnyPrimaryKey = !columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey;
return wasTheFieldARegularPrimaryKey && isNotAnyPrimaryKey;
};
const wasRegularPkModified = (columnJsonSchema, collection) => {
const oldName = columnJsonSchema.compMod.oldField.name;
const oldJsonSchema = collection.role.properties[oldName] || {};
const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey;
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
if (!(isRegularPrimaryKey && wasTheFieldARegularPrimaryKey)) {
return false;
}
const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(columnJsonSchema);
const oldConstraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(oldJsonSchema);
return !_.isEqual(oldConstraintOptions, constraintOptions);
};
const getAddPkScriptDtos = collection => {
const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName({ collectionSchema });
const entityName = getEntityName(collectionSchema);
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isContainerActivated && isObjectInDeltaModelActivated(collection);
return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
if (wasFieldChangedToBeARegularPk(jsonSchema, collection)) {
return true;
}
const transitionToRegularDto = wasRegularPkChangedInTransitionFromCompositeToRegular(
jsonSchema,
collection,
);
if (transitionToRegularDto.didTransitionHappen) {
return transitionToRegularDto.wasPkChangedInTransition;
}
return wasRegularPkModified(jsonSchema, collection);
})
.map(([name, jsonSchema]) => {
const ddlConfig = getCreateRegularPKDDLProviderConfig(name, jsonSchema, entityName, collection);
const statementDto = alterPkConstraint(fullTableName, isCollectionActivated, ddlConfig);
return new KeyScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated);
})
.filter(scriptDto => Boolean(scriptDto.script));
};
const getDropPkScriptDto = collection => {
const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName({ collectionSchema });
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isContainerActivated && isObjectInDeltaModelActivated(collection);
return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
if (isFieldNoLongerARegularPk(jsonSchema, collection)) {
return true;
}
const transitionToRegularDto = wasRegularPkChangedInTransitionFromRegularToComposite(
jsonSchema,
collection,
);
if (transitionToRegularDto.didTransitionHappen) {
return transitionToRegularDto.wasPkChangedInTransition;
}
return wasRegularPkModified(jsonSchema, collection);
})
.map(([name, jsonSchema]) => {
const script = dropPK(fullTableName);
return new KeyScriptModificationDto(script, fullTableName, true, isCollectionActivated);
})
.filter(scriptDto => Boolean(scriptDto.script));
};
const getModifyPkScriptDtos = collection => {
const dropPkScriptDtos = getDropPkScriptDto(collection);
const addPkScriptDtos = getAddPkScriptDtos(collection);
return [...dropPkScriptDtos, ...addPkScriptDtos].filter(Boolean);
};
const sortModifyPkConstraints = constraintDtos => {
return constraintDtos.sort((c1, c2) => {
if (c1.fullTableName === c2.fullTableName) {
// Number(true) = 1, Number(false) = 0;
// This ensures that DROP script appears before CREATE script
// if the same table has 2 scripts that drop and recreate PK
return Number(c2.isDropScript) - Number(c1.isDropScript);
}
// This sorts all statements based on full table name, ASC
return c1.fullTableName.localeCompare(c2.fullTableName);
});
};
const getModifyPkConstraintsScriptDtos = collection => {
const modifyCompositePkScriptDtos = getModifyCompositePkScriptDtos(collection);
const modifyPkScriptDtos = getModifyPkScriptDtos(collection);
const allDtos = [...modifyCompositePkScriptDtos, ...modifyPkScriptDtos];
const sortedAllDtos = sortModifyPkConstraints(allDtos);
return sortedAllDtos
.map(dto => {
return AlterScriptDto.getInstance([dto.script], dto.isActivated, dto.isDropScript);
})
.filter(Boolean);
};
module.exports = {
getModifyPkConstraintsScriptDtos,
};