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
2 changes: 1 addition & 1 deletion forward_engineering/api/generateScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function generateScript(data, logger, callback, app) {
}

const scriptFormat = _.get(data, 'options.targetScriptOptions.keyword');
const script = getAlterScript({ scriptFormat, collection, ddlProvider, app });
const script = getAlterScript({ scriptFormat, collection, ddlProvider, app, options: data.options });

const applyDropStatements = data.options?.additionalOptions?.some(
option => option.id === 'applyDropStatements' && option.value,
Expand Down
16 changes: 12 additions & 4 deletions forward_engineering/helpers/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const getAlterContainersScripts = (collection, ddlProvider, app) => {
return { addedContainerScripts, deletedContainerScripts, modifiedContainerScripts };
};

const getAlterCollectionsScripts = ({ collection, ddlProvider, app, scriptFormat }) => {
const getAlterCollectionsScripts = ({ collection, ddlProvider, app, scriptFormat, shouldIgnoreColumnComments = false }) => {
const getCollectionScripts = (items, compMode, getScript) =>
items.filter(item => item.compMod?.[compMode]).map(getScript);

Expand All @@ -67,7 +67,7 @@ const getAlterCollectionsScripts = ({ collection, ddlProvider, app, scriptFormat

const addedColumnScripts = getColumnScripts(
getItems(collection, 'entities', 'added', 'values'),
getAddColumnScript({ ddlProvider, scriptFormat }),
getAddColumnScript({ ddlProvider, scriptFormat, shouldIgnoreColumnComments }),
);
const deletedColumnScripts = getColumnScripts(
getItems(collection, 'entities', 'deleted', 'values'),
Expand Down Expand Up @@ -183,9 +183,17 @@ const getAlterForeignKeysScripts = ({ collection, ddlProvider }) => {
return { addedFkScripts, deletedFkScripts, modifiedFkScripts };
};

const getAlterScript = ({ scriptFormat, collection, ddlProvider, app }) => {
const getAlterScript = ({ scriptFormat, collection, ddlProvider, app, options }) => {
const shouldIgnoreColumnComments =
options?.scriptGenerationOptions?.feActiveOptions?.columnComments === 'ignore';
const script = {
...getAlterCollectionsScripts({ collection, ddlProvider, app, scriptFormat }),
...getAlterCollectionsScripts({
collection,
ddlProvider,
app,
scriptFormat,
shouldIgnoreColumnComments,
}),
...getAlterContainersScripts(collection, ddlProvider, app),
...getAlterViewsScripts({ schema: collection, ddlProvider, app }),
...getAlterTagsScripts({ collection, ddlProvider, app }),
Expand Down
33 changes: 23 additions & 10 deletions forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const { escapeString } = require('../../utils/escapeString');
const { getModifyPkScripts } = require('./entityHelper/primaryKeyHelper');
const { getModifyUkScripts } = require('./entityHelper/uniqueKeyHelper');
const { getModifyNotNullColumnsScriptDtos } = require('./columnHelpers/notNullConstraintHelper');
const { getAddedCommentOnColumnScript } = require('./columnHelpers/commentsHelper');

const getAddCollectionScript =
({ ddlProvider, scriptFormat }) =>
Expand Down Expand Up @@ -70,31 +71,43 @@ const getModifyCollectionScript = ddlProvider => collection => {
};

const getAddColumnScript =
({ ddlProvider, scriptFormat }) =>
({ ddlProvider, scriptFormat, shouldIgnoreColumnComments = false }) =>
collection => {
const collectionSchema = {
...collection,
..._.omit(collection?.role, 'properties'),
};
const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName);
const fullName = getFullName(databaseName, getFullName(schemaName, tableName));
const isContainerActivated = isParentContainerActivated(collection) !== false;
const isCollectionActivated = isObjectInDeltaModelActivated(collection) !== false;

return _.toPairs(collection.properties)
.filter(([_, jsonSchema]) => !jsonSchema.compMod)
.map(([name, jsonSchema]) =>
createColumnDefinitionBySchema({
.flatMap(([name, jsonSchema]) => {
const columnDefinition = createColumnDefinitionBySchema({
name,
jsonSchema,
parentJsonSchema: collectionSchema,
ddlProvider,
scriptFormat,
}),
)
.map(ddlProvider.convertColumnDefinition)
.map(
column =>
`ALTER TABLE IF EXISTS ${fullName} ADD COLUMN ${commentIfDeactivated(column.statement, column)};`,
);
});
const column = ddlProvider.convertColumnDefinition(columnDefinition);
const isActivated =
isContainerActivated && isCollectionActivated && jsonSchema.isActivated !== false;
const addColumnScript = `ALTER TABLE IF EXISTS ${fullName} ADD COLUMN ${commentIfDeactivated(column.statement, { isActivated })};`;
const commentScript = getAddedCommentOnColumnScript({
collection,
name,
jsonSchema,
fullName,
scriptFormat,
isCaseSensitive: collectionSchema.isCaseSensitive,
shouldIgnoreColumnComments,
});

return [addColumnScript, commentScript].filter(Boolean);
});
};

const getDeleteColumnScript = collection => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const { commentIfDeactivated } = require('../../commentHelpers/commentDeactivatedHelper');
const {
getName,
isParentContainerActivated,
isObjectInDeltaModelActivated,
} = require('../../general');
const assignTemplates = require('../../../utils/assignTemplates');
const templates = require('../../../configs/templates');
const { escapeString } = require('../../../utils/escapeString');

/**
* Studio clears jsonSchema.description on newly added columns so comments cannot be inlined.
* The original text is kept on collection.role.properties[name].
*
* @param {{ jsonSchema: Object, roleProperty: Object }} dto
* @return {string | undefined}
*/
const getAddedColumnComment = ({ jsonSchema, roleProperty } = {}) => {
return (
jsonSchema?.description ||
jsonSchema?.refDescription ||
roleProperty?.description ||
roleProperty?.refDescription
);
};

/**
* @param {{
* collection: Object,
* name: string,
* jsonSchema: Object,
* fullName: string,
* scriptFormat: string,
* isCaseSensitive?: boolean,
* shouldIgnoreColumnComments?: boolean,
* }} dto
* @return {string | undefined}
*/
const getAddedCommentOnColumnScript = ({
collection,
name,
jsonSchema,
fullName,
scriptFormat,
isCaseSensitive,
shouldIgnoreColumnComments = false,
} = {}) => {
if (shouldIgnoreColumnComments) {
return undefined;
}

if (jsonSchema?.description || jsonSchema?.refDescription) {
return undefined;
}

const roleProperty = collection?.role?.properties?.[name];
const comment = getAddedColumnComment({ jsonSchema, roleProperty });
if (!comment) {
return undefined;
}

const isContainerActivated = isParentContainerActivated(collection) !== false;
const isCollectionActivated = isObjectInDeltaModelActivated(collection) !== false;
const isColumnActivated = jsonSchema.isActivated !== false;
const isActivated = isContainerActivated && isCollectionActivated && isColumnActivated;
const columnName = getName(isCaseSensitive, name);
const statement = assignTemplates(templates.columnComment, {
fullName: `${fullName}.${columnName}`,
comment: escapeString(scriptFormat, comment),
});

return commentIfDeactivated(statement, { isActivated });
};

module.exports = {
getAddedCommentOnColumnScript,
};
Loading