-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdefaultConstraintHelper.js
More file actions
52 lines (43 loc) · 1.78 KB
/
Copy pathdefaultConstraintHelper.js
File metadata and controls
52 lines (43 loc) · 1.78 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
const _ = require('lodash');
const templates = require('./config/templates');
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
const { getTypeByProperty } = require('../columnHelper');
const { commentDeactivatedStatements } = require('../generalHelper');
const { CONSTRAINT_POSTFIX } = require('../constants');
const getModifyDefaultValueConstraintsScripts = ({ collection, provider, definitions }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.default });
const isActivated = collection.role.isActivated;
return _.toPairs(collection.properties).flatMap(([columnName, jsonSchema]) => {
const oldName = jsonSchema.compMod.oldField.name;
const newField = jsonSchema.compMod.newField;
const newDefaultValue = jsonSchema.default || '';
const oldDefaultValue = collection.role.properties[oldName]?.default || '';
const type = getTypeByProperty(definitions)({ ...jsonSchema, ...newField });
const noValidate = jsonSchema.noValidateSpecification ? ` ${jsonSchema.noValidateSpecification}` : '';
const rely = jsonSchema.rely ? ` ${jsonSchema.rely}` : '';
const enable = jsonSchema.enableSpecification ? ` ${jsonSchema.enableSpecification}` : '';
const scriptParams = {
tableName,
columnName,
constraintName,
type,
enable,
noValidate,
rely,
};
const scripts = [];
if (newDefaultValue && !oldDefaultValue) {
scripts.push(
provider.assignTemplates(templates.addDefaultValueConstraint, {
...scriptParams,
defaultValue: newDefaultValue,
}),
);
}
return scripts.map(statement => commentDeactivatedStatements(statement, isActivated));
});
};
module.exports = {
getModifyDefaultValueConstraintsScripts,
};