Skip to content

Commit 26c29ef

Browse files
committed
FE: added generation of DDL for renaming and changing name of columns
1 parent 228cd67 commit 26c29ef

File tree

5 files changed

+114
-19
lines changed

5 files changed

+114
-19
lines changed

forward_engineering/config.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
"mode": "pgsql",
99
"fileExtensions": [
1010
{
11-
"value": "sql",
12-
"label": "SQL"
11+
"value": "sql",
12+
"label": "SQL"
1313
}
14-
]
14+
]
1515
},
1616
{
1717
"name": "Alter Script from Delta model",
1818
"keyword": "alterFromDelta",
1919
"cliOnly": true,
2020
"fileExtensions": [
2121
{
22-
"value": "sql",
23-
"label": "SQL"
22+
"value": "sql",
23+
"label": "SQL"
2424
}
25-
]
25+
]
2626
}
2727
],
2828
"namePrefix": "PostgreSQL",

forward_engineering/helpers/alterScriptFromDeltaHelper.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const {
44
getDeleteCollectionScript,
55
getAddColumnScript,
66
getDeleteColumnScript,
7+
getModifyColumnScript,
78
} = require('./alterScriptHelpers/alterEntityHelper');
89
const {
910
getDeleteUdtScript,
1011
getCreateUdtScript,
1112
getAddColumnToTypeScript,
1213
getDeleteColumnFromTypeScript,
14+
getModifyColumnOfTypeScript,
1315
} = require('./alterScriptHelpers/alterUdtHelper');
1416
const { getAddViewScript, getDeleteViewScript } = require('./alterScriptHelpers/alterViewHelper');
1517

@@ -60,10 +62,20 @@ const getAlterCollectionsScripts = (collection, app, dbVersion) => {
6062
.map(item => Object.values(item.properties)[0])
6163
.filter(collection => !collection.compMod)
6264
.flatMap(getDeleteColumnScript(app));
65+
const modifyColumnScript = []
66+
.concat(collection.properties?.entities?.properties?.modified?.items)
67+
.filter(Boolean)
68+
.map(item => Object.values(item.properties)[0])
69+
.filter(collection => !collection.compMod)
70+
.flatMap(getModifyColumnScript(app));
6371

64-
return [...createCollectionsScripts, ...deleteCollectionScripts, ...addColumnScripts, ...deleteColumnScripts].map(
65-
script => script.trim(),
66-
);
72+
return [
73+
...createCollectionsScripts,
74+
...deleteCollectionScripts,
75+
...addColumnScripts,
76+
...deleteColumnScripts,
77+
...modifyColumnScript,
78+
].map(script => script.trim());
6779
};
6880

6981
const getAlterViewScripts = (collection, app) => {
@@ -80,7 +92,6 @@ const getAlterViewScripts = (collection, app) => {
8092
.filter(Boolean)
8193
.map(item => Object.values(item.properties)[0])
8294
.map(view => ({ ...view, ...(view.role || {}) }))
83-
.filter(view => view.compMod?.deleted)
8495
.map(getDeleteViewScript(app));
8596

8697
return [...deleteViewsScripts, ...createViewsScripts].map(script => script.trim());
@@ -118,9 +129,22 @@ const getAlterModelDefinitionsScripts = (collection, app, dbVersion) => {
118129
.filter(item => item.childType === 'composite')
119130
.flatMap(getDeleteColumnFromTypeScript(app));
120131

121-
return [...deleteUdtScripts, ...createUdtScripts, ...addColumnScripts, ...deleteColumnScripts].map(script =>
122-
script.trim(),
123-
);
132+
const modifyColumnScripts = []
133+
.concat(collection.properties?.modelDefinitions?.properties?.modified?.items)
134+
.filter(Boolean)
135+
.map(item => Object.values(item.properties)[0])
136+
.filter(item => !item.compMod)
137+
.map(item => ({ ...item, ...(app.require('lodash').omit(item.role, 'properties') || {}) }))
138+
.filter(item => item.childType === 'composite')
139+
.flatMap(getModifyColumnOfTypeScript(app));
140+
141+
return [
142+
...deleteUdtScripts,
143+
...createUdtScripts,
144+
...addColumnScripts,
145+
...deleteColumnScripts,
146+
...modifyColumnScripts,
147+
].map(script => script.trim());
124148
};
125149

126150
module.exports = {

forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { checkFieldPropertiesChanged } = require('./common');
2+
13
const getAddCollectionScript = (app, dbVersion) => collection => {
24
const _ = app.require('lodash');
35
const { getEntityName } = require('../../utils/general')(_);
@@ -16,7 +18,7 @@ const getAddCollectionScript = (app, dbVersion) => collection => {
1618
schemaData,
1719
}),
1820
);
19-
const checkConstraints = jsonSchema.chkConstr.map(check =>
21+
const checkConstraints = (jsonSchema.chkConstr || []).map(check =>
2022
ddlProvider.createCheckConstraint(ddlProvider.hydrateCheckConstraint(check)),
2123
);
2224
const tableData = {
@@ -70,10 +72,10 @@ const getAddColumnScript = (app, dbVersion) => collection => {
7072
}),
7173
)
7274
.map(ddlProvider.convertColumnDefinition)
73-
.map(script => `ALTER TABLE ${fullName} ADD COLUMN IF NOT EXISTS ${script};`);
75+
.map(script => `ALTER TABLE IF EXISTS ${fullName} ADD COLUMN IF NOT EXISTS ${script};`);
7476
};
7577

76-
const getDeleteColumnScript = (app, dbVersion) => collection => {
78+
const getDeleteColumnScript = app => collection => {
7779
const _ = app.require('lodash');
7880
const { getEntityName } = require('../../utils/general')(_);
7981
const { getNamePrefixedWithSchemaName, wrapInQuotes } = require('../general')({ _ });
@@ -85,12 +87,44 @@ const getDeleteColumnScript = (app, dbVersion) => collection => {
8587

8688
return _.toPairs(collection.properties)
8789
.filter(([name, jsonSchema]) => !jsonSchema.compMod)
88-
.map(([name]) => `ALTER TABLE ${fullName} DROP COLUMN IF EXISTS ${wrapInQuotes(name)};`);
90+
.map(([name]) => `ALTER TABLE IF EXISTS ${fullName} DROP COLUMN IF EXISTS ${wrapInQuotes(name)};`);
91+
};
92+
93+
const getModifyColumnScript = app => collection => {
94+
const _ = app.require('lodash');
95+
const { getEntityName } = require('../../utils/general')(_);
96+
const { getNamePrefixedWithSchemaName, wrapInQuotes } = require('../general')({ _ });
97+
98+
const collectionSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
99+
const tableName = getEntityName(collectionSchema);
100+
const schemaName = collectionSchema.compMod?.keyspaceName;
101+
const fullName = getNamePrefixedWithSchemaName(tableName, schemaName);
102+
103+
const renameColumnScripts = _.values(collection.properties)
104+
.filter(jsonSchema => checkFieldPropertiesChanged(jsonSchema.compMod, ['name']))
105+
.map(
106+
jsonSchema =>
107+
`ALTER TABLE IF EXISTS ${fullName} RENAME COLUMN ${wrapInQuotes(
108+
jsonSchema.compMod.oldField.name,
109+
)} TO ${wrapInQuotes(jsonSchema.compMod.newField.name)};`,
110+
);
111+
112+
const changeTypeScripts = _.toPairs(collection.properties)
113+
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
114+
.map(
115+
([name, jsonSchema]) =>
116+
`ALTER TABLE IF EXISTS ${fullName} ALTER COLUMN ${wrapInQuotes(name)} SET DATA TYPE ${
117+
jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type
118+
};`,
119+
);
120+
121+
return [...renameColumnScripts, ...changeTypeScripts];
89122
};
90123

91124
module.exports = {
92125
getAddCollectionScript,
93126
getDeleteCollectionScript,
94127
getAddColumnScript,
95128
getDeleteColumnScript,
129+
getModifyColumnScript,
96130
};

forward_engineering/helpers/alterScriptHelpers/alterUdtHelper.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { checkFieldPropertiesChanged } = require('./common');
2+
13
const getCreateUdtScript = (app, dbVersion) => jsonSchema => {
24
const _ = app.require('lodash');
35
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(_);
@@ -33,9 +35,9 @@ const getDeleteUdtScript = app => udt => {
3335
const { wrapInQuotes } = require('../general')({ _ });
3436

3537
if (udt.type === 'domain') {
36-
return `DROP DOMAIN IF EXISTS ${wrapInQuotes(udt.code || udt.name)}`;
38+
return `DROP DOMAIN IF EXISTS ${wrapInQuotes(udt.code || udt.name)};`;
3739
} else {
38-
return `DROP TYPE IF EXISTS ${wrapInQuotes(udt.code || udt.name)}`;
40+
return `DROP TYPE IF EXISTS ${wrapInQuotes(udt.code || udt.name)};`;
3941
}
4042
};
4143

@@ -74,9 +76,37 @@ const getDeleteColumnFromTypeScript = app => udt => {
7476
.map(([name]) => `ALTER TYPE ${fullName} DROP ATTRIBUTE IF EXISTS ${wrapInQuotes(name)};`);
7577
};
7678

79+
const getModifyColumnOfTypeScript = app => udt => {
80+
const _ = app.require('lodash');
81+
const { wrapInQuotes } = require('../general')({ _ });
82+
83+
const fullName = wrapInQuotes(udt.code || udt.name);
84+
85+
const renameColumnScripts = _.values(udt.properties)
86+
.filter(jsonSchema => checkFieldPropertiesChanged(jsonSchema.compMod, ['name']))
87+
.map(
88+
jsonSchema =>
89+
`ALTER TYPE ${fullName} RENAME ATTRIBUTE ${wrapInQuotes(
90+
jsonSchema.compMod.oldField.name,
91+
)} TO ${wrapInQuotes(jsonSchema.compMod.newField.name)};`,
92+
);
93+
94+
const changeTypeScripts = _.toPairs(udt.properties)
95+
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
96+
.map(
97+
([name, jsonSchema]) =>
98+
`ALTER TYPE ${fullName} ALTER ATTRIBUTE ${wrapInQuotes(name)} SET DATA TYPE ${
99+
jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type
100+
};`,
101+
);
102+
103+
return [...renameColumnScripts, ...changeTypeScripts];
104+
};
105+
77106
module.exports = {
78107
getCreateUdtScript,
79108
getDeleteUdtScript,
80109
getAddColumnToTypeScript,
81110
getDeleteColumnFromTypeScript,
111+
getModifyColumnOfTypeScript,
82112
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const checkFieldPropertiesChanged = (compMod, propertiesToCheck) => {
2+
return propertiesToCheck.some(prop => compMod?.oldField[prop] !== compMod?.newField[prop]);
3+
};
4+
5+
module.exports = {
6+
checkFieldPropertiesChanged,
7+
};

0 commit comments

Comments
 (0)