Skip to content

Commit b8a380b

Browse files
author
unknown
committed
Added ability to DROP regular pk constraints (with and without extra options)
1 parent fd26c16 commit b8a380b

File tree

5 files changed

+240
-106
lines changed

5 files changed

+240
-106
lines changed

forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const { AlterScriptDto } = require('../../types/AlterScriptDto');
2-
const {AlterCollectionDto, AlterCollectionColumnDto,AlterCollectionRoleCompModPKDto} = require('../../types/AlterCollectionDto');
2+
const {
3+
AlterCollectionDto,
4+
AlterCollectionColumnDto,
5+
AlterCollectionRoleCompModPKDto,
6+
AlterCollectionColumnPrimaryKeyOptionDto
7+
} = require('../../types/AlterCollectionDto');
38

49

510
/**
@@ -41,6 +46,14 @@ const didCompositePkChange = (_) => (collection) => {
4146
return !areKeyArraysEqual;
4247
}
4348

49+
/**
50+
* @param wrapInQuotes {(s: string) => string}
51+
* @return {(entityName: string) => string}
52+
* */
53+
const getDefaultConstraintName = (wrapInQuotes) => (entityName) => {
54+
return wrapInQuotes(`${entityName}_pk`);
55+
}
56+
4457
/**
4558
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
4659
* */
@@ -107,11 +120,17 @@ const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => {
107120
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
108121
* */
109122
const getAddPkScripts = (_, ddlProvider) => (collection) => {
110-
const { getFullCollectionName, getSchemaOfAlterCollection } = require('../../../utils/general')(_);
123+
const {
124+
getFullCollectionName,
125+
getSchemaOfAlterCollection,
126+
getEntityName,
127+
wrapInQuotes
128+
} = require('../../../utils/general')(_);
111129
const collectionSchema = getSchemaOfAlterCollection(collection);
112130

113131
const fullTableName = getFullCollectionName(collectionSchema);
114-
const constraintName = getEntityNameFromCollection(collection) + '_pk';
132+
const entityName = getEntityName(collectionSchema);
133+
const constraintName = getDefaultConstraintName(wrapInQuotes)(entityName);
115134

116135
return _.toPairs(collection.properties)
117136
.filter(([name, jsonSchema]) => {
@@ -121,19 +140,40 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
121140
return isRegularPrimaryKey && !wasTheFieldAPrimaryKey;
122141
})
123142
.map(([name, jsonSchema]) => {
124-
const nameForDDl = prepareName(name);
125-
const columnNamesForDDL = [nameForDDl];
126-
return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL);
143+
const pkColumns = [{
144+
name: wrapInQuotes(name),
145+
isActivated: jsonSchema.isActivated,
146+
}]
147+
148+
return ddlProvider.createKeyConstraint(
149+
fullTableName,
150+
collection.isActivated,
151+
{
152+
name: constraintName,
153+
columns: pkColumns,
154+
include: [],
155+
storageParameters: '',
156+
tablespace: '',
157+
});
127158
})
128-
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false))
159+
.map(scriptDto => AlterScriptDto.getInstance([scriptDto.statement], scriptDto.isActivated, false))
129160
.filter(Boolean);
130161
}
131162

132163
/**
133164
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
134165
* */
135-
const getDropPkScripts = (_, ddlProvider) => (collection) => {
136-
const fullTableName = generateFullEntityName(collection);
166+
const getDropPkScript = (_, ddlProvider) => (collection) => {
167+
const {
168+
getFullCollectionName,
169+
getSchemaOfAlterCollection,
170+
getEntityName,
171+
wrapInQuotes
172+
} = require('../../../utils/general')(_);
173+
174+
const collectionSchema = getSchemaOfAlterCollection(collection);
175+
const fullTableName = getFullCollectionName(collectionSchema);
176+
const entityName = getEntityName(collectionSchema);
137177

138178
return _.toPairs(collection.properties)
139179
.filter(([name, jsonSchema]) => {
@@ -145,7 +185,19 @@ const getDropPkScripts = (_, ddlProvider) => (collection) => {
145185
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
146186
})
147187
.map(([name, jsonSchema]) => {
148-
return ddlProvider.dropConstraint(fullTableName, );
188+
const constraintOptions = jsonSchema.primaryKeyOptions;
189+
let constraintName = getDefaultConstraintName(wrapInQuotes)(entityName);
190+
if (constraintOptions?.length && constraintOptions?.length > 0) {
191+
/**
192+
* @type {AlterCollectionColumnPrimaryKeyOptionDto}
193+
* */
194+
const constraintOption = constraintOptions[0];
195+
if (constraintOption.constraintName) {
196+
constraintName = wrapInQuotes(constraintOption.constraintName);
197+
}
198+
}
199+
200+
return ddlProvider.dropPkConstraint(fullTableName, constraintName);
149201
})
150202
.map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true))
151203
.filter(Boolean);
@@ -155,7 +207,7 @@ const getDropPkScripts = (_, ddlProvider) => (collection) => {
155207
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
156208
* */
157209
const getModifyPkScripts = (_, ddlProvider) => (collection) => {
158-
const dropPkScripts = getDropPkScripts(_, ddlProvider)(collection);
210+
const dropPkScripts = getDropPkScript(_, ddlProvider)(collection);
159211
const addPkScripts = getAddPkScripts(_, ddlProvider)(collection);
160212

161213
return [
@@ -168,11 +220,11 @@ const getModifyPkScripts = (_, ddlProvider) => (collection) => {
168220
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
169221
* */
170222
const getModifyPkConstraintsScriptDtos = (_, ddlProvider) => (collection) => {
171-
const modifyCompositePkScripts = getModifyCompositePkScripts(_, ddlProvider)(collection);
223+
// const modifyCompositePkScripts = getModifyCompositePkScripts(_, ddlProvider)(collection);
172224
const modifyPkScripts = getModifyPkScripts(_, ddlProvider)(collection);
173225

174226
return [
175-
...modifyCompositePkScripts,
227+
// ...modifyCompositePkScripts,
176228
...modifyPkScripts,
177229
].filter(Boolean);
178230
}

forward_engineering/alterScript/types/AlterCollectionDto.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ class AlterCollectionColumnCompModDto {
3030

3131
}
3232

33+
class AlterCollectionColumnPrimaryKeyOptionDto {
34+
/**
35+
* @type {string}
36+
* */
37+
id
38+
39+
/**
40+
* @type {string}
41+
* */
42+
constraintName
43+
}
44+
3345
class AlterCollectionColumnDto {
3446
/**
3547
* @type {string}
@@ -86,6 +98,11 @@ class AlterCollectionColumnDto {
8698
*/
8799
compositeUniqueKey
88100

101+
/**
102+
* @type {Array<AlterCollectionColumnPrimaryKeyOptionDto> | undefined}
103+
* */
104+
primaryKeyOptions
105+
89106
/**
90107
* @type {boolean}
91108
*/
Lines changed: 114 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,128 @@
11
module.exports = ({
2-
_,
3-
commentIfDeactivated,
4-
checkAllKeysDeactivated,
5-
assignTemplates,
6-
wrapInQuotes,
7-
getColumnsList,
8-
}) => {
9-
const generateConstraintsString = (dividedConstraints, isParentActivated) => {
10-
const deactivatedItemsAsString = commentIfDeactivated(
11-
(dividedConstraints?.deactivatedItems || []).join(',\n\t'),
12-
{
13-
isActivated: !isParentActivated,
14-
isPartOfLine: true,
15-
},
16-
);
17-
const activatedConstraints = dividedConstraints?.activatedItems?.length
18-
? ',\n\t' + dividedConstraints.activatedItems.join(',\n\t')
19-
: '';
2+
_,
3+
commentIfDeactivated,
4+
checkAllKeysDeactivated,
5+
assignTemplates,
6+
wrapInQuotes,
7+
getColumnsList,
8+
}) => {
9+
const generateConstraintsString = (dividedConstraints, isParentActivated) => {
10+
const deactivatedItemsAsString = commentIfDeactivated(
11+
(dividedConstraints?.deactivatedItems || []).join(',\n\t'),
12+
{
13+
isActivated: !isParentActivated,
14+
isPartOfLine: true,
15+
},
16+
);
17+
const activatedConstraints = dividedConstraints?.activatedItems?.length
18+
? ',\n\t' + dividedConstraints.activatedItems.join(',\n\t')
19+
: '';
2020

21-
const deactivatedConstraints = dividedConstraints?.deactivatedItems?.length
22-
? '\n\t' + deactivatedItemsAsString
23-
: '';
21+
const deactivatedConstraints = dividedConstraints?.deactivatedItems?.length
22+
? '\n\t' + deactivatedItemsAsString
23+
: '';
2424

25-
return activatedConstraints + deactivatedConstraints;
26-
};
25+
return activatedConstraints + deactivatedConstraints;
26+
};
2727

28-
const foreignKeysToString = keys => {
29-
if (Array.isArray(keys)) {
30-
const activatedKeys = keys
31-
.filter(key => _.get(key, 'isActivated', true))
32-
.map(key => wrapInQuotes(_.trim(key.name)));
33-
const deactivatedKeys = keys
34-
.filter(key => !_.get(key, 'isActivated', true))
35-
.map(key => wrapInQuotes(_.trim(key.name)));
36-
const deactivatedKeysAsString = deactivatedKeys.length
37-
? commentIfDeactivated(deactivatedKeys, { isActivated: false, isPartOfLine: true })
38-
: '';
28+
const foreignKeysToString = keys => {
29+
if (Array.isArray(keys)) {
30+
const activatedKeys = keys
31+
.filter(key => _.get(key, 'isActivated', true))
32+
.map(key => wrapInQuotes(_.trim(key.name)));
33+
const deactivatedKeys = keys
34+
.filter(key => !_.get(key, 'isActivated', true))
35+
.map(key => wrapInQuotes(_.trim(key.name)));
36+
const deactivatedKeysAsString = deactivatedKeys.length
37+
? commentIfDeactivated(deactivatedKeys, {isActivated: false, isPartOfLine: true})
38+
: '';
3939

40-
return activatedKeys.join(', ') + deactivatedKeysAsString;
41-
}
42-
return keys;
43-
};
40+
return activatedKeys.join(', ') + deactivatedKeysAsString;
41+
}
42+
return keys;
43+
};
4444

45-
const foreignActiveKeysToString = keys => {
46-
return keys.map(key => _.trim(key.name)).join(', ');
47-
};
45+
const foreignActiveKeysToString = keys => {
46+
return keys.map(key => _.trim(key.name)).join(', ');
47+
};
4848

49-
const createKeyConstraint = (templates, isParentActivated) => keyData => {
50-
const constraintName = wrapInQuotes(_.trim(keyData.name));
51-
const isAllColumnsDeactivated = checkAllKeysDeactivated(keyData.columns || []);
52-
const columns = !_.isEmpty(keyData.columns)
53-
? getColumnsList(keyData.columns, isAllColumnsDeactivated, isParentActivated)
54-
: '';
55-
const includeNonKey = keyData.include.length
56-
? ` INCLUDE${getColumnsList(keyData.include, isAllColumnsDeactivated, isParentActivated)}`
57-
: '';
58-
const storageParameters = keyData.storageParameters ? ` WITH (${keyData.storageParameters})` : '';
59-
const tablespace = keyData.tablespace ? ` USING INDEX TABLESPACE ${wrapInQuotes(keyData.tablespace)}` : '';
49+
/**
50+
* @param templates {Object}
51+
* @param isParentActivated {boolean}
52+
* @return {(
53+
* keyData: {
54+
* name: string,
55+
* columns: Array<{
56+
* isActivated: boolean,
57+
* name: string,
58+
* }>,
59+
* include: Array<{
60+
* isActivated: boolean,
61+
* name: string,
62+
* }>,
63+
* storageParameters: string,
64+
* tablespace: string,
65+
* }
66+
* ) => {
67+
* statement: string,
68+
* isActivated: boolean,
69+
* }}
70+
* */
71+
const createKeyConstraint = (templates, isParentActivated) => keyData => {
72+
const constraintName = wrapInQuotes(_.trim(keyData.name));
73+
const isAllColumnsDeactivated = checkAllKeysDeactivated(keyData.columns || []);
74+
const columns = !_.isEmpty(keyData.columns)
75+
? getColumnsList(keyData.columns, isAllColumnsDeactivated, isParentActivated)
76+
: '';
77+
const includeNonKey = keyData.include.length
78+
? ` INCLUDE${getColumnsList(keyData.include, isAllColumnsDeactivated, isParentActivated)}`
79+
: '';
80+
const storageParameters = keyData.storageParameters ? ` WITH (${keyData.storageParameters})` : '';
81+
const tablespace = keyData.tablespace ? ` USING INDEX TABLESPACE ${wrapInQuotes(keyData.tablespace)}` : '';
6082

61-
return {
62-
statement: assignTemplates(templates.createKeyConstraint, {
63-
constraintName: keyData.name ? `CONSTRAINT ${wrapInQuotes(constraintName)} ` : '',
64-
keyType: keyData.keyType,
65-
columns,
66-
includeNonKey,
67-
storageParameters,
68-
tablespace,
69-
}),
70-
isActivated: !isAllColumnsDeactivated,
71-
};
72-
};
83+
return {
84+
statement: assignTemplates(templates.createKeyConstraint, {
85+
constraintName: keyData.name ? `CONSTRAINT ${wrapInQuotes(constraintName)} ` : '',
86+
keyType: keyData.keyType,
87+
columns,
88+
includeNonKey,
89+
storageParameters,
90+
tablespace,
91+
}),
92+
isActivated: !isAllColumnsDeactivated,
93+
};
94+
};
7395

74-
const getConstraintsWarnings = (invalidConstraints = []) => {
75-
if (_.isEmpty(invalidConstraints)) {
76-
return '';
77-
}
96+
const getConstraintsWarnings = (invalidConstraints = []) => {
97+
if (_.isEmpty(invalidConstraints)) {
98+
return '';
99+
}
78100

79-
return (
80-
'\n\t' +
81-
invalidConstraints
82-
.map(keyData => {
83-
const constraintName = keyData.name ? ` [constraint name: ${keyData.name}]` : '';
101+
return (
102+
'\n\t' +
103+
invalidConstraints
104+
.map(keyData => {
105+
const constraintName = keyData.name ? ` [constraint name: ${keyData.name}]` : '';
84106

85-
return `-- ${keyData.errorMessage}${constraintName}`;
86-
})
87-
.join('\n\t')
88-
);
89-
};
107+
return `-- ${keyData.errorMessage}${constraintName}`;
108+
})
109+
.join('\n\t')
110+
);
111+
};
90112

91-
const additionalPropertiesForForeignKey = relationship => {
92-
const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', '');
93-
const foreignOnUpdate = _.get(relationship, 'relationshipOnUpdate', '');
94-
const foreignMatch = _.get(relationship, 'relationshipMatch', '');
95-
return { foreignOnDelete, foreignOnUpdate, foreignMatch };
96-
};
113+
const additionalPropertiesForForeignKey = relationship => {
114+
const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', '');
115+
const foreignOnUpdate = _.get(relationship, 'relationshipOnUpdate', '');
116+
const foreignMatch = _.get(relationship, 'relationshipMatch', '');
117+
return {foreignOnDelete, foreignOnUpdate, foreignMatch};
118+
};
97119

98-
return {
99-
generateConstraintsString,
100-
foreignKeysToString,
101-
foreignActiveKeysToString,
102-
createKeyConstraint,
103-
getConstraintsWarnings,
104-
additionalPropertiesForForeignKey,
105-
};
120+
return {
121+
generateConstraintsString,
122+
foreignKeysToString,
123+
foreignActiveKeysToString,
124+
createKeyConstraint,
125+
getConstraintsWarnings,
126+
additionalPropertiesForForeignKey,
127+
};
106128
};

0 commit comments

Comments
 (0)