Skip to content

Commit e9054d5

Browse files
Merge pull request #58 from ArzamastsevVladyslav/feature/HCK-3435-comment-updates
Feature/hck 3435 comment updates
2 parents 2fce279 + 16da9a5 commit e9054d5

File tree

17 files changed

+700
-226
lines changed

17 files changed

+700
-226
lines changed

forward_engineering/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
const internalDefinitions = JSON.parse(data.internalDefinitions);
2626
const externalDefinitions = JSON.parse(data.externalDefinitions);
2727
const dbVersion = data.modelData[0]?.dbVersion;
28-
const containersScripts = getAlterContainersScripts(collection);
28+
const containersScripts = getAlterContainersScripts({collection, app});
2929
const collectionsScripts = getAlterCollectionsScripts({
3030
collection,
3131
app,

forward_engineering/configs/templates.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ module.exports = {
3939
createForeignKey:
4040
'ALTER TABLE IF EXISTS ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable}(${primaryKey})${match}${onDelete}${onUpdate};',
4141

42+
updateCommentOnTable: 'COMMENT ON TABLE ${tableName} IS ${comment};',
43+
44+
updateCommentOnColumn: 'COMMENT ON COLUMN ${columnName} IS ${comment};',
45+
46+
updateCommentOnSchema: 'COMMENT ON SCHEMA ${schemaName} IS ${comment};',
47+
48+
updateCommentOnView: 'COMMENT ON VIEW ${viewName} IS ${comment};',
49+
50+
createSchemaOnly: 'CREATE SCHEMA IF NOT EXISTS ${schemaName};',
51+
52+
dropSchema: 'DROP SCHEMA IF EXISTS ${schemaName};',
53+
4254
index:
4355
'CREATE${unique} INDEX${concurrently}${ifNotExist} ${name}\n' +
4456
' ON${only} ${tableName}${using}${keys}${nullsDistinct}${options};\n',
@@ -48,6 +60,8 @@ module.exports = {
4860

4961
viewSelectStatement: 'SELECT ${keys}\n\tFROM ${tableName}',
5062

63+
dropView: 'DROP VIEW IF EXISTS ${viewName};',
64+
5165
createFunction:
5266
'CREATE${orReplace} FUNCTION ${name}\n' +
5367
'\t(${parameters})\n' +

forward_engineering/ddlProvider.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,17 @@ module.exports = (baseProvider, options, app) => {
491491
return [createViewScript, createTriggersStatements].map(_.trim).join('\n\n').trim() + '\n';
492492
},
493493

494+
/**
495+
* @param viewName {string}
496+
* @return string
497+
* */
498+
dropView(viewName) {
499+
const templatesConfig = {
500+
viewName,
501+
}
502+
return assignTemplates(templates.dropView, templatesConfig);
503+
},
504+
494505
createViewIndex() {
495506
return '';
496507
},
@@ -799,5 +810,127 @@ module.exports = (baseProvider, options, app) => {
799810
};
800811
return assignTemplates(templates.dropConstraint, templateConfig);
801812
},
813+
814+
/**
815+
* @param tableName {string}
816+
* @param comment {string}
817+
* @return string
818+
* */
819+
updateTableComment(tableName, comment) {
820+
const templateConfig = {
821+
tableName,
822+
comment
823+
}
824+
return assignTemplates(templates.updateCommentOnTable, templateConfig);
825+
},
826+
827+
/**
828+
* @param tableName {string}
829+
* @return string
830+
* */
831+
dropTableComment(tableName) {
832+
const templateConfig = {
833+
tableName,
834+
comment: 'NULL'
835+
}
836+
return assignTemplates(templates.updateCommentOnTable, templateConfig);
837+
},
838+
839+
/**
840+
* @param columnName {string}
841+
* @param comment {string}
842+
* @return string
843+
* */
844+
updateColumnComment(columnName, comment) {
845+
const templateConfig = {
846+
columnName,
847+
comment
848+
}
849+
return assignTemplates(templates.updateCommentOnColumn, templateConfig);
850+
},
851+
852+
/**
853+
* @param columnName {string}
854+
* @return string
855+
* */
856+
dropColumnComment(columnName) {
857+
const templateConfig = {
858+
columnName,
859+
comment: 'NULL'
860+
}
861+
return assignTemplates(templates.updateCommentOnColumn, templateConfig);
862+
},
863+
864+
/**
865+
* @param schemaName {string}
866+
* @param comment {string}
867+
* @return string
868+
* */
869+
updateSchemaComment(schemaName, comment) {
870+
const templateConfig = {
871+
schemaName,
872+
comment
873+
}
874+
return assignTemplates(templates.updateCommentOnSchema, templateConfig);
875+
},
876+
877+
/**
878+
* @param schemaName {string}
879+
* @return string
880+
* */
881+
dropSchemaComment(schemaName) {
882+
const templateConfig = {
883+
schemaName,
884+
comment: 'NULL'
885+
}
886+
return assignTemplates(templates.updateCommentOnSchema, templateConfig);
887+
},
888+
889+
/**
890+
* @param viewName {string}
891+
* @param comment {string}
892+
* @return string
893+
* */
894+
updateViewComment(viewName, comment) {
895+
const templateConfig = {
896+
viewName,
897+
comment
898+
}
899+
return assignTemplates(templates.updateCommentOnView, templateConfig);
900+
},
901+
902+
/**
903+
* @param viewName {string}
904+
* @return string
905+
* */
906+
dropViewComment(viewName) {
907+
const templateConfig = {
908+
viewName,
909+
comment: 'NULL'
910+
}
911+
return assignTemplates(templates.updateCommentOnView, templateConfig);
912+
},
913+
914+
/**
915+
* @param schemaName {string}
916+
* @return string
917+
* */
918+
createSchemaOnly(schemaName) {
919+
const templateConfig = {
920+
schemaName,
921+
}
922+
return assignTemplates(templates.createSchemaOnly, templateConfig);
923+
},
924+
925+
/**
926+
* @param schemaName {string}
927+
* @return string
928+
* */
929+
dropSchema(schemaName) {
930+
const templateConfig = {
931+
schemaName,
932+
}
933+
return assignTemplates(templates.dropSchema, templateConfig);
934+
},
802935
};
803936
};

forward_engineering/helpers/alterScriptFromDeltaHelper.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getAddContainerScript, getDeleteContainerScript } = require('./alterScriptHelpers/alterContainerHelper');
1+
const { getAddContainerScript, getDeleteContainerScript, getModifyContainerScript} = require('./alterScriptHelpers/alterContainerHelper');
22
const {
33
getAddCollectionScript,
44
getDeleteCollectionScript,
@@ -13,28 +13,38 @@ const {
1313
getDeleteColumnFromTypeScript,
1414
getModifyColumnOfTypeScript,
1515
} = require('./alterScriptHelpers/alterUdtHelper');
16-
const { getAddViewScript, getDeleteViewScript } = require('./alterScriptHelpers/alterViewHelper');
16+
const { getAddViewScript, getDeleteViewScript, getModifyViewScript} = require('./alterScriptHelpers/alterViewHelper');
1717

1818
const getComparisonModelCollection = collections => {
1919
return collections
2020
.map(collection => JSON.parse(collection))
2121
.find(collection => collection.collectionName === 'comparisonModelCollection');
2222
};
2323

24-
const getAlterContainersScripts = collection => {
24+
const getAlterContainersScripts = ({ collection, app}) => {
2525
const addedContainers = collection.properties?.containers?.properties?.added?.items;
2626
const deletedContainers = collection.properties?.containers?.properties?.deleted?.items;
27+
const modifiedContainers = collection.properties?.containers?.properties?.modified?.items;
2728

2829
const addContainersScripts = []
2930
.concat(addedContainers)
3031
.filter(Boolean)
31-
.map(container => getAddContainerScript(Object.keys(container.properties)[0]));
32+
.map(container => getAddContainerScript(app)(Object.keys(container.properties)[0]));
3233
const deleteContainersScripts = []
3334
.concat(deletedContainers)
3435
.filter(Boolean)
35-
.map(container => getDeleteContainerScript(Object.keys(container.properties)[0]));
36+
.map(container => getDeleteContainerScript(app)(Object.keys(container.properties)[0]));
37+
const modifyContainersScripts = []
38+
.concat(modifiedContainers)
39+
.filter(Boolean)
40+
.map(containerWrapper => Object.values(containerWrapper.properties)[0])
41+
.map(container => getModifyContainerScript(app)(container))
3642

37-
return [].concat(addContainersScripts).concat(deleteContainersScripts);
43+
return [
44+
...addContainersScripts,
45+
...deleteContainersScripts,
46+
...modifyContainersScripts,
47+
];
3848
};
3949

4050
const getAlterCollectionsScripts = ({
@@ -108,7 +118,18 @@ const getAlterViewScripts = (collection, app) => {
108118
.map(view => ({ ...view, ...(view.role || {}) }))
109119
.map(getDeleteViewScript(app));
110120

111-
return [...deleteViewsScripts, ...createViewsScripts].map(script => script.trim());
121+
const modifyViewsScripts = []
122+
.concat(collection.properties?.views?.properties?.modified?.items)
123+
.filter(Boolean)
124+
.map(viewWrapper => Object.values(viewWrapper.properties)[0])
125+
.map(view => ({ ...view, ...(view.role || {}) }))
126+
.flatMap(view => getModifyViewScript(app)(view));
127+
128+
return [
129+
...deleteViewsScripts,
130+
...createViewsScripts,
131+
...modifyViewsScripts,
132+
].map(script => script.trim());
112133
};
113134

114135
const getAlterModelDefinitionsScripts = ({
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
const getAddContainerScript = containerName => {
2-
return `CREATE SCHEMA IF NOT EXISTS "${containerName}";`;
1+
const {getModifySchemaCommentsScripts} = require("./containerHelpers/commentsHelper");
2+
const getAddContainerScript = (app) => (containerName) => {
3+
const _ = app.require('lodash');
4+
const ddlProvider = require('../../ddlProvider')(null, null, app);
5+
const {wrapInQuotes} = require('../general')({_});
6+
return ddlProvider.createSchemaOnly(wrapInQuotes(containerName));
37
};
48

5-
const getDeleteContainerScript = containerName => {
6-
return `DROP SCHEMA IF EXISTS "${containerName}";`;
9+
const getDeleteContainerScript = (app) => (containerName) => {
10+
const _ = app.require('lodash');
11+
const ddlProvider = require('../../ddlProvider')(null, null, app);
12+
const {wrapInQuotes} = require('../general')({_});
13+
14+
return ddlProvider.dropSchema(wrapInQuotes(containerName));
715
};
816

17+
/**
18+
* @return (collection: Object) => Array<string>
19+
* */
20+
const getModifyContainerScript = (app) => (container) => {
21+
const _ = app.require('lodash');
22+
const ddlProvider = require('../../ddlProvider')(null, null, app);
23+
24+
const modifyCommentScripts = getModifySchemaCommentsScripts(_, ddlProvider)(container);
25+
26+
return [
27+
...modifyCommentScripts
28+
];
29+
}
30+
931
module.exports = {
1032
getAddContainerScript,
1133
getDeleteContainerScript,
34+
getModifyContainerScript
1235
};

0 commit comments

Comments
 (0)