Skip to content

Commit

Permalink
Merge branch 'main' into feature/HCK-4585
Browse files Browse the repository at this point in the history
  • Loading branch information
chulanovskyi-bs authored Mar 7, 2024
2 parents 819942b + f81feb6 commit d8d5f7b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 28 deletions.
2 changes: 1 addition & 1 deletion forward_engineering/alterScript/alterScriptBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const getEntityLevelAlterScriptDtos = (data, app) => ({
* */
const buildEntityLevelAlterScript = (data, app) => (entityLevelAlterScriptDto) => {
const alterScriptDtos = getEntityLevelAlterScriptDtos(data, app)(entityLevelAlterScriptDto);
return joinAlterScriptDtosIntoAlterScript(alterScriptDtos, data);
return joinAlterScriptDtosIntoAlterScript(alterScriptDtos, data);
}

/**
Expand Down
83 changes: 68 additions & 15 deletions forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,41 @@ const getAlterContainersScriptDtos = ({ schema, isUnityCatalogSupports, provider
];
};

/**
* @typedef FilterOutExistingStatementsParams
* @type {object}
* @property {Array<AlterScriptDto>} alterScriptDtos
* @property {Set<string>} existingAlterStatements
*/
/**
* @param {FilterOutExistingStatementsParams} param
* @returns {FilterOutExistingStatementsParams}
*/
const filterOutExistingStatements = ({ alterScriptDtos, existingAlterStatements }) => {
const filteredAlterScriptDtos = alterScriptDtos
.filter(Boolean)
.flatMap(alterScriptDto =>
alterScriptDto?.scripts
.filter(scriptDto => !existingAlterStatements.has(scriptDto?.script))
.map(scriptDto =>
AlterScriptDto.getInstance([scriptDto.script], alterScriptDto.isActivated, scriptDto.isDropScript),
),
)
.filter(Boolean);

const filteredExistingAlterScriptStatements = new Set([
...Array.from(existingAlterStatements),
...alterScriptDtos.flatMap(dto => dto?.scripts.map(scriptDto => scriptDto?.script)).filter(Boolean),
]);

return { alterScriptDtos: filteredAlterScriptDtos, existingAlterStatements: filteredExistingAlterScriptStatements };
};

/**
* @return Array<AlterScriptDto>
* */
const getAlterCollectionsScriptDtos = ({schema, definitions, provider, data, _, app}) => {
const existingAlterStatements = new Set();
const getCollectionScripts = (items, compMode, getScript) =>
items.filter(item => item.compMod?.[compMode]).flatMap(getScript);

Expand Down Expand Up @@ -127,28 +158,50 @@ const getAlterCollectionsScriptDtos = ({schema, definitions, provider, data, _,
}

const addedColumnsScriptDtos = getColumnScripts(
getItems(schema, 'entities', 'added'),
getAddColumnsScripts(app, definitions, provider, dbVersion)
);
getItems(schema, 'entities', 'added'),
getAddColumnsScripts(app, definitions, provider, dbVersion),
);
const {
alterScriptDtos: addedColumnsScriptDtosWithNoDuplicates,
existingAlterStatements: existingAlterStatementsWithAddedColumns,
} = filterOutExistingStatements({
alterScriptDtos: addedColumnsScriptDtos,
existingAlterStatements,
});

const deletedColumnsScriptDtos = getColumnScripts(
getItems(schema, 'entities', 'deleted'),
getDeletedColumnsScriptsMethod(app, definitions, provider)
getDeletedColumnsScriptsMethod(app, definitions, provider),
);
const {
alterScriptDtos: deletedColumnsScriptDtosWithNoDuplicates,
existingAlterStatements: existingAlterStatementsWithDeletedColumns,
} = filterOutExistingStatements({
alterScriptDtos: deletedColumnsScriptDtos,
existingAlterStatements: existingAlterStatementsWithAddedColumns,
});

const modifiedColumnsScriptDtos = getColumnScripts(
getItems(schema, 'entities', 'modified'),
getModifyColumnsScriptsMethod(app, definitions, provider)
getModifyColumnsScriptsMethod(app, definitions, provider),
);
const {
alterScriptDtos: modifiedColumnsScriptDtosWithNoDuplicates,
} = filterOutExistingStatements({
alterScriptDtos: modifiedColumnsScriptDtos,
existingAlterStatements: existingAlterStatementsWithDeletedColumns,
});

return [
...deletedCollectionsScriptDtos,
...addedCollectionsScriptDtos,
...modifiedCollectionsScriptDtos,
...modifiedCollectionCommentsScriptDtos,
...modifiedCollectionPrimaryKeysScriptDtos,
...deletedColumnsScriptDtos,
...addedColumnsScriptDtos,
...modifiedColumnsScriptDtos
];
return [
...deletedCollectionsScriptDtos,
...addedCollectionsScriptDtos,
...modifiedCollectionsScriptDtos,
...modifiedCollectionCommentsScriptDtos,
...modifiedCollectionPrimaryKeysScriptDtos,
...deletedColumnsScriptDtosWithNoDuplicates,
...addedColumnsScriptDtosWithNoDuplicates,
...modifiedColumnsScriptDtosWithNoDuplicates,
].filter(Boolean);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ const getAddCollectionsScripts = (app, definitions, dbVersion) => entity => {
arePkFkConstraintsAvailable,
areNotNullConstraintsAvailable,
null,
dbVersion
dbVersion,
true
);
const indexScript = getIndexes(_)(...hydrateAddIndexes(_)(entity, indexes, properties, definitions));

Expand Down Expand Up @@ -191,7 +192,8 @@ const getDeleteColumnScripsForOlderRuntime = (app, definitions, provider, dbVers
arePkFkConstraintsAvailable,
areNotNullConstraintsAvailable,
null,
dbVersion
dbVersion,
true
);

const dropIndexScriptDto = AlterScriptDto.getInstance([dropIndexScript], true, true);
Expand Down Expand Up @@ -309,7 +311,8 @@ const getModifyColumnsScriptsForOlderRuntime = (
arePkFkConstraintsAvailable,
areNotNullConstraintsAvailable,
null,
dbVersion
dbVersion,
true
);
tableModificationScriptDtos = [
AlterScriptDto.getInstance([deleteCollectionScript], true, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ const getDropAndRecreateCollectionScriptDtos = (app, ddlProvider) => (collection
arePkFkConstraintsAvailable,
areNotNullConstraintsAvailable,
null,
dbVersion
dbVersion,
true
);
const deleteCollectionScript = ddlProvider.dropTable(fullCollectionName);
return {
Expand Down
20 changes: 12 additions & 8 deletions forward_engineering/helpers/tableHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
prepareName,
getDifferentItems,
getFullEntityName,
getDBVersionNumber
getDBVersionNumber,
generateFullEntityName
} = require('../utils/general');
const { getColumnsStatement, getColumns } = require('./columnHelper');
const keyHelper = require('./keyHelper');
Expand All @@ -20,7 +21,7 @@ const { getColumnTagsStatement } = require('./unityTagsHelper');
const { Runtime } = require('../enums/runtime');

const getCreateStatement = (_) => ({
dbName, tableName, isTemporary, isExternal, using, likeStatement, columnStatement, primaryKeyStatement, foreignKeyStatement, comment, partitionedByKeys,
fullTableName, isTemporary, isExternal, using, likeStatement, columnStatement, primaryKeyStatement, foreignKeyStatement, comment, partitionedByKeys,
clusteredKeys, sortedKeys, numBuckets, skewedStatement, rowFormatStatement, storedAsStatement, location, tableProperties, selectStatement,
isActivated, tableOptions, orReplace, ifNotExists,
}) => {
Expand All @@ -29,7 +30,6 @@ const getCreateStatement = (_) => ({
const orReplaceStatement = orReplace ? 'OR REPLACE' : '';
const isNotExistsStatement = ifNotExists ? ' IF NOT EXISTS' : '';
const tempExtStatement = ' ' + [orReplaceStatement, temporary, external].filter(d => d).map(item => item + ' ').join('');
const fullTableName = dbName ? `${dbName}.${tableName}` : tableName;

if (using && likeStatement) {
return getCreateLikeStatement(_)({
Expand Down Expand Up @@ -267,6 +267,8 @@ const getStoredAsStatement = (tableData) => {
* arePkFkConstraintsAvailable: boolean,
* areNotNullConstraintsAvailable: boolean,
* likeTableData: any,
* dbVersion: string,
* isCalledFromAlterScript: boolean,
* ) => string}
* */
const getTableStatement = (app) => (
Expand All @@ -277,7 +279,8 @@ const getTableStatement = (app) => (
arePkFkConstraintsAvailable,
areNotNullConstraintsAvailable,
likeTableData,
dbVersion
dbVersion,
isCalledFromAlterScript = false,
) => {
const _ = app.require('lodash');
const ddlProvider = require('../ddlProvider/ddlProvider')(app);
Expand All @@ -288,16 +291,17 @@ const getTableStatement = (app) => (
const container = getTab(0, containerData);
const isTableActivated = tableData.isActivated && (typeof container.isActivated === 'boolean' ? container.isActivated : true);
const tableName = replaceSpaceWithUnderscore(prepareName(getName(tableData)));
const fullTableName = getFullEntityName(dbName, tableName);
const fullTableName = isCalledFromAlterScript
? generateFullEntityName({ entity: { role: tableData }, dbVersion })
: getFullEntityName(dbName, tableName);
const { columns, deactivatedColumnNames } = getColumns(entityJsonSchema, definitions, dbVersion);
const keyNames = keyHelper.getKeyNames(tableData, entityJsonSchema, definitions);
const tableColumns = getTableColumnsStatement(columns, tableData.using, keyNames.compositePartitionKey);
const primaryKeyStatement = arePkFkConstraintsAvailable
? constraintHelper.getPrimaryKeyStatement(_)(entityJsonSchema, keyNames.primaryKeys, deactivatedColumnNames, isTableActivated)
: '';
let tableStatement = getCreateStatement(_)({
dbName,
tableName,
fullTableName,
isTemporary: tableData.temporaryTable,
isExternal: tableData.externalTable,
orReplace: tableData.orReplace,
Expand Down Expand Up @@ -338,7 +342,7 @@ const getTableStatement = (app) => (

if (getDBVersionNumber(dbVersion) >= Runtime.MINIMUM_UNITY_TAGS_SUPPORT_VERSION) {
const columnsUnityTags = getColumnTagsStatement(_, entityJsonSchema.properties, fullTableName);
tableStatement = tableStatement + columnsUnityTags;
tableStatement = [tableStatement, ...columnsUnityTags].join('\n');
}

return tableStatement;
Expand Down

0 comments on commit d8d5f7b

Please sign in to comment.