Skip to content

Commit 9075001

Browse files
author
unknown
committed
Added support for adding non-composite PK constraints
1 parent 70a6304 commit 9075001

File tree

5 files changed

+133
-63
lines changed

5 files changed

+133
-63
lines changed

forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js

Lines changed: 104 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
1-
const { AlterScriptDto } = require('../../types/AlterScriptDto');
1+
const {AlterScriptDto} = require('../../types/AlterScriptDto');
22
const {
33
AlterCollectionDto,
44
AlterCollectionColumnDto,
55
AlterCollectionRoleCompModPKDto,
66
AlterCollectionColumnPrimaryKeyOptionDto
77
} = require('../../types/AlterCollectionDto');
88

9-
10-
/**
11-
* @return {(collection: AlterCollectionDto, guid: string) => AlterCollectionColumnDto | undefined}
12-
* */
13-
const getPropertyByGuid = (_) => (collection, guid) => {
14-
const propertyInArray = _.toPairs(collection?.role?.properties)
15-
.filter(([name, jsonSchema]) => jsonSchema.GUID === guid)
16-
.map(([name, jsonSchema]) => jsonSchema);
17-
if (!propertyInArray.length) {
18-
return undefined;
19-
}
20-
return propertyInArray[0];
21-
}
22-
23-
/**
24-
* @return {(collection: AlterCollectionDto, guids: string[]) => Array<AlterCollectionColumnDto>}
25-
* */
26-
const getPropertiesByGuids = (_) => (collection, guids) => {
27-
return guids
28-
.map(guid => getPropertyByGuid(_)(collection, guid))
29-
.filter(Boolean);
30-
}
31-
329
/**
3310
* @return {(collection: AlterCollectionDto) => boolean}
3411
* */
@@ -47,11 +24,11 @@ const didCompositePkChange = (_) => (collection) => {
4724
}
4825

4926
/**
50-
* @param wrapInQuotes {(s: string) => string}
51-
* @return {(entityName: string) => string}
27+
* @param entityName {string}
28+
* @return {string}
5229
* */
53-
const getDefaultConstraintName = (wrapInQuotes) => (entityName) => {
54-
return wrapInQuotes(`${entityName}_pk`);
30+
const getDefaultConstraintName = (entityName) => {
31+
return `${entityName}_pk`;
5532
}
5633

5734
/**
@@ -116,7 +93,7 @@ const getDropCompositePkScripts = (_, ddlProvider) => (collection) => {
11693

11794
return oldPrimaryKeys
11895
.map((oldPk) => {
119-
let constraintName = getDefaultConstraintName(wrapInQuotes)(entityName);
96+
let constraintName = getDefaultConstraintName(entityName);
12097
if (oldPk.constraintName) {
12198
constraintName = wrapInQuotes(oldPk.constraintName);
12299
}
@@ -139,6 +116,97 @@ const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => {
139116
].filter(Boolean);
140117
}
141118

119+
/**
120+
* @param columnJsonSchema {AlterCollectionColumnDto}
121+
* @param entityName {string}
122+
* @return {string}
123+
* */
124+
const getConstraintNameForRegularPk = (columnJsonSchema, entityName) => {
125+
const constraintOptions = columnJsonSchema.primaryKeyOptions;
126+
if (constraintOptions?.length && constraintOptions?.length > 0) {
127+
/**
128+
* @type {AlterCollectionColumnPrimaryKeyOptionDto}
129+
* */
130+
const constraintOption = constraintOptions[0];
131+
if (constraintOption.constraintName) {
132+
return constraintOption.constraintName;
133+
}
134+
}
135+
return getDefaultConstraintName(entityName);
136+
}
137+
138+
/**
139+
* @param _
140+
* @param wrapInQuotes {(s: string) => string }
141+
* @return {(
142+
* name: string,
143+
* columnJsonSchema: AlterCollectionColumnDto,
144+
* entityName: string,
145+
* entityJsonSchema: AlterCollectionDto,
146+
* ) => {
147+
* name: string,
148+
* keyType: string,
149+
* columns: Array<{
150+
* isActivated: boolean,
151+
* name: string,
152+
* }>,
153+
* include: Array<{
154+
* isActivated: boolean,
155+
* name: string,
156+
* }>,
157+
* storageParameters: string,
158+
* tablespace: string,
159+
* }
160+
* }
161+
* */
162+
const getCreateRegularPKDDLProviderConfig = (_, wrapInQuotes) => (
163+
columnName,
164+
columnJsonSchema,
165+
entityName,
166+
entityJsonSchema
167+
) => {
168+
const constraintName = getConstraintNameForRegularPk(columnJsonSchema, entityName);
169+
const pkColumns = [{
170+
name: wrapInQuotes(columnName),
171+
isActivated: columnJsonSchema.isActivated,
172+
}];
173+
174+
let storageParameters = '';
175+
let indexTablespace = '';
176+
let includeColumns = [];
177+
const constraintOptions = columnJsonSchema.primaryKeyOptions;
178+
if (constraintOptions?.length && constraintOptions?.length > 0) {
179+
/**
180+
* @type {AlterCollectionColumnPrimaryKeyOptionDto}
181+
* */
182+
const constraintOption = constraintOptions[0];
183+
if (constraintOption.indexStorageParameters) {
184+
storageParameters = constraintOption.indexStorageParameters;
185+
}
186+
if (constraintOption.indexTablespace) {
187+
indexTablespace = constraintOption.indexTablespace;
188+
}
189+
if (constraintOption.indexInclude) {
190+
includeColumns = _.toPairs(entityJsonSchema.properties)
191+
.filter(([name, jsonSchema]) => Boolean(constraintOption.indexInclude.find(keyDto => keyDto.keyId === jsonSchema.id)))
192+
.map(([name, jsonSchema]) => ({
193+
name,
194+
isActivated: jsonSchema.isActivated,
195+
}));
196+
}
197+
}
198+
199+
return {
200+
name: constraintName,
201+
keyType: 'PRIMARY KEY',
202+
columns: pkColumns,
203+
include: includeColumns,
204+
storageParameters,
205+
tablespace: indexTablespace,
206+
}
207+
}
208+
209+
142210
/**
143211
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
144212
* */
@@ -149,11 +217,10 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
149217
getEntityName,
150218
wrapInQuotes
151219
} = require('../../../utils/general')(_);
152-
const collectionSchema = getSchemaOfAlterCollection(collection);
153220

221+
const collectionSchema = getSchemaOfAlterCollection(collection);
154222
const fullTableName = getFullCollectionName(collectionSchema);
155223
const entityName = getEntityName(collectionSchema);
156-
const constraintName = getDefaultConstraintName(wrapInQuotes)(entityName);
157224

158225
return _.toPairs(collection.properties)
159226
.filter(([name, jsonSchema]) => {
@@ -163,21 +230,12 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
163230
return isRegularPrimaryKey && !wasTheFieldAPrimaryKey;
164231
})
165232
.map(([name, jsonSchema]) => {
166-
const pkColumns = [{
167-
name: wrapInQuotes(name),
168-
isActivated: jsonSchema.isActivated,
169-
}]
170-
233+
const ddlConfig = getCreateRegularPKDDLProviderConfig(_, wrapInQuotes)(name, jsonSchema, entityName, collection);
171234
return ddlProvider.createKeyConstraint(
172235
fullTableName,
173236
collection.isActivated,
174-
{
175-
name: constraintName,
176-
columns: pkColumns,
177-
include: [],
178-
storageParameters: '',
179-
tablespace: '',
180-
});
237+
ddlConfig
238+
);
181239
})
182240
.map(scriptDto => AlterScriptDto.getInstance([scriptDto.statement], scriptDto.isActivated, false))
183241
.filter(Boolean);
@@ -208,18 +266,7 @@ const getDropPkScript = (_, ddlProvider) => (collection) => {
208266
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
209267
})
210268
.map(([name, jsonSchema]) => {
211-
const constraintOptions = jsonSchema.primaryKeyOptions;
212-
let constraintName = getDefaultConstraintName(wrapInQuotes)(entityName);
213-
if (constraintOptions?.length && constraintOptions?.length > 0) {
214-
/**
215-
* @type {AlterCollectionColumnPrimaryKeyOptionDto}
216-
* */
217-
const constraintOption = constraintOptions[0];
218-
if (constraintOption.constraintName) {
219-
constraintName = wrapInQuotes(constraintOption.constraintName);
220-
}
221-
}
222-
269+
const constraintName = wrapInQuotes(getConstraintNameForRegularPk(jsonSchema, entityName));
223270
return ddlProvider.dropPkConstraint(fullTableName, constraintName);
224271
})
225272
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true))
@@ -231,11 +278,11 @@ const getDropPkScript = (_, ddlProvider) => (collection) => {
231278
* */
232279
const getModifyPkScripts = (_, ddlProvider) => (collection) => {
233280
const dropPkScripts = getDropPkScript(_, ddlProvider)(collection);
234-
// const addPkScripts = getAddPkScripts(_, ddlProvider)(collection);
281+
const addPkScripts = getAddPkScripts(_, ddlProvider)(collection);
235282

236283
return [
237284
...dropPkScripts,
238-
// ...addPkScripts,
285+
...addPkScripts,
239286
].filter(Boolean);
240287
}
241288

forward_engineering/alterScript/types/AlterCollectionDto.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ class AlterCollectionColumnPrimaryKeyOptionDto {
4040
* @type {string}
4141
* */
4242
constraintName
43+
44+
/**
45+
* @type {string}
46+
* */
47+
indexStorageParameters
48+
49+
/**
50+
* @type {string}
51+
* */
52+
indexTablespace
53+
54+
/**
55+
* @type {Array<{
56+
* keyId: string
57+
* }>}
58+
* */
59+
indexInclude
4360
}
4461

4562
class AlterCollectionColumnDto {

forward_engineering/ddlProvider/ddlHelpers/constraintsHelper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = ({
5252
* @return {(
5353
* keyData: {
5454
* name: string,
55+
* keyType: string,
5556
* columns: Array<{
5657
* isActivated: boolean,
5758
* name: string,

forward_engineering/ddlProvider/ddlProvider.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const defaultTypes = require('../configs/defaultTypes');
22
const types = require('../configs/types');
33
const templates = require('./templates');
4+
const assignTemplates = require("../utils/assignTemplates");
45

56
module.exports = (baseProvider, options, app) => {
67
const _ = app.require('lodash');
@@ -1126,6 +1127,7 @@ module.exports = (baseProvider, options, app) => {
11261127
* @param isParentActivated {boolean}
11271128
* @param keyData {{
11281129
* name: string,
1130+
* keyType: string,
11291131
* columns: Array<{
11301132
* isActivated: boolean,
11311133
* name: string,
@@ -1143,11 +1145,14 @@ module.exports = (baseProvider, options, app) => {
11431145
* }}
11441146
* */
11451147
createKeyConstraint(tableName, isParentActivated, keyData) {
1146-
const constraintStatement = createKeyConstraint(templates, isParentActivated)(keyData);
1147-
return assignTemplates(templates.addPkConstraint, {
1148-
constraintStatement,
1149-
tableName,
1150-
})
1148+
const constraintStatementDto = createKeyConstraint(templates, isParentActivated)(keyData);
1149+
return {
1150+
statement: assignTemplates(templates.addPkConstraint, {
1151+
constraintStatement: (constraintStatementDto.statement || '').trim(),
1152+
tableName,
1153+
}),
1154+
isActivated: constraintStatementDto.isActivated,
1155+
}
11511156
},
11521157

11531158
/**

forward_engineering/ddlProvider/templates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = {
4141

4242
dropForeignKey: 'ALTER TABLE ${tableName} DROP CONSTRAINT ${fkConstraintName};',
4343

44-
addPkConstraint: 'ALTER TABLE IF EXISTS ${tableName} ADD CONSTRAINT ${constraintStatement}',
44+
addPkConstraint: 'ALTER TABLE IF EXISTS ${tableName} ADD ${constraintStatement};',
4545

4646
dropTable: 'DROP TABLE IF EXISTS ${tableName};',
4747

0 commit comments

Comments
 (0)