-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathalterEntityHelper.js
More file actions
308 lines (273 loc) · 12.1 KB
/
Copy pathalterEntityHelper.js
File metadata and controls
308 lines (273 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const _ = require('lodash');
const { getColumns, getColumnsStatement, getTypeByProperty } = require('../columnHelper');
const { getIndexes } = require('../indexHelper');
const { getTableStatement } = require('../tableHelper');
const { hydrateTableProperties, getDifferentItems, getIsChangeProperties } = require('./common');
const {
getFullEntityName,
generateFullEntityName,
getEntityProperties,
getContainerName,
getEntityData,
getEntityName,
prepareScript,
hydrateProperty,
} = require('./generalHelper');
const { hydrateKeys } = require('./tableKeysHelper');
const { replaceSpaceWithUnderscore } = require('../generalHelper');
const { getModifyPkConstraintsScripts } = require('./primaryKeyHelper');
const { getIsPkOrFkConstraintAvailable, getIsConstraintAvailable } = require('../constraintHelper');
const { getModifyUkConstraintsScripts } = require('./uniqueKeyHelper');
const { getModifyNonNullColumnsScripts } = require('./nonNullConstraintHelper');
const { getModifyDefaultValueConstraintsScripts } = require('./defaultConstraintHelper');
const { getModifyCheckConstraintsScripts } = require('./checkConstraintHelper');
const { getForeignKeyConstraint } = require('../foreignKeyHelper');
const tableProperties = [
'compositePartitionKey',
'storedAsTable',
'fieldsTerminatedBy',
'fieldsescapedBy',
'collectionItemsTerminatedBy',
'mapKeysTerminatedBy',
'linesTerminatedBy',
'nullDefinedAs',
'inputFormatClassname',
'outputFormatClassname',
];
const otherTableProperties = [
'code',
'collectionName',
'tableProperties',
'sortedByKey',
'numBuckets',
'skewedby',
'skewedOn',
'skewStoredAsDir',
'compositeClusteringKey',
'description',
'properties',
'location',
];
const hydrateSerDeProperties = (compMod, name) => {
const { serDeProperties, serDeLibrary } = compMod;
return {
properties: hydrateTableProperties(serDeProperties || {}, name),
serDe: !_.isEqual(serDeLibrary?.new, serDeLibrary?.old) && serDeLibrary?.new,
name,
};
};
const hydrateAlterTableName = compMod => {
const { newName, oldName } = getEntityName(compMod);
if ((!newName && !oldName) || newName === oldName) {
return {};
}
return {
oldName: getFullEntityName(getContainerName(compMod), oldName),
newName: getFullEntityName(getContainerName(compMod), newName),
};
};
const hydrateAlterTable = (collection, fullCollectionName, definition) => {
const compMod = _.get(collection, 'role.compMod', {});
const dataProperties = _.get(compMod, 'tableProperties', '');
const hydratedCollectionData = hydrateCollection(collection, definition);
return {
keys: hydrateKeys(hydratedCollectionData, collection, definition, fullCollectionName),
location: hydrateProperty(collection, compMod, 'location'),
alterTableName: hydrateAlterTableName(compMod),
tableProperties: hydrateTableProperties(dataProperties, fullCollectionName, compMod?.description),
serDeProperties: hydrateSerDeProperties(compMod, fullCollectionName),
name: fullCollectionName,
};
};
const hydrateAlterColumns = (entity, definitions) => {
const collectionName = generateFullEntityName(entity);
const columns = Object.values(entity.properties).map(property => {
const compMod = _.get(property, 'compMod', {});
const { newField = {}, oldField = {} } = compMod;
const newType = getTypeByProperty(definitions)({ ...property, ...newField });
const oldType = getTypeByProperty(definitions)({ ...property, ...oldField });
const oldName = oldField.name;
const newName = newField.name;
const newComment = property.description || '';
const oldComment = entity.role.properties[oldName]?.description || '';
const isCommentChanged = newComment !== oldComment;
const isColumnChanged = oldName !== newName || newType !== oldType || isCommentChanged;
const column = isColumnChanged ? { type: newType, oldName, newName } : null;
if (column && isCommentChanged) {
return { ...column, comment: newComment };
}
return column;
});
return { collectionName, columns: columns.filter(Boolean) };
};
const hydrateDropIndexes = entity => {
const indexes = _.get(entity, 'SecIndxs', []);
const name = generateFullEntityName(entity);
return indexes.map(index => ({ name, indexName: replaceSpaceWithUnderscore(index.name) }));
};
const hydrateAddIndexes = (entity, SecIndxs, properties, definitions) => {
const compMod = _.get(entity, 'role.compMod', {});
const entityData = _.get(entity, 'role', {});
const containerData = { name: getContainerName(compMod) };
return [[containerData], [entityData, {}, { SecIndxs }], { ...entityData, properties }, definitions];
};
const hydrateIndex = (entity, properties, definitions) => {
const indexes = _.get(entity, 'role.compMod.SecIndxs', {});
const { drop, add } = getDifferentItems(indexes.new, indexes.old);
return {
hydratedDropIndexes: hydrateDropIndexes({ ...entity, SecIndxs: drop }),
hydratedAddIndexes: hydrateAddIndexes(entity, add, properties, definitions),
};
};
const hydrateCollection = (entity, definitions) => {
const compMod = _.get(entity, 'role.compMod', {});
const entityData = _.get(entity, 'role', {});
const properties = getEntityProperties(entity);
const containerData = { name: getContainerName(compMod) };
return [[containerData], [entityData], { ...entityData, properties }, definitions];
};
const generateModifyCollectionScript = (entity, definitions, provider) => {
const compMod = _.get(entity, 'role.compMod', {});
const isChangedProperties = getIsChangeProperties(compMod, tableProperties);
const fullCollectionName = generateFullEntityName(entity);
if (isChangedProperties) {
const roleData = getEntityData(compMod, tableProperties.concat(otherTableProperties));
const hydratedCollection = hydrateCollection({ ...entity, role: { ...entity.role, ...roleData } }, definitions);
const addCollectionScript = getTableStatement(...hydratedCollection, null, true);
const deleteCollectionScript = provider.dropTable(fullCollectionName);
return { type: 'new', script: prepareScript(deleteCollectionScript, addCollectionScript) };
}
const hydratedAlterTable = hydrateAlterTable(entity, fullCollectionName, definitions);
return { type: 'modified', script: provider.alterTable(hydratedAlterTable) };
};
const getAddCollectionsScripts =
(definitions, data, inlineDeltaRelationships = []) =>
entity => {
const properties = getEntityProperties(entity);
const indexes = _.get(entity, 'role.SecIndxs', []);
const hydratedCollection = hydrateCollection(entity, definitions);
const isPkOrFkConstraintAvailable = getIsPkOrFkConstraintAvailable(data);
const foreignKeyConstraints = inlineDeltaRelationships
.filter(relationship => relationship.role.childCollection === entity.role.id)
.map(relationship => {
const compMod = relationship.role.compMod;
const relationshipName =
compMod.code?.new || compMod.name?.new || relationship.role.code || relationship.role.name || '';
const constraintName = relationshipName.includes(' ') ? `\`${relationshipName}\`` : relationshipName;
const parentTableName = compMod.parent.collection.name;
const childColumns = compMod.child.collection.fkFields.map(field => field.name).join(', ');
const parentColumns = compMod.parent.collection.fkFields.map(field => field.name).join(', ');
const disableNoValidate = relationship.role?.customProperties?.disableNoValidate;
const statement = getForeignKeyConstraint({
constraintName,
childColumns,
parentTableName,
parentColumns,
disableNoValidate,
});
return statement;
})
.join('\n');
const collectionScript = getTableStatement(
...hydratedCollection,
foreignKeyConstraints,
true,
isPkOrFkConstraintAvailable,
);
const indexScript = getIndexes(...hydrateAddIndexes(entity, indexes, properties, definitions));
return prepareScript(collectionScript, indexScript);
};
const getDeleteCollectionsScripts = provider => entity => {
const entityData = { ...entity, ..._.get(entity, 'role', {}) };
const fullCollectionName = generateFullEntityName(entity);
const collectionScript = provider.dropTable(fullCollectionName);
const indexScript = provider.dropTableIndex(hydrateDropIndexes(entityData));
return prepareScript(...indexScript, collectionScript);
};
const getModifyCollectionsScripts = (definitions, provider, data) => entity => {
const properties = getEntityProperties(entity);
const { script } = generateModifyCollectionScript(entity, definitions, provider);
const { hydratedAddIndexes, hydratedDropIndexes } = hydrateIndex(entity, properties, definitions);
const dropIndexScript = provider.dropTableIndex(hydratedDropIndexes);
const addIndexScript = getIndexes(...hydratedAddIndexes);
const modifyPKConstraintScripts = getIsPkOrFkConstraintAvailable(data)
? getModifyPkConstraintsScripts({ collection: entity, provider })
: [];
const modifyUKConstraintScripts = getIsConstraintAvailable(data)
? getModifyUkConstraintsScripts({ collection: entity, provider })
: [];
return prepareScript(
...dropIndexScript,
...script,
addIndexScript,
...modifyPKConstraintScripts,
...modifyUKConstraintScripts,
);
};
const getAddColumnsScripts = (definitions, provider) => entity => {
const entityData = { ...entity, ..._.omit(entity.role, ['properties']) };
const { columns } = getColumns(entityData, true, definitions);
const properties = getEntityProperties(entity);
const columnStatement = getColumnsStatement(columns, null, entityData.disableNoValidate);
const fullCollectionName = generateFullEntityName(entity);
const { hydratedAddIndexes, hydratedDropIndexes } = hydrateIndex(entity, properties, definitions);
const modifyScript = generateModifyCollectionScript(entity, definitions, provider);
const dropIndexScript = provider.dropTableIndex(hydratedDropIndexes);
const addIndexScript = getIndexes(...hydratedAddIndexes);
const addColumnScript = provider.addTableColumns({ name: fullCollectionName, columns: columnStatement });
return modifyScript.type === 'new'
? prepareScript(...dropIndexScript, ...modifyScript.script, addIndexScript)
: prepareScript(...dropIndexScript, addColumnScript, ...modifyScript.script, addIndexScript);
};
const getDeleteColumnsScripts = (definitions, provider) => entity => {
const deleteColumnsName = Object.keys(entity.properties || {});
const properties = _.omit(_.get(entity, 'role.properties', {}), deleteColumnsName);
const entityData = { role: { ..._.omit(entity.role, ['properties']), properties } };
const { hydratedAddIndexes, hydratedDropIndexes } = hydrateIndex(entity, properties, definitions);
const fullCollectionName = generateFullEntityName(entity);
const dropIndexScript = provider.dropTableIndex(hydratedDropIndexes);
const addIndexScript = getIndexes(...hydratedAddIndexes);
const deleteCollectionScript = provider.dropTable(fullCollectionName);
const hydratedCollection = hydrateCollection(entityData, definitions);
const addCollectionScript = getTableStatement(...hydratedCollection, null, true);
return prepareScript(...dropIndexScript, deleteCollectionScript, addCollectionScript, addIndexScript);
};
const getModifyColumnsScripts = (definitions, provider) => entity => {
const properties = _.get(entity, 'properties', {});
const hydratedAlterColumns = hydrateAlterColumns(entity, definitions);
const alterColumnScripts = provider.alterTableColumns(hydratedAlterColumns);
const { hydratedAddIndexes, hydratedDropIndexes } = hydrateIndex(entity, properties, definitions);
const dropIndexScript = provider.dropTableIndex(hydratedDropIndexes);
const addIndexScript = getIndexes(...hydratedAddIndexes);
const modifyNotNullConstraintsScripts = getModifyNonNullColumnsScripts({
collection: entity,
provider,
definitions,
});
const modifyDefaultValueConstraintsScripts = getModifyDefaultValueConstraintsScripts({
collection: entity,
provider,
definitions,
});
const modifyCheckConstraintsScripts = getModifyCheckConstraintsScripts({
collection: entity,
provider,
definitions,
});
return prepareScript(
...dropIndexScript,
...alterColumnScripts,
addIndexScript,
...modifyNotNullConstraintsScripts,
...modifyDefaultValueConstraintsScripts,
...modifyCheckConstraintsScripts,
);
};
module.exports = {
getAddCollectionsScripts,
getDeleteCollectionsScripts,
getModifyCollectionsScripts,
getAddColumnsScripts,
getDeleteColumnsScripts,
getModifyColumnsScripts,
};