Skip to content

Commit

Permalink
HCK-4447: add tags support for alter script (#201)
Browse files Browse the repository at this point in the history
* HCK-4585: add support for unity tags in view during fe

* HCK-4447: add support for tags in alter script on schema, entity and field level

* HCK-4585: add support for unity tags in view during re

* HCK-4447: add catalog name to alter script statements

* HCK-4585: improve regex validation of tag key

* HCK-4585: improve tooltip wording for unity tag keys

* HCK-4474: add alter script for unity tags on views

* chore: review remarks

---------

Co-authored-by: Oleksandr Lytovchenko <[email protected]>
Co-authored-by: chulanovskyi-bs <[email protected]>
Co-authored-by: chulanovskyi <[email protected]>
  • Loading branch information
4 people authored Feb 29, 2024
1 parent d38bfea commit 2079265
Show file tree
Hide file tree
Showing 23 changed files with 658 additions and 170 deletions.
17 changes: 9 additions & 8 deletions forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ const getAlterCollectionsScriptDtos = ({schema, definitions, provider, data, _,
const deletedCollectionsScriptDtos = getCollectionScripts(
getItems(schema, 'entities', 'deleted'),
'deleted',
getDeleteCollectionsScripts(app, provider)
getDeleteCollectionsScripts(app, provider, dbVersion)
);
const modifiedCollectionsScriptDtos = getCollectionScripts(
getItems(schema, 'entities', 'modified'),
'modified',
getModifyCollectionsScripts(app, definitions, provider, dbVersion)
);
const modifiedCollectionCommentsScriptDtos = getItems(schema, 'entities', 'modified')
.flatMap(item => getModifyCollectionCommentsScripts(provider)(item));
.flatMap(item => getModifyCollectionCommentsScripts(provider)({ collection: item, dbVersion }));

let modifiedCollectionPrimaryKeysScriptDtos = [];
if (getDBVersionNumber(dbVersion) >= Runtime.RUNTIME_SUPPORTING_PK_FK_CONSTRAINTS) {
modifiedCollectionPrimaryKeysScriptDtos = getItems(schema, 'entities', 'modified')
.flatMap(item => getModifyPkConstraintsScripts(_, provider)(item));
.flatMap(item => getModifyPkConstraintsScripts(_, provider)({ collection: item, dbVersion }));
}

const addedColumnsScriptDtos = getColumnScripts(
Expand Down Expand Up @@ -154,7 +154,7 @@ const getAlterCollectionsScriptDtos = ({schema, definitions, provider, data, _,
/**
* @return Array<AlterScriptDto>
* */
const getAlterViewsScriptDtos = (schema, provider, _) => {
const getAlterViewsScriptDtos = (schema, provider, _, dbVersion) => {

/**
* @return Array<AlterScriptDto>
Expand All @@ -180,11 +180,11 @@ const getAlterViewsScriptDtos = (schema, provider, _) => {
const deletedViewScriptDtos = getViewScripts(
getItems(schema, 'views', 'deleted'),
'deleted',
getDeleteViewsScripts(provider)
getDeleteViewsScripts(provider, dbVersion)
);
const modifiedViewScriptDtos = getColumnScripts(
getItems(schema, 'views', 'modified'),
getModifyViewsScripts(provider, _)
getModifyViewsScripts(provider, _, dbVersion)
);

return [
Expand Down Expand Up @@ -250,10 +250,11 @@ const getAlterStatementsWithCommentedUnwantedDDL = (scriptDtos, data) => {
const getAlterScriptDtos = (schema, definitions, data, app) => {
const provider = require('../ddlProvider/ddlProvider')(app);
const _ = app.require('lodash');
const isUnityCatalogSupports = isSupportUnityCatalog(data.modelData[0].dbVersion);
const dbVersion = data.modelData[0].dbVersion;
const isUnityCatalogSupports = isSupportUnityCatalog(dbVersion);
const containersScriptDtos = getAlterContainersScriptDtos({ schema, isUnityCatalogSupports, provider, _, data });
const collectionsScriptDtos = getAlterCollectionsScriptDtos({ schema, definitions, provider, data, _, app });
const viewsScriptDtos = getAlterViewsScriptDtos(schema, provider, _);
const viewsScriptDtos = getAlterViewsScriptDtos(schema, provider, _, dbVersion);
let relationshipsScriptDtos = [];
if (isUnityCatalogSupports) {
relationshipsScriptDtos = getAlterRelationshipsScriptDtos({ schema, ddlProvider: provider, _ });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const {getDatabaseStatement, getDatabaseAlterStatement, getBucketKeyword} = require('../../helpers/databaseHelper')
const {getEntityData, getIsChangeProperties, prepareName, replaceSpaceWithUnderscore} = require('../../utils/general');
const {getEntityData, getIsChangeProperties, prepareName, replaceSpaceWithUnderscore, isSupportUnityCatalog} = require('../../utils/general');
const {getAlterCommentsScriptDtos} = require("./containerHelpers/commentsHelper");
const {AlterScriptDto} = require("../types/AlterScriptDto");
const { getModifyUnityCatalogTagsScriptDtos, getModifyUnitySchemaTagsScriptDtos } = require('./containerHelpers/alterUnityTagsHelper');


const containerProperties = ['comment', 'location', 'dbProperties', 'description'];
Expand Down Expand Up @@ -75,33 +76,35 @@ const getModifyContainerScriptDtos = (provider, _, isUnityCatalogSupports, dbVer

const didPropertiesChange = getIsChangeProperties(_)({...compMod, name: names}, otherContainerProperties);
const containerData = {...getContainerData(compMod), name: names.new};
const catalogName = isSupportUnityCatalog(dbVersion) ? prepareName(compMod?.catalogName?.new) : undefined;
const databaseName = getDatabaseName({role: {...containerData, name: names.old}});
const fullDatabaseName = catalogName ? `${catalogName}.${databaseName}` : databaseName;
if (!didPropertiesChange) {
const alterCommentsScriptDtos = getAlterCommentsScriptDtos(provider)(container);
const alterDatabaseScript = getDatabaseAlterStatement([containerData], dbVersion);
const alterDatabaseScriptDto = AlterScriptDto.getInstance([alterDatabaseScript], true, false)
const alterUnityCatalogTagsScript = getModifyUnityCatalogTagsScriptDtos(provider)(container, catalogName);
const alterUnitySchemaTagsScript = getModifyUnitySchemaTagsScriptDtos({ ddlProvider: provider })({
entityData: container,
name: fullDatabaseName,
});

if (!alterDatabaseScript?.length) {
return alterCommentsScriptDtos;
return [...alterCommentsScriptDtos, ...alterUnityCatalogTagsScript, ...alterUnitySchemaTagsScript];
}
return [
...alterCommentsScriptDtos,
{
scripts: [{
script: alterDatabaseScript,
isDropScript: false,
}]
},
alterDatabaseScriptDto,
...alterUnityCatalogTagsScript,
...alterUnitySchemaTagsScript,
];
}
const databaseName = getDatabaseName({role: {...containerData, name: names.old}});
const deletedScript = provider.dropDatabase(databaseName, getBucketKeyword(dbVersion));
const addedScriptDto = getAddContainerScriptDto(isUnityCatalogSupports, dbVersion)({role: containerData});
const deletedScriptDto = AlterScriptDto.getInstance([deletedScript], true, true);

return [
{
scripts: [{
script: deletedScript,
isDropScript: true,
}]
},
deletedScriptDto,
addedScriptDto
].filter(Boolean);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const {getModifiedCommentOnColumnScriptDtos} = require("./columnHelpers/comments
const {AlterScriptDto} = require("../types/AlterScriptDto");
const {getModifiedDefaultColumnValueScriptDtos} = require("./columnHelpers/defaultValueHelper");
const {getUpdateTypesScriptDtos} = require("./columnHelpers/alterTypeHelper");
const { getModifyUnityColumnTagsScriptDtos } = require('./columnHelpers/alterUnityTagsHelper');

const hydrateAlterColumnName = (_) => (entity, properties = {}) => {
const collectionName = generateFullEntityName(entity);
const hydrateAlterColumnName = (_) => ({ entity, properties = {}, dbVersion }) => {
const collectionName = generateFullEntityName({ entity, dbVersion });
const columns = Object.values(properties).map(property => {
const compMod = _.get(property, 'compMod', {});
const {newField = {}, oldField = {}} = compMod;
Expand Down Expand Up @@ -96,10 +97,10 @@ const getAddCollectionsScripts = (app, definitions, dbVersion) => entity => {
/**
* @return {(entity: Object) => Array<AlterScriptDto>}
* */
const getDeleteCollectionsScripts = (app, provider) => entity => {
const getDeleteCollectionsScripts = (app, provider, dbVersion) => (entity) => {
const _ = app.require('lodash');
const entityData = {...entity, ..._.get(entity, 'role', {})};
const fullCollectionName = generateFullEntityName(entity)
const fullCollectionName = generateFullEntityName({ entity, dbVersion })
const collectionScript = provider.dropTable(fullCollectionName);
const indexScript = provider.dropTableIndex(hydrateDropIndexes(_)(entityData));

Expand All @@ -121,7 +122,7 @@ const getModifyCollectionsScripts = (app, definitions, ddlProvider, dbVersion) =
const _ = app.require('lodash');
const properties = getEntityProperties(collection);
const {script: modifyTableScriptDtos} = generateModifyCollectionScript(app)(collection, definitions, ddlProvider, dbVersion);
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)(collection, properties, definitions);
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)({ entity: collection, properties, definitions, dbVersion });
const dropIndexScript = ddlProvider.dropTableIndex(hydratedDropIndex);
const addIndexScript = getIndexes(_)(...hydratedAddIndex);

Expand All @@ -146,8 +147,8 @@ const getDeleteColumnsScripts = (app, definitions, provider, dbVersion) => entit
const {columns} = getColumns(entityData, definitions, dbVersion);
const properties = getEntityProperties(entity);
const columnStatement = getColumnsString(Object.keys(columns));
const fullCollectionName = generateFullEntityName(entity);
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)(entity, properties, definitions);
const fullCollectionName = generateFullEntityName({ entity, dbVersion });
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)({ entity, properties, definitions, dbVersion });
const modifyScript = generateModifyCollectionScript(app)(entity, definitions, provider, dbVersion);
const dropIndexScript = provider.dropTableIndex(hydratedDropIndex);
const addIndexScript = getIndexes(_)(...hydratedAddIndex);
Expand Down Expand Up @@ -177,8 +178,8 @@ const getDeleteColumnScripsForOlderRuntime = (app, definitions, provider, dbVers
);
const properties = _.omit(_.get(entity, 'role.properties', {}), deleteColumnsName);
const entityData = { role: { ..._.omit(entity.role, ['properties']), properties } };
const { hydratedAddIndex, hydratedDropIndex } = hydrateIndex(_)(entity, properties, definitions);
const fullCollectionName = generateFullEntityName(entity);
const { hydratedAddIndex, hydratedDropIndex } = hydrateIndex(_)({ entity, properties, definitions, dbVersion });
const fullCollectionName = generateFullEntityName({ entity, dbVersion });
const dropIndexScript = provider.dropTableIndex(hydratedDropIndex);
const addIndexScript = getIndexes(_)(...hydratedAddIndex);
const deleteCollectionScript = provider.dropTable(fullCollectionName);
Expand Down Expand Up @@ -218,22 +219,28 @@ const getModifyColumnsScripts = (app, definitions, ddlProvider, dbVersion) => co
properties: Object.fromEntries(unionProperties)
}
};
const hydratedAlterColumnName = hydrateAlterColumnName(_)(collection, properties);
const hydratedAlterColumnName = hydrateAlterColumnName(_)({ entity: collection, properties, dbVersion });
const alterColumnScripts = ddlProvider.alterTableColumnName(hydratedAlterColumnName);
const modifiedScript = generateModifyCollectionScript(app)(entityData, definitions, ddlProvider, dbVersion);
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)(collection, properties, definitions);
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)({ entity: collection, properties, definitions, dbVersion });
const dropIndexScript = ddlProvider.dropTableIndex(hydratedDropIndex);
const addIndexScript = getIndexes(_)(...hydratedAddIndex);

const modifiedCommentOnColumnsScriptDtos = getModifiedCommentOnColumnScriptDtos(_, ddlProvider)(collection);
const modifyNotNullConstraintsScriptDtos = getModifyNonNullColumnsScriptDtos(_, ddlProvider)(collection);
const modifyCheckConstraintsScriptDtos = getCheckConstraintsScriptDtos(_, ddlProvider)(collection);
const modifiedDefaultColumnValueScriptDtos = getModifiedDefaultColumnValueScriptDtos(_, ddlProvider)(collection);
const modifiedCommentOnColumnsScriptDtos = getModifiedCommentOnColumnScriptDtos(_, ddlProvider)({ collection, dbVersion });
const modifyNotNullConstraintsScriptDtos = getModifyNonNullColumnsScriptDtos(_, ddlProvider)({ collection, dbVersion });
const modifyCheckConstraintsScriptDtos = getCheckConstraintsScriptDtos(_, ddlProvider)({ collection, dbVersion });
const modifiedDefaultColumnValueScriptDtos = getModifiedDefaultColumnValueScriptDtos(_, ddlProvider)({ collection, dbVersion });

const dropIndexScriptDto = AlterScriptDto.getInstance([dropIndexScript], true, true);
const addIndexScriptDto = AlterScriptDto.getInstance([addIndexScript], true, false);
const alterColumnScriptDtos = AlterScriptDto.getInstances(alterColumnScripts, true, false);

const unityColumnTagsDtos = Object.keys(properties).flatMap(columnName =>
getModifyUnityColumnTagsScriptDtos({ ddlProvider })({
entityData: collection,
tableName: hydratedAlterColumnName.collectionName,
columnName,
}),
);
if (modifiedScript.type === 'new') {
return [dropIndexScriptDto, ...(modifiedScript.script || []), addIndexScriptDto]
.filter(Boolean);
Expand All @@ -250,7 +257,8 @@ const getModifyColumnsScripts = (app, definitions, ddlProvider, dbVersion) => co
...modifyCheckConstraintsScriptDtos,
...modifiedDefaultColumnValueScriptDtos,
...(modifiedScript.script || []),
addIndexScriptDto
addIndexScriptDto,
...unityColumnTagsDtos,
]
.filter(Boolean);
};
Expand All @@ -277,21 +285,21 @@ const getModifyColumnsScriptsForOlderRuntime = (
properties: Object.fromEntries(unionProperties)
}
};
const hydratedAlterColumnName = hydrateAlterColumnName(_)(collection, properties);
const hydratedAlterColumnName = hydrateAlterColumnName(_)({ entity: collection, properties, dbVersion });
const alterColumnScripts = ddlProvider.alterTableColumnName(hydratedAlterColumnName);
const modifiedScript = generateModifyCollectionScript(app)(entityData, definitions, ddlProvider, dbVersion);
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)(collection, properties, definitions);
const {hydratedAddIndex, hydratedDropIndex} = hydrateIndex(_)({ entity: collection, properties, definitions, dbVersion });
const dropIndexScript = ddlProvider.dropTableIndex(hydratedDropIndex);
const addIndexScript = getIndexes(_)(...hydratedAddIndex);

const {columnsToDelete} = hydrateAlterColumnType(_)(properties);
const modifiedCommentOnColumnsScriptDtos = getModifiedCommentOnColumnScriptDtos(_, ddlProvider)(collection);
const modifyNotNullConstraintsScriptDtos = getModifyNonNullColumnsScriptDtos(_, ddlProvider)(collection);
const modifyCheckConstraintsScriptDtos = getCheckConstraintsScriptDtos(_, ddlProvider)(collection);
const modifiedCommentOnColumnsScriptDtos = getModifiedCommentOnColumnScriptDtos(_, ddlProvider)({ collection, dbVersion });
const modifyNotNullConstraintsScriptDtos = getModifyNonNullColumnsScriptDtos(_, ddlProvider)({ collection, dbVersion });
const modifyCheckConstraintsScriptDtos = getCheckConstraintsScriptDtos(_, ddlProvider)({ collection, dbVersion });

let tableModificationScriptDtos = [];
if (!_.isEmpty(columnsToDelete)) {
const fullCollectionName = generateFullEntityName(collection);
const fullCollectionName = generateFullEntityName({ entity: collection, dbVersion });
const deleteCollectionScript = ddlProvider.dropTable(fullCollectionName);
const hydratedCollection = hydrateCollection(_)(entityData, definitions);
const arePkFkConstraintsAvailable = isSupportUnityCatalog(dbVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const {
getFullEntityName
} = require('../../utils/general');
const {AlterScriptDto} = require("../types/AlterScriptDto");
const { getModifyUnityViewTagsScriptDtos } = require('./entityHelpers/alterUnityTagsHelper');

const viewProperties = ['code', 'name', 'tableProperties', 'selectStatement'];
const viewProperties = ['code', 'name', 'tableProperties', 'selectStatement', 'unityViewTags'];
const otherViewProperties = ['viewTemporary', 'viewOrReplace', 'isGlobal', 'description'];

const compareProperties = (_) => (view, properties) => {
Expand Down Expand Up @@ -93,8 +94,8 @@ const getAddViewsScripts = (_) => view => {
/**
* @return {(view: Object) => AlterScriptDto}
* */
const getDeleteViewsScripts = provider => view => {
const viewName = generateFullEntityName(view);
const getDeleteViewsScripts = (provider, dbVersion) => view => {
const viewName = generateFullEntityName({ entity: view, dbVersion });
const script = provider.dropView(viewName);
return {
isActivated: true,
Expand All @@ -108,36 +109,24 @@ const getDeleteViewsScripts = provider => view => {
/**
* @return {(view: Object) => Array<AlterScriptDto>}
* */
const getModifyViewsScripts = (provider, _) => view => {
const getModifyViewsScripts = (provider, _, dbVersion) => view => {
const comparedProperties = compareProperties(_)(view, viewProperties);
const viewName = generateFullEntityName({ entity: view, dbVersion });
if (comparedProperties) {
const hydratedAlterView = hydrateAlterView(_)(view);
const alterViewScript = prepareScript(...provider.alterView(hydratedAlterView));
return alterViewScript.map(script => ({
isActivated: true,
scripts: [{
isDropScript: false,
script
}]
}));
const alterUnityViewTagsScriptDtos = getModifyUnityViewTagsScriptDtos({ ddlProvider: provider })( { viewData: view, viewName })
const alterViewScriptDtos = alterViewScript.map(script => AlterScriptDto.getInstance([script], true, false));

return [...alterViewScriptDtos, ...alterUnityViewTagsScriptDtos];
}
const viewName = generateFullEntityName(view);
const dropViewScript = provider.dropView(viewName);
const hydratedView = hydrateView(_)(view);
const addViewScript = getViewScript({_, ...hydratedView});
return [{
isActivated: true,
scripts: [{
isDropScript: true,
script: dropViewScript
}]
}, [{
isActivated: true,
scripts: [{
isDropScript: false,
script: addViewScript
}]
}]];
const dropViewScriptDto = AlterScriptDto.getInstance([dropViewScript], true, true)
const addViewScriptDto = AlterScriptDto.getInstance([addViewScript], true, false)

return [dropViewScriptDto, addViewScriptDto].filter(Boolean);
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ const getEntityData = (_) => (collection) => {
}

/**
* @return {(collection: Object, definitions: any) => AlterScriptDto[]}
* @return {(collection: Object, definitions: any, dbVersion: string) => AlterScriptDto[]}
* */
const getUpdateTypesScriptDtos = (_, ddlProvider) => (collection, definitions, dbVersion) => {
const fullTableName = generateFullEntityName(collection);
const fullTableName = generateFullEntityName({ entity: collection, dbVersion });
const entityData = getEntityData(_)(collection);
const {columns: columnsInfo} = getColumns(entityData.role, definitions, dbVersion);

Expand Down
Loading

0 comments on commit 2079265

Please sign in to comment.