Skip to content

Commit 13aac1d

Browse files
HCK-12978: Script generation options (#161)
* add script generation options config * fix order of constrains in alter statement and fix escape default value logic in alter statement * add browser support * delete unnecessary .then statement * adjust column types whose default values need to be wrapped with escape characters
1 parent ce0d5c3 commit 13aac1d

File tree

16 files changed

+567
-85
lines changed

16 files changed

+567
-85
lines changed

api/fe.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { generateScript } = require('../forward_engineering/api/generateScript');
2+
const { generateViewScript } = require('../forward_engineering/api/generateViewScript');
3+
const { generateContainerScript } = require('../forward_engineering/api/generateContainerScript');
4+
const { isDropInStatements } = require('../forward_engineering/api/isDropInStatements');
5+
6+
module.exports = {
7+
generateScript,
8+
generateViewScript,
9+
generateContainerScript,
10+
isDropInStatements,
11+
};

esbuild.package.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ esbuild
1313
.build({
1414
entryPoints: [
1515
path.resolve(__dirname, 'forward_engineering', 'api.js'),
16+
path.resolve(__dirname, 'api', 'fe.js'),
1617
path.resolve(__dirname, 'forward_engineering', 'ddlProvider.js'),
1718
path.resolve(__dirname, 'forward_engineering', 'dbtProvider.js'),
1819
path.resolve(__dirname, 'reverse_engineering', 'api.js'),

forward_engineering/alterScript/alterScriptFromDeltaHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ const getAlterCollectionsScriptDtos = ({
133133
...deleteCollectionScriptDtos,
134134
...modifyCollectionScriptDtos,
135135
...addColumnScriptDtos,
136-
...modifyCollectionKeysScriptDtos,
137136
...deleteColumnScriptDtos,
138137
...modifyColumnScriptDtos,
138+
...modifyCollectionKeysScriptDtos,
139139
].filter(Boolean);
140140
};
141141

forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ const getModifyColumnScriptDtos =
290290
...renameColumnScriptDtos,
291291
...updateTypeScriptDtos,
292292
...modifyNotNullScriptDtos,
293-
...modifyCommentScriptDtos,
294293
...modifyDefaultColumnValueScriptDtos,
294+
...modifyCommentScriptDtos,
295295
].filter(Boolean);
296296
};
297297

forward_engineering/alterScript/alterScriptHelpers/columnHelpers/defaultValueHelper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { toPairs } = require('lodash');
22
const { AlterScriptDto } = require('../../types/AlterScriptDto');
3-
const { getFullTableName, wrapInQuotes, wrapInSingleQuotes } = require('../../../utils/general');
3+
const { getFullTableName, wrapInQuotes } = require('../../../utils/general');
4+
const { decorateDefault } = require('../../../ddlProvider/ddlHelpers/columnDefinitionHelper');
45
const assignTemplates = require('../../../utils/assignTemplates');
56
const templates = require('../../../ddlProvider/templates');
67

@@ -35,10 +36,12 @@ const getUpdatedDefaultColumnValueScriptDtos = ({ collection }) =>
3536
})
3637
.map(([columnName, jsonSchema]) => {
3738
const newDefaultValue = jsonSchema.default;
39+
const type = jsonSchema.mode || jsonSchema.childType || jsonSchema.type;
40+
const isArrayType = Array.isArray(jsonSchema.array_type) && jsonSchema.array_type.length > 0;
3841
const scriptGenerationConfig = {
3942
tableName: getFullTableName(collection),
4043
columnName: wrapInQuotes(columnName),
41-
defaultValue: wrapInSingleQuotes({ name: newDefaultValue }),
44+
defaultValue: decorateDefault(type, newDefaultValue, isArrayType),
4245
};
4346
return updateColumnDefaultValue(scriptGenerationConfig);
4447
})

forward_engineering/api.js

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,17 @@
1-
const _ = require('lodash');
2-
const reApi = require('../reverse_engineering/api');
3-
const { createLogger } = require('../reverse_engineering/helpers/loggerHelper');
4-
const applyToInstanceHelper = require('./applyToInstanceHelper');
5-
const {
6-
buildEntityLevelAlterScript,
7-
buildContainerLevelAlterScript,
8-
doesContainerLevelAlterScriptContainDropStatements,
9-
doesEntityLevelAlterScriptContainDropStatements,
10-
} = require('./alterScript/alterScriptBuilder');
1+
const { generateScript } = require('./api/generateScript');
2+
const { generateViewScript } = require('./api/generateViewScript');
3+
const { generateContainerScript } = require('./api/generateContainerScript');
4+
const { getDatabases } = require('./api/getDatabases');
5+
const { applyToInstance } = require('./api/applyToInstance');
6+
const { testConnection } = require('./api/testConnection');
7+
const { isDropInStatements } = require('./api/isDropInStatements');
118

129
module.exports = {
13-
generateScript(data, logger, callback, app) {
14-
try {
15-
const script = buildEntityLevelAlterScript(data, app);
16-
callback(null, script);
17-
} catch (error) {
18-
logger.log('error', { message: error.message, stack: error.stack }, 'PostgreSQL Forward-Engineering Error');
19-
20-
callback({ message: error.message, stack: error.stack });
21-
}
22-
},
23-
24-
generateViewScript(data, logger, callback, app) {
25-
callback(new Error('Forward-Engineering of delta model on view level is not supported'));
26-
},
27-
28-
generateContainerScript(data, logger, callback, app) {
29-
try {
30-
const script = buildContainerLevelAlterScript(data, app);
31-
callback(null, script);
32-
} catch (error) {
33-
logger.log('error', { message: error.message, stack: error.stack }, 'PostgreSQL Forward-Engineering Error');
34-
35-
callback({ message: error.message, stack: error.stack });
36-
}
37-
},
38-
39-
getDatabases(connectionInfo, logger, callback, app) {
40-
logger.progress({ message: 'Find all databases' });
41-
42-
reApi.getDatabases(connectionInfo, logger, callback, app);
43-
},
44-
45-
applyToInstance(connectionInfo, logger, callback, app) {
46-
logger.clear();
47-
logger.log(
48-
'info',
49-
_.omit(connectionInfo, 'script', 'containerData'),
50-
'connectionInfo',
51-
connectionInfo.hiddenKeys,
52-
);
53-
54-
const postgresLogger = createLogger({
55-
title: 'Apply to instance',
56-
hiddenKeys: connectionInfo.hiddenKeys,
57-
logger,
58-
});
59-
60-
applyToInstanceHelper.applyToInstance(connectionInfo, postgresLogger, app).then(callback, callback);
61-
},
62-
63-
testConnection(connectionInfo, logger, callback, app) {
64-
reApi.testConnection(connectionInfo, logger, callback, app).then(callback, callback);
65-
},
66-
67-
isDropInStatements(data, logger, callback, app) {
68-
try {
69-
if (data.level === 'container') {
70-
const containsDropStatements = doesContainerLevelAlterScriptContainDropStatements(data, app);
71-
callback(null, containsDropStatements);
72-
} else {
73-
const containsDropStatements = doesEntityLevelAlterScriptContainDropStatements(data, app);
74-
callback(null, containsDropStatements);
75-
}
76-
} catch (e) {
77-
callback({ message: e.message, stack: e.stack });
78-
}
79-
},
10+
generateScript,
11+
generateViewScript,
12+
generateContainerScript,
13+
getDatabases,
14+
applyToInstance,
15+
testConnection,
16+
isDropInStatements,
8017
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const _ = require('lodash');
2+
const { createLogger } = require('../../reverse_engineering/helpers/loggerHelper');
3+
const applyToInstanceHelper = require('../applyToInstanceHelper');
4+
5+
function applyToInstance(connectionInfo, logger, callback, app) {
6+
logger.clear();
7+
logger.log('info', _.omit(connectionInfo, 'script', 'containerData'), 'connectionInfo', connectionInfo.hiddenKeys);
8+
9+
const postgresLogger = createLogger({
10+
title: 'Apply to instance',
11+
hiddenKeys: connectionInfo.hiddenKeys,
12+
logger,
13+
});
14+
15+
applyToInstanceHelper.applyToInstance(connectionInfo, postgresLogger, app).then(callback, callback);
16+
}
17+
18+
module.exports = {
19+
applyToInstance,
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { buildContainerLevelAlterScript } = require('../alterScript/alterScriptBuilder');
2+
3+
function generateContainerScript(data, logger, callback, app) {
4+
try {
5+
const script = buildContainerLevelAlterScript(data, app);
6+
callback(null, script);
7+
} catch (error) {
8+
logger.log('error', { message: error.message, stack: error.stack }, 'PostgreSQL Forward-Engineering Error');
9+
10+
callback({ message: error.message, stack: error.stack });
11+
}
12+
}
13+
14+
module.exports = {
15+
generateContainerScript,
16+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { buildEntityLevelAlterScript } = require('../alterScript/alterScriptBuilder');
2+
3+
function generateScript(data, logger, callback, app) {
4+
try {
5+
const script = buildEntityLevelAlterScript(data, app);
6+
callback(null, script);
7+
} catch (error) {
8+
logger.log('error', { message: error.message, stack: error.stack }, 'PostgreSQL Forward-Engineering Error');
9+
10+
callback({ message: error.message, stack: error.stack });
11+
}
12+
}
13+
14+
module.exports = {
15+
generateScript,
16+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function generateViewScript(data, logger, callback, app) {
2+
callback(new Error('Forward-Engineering of delta model on view level is not supported'));
3+
}
4+
5+
module.exports = {
6+
generateViewScript,
7+
};

0 commit comments

Comments
 (0)