Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions forward_engineering/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,23 @@
"exist": false
},
{
"key": "description",
"exist": true
"type": "or",
"values": [
{
"key": "description",
"exist": true
},
{
"key": "refDescription",
"exist": true
}
]
}
]
},
"defaultValue": {
"description": ""
"description": "",
"refDescription": ""
}
}
]
Expand Down
9 changes: 9 additions & 0 deletions forward_engineering/configs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ module.exports = {
createViewComment:
"EXEC sp_addextendedproperty 'MS_Description', N'${value}', 'schema', ${schemaName}, 'view', ${viewName}${terminator}",

createViewColumnComment:
"EXEC sp_addextendedproperty 'MS_Description', N'${value}', 'schema', ${schemaName}, 'view', ${viewName}, 'column', ${columnName}${terminator}",

dropSchemaComment: "EXEC sp_dropextendedproperty 'MS_Description', 'schema', ${schemaName}${terminator}",

dropTableComment:
Expand All @@ -122,6 +125,9 @@ module.exports = {
dropViewComment:
"EXEC sp_dropextendedproperty 'MS_Description', 'schema', ${schemaName}, 'view', ${viewName}${terminator}",

dropViewColumnComment:
"EXEC sp_dropextendedproperty 'MS_Description', 'schema', ${schemaName}, 'view', ${viewName}, 'column', ${columnName}${terminator}",

updateSchemaComment:
"EXEC sp_updateextendedproperty 'MS_Description', N'${value}', 'schema', ${schemaName}${terminator}",

Expand All @@ -133,4 +139,7 @@ module.exports = {

updateViewComment:
"EXEC sp_updateextendedproperty 'MS_Description', N'${value}', 'schema', ${schemaName}, 'view', ${viewName}${terminator}",

updateViewColumnComment:
"EXEC sp_updateextendedproperty 'MS_Description', N'${value}', 'schema', ${schemaName}, 'view', ${viewName}, 'column', ${columnName}${terminator}",
};
41 changes: 41 additions & 0 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,20 @@ const ddlProvider = (baseProvider, options, app) => {
});
},

createViewColumnComment({ schemaName, viewName, columnName, comment, customTerminator }) {
if (!viewName || !columnName) {
return '';
}

return assignTemplates(templates.createViewColumnComment, {
value: escapeSpecialCharacters(comment),
schemaName: wrapInBrackets(schemaName),
viewName: wrapInBrackets(viewName),
columnName: wrapInBrackets(columnName),
terminator: customTerminator ?? terminator,
});
},

dropSchemaComment({ schemaName, customTerminator }) {
return assignTemplates(templates.dropSchemaComment, {
schemaName: wrapInBrackets(schemaName),
Expand Down Expand Up @@ -948,6 +962,19 @@ const ddlProvider = (baseProvider, options, app) => {
});
},

dropViewColumnComment({ schemaName, viewName, columnName, customTerminator }) {
if (!schemaName || !viewName) {
return '';
}

return assignTemplates(templates.dropViewColumnComment, {
schemaName: wrapInBrackets(schemaName),
viewName: wrapInBrackets(viewName),
columnName: wrapInBrackets(columnName),
terminator: customTerminator ?? terminator,
});
},

updateSchemaComment({ schemaName, comment, customTerminator }) {
return assignTemplates(templates.updateSchemaComment, {
value: escapeSpecialCharacters(comment),
Expand Down Expand Up @@ -996,6 +1023,20 @@ const ddlProvider = (baseProvider, options, app) => {
});
},

updateViewColumnComment({ schemaName, viewName, columnName, comment, customTerminator }) {
if (!schemaName || !viewName) {
return '';
}

return assignTemplates(templates.updateViewColumnComment, {
value: escapeSpecialCharacters(comment),
schemaName: wrapInBrackets(schemaName),
viewName: wrapInBrackets(viewName),
columnName: wrapInBrackets(columnName),
terminator: customTerminator ?? terminator,
});
},

addCheckConstraint(tableName, constraintName, expression, check) {
const templateConfig = {
tableName,
Expand Down
27 changes: 25 additions & 2 deletions forward_engineering/helpers/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,31 +348,51 @@ const getCollectionsCommentsAlterScriptsDtos = (collection, app, options) => {
};

const getViewsCommentsAlterScriptsDtos = (collection, app, options) => {
const { getViewsDropCommentAlterScriptsDto, getViewsModifyCommentsAlterScriptsDto } =
require('./alterScriptHelpers/alterViewHelper')(app, options);
const {
getViewsDropCommentAlterScriptsDto,
getViewsModifyCommentsAlterScriptsDto,
getViewColumnsCreateCommentAlterScriptsDto,
getViewColumnsDropCommentAlterScriptsDto,
getViewColumnsModifyCommentAlterScriptsDto,
} = require('./alterScriptHelpers/alterViewHelper')(app, options);

//Added views comments creation is already done in generation of ddl
const modifiedViews = collection.properties?.views?.properties?.modified?.items;
const deletedViews = collection.properties?.views?.properties?.deleted?.items;

let addViewsModifyCommentsScriptsDtos = [];
let addViewsDropCommentsScriptsDtos = [];
let addViewColumnCreateCommentsScriptsDtos = [];
let addViewColumnModifyCommentsScriptsDtos = [];
let addViewColumnDropCommentsScriptsDtos = [];

if (modifiedViews) {
addViewsModifyCommentsScriptsDtos = Array.isArray(modifiedViews)
? modifiedViews.flatMap(schema => getViewsModifyCommentsAlterScriptsDto(schema?.properties))
: getViewsModifyCommentsAlterScriptsDto(modifiedViews?.properties);
addViewColumnCreateCommentsScriptsDtos = Array.isArray(modifiedViews)
? modifiedViews.flatMap(schema => getViewColumnsCreateCommentAlterScriptsDto(schema?.properties))
: getViewColumnsCreateCommentAlterScriptsDto(modifiedViews?.properties);
addViewColumnModifyCommentsScriptsDtos = Array.isArray(modifiedViews)
? modifiedViews.flatMap(schema => getViewColumnsModifyCommentAlterScriptsDto(schema?.properties))
: getViewColumnsModifyCommentAlterScriptsDto(modifiedViews?.properties);
}

if (deletedViews) {
addViewsDropCommentsScriptsDtos = Array.isArray(deletedViews)
? deletedViews.flatMap(schema => getViewsDropCommentAlterScriptsDto(schema?.properties))
: getViewsDropCommentAlterScriptsDto(deletedViews?.properties);
addViewColumnDropCommentsScriptsDtos = Array.isArray(deletedViews)
? deletedViews.flatMap(schema => getViewColumnsDropCommentAlterScriptsDto(schema?.properties))
: getViewColumnsDropCommentAlterScriptsDto(deletedViews?.properties);
}

return {
addViewsModifyCommentsScriptsDtos,
addViewsDropCommentsScriptsDtos,
addViewColumnCreateCommentsScriptsDtos,
addViewColumnModifyCommentsScriptsDtos,
addViewColumnDropCommentsScriptsDtos,
};
};

Expand Down Expand Up @@ -445,6 +465,7 @@ const getAlterScriptDtos = (collection, app, options) => {
return [
'addContainersScriptsDtos',
'addViewsDropCommentsScriptsDtos',
'addViewColumnDropCommentsScriptsDtos',
'deleteViewsScriptsDtos',
'addColumnDropCommentsScriptsDtos',
'addTablesDropCommentsScriptsDtos',
Expand All @@ -465,6 +486,8 @@ const getAlterScriptDtos = (collection, app, options) => {
'addSchemasModifyCommentsScriptsDtos',
'addTablesModifyCommentsScriptsDtos',
'addViewsModifyCommentsScriptsDtos',
'addViewColumnCreateCommentsScriptsDtos',
'addViewColumnModifyCommentsScriptsDtos',
'deleteFkScriptDtos',
'addFkScriptDtos',
'modifiedFkScriptDtos',
Expand Down
102 changes: 102 additions & 0 deletions forward_engineering/helpers/alterScriptHelpers/alterViewHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,105 @@ const alterViewHelper = (app, options) => {
.filter(Boolean);
};

const getViewColumnCreateCommentScript = ({ schemaName, viewName, columnName, comment }) =>
ddlProvider.createViewColumnComment({ schemaName, viewName, columnName, comment });
const getViewColumnUpdateCommentScript = ({ schemaName, viewName, columnName, comment }) =>
ddlProvider.updateViewColumnComment({ schemaName, viewName, columnName, comment });
const getViewColumnDropCommentScript = ({ schemaName, viewName, columnName }) =>
ddlProvider.dropViewColumnComment({ schemaName, viewName, columnName });

const getViewColumnsCreateCommentAlterScriptsDto = views => {
return Object.keys(views)
.flatMap(viewName => {
const columns = views[viewName].properties;
if (!columns) {
return [];
}
const schemaName = views[viewName].role?.compMod.keyspaceName;
return Object.keys(columns).map(columnName => {
const column = columns[columnName];
const isColumnRenamed = column?.compMod?.oldField?.name !== column?.compMod?.newField?.name;
const columnNameToSearchComment = isColumnRenamed ? column?.compMod?.oldField?.name : columnName;
const comment = column.refDescription;
const oldComment = views[viewName].role?.properties[columnNameToSearchComment]?.refDescription;

if (!comment || oldComment) {
return undefined;
}

const script = getViewColumnCreateCommentScript({ schemaName, viewName, columnName, comment });

return AlterScriptDto.getInstance([script], true, false);
});
})
.filter(Boolean);
};

const getViewColumnsDropCommentAlterScriptsDto = views => {
return Object.keys(views)
.flatMap(viewName => {
const columns = views[viewName].properties;
if (!columns) {
return [];
}
const schemaName = views[viewName].role?.compMod.keyspaceName;
return Object.keys(columns)
.filter(columnName => Boolean(columns[columnName].refDescription))
.map(columnName => {
const script = getViewColumnDropCommentScript({ schemaName, viewName, columnName });

return AlterScriptDto.getInstance([script], true, true);
});
})
.filter(Boolean);
};

const getViewColumnsModifyCommentAlterScriptsDto = views => {
return Object.keys(views)
.flatMap(viewName => {
const columns = views[viewName].properties;
if (!columns) {
return undefined;
}
const schemaName = views[viewName].role?.compMod.keyspaceName;
return Object.keys(columns).map(columnName => {
let script = '';
const newComment = columns[columnName]?.refDescription;
const oldComment = views[viewName].role?.properties[columnName]?.refDescription;
const isCommentRemoved = oldComment && !newComment;

if (isCommentRemoved) {
script = getViewColumnDropCommentScript({ schemaName, viewName, columnName });

return AlterScriptDto.getInstance([script], true, true);
}

if (!newComment || !oldComment || newComment === oldComment) {
return undefined;
}

if (oldComment) {
script = getViewColumnUpdateCommentScript({
schemaName,
viewName,
columnName,
comment: newComment,
});
} else {
script = getViewColumnCreateCommentScript({
schemaName,
viewName,
columnName,
comment: newComment,
});
}

return AlterScriptDto.getInstance([script], true, false);
});
})
.filter(Boolean);
};

const generateRefToNameHashTable = view => {
const refToNameHashTable = {};

Expand All @@ -228,6 +327,9 @@ const alterViewHelper = (app, options) => {
getModifiedViewScriptDto,
getViewsDropCommentAlterScriptsDto,
getViewsModifyCommentsAlterScriptsDto,
getViewColumnsCreateCommentAlterScriptsDto,
getViewColumnsDropCommentAlterScriptsDto,
getViewColumnsModifyCommentAlterScriptsDto,
};
};

Expand Down
Loading