Skip to content

Commit bd69424

Browse files
author
unknown
committed
Added support for adding composite PK constraints
1 parent 9075001 commit bd69424

File tree

2 files changed

+119
-59
lines changed

2 files changed

+119
-59
lines changed

forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js

Lines changed: 117 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,119 @@ const getDefaultConstraintName = (entityName) => {
3131
return `${entityName}_pk`;
3232
}
3333

34+
/**
35+
* @param primaryKey {AlterCollectionRoleCompModPKDto}
36+
* @param entityName {string}
37+
* @return {string}
38+
* */
39+
const getConstraintNameForCompositePk = (primaryKey, entityName) => {
40+
if (primaryKey.constraintName) {
41+
return primaryKey.constraintName;
42+
}
43+
return getDefaultConstraintName(entityName);
44+
}
45+
46+
/**
47+
* @param _
48+
* @param wrapInQuotes {(s: string) => string }
49+
* @return {(
50+
* primaryKey: AlterCollectionRoleCompModPKDto,
51+
* entityName: string,
52+
* entityJsonSchema: AlterCollectionDto,
53+
* ) => {
54+
* name: string,
55+
* keyType: string,
56+
* columns: Array<{
57+
* isActivated: boolean,
58+
* name: string,
59+
* }>,
60+
* include: Array<{
61+
* isActivated: boolean,
62+
* name: string,
63+
* }>,
64+
* storageParameters: string,
65+
* tablespace: string,
66+
* }
67+
* }
68+
* */
69+
const getCreateCompositePKDDLProviderConfig = (_) => (
70+
primaryKey,
71+
entityName,
72+
entity
73+
) => {
74+
const constraintName = getConstraintNameForCompositePk(primaryKey, entityName);
75+
const pkColumns = _.toPairs(entity.properties)
76+
.filter(([name, jsonSchema]) => Boolean(primaryKey.compositePrimaryKey.find(keyDto => keyDto.keyId === jsonSchema.GUID)))
77+
.map(([name, jsonSchema]) => ({
78+
name,
79+
isActivated: jsonSchema.isActivated,
80+
}));
81+
82+
let storageParameters = '';
83+
let indexTablespace = '';
84+
let includeColumns = [];
85+
if (primaryKey.indexStorageParameters) {
86+
storageParameters = primaryKey.indexStorageParameters;
87+
}
88+
if (primaryKey.indexTablespace) {
89+
indexTablespace = primaryKey.indexTablespace;
90+
}
91+
if (primaryKey.indexInclude) {
92+
includeColumns = _.toPairs(entity.role.properties)
93+
.filter(([name, jsonSchema]) => Boolean(primaryKey.indexInclude.find(keyDto => keyDto.keyId === jsonSchema.GUID)))
94+
.map(([name, jsonSchema]) => ({
95+
name,
96+
isActivated: jsonSchema.isActivated,
97+
}));
98+
}
99+
100+
return {
101+
name: constraintName,
102+
keyType: 'PRIMARY KEY',
103+
columns: pkColumns,
104+
include: includeColumns,
105+
storageParameters,
106+
tablespace: indexTablespace,
107+
}
108+
}
109+
34110
/**
35111
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
36112
* */
37113
const getAddCompositePkScripts = (_, ddlProvider) => (collection) => {
38-
// const didPkChange = didCompositePkChange(_)(collection);
39-
// if (!didPkChange) {
40-
// return []
41-
// }
42-
// const fullTableName = generateFullEntityName(collection);
43-
// const constraintName = getEntityNameFromCollection(collection) + '_pk';
44-
// const pkDto = collection?.role?.compMod?.primaryKey || {};
45-
// const newPrimaryKeys = pkDto.new || [];
46-
//
47-
// return newPrimaryKeys
48-
// .map((newPk) => {
49-
// /**
50-
// * @type {Array<AlterCollectionRoleCompModPKDto>}
51-
// * */
52-
// const compositePrimaryKey = newPk.compositePrimaryKey || [];
53-
// const guidsOfColumnsInPk = compositePrimaryKey.map((compositePkEntry) => compositePkEntry.keyId);
54-
// const columnsInPk = getPropertiesByGuids(_)(collection, guidsOfColumnsInPk);
55-
// const columnNamesForDDL = columnsInPk.map(column => prepareName(column.compMod.newField.name));
56-
// if (!columnNamesForDDL.length) {
57-
// return undefined;
58-
// }
59-
// return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL);
60-
// })
61-
// .filter(Boolean)
62-
// .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false))
63-
// .filter(Boolean);
64-
65-
return [];
114+
const {
115+
getFullCollectionName,
116+
getSchemaOfAlterCollection,
117+
getEntityName,
118+
} = require('../../../utils/general')(_);
119+
120+
const didPkChange = didCompositePkChange(_)(collection);
121+
if (!didPkChange) {
122+
return []
123+
}
124+
125+
const collectionSchema = getSchemaOfAlterCollection(collection);
126+
const fullTableName = getFullCollectionName(collectionSchema);
127+
const entityName = getEntityName(collectionSchema);
128+
129+
const pkDto = collection?.role?.compMod?.primaryKey || {};
130+
/**
131+
* @type {Array<AlterCollectionRoleCompModPKDto>}
132+
* */
133+
const newPrimaryKeys = pkDto.new || [];
134+
135+
return newPrimaryKeys
136+
.map((newPk) => {
137+
const ddlConfig = getCreateCompositePKDDLProviderConfig(_)(newPk, entityName, collection);
138+
return ddlProvider.createKeyConstraint(
139+
fullTableName,
140+
collection.isActivated,
141+
ddlConfig
142+
);
143+
})
144+
.filter(Boolean)
145+
.map(scriptDto => AlterScriptDto.getInstance([scriptDto.statement], scriptDto.isActivated, false))
146+
.filter(Boolean);
66147
}
67148

68149
/**
@@ -108,11 +189,11 @@ const getDropCompositePkScripts = (_, ddlProvider) => (collection) => {
108189
* */
109190
const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => {
110191
const dropCompositePkScripts = getDropCompositePkScripts(_, ddlProvider)(collection);
111-
// const addCompositePkScripts = getAddCompositePkScripts(_, ddlProvider)(collection);
192+
const addCompositePkScripts = getAddCompositePkScripts(_, ddlProvider)(collection);
112193

113194
return [
114195
...dropCompositePkScripts,
115-
// ...addCompositePkScripts,
196+
...addCompositePkScripts,
116197
].filter(Boolean);
117198
}
118199

@@ -137,7 +218,6 @@ const getConstraintNameForRegularPk = (columnJsonSchema, entityName) => {
137218

138219
/**
139220
* @param _
140-
* @param wrapInQuotes {(s: string) => string }
141221
* @return {(
142222
* name: string,
143223
* columnJsonSchema: AlterCollectionColumnDto,
@@ -159,15 +239,15 @@ const getConstraintNameForRegularPk = (columnJsonSchema, entityName) => {
159239
* }
160240
* }
161241
* */
162-
const getCreateRegularPKDDLProviderConfig = (_, wrapInQuotes) => (
242+
const getCreateRegularPKDDLProviderConfig = (_) => (
163243
columnName,
164244
columnJsonSchema,
165245
entityName,
166-
entityJsonSchema
246+
entity
167247
) => {
168248
const constraintName = getConstraintNameForRegularPk(columnJsonSchema, entityName);
169249
const pkColumns = [{
170-
name: wrapInQuotes(columnName),
250+
name: columnName,
171251
isActivated: columnJsonSchema.isActivated,
172252
}];
173253

@@ -187,8 +267,8 @@ const getCreateRegularPKDDLProviderConfig = (_, wrapInQuotes) => (
187267
indexTablespace = constraintOption.indexTablespace;
188268
}
189269
if (constraintOption.indexInclude) {
190-
includeColumns = _.toPairs(entityJsonSchema.properties)
191-
.filter(([name, jsonSchema]) => Boolean(constraintOption.indexInclude.find(keyDto => keyDto.keyId === jsonSchema.id)))
270+
includeColumns = _.toPairs(entity.role.properties)
271+
.filter(([name, jsonSchema]) => Boolean(constraintOption.indexInclude.find(keyDto => keyDto.keyId === jsonSchema.GUID)))
192272
.map(([name, jsonSchema]) => ({
193273
name,
194274
isActivated: jsonSchema.isActivated,
@@ -215,7 +295,6 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
215295
getFullCollectionName,
216296
getSchemaOfAlterCollection,
217297
getEntityName,
218-
wrapInQuotes
219298
} = require('../../../utils/general')(_);
220299

221300
const collectionSchema = getSchemaOfAlterCollection(collection);
@@ -230,7 +309,7 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
230309
return isRegularPrimaryKey && !wasTheFieldAPrimaryKey;
231310
})
232311
.map(([name, jsonSchema]) => {
233-
const ddlConfig = getCreateRegularPKDDLProviderConfig(_, wrapInQuotes)(name, jsonSchema, entityName, collection);
312+
const ddlConfig = getCreateRegularPKDDLProviderConfig(_)(name, jsonSchema, entityName, collection);
234313
return ddlProvider.createKeyConstraint(
235314
fullTableName,
236315
collection.isActivated,

forward_engineering/alterScript/types/AlterCollectionDto.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,7 @@ class AlterCollectionRoleDefinitionDto {
154154
properties
155155
}
156156

157-
class AlterCollectionRoleCompModPKDto {
158-
159-
/**
160-
* @type {string}
161-
* */
162-
id
157+
class AlterCollectionRoleCompModPKDto extends AlterCollectionColumnPrimaryKeyOptionDto{
163158

164159
/**
165160
* @type {Array<{
@@ -169,20 +164,6 @@ class AlterCollectionRoleCompModPKDto {
169164
* */
170165
compositePrimaryKey
171166

172-
/**
173-
* @type {string | undefined}
174-
* */
175-
constraintName
176-
177-
/**
178-
* @type {string | undefined}
179-
* */
180-
indexStorageParameters
181-
182-
/**
183-
* @type {string | undefined}
184-
* */
185-
indexTablespace
186167
}
187168

188169
class AlterCollectionRoleCompModDto {
@@ -243,7 +224,7 @@ class AlterCollectionRoleCompModDto {
243224
/**
244225
* @type {{
245226
* new: Array<AlterCollectionRoleCompModPKDto>,
246-
* old: Array<AlterCollectionRoleCompModPKDto>,
227+
* old: Array<AlterCollectionRoleCompModPKDto>,
247228
* }}
248229
*/
249230
primaryKey

0 commit comments

Comments
 (0)