-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckConstraintHelper.js
More file actions
122 lines (108 loc) · 4.23 KB
/
checkConstraintHelper.js
File metadata and controls
122 lines (108 loc) · 4.23 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
const _ = require('lodash');
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const {
getFullCollectionName,
wrapInQuotes,
isParentContainerActivated,
isObjectInDeltaModelActivated,
getSchemaOfAlterCollection,
} = require('../../../utils/general');
const { assignTemplates } = require('../../../utils/assignTemplates');
const templates = require('../../../ddlProvider/templates');
const addCheckConstraint = (tableName, constraintName, expression) => {
return assignTemplates({
template: templates.alterCheckConstraint,
templateData: {
tableName,
constraintName,
expression,
},
});
};
const dropConstraint = (tableName, constraintName) => {
return assignTemplates({
template: templates.dropCheckConstraint,
templateData: {
tableName,
constraintName,
},
});
};
const mapCheckConstraintNamesToChangeHistory = collection => {
const checkConstraintHistory = collection?.compMod?.chkConstr;
if (!checkConstraintHistory) {
return [];
}
const newConstraints = checkConstraintHistory.new || [];
const oldConstraints = checkConstraintHistory.old || [];
const constrNames = _.chain([...newConstraints, ...oldConstraints])
.map(constr => constr.chkConstrName)
.uniq()
.value();
return constrNames.map(chkConstrName => {
return {
old: _.find(oldConstraints, { chkConstrName }),
new: _.find(newConstraints, { chkConstrName }),
};
});
};
const getDropCheckConstraintScriptDtos = (constraintHistory, fullTableName) => {
return constraintHistory
.filter(historyEntry => historyEntry.old?.constrExpression && !historyEntry.new?.constrExpression)
.map(historyEntry => {
const wrappedConstraintName = wrapInQuotes(historyEntry.old.chkConstrName);
return dropConstraint(fullTableName, wrappedConstraintName);
})
.map(script => AlterScriptDto.getInstance([script], true, true));
};
const getAddCheckConstraintScriptDtos = (constraintHistory, fullTableName) => {
return constraintHistory
.filter(historyEntry => historyEntry.new?.constrExpression && !historyEntry.old?.constrExpression)
.map(historyEntry => {
const { chkConstrName, constrExpression } = historyEntry.new;
return addCheckConstraint(fullTableName, wrapInQuotes(chkConstrName), constrExpression);
})
.map(script => AlterScriptDto.getInstance([script], true, false));
};
const getUpdateCheckConstraintScriptDtos = (constraintHistory, fullTableName) => {
return constraintHistory
.filter(historyEntry => {
if (historyEntry.old?.constrExpression && historyEntry.new?.constrExpression) {
const oldExpression = historyEntry.old.constrExpression;
const newExpression = historyEntry.new.constrExpression;
return oldExpression !== newExpression;
}
return false;
})
.flatMap(historyEntry => {
const { chkConstrName: oldConstrainName } = historyEntry.old;
const dropConstraintScript = dropConstraint(fullTableName, wrapInQuotes(oldConstrainName));
const { chkConstrName: newConstrainName, constrExpression: newConstraintExpression } = historyEntry.new;
const addConstraintScript = addCheckConstraint(
fullTableName,
wrapInQuotes(newConstrainName),
newConstraintExpression,
);
return [
AlterScriptDto.getInstance([dropConstraintScript], true, true),
AlterScriptDto.getInstance([addConstraintScript], true, false),
];
});
};
const getModifyCheckConstraintScriptDtos = collection => {
const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName({ collectionSchema });
const constraintHistory = mapCheckConstraintNamesToChangeHistory(collection);
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);
const addCheckConstraintScripts = getAddCheckConstraintScriptDtos(constraintHistory, fullTableName);
const dropCheckConstraintScripts = getDropCheckConstraintScriptDtos(constraintHistory, fullTableName);
const updateCheckConstraintScripts = getUpdateCheckConstraintScriptDtos(constraintHistory, fullTableName);
return [...addCheckConstraintScripts, ...dropCheckConstraintScripts, ...updateCheckConstraintScripts].map(dto => ({
...dto,
isActivated: isContainerActivated && isCollectionActivated,
}));
};
module.exports = {
getModifyCheckConstraintScriptDtos,
};