Skip to content

Commit 70a6304

Browse files
author
unknown
committed
Added support for dropping composite PK constraints (with and without custom name)
1 parent b8a380b commit 70a6304

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ const getModifyCollectionScriptDtos = (app) => (collection) => {
8484
const modifyPKConstraintDtos = getModifyPkConstraintsScriptDtos(_, ddlProvider)(collection);
8585
return [
8686
...modifyCheckConstraintScriptDtos,
87-
...modifyCommentScriptDtos
87+
...modifyCommentScriptDtos,
88+
...modifyPKConstraintDtos
8889
].filter(Boolean);
8990
}
9091

forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,47 +58,70 @@ const getDefaultConstraintName = (wrapInQuotes) => (entityName) => {
5858
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
5959
* */
6060
const getAddCompositePkScripts = (_, ddlProvider) => (collection) => {
61-
const didPkChange = didCompositePkChange(_)(collection);
62-
if (!didPkChange) {
63-
return []
64-
}
65-
const fullTableName = generateFullEntityName(collection);
66-
const constraintName = getEntityNameFromCollection(collection) + '_pk';
67-
const pkDto = collection?.role?.compMod?.primaryKey || {};
68-
const newPrimaryKeys = pkDto.new || [];
61+
// const didPkChange = didCompositePkChange(_)(collection);
62+
// if (!didPkChange) {
63+
// return []
64+
// }
65+
// const fullTableName = generateFullEntityName(collection);
66+
// const constraintName = getEntityNameFromCollection(collection) + '_pk';
67+
// const pkDto = collection?.role?.compMod?.primaryKey || {};
68+
// const newPrimaryKeys = pkDto.new || [];
69+
//
70+
// return newPrimaryKeys
71+
// .map((newPk) => {
72+
// /**
73+
// * @type {Array<AlterCollectionRoleCompModPKDto>}
74+
// * */
75+
// const compositePrimaryKey = newPk.compositePrimaryKey || [];
76+
// const guidsOfColumnsInPk = compositePrimaryKey.map((compositePkEntry) => compositePkEntry.keyId);
77+
// const columnsInPk = getPropertiesByGuids(_)(collection, guidsOfColumnsInPk);
78+
// const columnNamesForDDL = columnsInPk.map(column => prepareName(column.compMod.newField.name));
79+
// if (!columnNamesForDDL.length) {
80+
// return undefined;
81+
// }
82+
// return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL);
83+
// })
84+
// .filter(Boolean)
85+
// .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false))
86+
// .filter(Boolean);
6987

70-
return newPrimaryKeys
71-
.map((newPk) => {
72-
/**
73-
* @type {Array<AlterCollectionRoleCompModPKDto>}
74-
* */
75-
const compositePrimaryKey = newPk.compositePrimaryKey || [];
76-
const guidsOfColumnsInPk = compositePrimaryKey.map((compositePkEntry) => compositePkEntry.keyId);
77-
const columnsInPk = getPropertiesByGuids(_)(collection, guidsOfColumnsInPk);
78-
const columnNamesForDDL = columnsInPk.map(column => prepareName(column.compMod.newField.name));
79-
if (!columnNamesForDDL.length) {
80-
return undefined;
81-
}
82-
return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL);
83-
})
84-
.filter(Boolean)
85-
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false))
86-
.filter(Boolean);
88+
return [];
8789
}
8890

8991
/**
9092
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
9193
* */
9294
const getDropCompositePkScripts = (_, ddlProvider) => (collection) => {
95+
const {
96+
getFullCollectionName,
97+
getSchemaOfAlterCollection,
98+
getEntityName,
99+
wrapInQuotes
100+
} = require('../../../utils/general')(_);
101+
93102
const didPkChange = didCompositePkChange(_)(collection);
94103
if (!didPkChange) {
95-
return []
104+
return [];
96105
}
97-
const fullTableName = generateFullEntityName(collection);
106+
107+
const collectionSchema = getSchemaOfAlterCollection(collection);
108+
const fullTableName = getFullCollectionName(collectionSchema);
109+
const entityName = getEntityName(collectionSchema);
110+
98111
const pkDto = collection?.role?.compMod?.primaryKey || {};
112+
/**
113+
* @type {AlterCollectionRoleCompModPKDto[]}
114+
* */
99115
const oldPrimaryKeys = pkDto.old || [];
116+
100117
return oldPrimaryKeys
101-
.map(oldPk => ddlProvider.dropPkConstraint(fullTableName))
118+
.map((oldPk) => {
119+
let constraintName = getDefaultConstraintName(wrapInQuotes)(entityName);
120+
if (oldPk.constraintName) {
121+
constraintName = wrapInQuotes(oldPk.constraintName);
122+
}
123+
return ddlProvider.dropPkConstraint(fullTableName, constraintName);
124+
})
102125
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true))
103126
.filter(Boolean);
104127
}
@@ -108,11 +131,11 @@ const getDropCompositePkScripts = (_, ddlProvider) => (collection) => {
108131
* */
109132
const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => {
110133
const dropCompositePkScripts = getDropCompositePkScripts(_, ddlProvider)(collection);
111-
const addCompositePkScripts = getAddCompositePkScripts(_, ddlProvider)(collection);
134+
// const addCompositePkScripts = getAddCompositePkScripts(_, ddlProvider)(collection);
112135

113136
return [
114137
...dropCompositePkScripts,
115-
...addCompositePkScripts,
138+
// ...addCompositePkScripts,
116139
].filter(Boolean);
117140
}
118141

@@ -208,23 +231,23 @@ const getDropPkScript = (_, ddlProvider) => (collection) => {
208231
* */
209232
const getModifyPkScripts = (_, ddlProvider) => (collection) => {
210233
const dropPkScripts = getDropPkScript(_, ddlProvider)(collection);
211-
const addPkScripts = getAddPkScripts(_, ddlProvider)(collection);
234+
// const addPkScripts = getAddPkScripts(_, ddlProvider)(collection);
212235

213236
return [
214237
...dropPkScripts,
215-
...addPkScripts,
238+
// ...addPkScripts,
216239
].filter(Boolean);
217240
}
218241

219242
/**
220243
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
221244
* */
222245
const getModifyPkConstraintsScriptDtos = (_, ddlProvider) => (collection) => {
223-
// const modifyCompositePkScripts = getModifyCompositePkScripts(_, ddlProvider)(collection);
246+
const modifyCompositePkScripts = getModifyCompositePkScripts(_, ddlProvider)(collection);
224247
const modifyPkScripts = getModifyPkScripts(_, ddlProvider)(collection);
225248

226249
return [
227-
// ...modifyCompositePkScripts,
250+
...modifyCompositePkScripts,
228251
...modifyPkScripts,
229252
].filter(Boolean);
230253
}

forward_engineering/alterScript/types/AlterCollectionDto.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,30 @@ class AlterCollectionRoleCompModPKDto {
142142
/**
143143
* @type {string}
144144
* */
145-
keyId
145+
id
146+
147+
/**
148+
* @type {Array<{
149+
* type: string,
150+
* keyId: string,
151+
* }>}
152+
* */
153+
compositePrimaryKey
154+
155+
/**
156+
* @type {string | undefined}
157+
* */
158+
constraintName
159+
160+
/**
161+
* @type {string | undefined}
162+
* */
163+
indexStorageParameters
164+
165+
/**
166+
* @type {string | undefined}
167+
* */
168+
indexTablespace
146169
}
147170

148171
class AlterCollectionRoleCompModDto {
@@ -202,12 +225,8 @@ class AlterCollectionRoleCompModDto {
202225

203226
/**
204227
* @type {{
205-
* new: Array<{
206-
* compositePrimaryKey: AlterCollectionRoleCompModPKDto[],
207-
* }>,
208-
* old: Array<{
209-
* compositePrimaryKey: AlterCollectionRoleCompModPKDto[],
210-
* }>,
228+
* new: Array<AlterCollectionRoleCompModPKDto>,
229+
* old: Array<AlterCollectionRoleCompModPKDto>,
211230
* }}
212231
*/
213232
primaryKey

0 commit comments

Comments
 (0)