-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathalterForeignKeyHelper.js
More file actions
186 lines (154 loc) · 6.49 KB
/
Copy pathalterForeignKeyHelper.js
File metadata and controls
186 lines (154 loc) · 6.49 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
const {
getFullEntityName,
generateFullEntityName,
getEntityProperties,
getContainerName,
getEntityData,
getEntityName,
prepareScript,
hydrateProperty,
} = require('./generalHelper');
const { replaceSpaceWithUnderscore, prepareName, commentDeactivatedStatements } = require('../generalHelper');
const templates = require('./config/templates');
const { getItems } = require('./common');
const getRelationshipName = relationship => {
return relationship.role.code || relationship.role.name;
};
const getFullParentTableName = relationship => {
const compMod = relationship.role.compMod;
const parentDBName = replaceSpaceWithUnderscore(prepareName(compMod.parent.bucket.name));
const parentEntityName = replaceSpaceWithUnderscore(compMod.parent.collection.name);
return getFullEntityName(parentDBName, parentEntityName);
};
const getFullChildTableName = relationship => {
const compMod = relationship.role.compMod;
const childDBName = replaceSpaceWithUnderscore(prepareName(compMod.child.bucket.name));
const childEntityName = replaceSpaceWithUnderscore(compMod.child.collection.name);
return getFullEntityName(childDBName, childEntityName);
};
const getAddSingleForeignKeyScript = provider => relationship => {
const compMod = relationship.role.compMod;
const parentTableName = getFullParentTableName(relationship);
const childTableName = getFullChildTableName(relationship);
const relationshipName = compMod.code?.new || compMod.name?.new || getRelationshipName(relationship) || '';
const constraintName = relationshipName.includes(' ') ? `\`${relationshipName}\`` : relationshipName;
const childColumns = compMod.child.collection.fkFields.map(field => prepareName(field.name));
const parentColumns = compMod.parent.collection.fkFields.map(field => prepareName(field.name));
const disableNoValidate = relationship.role?.compMod?.customProperties?.new?.disableNoValidate;
const disableNoValidateClause = disableNoValidate ? ' DISABLE NOVALIDATE' : '';
return provider.assignTemplates(templates.addFkConstraint, {
childTableName,
constraintName,
childColumns,
parentTableName,
parentColumns,
disableNoValidate: disableNoValidateClause,
});
};
const canRelationshipBeAdded = relationship => {
const compMod = relationship.role.compMod;
if (!compMod) {
return false;
}
return [
compMod.code?.new || compMod.name?.new || getRelationshipName(relationship),
compMod.parent?.bucket,
compMod.parent?.collection,
compMod.parent?.collection?.fkFields?.length,
compMod.child?.bucket,
compMod.child?.collection,
compMod.child?.collection?.fkFields?.length,
].every(property => Boolean(property));
};
const getAddForeignKeyScript = provider => relationship => {
const script = getAddSingleForeignKeyScript(provider)(relationship);
const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new);
return commentDeactivatedStatements(script, isActivated);
};
const getDeleteSingleForeignKeyScript = provider => relationship => {
const compMod = relationship.role.compMod;
const tableName = getFullChildTableName(relationship);
const relationshipName = compMod.code?.old || compMod.name?.old || getRelationshipName(relationship) || '';
const constraintName = prepareName(relationshipName);
return provider.assignTemplates(templates.dropConstraint, {
tableName,
constraintName,
});
};
const canRelationshipBeDeleted = relationship => {
const compMod = relationship.role.compMod;
if (!compMod) {
return false;
}
return [
compMod.code?.old || compMod.name?.old || getRelationshipName(relationship),
compMod.child?.bucket,
compMod.child?.collection,
].every(property => Boolean(property));
};
const getDeleteForeignKeyScripts = provider => deletedRelationships => {
return deletedRelationships
.filter(relationship => canRelationshipBeDeleted(relationship))
.map(relationship => {
const script = getDeleteSingleForeignKeyScript(provider)(relationship);
const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new);
return commentDeactivatedStatements(script, isActivated);
});
};
const getModifyForeignKeyScript = provider => relationship => {
const deleteScript = getDeleteSingleForeignKeyScript(provider)(relationship);
const addScript = getAddSingleForeignKeyScript(provider)(relationship);
const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new);
return (
commentDeactivatedStatements(deleteScript, isActivated) + commentDeactivatedStatements(addScript, isActivated)
);
};
const getAlterForeignKeyScripts = ({ schema, provider, currentSchemaName, ignoreRelationshipIDs = [] }) => {
const generateAddFkScripts = (addedRelationships, getScript) => {
return addedRelationships.filter(relationship => canRelationshipBeAdded(relationship)).flatMap(getScript);
};
const generateModifyFkScripts = (modifiedRelationships, getScript) => {
return modifiedRelationships
.filter(relationship => canRelationshipBeAdded(relationship) && canRelationshipBeDeleted(relationship))
.flatMap(getScript);
};
const getRelationshipsScriptsWithUseSchema = (relationships, processRelationships, getScript) => {
return processRelationships(relationships, relationship => {
const script = getScript(provider)(relationship);
if (!script) {
return [];
}
const schemaName = replaceSpaceWithUnderscore(
prepareName(relationship.role.compMod.child.bucket?.name || ''),
);
if (currentSchemaName === schemaName) {
return [script];
}
currentSchemaName = schemaName;
const useSchemaScript = provider.assignTemplates(templates.useSchema, { schemaName });
return [useSchemaScript, script];
});
};
const deletedRelationships = getItems(schema, 'relationships', 'deleted').filter(
relationship => relationship.role?.compMod?.deleted && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);
const addedRelationships = getItems(schema, 'relationships', 'added').filter(
relationship => relationship.role?.compMod?.created && !ignoreRelationshipIDs.includes(relationship?.role?.id),
);
const modifiedRelationships = getItems(schema, 'relationships', 'modified');
const deleteFkScripts = getDeleteForeignKeyScripts(provider)(deletedRelationships);
const addFkScripts = getRelationshipsScriptsWithUseSchema(
addedRelationships,
generateAddFkScripts,
getAddForeignKeyScript,
);
const modifiedFkScripts = getRelationshipsScriptsWithUseSchema(
modifiedRelationships,
generateModifyFkScripts,
getModifyForeignKeyScript,
);
return { deleteFkScripts, addFkScripts, modifiedFkScripts };
};
module.exports = {
getAlterForeignKeyScripts,
};