Skip to content

Commit 0b7aebe

Browse files
committed
RE: implement reverse-engineering of unique keys with single key as unique on field level
1 parent 3156c71 commit 0b7aebe

File tree

3 files changed

+103
-39
lines changed

3 files changed

+103
-39
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const reorganizeConstraints = (attributes, entityLevel) => {
2+
return [reorganizePrimaryKeys, reorganizeUniqueKeys].reduce(
3+
({ attributes, entityLevel }, reorganize) => reorganize(attributes, entityLevel),
4+
{ attributes, entityLevel },
5+
);
6+
};
7+
8+
const reorganizePrimaryKeys = (attributes, entityLevel) => {
9+
const isSinglePrimaryKey = entityLevel.primaryKey?.[0]?.compositePrimaryKey?.length !== 1;
10+
11+
if (isSinglePrimaryKey) {
12+
return { attributes, entityLevel };
13+
}
14+
15+
const primaryKey = entityLevel.primaryKey[0];
16+
const attributeName = primaryKey.compositePrimaryKey[0];
17+
18+
return {
19+
attributes: setAttributesPrimaryKeyData(attributes, attributeName, primaryKey),
20+
entityLevel: clearPrimaryKeys(entityLevel),
21+
};
22+
};
23+
24+
const reorganizeUniqueKeys = (attributes, entityLevel) => {
25+
return (entityLevel.uniqueKey || []).reduce(
26+
({ attributes, entityLevel }, uniqueKey) => {
27+
const hasSingleUniqueKey = uniqueKey.compositeUniqueKey.length === 1;
28+
if (!hasSingleUniqueKey) {
29+
return { attributes, entityLevel };
30+
}
31+
32+
const attributeName = uniqueKey.compositeUniqueKey[0];
33+
34+
return {
35+
attributes: setAttributesUniqueKeyData(attributes, attributeName, uniqueKey),
36+
entityLevel: filterUniqueKeys(entityLevel, uniqueKey),
37+
};
38+
},
39+
{ attributes, entityLevel },
40+
);
41+
};
42+
43+
const setAttributesPrimaryKeyData = (attributes, attributeName, primaryKey) => {
44+
return attributes.map(attribute => {
45+
if (attribute.name !== attributeName) {
46+
return attribute;
47+
}
48+
49+
return setPrimaryKeyData(attribute, primaryKey);
50+
});
51+
};
52+
53+
const setPrimaryKeyData = (attribute, primaryKey) => {
54+
return {
55+
...attribute,
56+
primaryKey: true,
57+
primaryKeyOptions: getOptions(primaryKey),
58+
};
59+
};
60+
61+
const setAttributesUniqueKeyData = (attributes, attributeName, uniqueKey) => {
62+
return attributes.map(attribute => {
63+
if (attribute.name !== attributeName) {
64+
return attribute;
65+
}
66+
67+
return setUniqueKeyData(attribute, uniqueKey);
68+
});
69+
};
70+
71+
const setUniqueKeyData = (attribute, uniqueKey) => {
72+
return {
73+
...attribute,
74+
unique: true,
75+
uniqueKeyOptions: getOptions(uniqueKey),
76+
};
77+
};
78+
79+
const getOptions = key => {
80+
return [
81+
{
82+
constraintName: key.constraintName,
83+
indexInclude: key.indexInclude,
84+
indexStorageParameters: key.indexStorageParameters,
85+
indexTablespace: key.indexTablespace,
86+
indexComment: key.indexComment,
87+
},
88+
];
89+
};
90+
91+
const clearPrimaryKeys = entityLevel => {
92+
return { ...entityLevel, primaryKey: [] };
93+
};
94+
95+
const filterUniqueKeys = (entityLevel, uniqueKey) => {
96+
const filteredKeys = entityLevel.uniqueKey.filter(key => key.constraintName !== uniqueKey.constraintName);
97+
98+
return { ...entityLevel, uniqueKey: filteredKeys };
99+
};
100+
101+
module.exports = { reorganizeConstraints };

reverse_engineering/helpers/postgresHelpers/reorganizePrimaryKeys.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

reverse_engineering/helpers/postgresService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const {
4545
} = require('./postgresHelpers/viewHelper');
4646
const { setDependencies: setDependenciesInTriggerHelper, getTriggers } = require('./postgresHelpers/triggerHelper');
4747
const queryConstants = require('./queryConstants');
48-
const { reorganizePrimaryKeys } = require('./postgresHelpers/reorganizePrimaryKeys');
48+
const { reorganizeConstraints } = require('./postgresHelpers/reorganizeConstraints');
4949

5050
let currentSshTunnel = null;
5151
let _ = null;
@@ -305,7 +305,7 @@ module.exports = {
305305
targetAttributes = setSubtypeFromSampledJsonValues(targetAttributes, documents);
306306
}
307307

308-
const { attributes, entityLevel: updatedEntityLevel } = reorganizePrimaryKeys(targetAttributes, entityLevel);
308+
const { attributes, entityLevel: updatedEntityLevel } = reorganizeConstraints(targetAttributes, entityLevel);
309309

310310
return {
311311
name: tableName,

0 commit comments

Comments
 (0)