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
36 changes: 35 additions & 1 deletion forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { getContainersScripts } = require('./alterScriptHelpers/alterContainerHelper');
const {
getModifyCollectionScriptDtos,
getModifyCollectionKeysScriptDtos,
Expand All @@ -12,6 +13,37 @@ const { getModifyViewScriptDtos } = require('./alterScriptHelpers/alterViewHelpe

const getItems = data => [data?.items].flat().filter(Boolean);

/**
* @param dto {{
* collection: Object,
* app: App
* }}
* @return {AlterScriptDto[]}
* */
const getAlterContainersScriptDtos = ({ collection, app }) => {
const { added, deleted, modified } = collection.properties?.containers?.properties || {};
const addedContainers = getItems(added);
const deletedContainers = getItems(deleted);
const modifiedContainers = getItems(modified);

const { getAddContainerScriptDto, getDeleteContainerScriptDto, getModifyContainerScriptDto } =
getContainersScripts(app);

const addContainersScriptDtos = addedContainers
.map(container => Object.values(container.properties)[0])
.flatMap(getAddContainerScriptDto);

const deleteContainersScriptDtos = deletedContainers
.map(container => Object.values(container.properties)[0])
.flatMap(getDeleteContainerScriptDto);

const modifyContainersScriptDtos = modifiedContainers
.map(containerWrapper => Object.values(containerWrapper.properties)[0])
.flatMap(getModifyContainerScriptDto);

return [...addContainersScriptDtos, ...deleteContainersScriptDtos, ...modifyContainersScriptDtos].filter(Boolean);
};

const getAlterCollectionScriptDtos = ({
collection,
app,
Expand Down Expand Up @@ -127,6 +159,8 @@ const getAlterScriptDtos = (data, app) => {
const inlineDeltaRelationships = getInlineRelationships({ collection, options: data.options });
const ignoreRelationshipIDs = inlineDeltaRelationships.map(relationship => relationship.role.id);

const containersScriptDtos = getAlterContainersScriptDtos({ collection, app });

const collectionsScriptDtos = getAlterCollectionScriptDtos({
collection,
app,
Expand All @@ -143,7 +177,7 @@ const getAlterScriptDtos = (data, app) => {
ignoreRelationshipIDs,
});

return [...collectionsScriptDtos, ...viewScriptDtos, ...relationshipScriptDtos]
return [...containersScriptDtos, ...collectionsScriptDtos, ...viewScriptDtos, ...relationshipScriptDtos]
.filter(Boolean)
.map(dto => dto && prettifyAlterScriptDto(dto))
.filter(Boolean);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { isEmpty } = require('lodash');
const ddlProvider = require('../../ddlProvider');
const { AlterScriptDto } = require('../types/AlterScriptDto');
const {
getSchemaCommentStatement,
getDeleteCommentStatement,
} = require('../../ddlProvider/ddlHelpers/comment/commentHelper');
const { wrapInQuotes, getIsChangeProperties, getUpdatedProperties } = require('../../utils/general');
const { getModifiedCommentOnSchemaScriptDtos } = require('./containerHelpers/commentsHelper');

const extractSchemaName = containerData => containerData.role.name;

const getAddContainerScriptDto = ddlProvider => containerData => {
const schemaData = {
...containerData.role,
schemaName: extractSchemaName(containerData),
};
const script = ddlProvider.createSchema(schemaData);

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

const getDeleteContainerScriptDto = ddlProvider => containerData => {
const script = ddlProvider.dropSchema({ name: extractSchemaName(containerData) });

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

const getModifyContainerScriptDto = ddlProvider => containerData => {
const scripts = [];
const compMod = containerData.role?.compMod || {};
const schemaName = extractSchemaName(containerData);
const wrappedSchemaName = wrapInQuotes(schemaName);
const isActivated = containerData.isActivated !== false;
const updatedProperties = getUpdatedProperties(compMod, ['dataCapture']);

if (!isEmpty(updatedProperties)) {
const alterDataCaptureScript = ddlProvider.alterSchema(schemaName, updatedProperties);
scripts.push(AlterScriptDto.getInstance([alterDataCaptureScript], isActivated, false));
}

const commentScript = getModifiedCommentOnSchemaScriptDtos({
schemaName: wrappedSchemaName,
compMod,
isActivated,
});

if (commentScript) {
scripts.push(commentScript);
}

return scripts;
};

const getContainersScripts = app => {
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);

return {
getAddContainerScriptDto: getAddContainerScriptDto(ddlProvider),
getDeleteContainerScriptDto: getDeleteContainerScriptDto(ddlProvider),
getModifyContainerScriptDto: getModifyContainerScriptDto(ddlProvider),
};
};

module.exports = {
getContainersScripts,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const {
getSchemaCommentStatement,
dropSchemaCommentStatement,
} = require('../../../ddlProvider/ddlHelpers/comment/commentHelper');
const { wrapInQuotes } = require('../../../utils/general');

const getModifiedCommentOnSchemaScriptDtos = ({ schemaName, compMod, isActivated }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Db2 doesn't support comments on schemas, or does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supports.
Screenshot 2026-01-13 at 12 41 51

const description = compMod.description || {};

if (description.new && description.new !== description.old) {
const script = getSchemaCommentStatement({ schemaName, description: description.new });
return AlterScriptDto.getInstance([script], isActivated, false);
}

if (description.old && !description.new) {
const script = dropSchemaCommentStatement({ schemaName });
return AlterScriptDto.getInstance([script], isActivated, true);
}

return undefined;
};

module.exports = {
getModifiedCommentOnSchemaScriptDtos,
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@ const { wrapInQuotes, commentIfDeactivated, wrapInSingleQuotes } = require('../.
* @enum {string}
*/
const OBJECT_TYPE = {
schema: 'SCHEMA',
column: 'COLUMN',
table: 'TABLE',
index: 'INDEX',
};

/**
* @enum {string}
*/
const COMMENT_MODE = {
set: 'set',
remove: 'remove',
};

/**
* @param {string} description
* @returns {string}
*/
const escapeSpecialCharacters = description => description.replace(/'/g, "''");

/**
* @param {{ objectName: string, objectType: OBJECT_TYPE, description?: string }}
* @param {{ objectName: string, objectType: OBJECT_TYPE, description?: string, mode?: COMMENT_MODE }}
* @returns {string}
*/
const getCommentStatement = ({ objectName, objectType, description }) => {
if (!description) {
const getCommentStatement = ({ objectName, objectType, description, mode = COMMENT_MODE.set }) => {
if (mode === COMMENT_MODE.set && !description) {
return '';
}

Expand All @@ -32,34 +41,62 @@ const getCommentStatement = ({ objectName, objectType, description }) => {
templateData: {
objectType,
objectName: trim(objectName),
comment: wrapInSingleQuotes({ name: escapeSpecialCharacters(description) }),
comment: wrapInSingleQuotes({ name: escapeSpecialCharacters(description || '') }),
},
});
};

/**
* @param {{ tableName, string, columnName: string, description?: string }}
* @param {{ tableName, columnName: string, description?: string }}
* @returns {string}
*/
const getColumnCommentStatement = ({ tableName, columnName, description }) => {
const objectName = tableName + '.' + wrapInQuotes(columnName);
return getCommentStatement({ objectName, objectType: OBJECT_TYPE.column, description });
return getCommentStatement({
objectName,
objectType: OBJECT_TYPE.column,
description,
mode: COMMENT_MODE.set,
});
};

/**
* @param {{ tableName: string, description?: string }}
* @returns {string}
*/
const getTableCommentStatement = ({ tableName, description }) => {
return getCommentStatement({ objectName: tableName, objectType: OBJECT_TYPE.table, description });
return getCommentStatement({
objectName: tableName,
objectType: OBJECT_TYPE.table,
description,
mode: COMMENT_MODE.set,
});
};

/**
* @param {{ indexName: string, description?: string }}
* @returns {string}
*/
const getIndexCommentStatement = ({ indexName, description }) => {
return getCommentStatement({ objectName: indexName, objectType: OBJECT_TYPE.index, description });
return getCommentStatement({
objectName: indexName,
objectType: OBJECT_TYPE.index,
description,
mode: COMMENT_MODE.set,
});
};

/**
* @param {{ schemaName: string, description?: string }}
* @returns {string}
*/
const getSchemaCommentStatement = ({ schemaName, description }) => {
return getCommentStatement({
objectName: schemaName,
objectType: OBJECT_TYPE.schema,
description,
mode: COMMENT_MODE.set,
});
};

/**
Expand All @@ -81,9 +118,24 @@ const getColumnComments = ({ tableName, columnDefinitions = [] }) => {
.join('\n');
};

/**
* @param {{ schemaName: string }}
* @returns {string}
*/
const dropSchemaCommentStatement = ({ schemaName }) => {
return getCommentStatement({
objectName: schemaName,
objectType: OBJECT_TYPE.schema,
mode: COMMENT_MODE.remove,
});
};

module.exports = {
getColumnCommentStatement,
getSchemaCommentStatement,
getTableCommentStatement,
getColumnComments,
getIndexCommentStatement,

dropSchemaCommentStatement,
};
33 changes: 30 additions & 3 deletions forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
getTableCommentStatement,
getColumnComments,
getIndexCommentStatement,
getSchemaCommentStatement,
} = require('./ddlHelpers/comment/commentHelper.js');
const { getTableProps } = require('./ddlHelpers/table/getTableProps.js');
const { getTableOptions } = require('./ddlHelpers/table/getTableOptions.js');
Expand Down Expand Up @@ -65,20 +66,46 @@ module.exports = (baseProvider, options, app) => {
authorizationName: containerData.authorizationName,
dataCapture: containerData.dataCapture,
isActivated: containerData.isActivated,
description: containerData.description,
};
},

createSchema({ schemaName, ifNotExist, authorizationName, dataCapture, isActivated = true }) {
createSchema({ schemaName, ifNotExist, authorizationName, dataCapture, description, isActivated = true }) {
const wrappedSchemaName = wrapInQuotes(schemaName);
const schemaStatement = assignTemplates({
template: templates.createSchema,
templateData: {
schemaName: wrapInQuotes(schemaName),
schemaName: wrappedSchemaName,
authorization: authorizationName ? ' AUTHORIZATION ' + authorizationName : '',
dataCapture: dataCapture ? ' DATA CAPTURE ' + dataCapture : '',
},
});

return commentIfDeactivated(schemaStatement, { isActivated });
const comment = getSchemaCommentStatement({ schemaName: wrappedSchemaName, description });
const commentStatement = comment ? '\n' + comment + '\n' : '\n';

return commentIfDeactivated(schemaStatement + commentStatement, { isActivated });
},

dropSchema({ name, isActivated = true }) {
const dropSchemaStatement = assignTemplates({
template: templates.dropSchema,
templateData: {
schemaName: wrapInQuotes(name),
},
});

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

alterSchema(schemaName, { dataCapture }) {
return assignTemplates({
template: templates.alterSchema,
templateData: {
schemaName: wrapInQuotes(schemaName),
dataCapture: dataCapture ? ' DATA CAPTURE ' + dataCapture : '',
},
});
},

hydrateColumn({ columnDefinition, jsonSchema, schemaData, definitionJsonSchema = {} }) {
Expand Down
4 changes: 4 additions & 0 deletions forward_engineering/ddlProvider/templates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module.exports = {
createSchema: 'CREATE SCHEMA ${schemaName}${authorization}${dataCapture};',

dropSchema: 'DROP SCHEMA ${schemaName} RESTRICT',

alterSchema: 'ALTER SCHEMA ${schemaName}${dataCapture};',

createTable: 'CREATE${tableType} TABLE${ifNotExists} ${name}${tableProps}${tableOptions};',

createAuxiliaryTable: 'CREATE${tableType} TABLE ${name}${tableOptions};',
Expand Down
30 changes: 29 additions & 1 deletion forward_engineering/utils/general.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { toLower, omit } = require('lodash');
const { toLower, omit, isEqual } = require('lodash');
const { INLINE_COMMENT } = require('../../constants/constants');

/**
Expand Down Expand Up @@ -146,6 +146,33 @@ const isParentContainerActivated = collection => {
);
};

/**
*
* @template {object} T
* @param {{ new: T, old: T }}
* @returns {boolean}
*/
const compareProperties = ({ new: newProperty, old: oldProperty }) => {
if (!newProperty && !oldProperty) {
return;
}
return !isEqual(newProperty, oldProperty);
};

/**
* @param {object} compMod
* @param {string[]} properties
* @returns {object} Only changed properties with their new values
*/
const getUpdatedProperties = (compMod, properties) =>
properties.reduce((acc, property) => {
const propCompMod = compMod[property] || {};
if (compareProperties(propCompMod) && propCompMod.new !== undefined) {
acc[property] = propCompMod.new;
}
return acc;
}, {});

module.exports = {
setTab,
hasType,
Expand All @@ -165,4 +192,5 @@ module.exports = {
isObjectInDeltaModelActivated,
isParentContainerActivated,
getSchemaNameFromCollection,
getUpdatedProperties,
};