-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprimaryKeyHelper.js
More file actions
157 lines (130 loc) · 5.53 KB
/
Copy pathprimaryKeyHelper.js
File metadata and controls
157 lines (130 loc) · 5.53 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
const _ = require('lodash');
const { prepareName, commentDeactivatedStatements } = require('../generalHelper');
const templates = require('./config/templates');
const { generateFullEntityName, getDefaultConstraintName } = require('./generalHelper');
const { CONSTRAINT_POSTFIX } = require('../constants');
const getPropertyNameByGuid = (collection, guid) => {
const property = _.toPairs(collection?.role?.properties).find(([, jsonSchema]) => jsonSchema.GUID === guid);
return property?.[0] && prepareName(property[0]);
};
const getPropertiesNamesByGUIDs = (collection, guids) => {
return guids.map(guid => getPropertyNameByGuid(collection, guid)).filter(Boolean);
};
const didCompositePkChange = collection => {
const pkDto = collection?.role?.compMod?.primaryKey || {};
const newPrimaryKeys = pkDto.new || [];
const oldPrimaryKeys = pkDto.old || [];
if (newPrimaryKeys.length !== oldPrimaryKeys.length) {
return true;
}
if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) {
return false;
}
return !_.isEmpty(_.differenceWith(oldPrimaryKeys, newPrimaryKeys, _.isEqual));
};
const getDropCompositePkScripts = ({ collection, provider }) => {
const didPkChange = didCompositePkChange(collection);
if (!didPkChange) {
return [];
}
const tableName = generateFullEntityName(collection);
const pkDto = collection?.role?.compMod?.primaryKey || {};
const oldPrimaryKeys = pkDto.old || [];
return oldPrimaryKeys.map(oldPk => {
const pkConstraintName =
oldPk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
const constraintName = prepareName(pkConstraintName);
return provider.assignTemplates(templates.dropConstraint, {
tableName,
constraintName,
});
});
};
const getAddCompositePkScripts = ({ collection, provider }) => {
const didPkChange = didCompositePkChange(collection);
if (!didPkChange) {
return [];
}
const tableName = generateFullEntityName(collection);
const pkDto = collection?.role?.compMod?.primaryKey || {};
const newPrimaryKeys = pkDto.new || [];
return newPrimaryKeys.map(newPk => {
const compositePrimaryKey = newPk.compositePrimaryKey || [];
const guidsOfColumnsInPk = compositePrimaryKey.map(compositePkEntry => compositePkEntry.keyId);
const columnNames = getPropertiesNamesByGUIDs(collection, guidsOfColumnsInPk);
const pkConstraintName =
newPk.constraintName || getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
const constraintName = prepareName(pkConstraintName);
const noValidate = newPk.noValidateSpecification ? ` ${newPk.noValidateSpecification}` : '';
const rely = newPk.rely ? ` ${newPk.rely}` : '';
return provider.assignTemplates(templates.addPkConstraint, {
tableName,
constraintName,
columnNames,
noValidate,
rely,
});
});
};
const getModifyCompositePkScripts = ({ collection, provider }) => {
const dropCompositePkScripts = getDropCompositePkScripts({ collection, provider });
const addCompositePkScripts = getAddCompositePkScripts({ collection, provider });
return [...dropCompositePkScripts, ...addCompositePkScripts];
};
const getDropPkScripts = ({ collection, provider }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
return _.toPairs(collection.properties)
.filter(([, jsonSchema]) => {
const oldName = jsonSchema.compMod.oldField.name;
const oldJsonSchema = collection.role.properties[oldName];
const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey;
const isNotAPrimaryKey = !jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey;
})
.map(() => {
return provider.assignTemplates(templates.dropConstraint, {
tableName,
constraintName,
});
});
};
const getAddPkScripts = ({ collection, provider }) => {
const tableName = generateFullEntityName(collection);
const constraintName = getDefaultConstraintName({ collection, postfix: CONSTRAINT_POSTFIX.primaryKey });
return _.toPairs(collection.properties)
.filter(([, jsonSchema]) => {
const isRegularPrimaryKey = jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey;
const oldName = jsonSchema.compMod.oldField.name;
const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey);
return isRegularPrimaryKey && !wasTheFieldAPrimaryKey;
})
.map(([name, jsonSchema]) => {
const columnNames = [prepareName(name)];
const noValidate = jsonSchema.noValidateSpecification ? ` ${jsonSchema.noValidateSpecification}` : '';
const rely = jsonSchema.rely ? ` ${jsonSchema.rely}` : '';
return provider.assignTemplates(templates.addPkConstraint, {
tableName,
constraintName,
columnNames,
noValidate,
rely,
});
});
};
const getModifyPkScripts = ({ collection, provider }) => {
const dropPkScripts = getDropPkScripts({ collection, provider });
const addPkScripts = getAddPkScripts({ collection, provider });
return [...dropPkScripts, ...addPkScripts];
};
const getModifyPkConstraintsScripts = ({ collection, provider }) => {
const modifyCompositePkScripts = getModifyCompositePkScripts({ collection, provider });
const modifyPkScripts = getModifyPkScripts({ collection, provider });
const isActivated = collection.role.isActivated;
return [...modifyCompositePkScripts, ...modifyPkScripts].map(statement =>
commentDeactivatedStatements(statement, isActivated),
);
};
module.exports = {
getModifyPkConstraintsScripts,
};