Skip to content

Commit a827c17

Browse files
author
unknown
committed
Fixed errors:
- Not taking into account dropped pks with custom properties - Using new name of constraint instead of old one when dropping updated constraint
1 parent b30a849 commit a827c17

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,49 @@ const getCreateRegularPKDDLProviderConfig = (_) => (
286286
}
287287

288288

289+
/**
290+
* @return {(columnJsonSchema: AlterCollectionColumnDto, collection: AlterCollectionDto) => boolean}
291+
* */
292+
const wasFieldChangedToBeARegularPk = (_) => (columnJsonSchema, collection) => {
293+
const oldName = columnJsonSchema.compMod.oldField.name;
294+
295+
const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey;
296+
const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey);
297+
return isRegularPrimaryKey && !wasTheFieldAPrimaryKey;
298+
}
299+
300+
/**
301+
* @return {(columnJsonSchema: AlterCollectionColumnDto, collection: AlterCollectionDto) => boolean}
302+
* */
303+
const isFieldNoLongerARegularPk = (_) => (columnJsonSchema, collection) => {
304+
const oldName = columnJsonSchema.compMod.oldField.name;
305+
306+
const oldJsonSchema = collection.role.properties[oldName];
307+
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
308+
309+
const isNotAPrimaryKey = !columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey;
310+
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
311+
}
312+
313+
/**
314+
* @return {(columnJsonSchema: AlterCollectionColumnDto, collection: AlterCollectionDto) => boolean}
315+
* */
316+
const wasRegularPkModified = (_) => (columnJsonSchema, collection) => {
317+
const oldName = columnJsonSchema.compMod.oldField.name;
318+
const oldJsonSchema = collection.role.properties[oldName];
319+
320+
const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey;
321+
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
322+
323+
if (!(isRegularPrimaryKey && wasTheFieldARegularPrimaryKey)) {
324+
return false;
325+
}
326+
const constraintOptions = columnJsonSchema.primaryKeyOptions;
327+
const oldConstraintOptions = oldJsonSchema?.primaryKeyOptions;
328+
const areOptionsEqual = _(oldConstraintOptions).differenceWith(constraintOptions, _.isEqual).isEmpty();
329+
return !areOptionsEqual;
330+
}
331+
289332
/**
290333
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
291334
* */
@@ -302,10 +345,7 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
302345

303346
return _.toPairs(collection.properties)
304347
.filter(([name, jsonSchema]) => {
305-
const isRegularPrimaryKey = jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
306-
const oldName = jsonSchema.compMod.oldField.name;
307-
const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey);
308-
return isRegularPrimaryKey && !wasTheFieldAPrimaryKey;
348+
return wasFieldChangedToBeARegularPk(_)(jsonSchema, collection) || wasRegularPkModified(_)(jsonSchema, collection);
309349
})
310350
.map(([name, jsonSchema]) => {
311351
const ddlConfig = getCreateRegularPKDDLProviderConfig(_)(name, jsonSchema, entityName, collection);
@@ -336,15 +376,12 @@ const getDropPkScript = (_, ddlProvider) => (collection) => {
336376

337377
return _.toPairs(collection.properties)
338378
.filter(([name, jsonSchema]) => {
339-
const oldName = jsonSchema.compMod.oldField.name;
340-
const oldJsonSchema = collection.role.properties[oldName];
341-
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
342-
343-
const isNotAPrimaryKey = !jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
344-
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
379+
return isFieldNoLongerARegularPk(_)(jsonSchema, collection) || wasRegularPkModified(_)(jsonSchema, collection);
345380
})
346381
.map(([name, jsonSchema]) => {
347-
const constraintName = wrapInQuotes(getConstraintNameForRegularPk(jsonSchema, entityName));
382+
const oldName = jsonSchema.compMod.oldField.name;
383+
const oldJsonSchema = collection.role.properties[oldName];
384+
const constraintName = wrapInQuotes(getConstraintNameForRegularPk(oldJsonSchema, entityName));
348385
return ddlProvider.dropPkConstraint(fullTableName, constraintName);
349386
})
350387
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true))

0 commit comments

Comments
 (0)