Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const _ = require('lodash');
const { AlterScriptDto, SCRIPT_TYPE } = require('../../types/AlterScriptDto.js');
const { AlterCollectionDto } = require('../../types/AlterCollectionDto');
const { AlterIndexDto } = require('../../types/AlterIndexDto');
const { prepareNameForScriptFormat } = require('../../../utils/general.js');
const {
prepareNameForScriptFormat,
isParentContainerActivated,
isObjectInDeltaModelActivated,
} = require('../../../utils/general.js');

/**
* @typedef {{
Expand Down Expand Up @@ -135,11 +139,17 @@ const getDeletedIndexesScriptDtos =
*/
const getDeleteIndexScriptDto =
({ ddlProvider, scriptFormat }) =>
({ index }) => {
({ index, collection }) => {
const name = prepareNameForScriptFormat(scriptFormat)(index.indxName);
const script = ddlProvider.dropIndex({ name });

return AlterScriptDto.getInstance(script, index.isActivated, true, SCRIPT_TYPE.dropEntityIndex);
const isCollectionActivated =
isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);
return AlterScriptDto.getInstance(
script,
isCollectionActivated && index.isActivated,
true,
SCRIPT_TYPE.dropEntityIndex,
);
};

/**
Expand Down Expand Up @@ -176,8 +186,14 @@ const getAddIndexScriptDto =
...indexWithAddedKeyNames,
schemaName: collection?.role?.compMod?.bucketProperties?.name,
});

return AlterScriptDto.getInstance(script, index.isActivated, false, SCRIPT_TYPE.createEntityIndex);
const isCollectionActivated =
isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);
return AlterScriptDto.getInstance(
script,
isCollectionActivated && index.isActivated,
false,
SCRIPT_TYPE.createEntityIndex,
);
};

/**
Expand Down Expand Up @@ -220,13 +236,15 @@ const getModifyIndexScriptDto =
({ modifiedIndex: { oldIndex, newIndex }, collection }) => {
const oldName = prepareNameForScriptFormat(scriptFormat)(oldIndex.indxName);
const newName = prepareNameForScriptFormat(scriptFormat)(newIndex.indxName);
const isActivated =
newIndex.isActivated && isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);

let alterScriptDtos = [];

if (shouldDropAndRecreateIndex({ oldIndex, newIndex })) {
const dropIndexDto = AlterScriptDto.getInstance(
ddlProvider.dropIndex({ name: oldName }),
newIndex.isActivated,
isActivated,
true,
SCRIPT_TYPE.dropEntityIndex,
);
Expand All @@ -236,7 +254,7 @@ const getModifyIndexScriptDto =
...newIndexWithAddedKeyNames,
schemaName: collection?.role?.compMod?.bucketProperties?.name,
}),
newIndex.isActivated,
isActivated,
false,
SCRIPT_TYPE.createEntityIndex,
);
Expand All @@ -249,7 +267,7 @@ const getModifyIndexScriptDto =
if (shouldRenameIndex) {
const alterIndexDto = AlterScriptDto.getInstance(
ddlProvider.alterIndexRename({ oldName, newName }),
newIndex.isActivated,
isActivated,
false,
SCRIPT_TYPE.alterEntityIndex,
);
Expand All @@ -259,7 +277,7 @@ const getModifyIndexScriptDto =
if (shouldAlterIndexRebuild({ oldIndex, newIndex })) {
const alterIndexRebuildDto = AlterScriptDto.getInstance(
ddlProvider.alterIndexRebuild({ name: newName, indexData: newIndex }),
newIndex.isActivated,
isActivated,
false,
SCRIPT_TYPE.alterEntityIndex,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,19 @@ const getAddCompositePkScriptDtos = ({ scriptFormat, collection }) => {

const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName(scriptFormat)(collectionSchema);
const isCollectionActivated = isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);

return newPrimaryKeys
.map(newPk => {
const ddlConfig = getCreateCompositePKDDLProviderConfig(newPk, collection);
const statementDto = alterKeyConstraint(fullTableName, collection.isActivated, ddlConfig);
const statementDto = alterKeyConstraint(fullTableName, isCollectionActivated, ddlConfig);

return new KeyScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated);
return new KeyScriptModificationDto(
statementDto.statement,
fullTableName,
false,
statementDto.isActivated && isCollectionActivated,
);
})
.filter(scriptDto => Boolean(scriptDto.script));
};
Expand Down Expand Up @@ -407,6 +413,7 @@ const getAddPkScriptDtos = ({ scriptFormat, collection }) => {

const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName(scriptFormat)(collectionSchema);
const isCollectionActivated = isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
Expand All @@ -424,8 +431,13 @@ const getAddPkScriptDtos = ({ scriptFormat, collection }) => {
})
.map(([name, jsonSchema]) => {
const ddlConfig = getCreateRegularPKDDLProviderConfig(name, jsonSchema);
const statementDto = alterKeyConstraint(fullTableName, collection.isActivated, ddlConfig);
return new KeyScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated);
const statementDto = alterKeyConstraint(fullTableName, isCollectionActivated, ddlConfig);
return new KeyScriptModificationDto(
statementDto.statement,
fullTableName,
false,
isCollectionActivated && statementDto.isActivated,
);
})
.filter(scriptDto => Boolean(scriptDto.script));
};
Expand Down Expand Up @@ -487,9 +499,15 @@ const getModifyPkConstraintsScriptDtos = ({ scriptFormat, collection }) => {

const allDtos = [...modifyCompositePkScriptDtos, ...modifyPkScriptDtos].filter(Boolean);
const sortedAllDtos = sortModifyKeyConstraints(allDtos);
const isCollectionActivated = isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);

return sortedAllDtos.map(dto =>
AlterScriptDto.getInstance(dto.script, dto.isActivated, dto.isDropScript, SCRIPT_TYPE.alterEntity),
AlterScriptDto.getInstance(
dto.script,
isCollectionActivated && dto.isActivated,
dto.isDropScript,
SCRIPT_TYPE.alterEntity,
),
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,19 @@ const getAddCompositeUniqueKeyScriptDtos = ({ scriptFormat, collection }) => {

const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName(scriptFormat)(collectionSchema);
const isCollectionActivated = isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);

return newUniqueKeys
.map(newUniqueKey => {
const ddlConfig = getCreateCompositeUniqueKeyDDLProviderConfig(newUniqueKey, collection);
const statementDto = alterKeyConstraint(fullTableName, collection.isActivated, ddlConfig);

return new KeyScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated);
return new KeyScriptModificationDto(
statementDto.statement,
fullTableName,
false,
isCollectionActivated && statementDto.isActivated,
);
})
.filter(scriptDto => Boolean(scriptDto.script));
};
Expand Down Expand Up @@ -412,6 +418,7 @@ const getAddUniqueKeyScriptDtos = ({ scriptFormat, collection }) => {

const collectionSchema = getSchemaOfAlterCollection(collection);
const fullTableName = getFullCollectionName(scriptFormat)(collectionSchema);
const isCollectionActivated = isParentContainerActivated(collection) && isObjectInDeltaModelActivated(collection);

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
Expand All @@ -430,7 +437,12 @@ const getAddUniqueKeyScriptDtos = ({ scriptFormat, collection }) => {
.map(([name, jsonSchema]) => {
const ddlConfig = getCreateRegularUniqueKeyDDLProviderConfig(name, jsonSchema);
const statementDto = alterKeyConstraint(fullTableName, collection.isActivated, ddlConfig);
return new KeyScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated);
return new KeyScriptModificationDto(
statementDto.statement,
fullTableName,
false,
isCollectionActivated && statementDto.isActivated,
);
})
.filter(scriptDto => Boolean(scriptDto.script));
};
Expand Down
45 changes: 34 additions & 11 deletions forward_engineering/ddlProvider/ddlHelpers/indexHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const _ = require('lodash');
const { normalizeLineEndings } = require('../../utils/general');
const { normalizeLineEndings, commentIfDeactivated } = require('../../utils/general');

module.exports = ({ prepareName, getNamePrefixedWithSchemaName }) => {
const getIndexType = indexType => {
Expand Down Expand Up @@ -64,22 +64,45 @@ module.exports = ({ prepareName, getNamePrefixedWithSchemaName }) => {
* @param column_expression {string | undefined}
* @return {string}
* */
const getIndexKeys = ({ indxKey, column_expression }) => {
const columnExpression = _.trim(column_expression);
const getIndexKeys = ({ index, isParentActivated }) => {
const columnExpression = _.trim(index.column_expression);

if (columnExpression) {
return `\n(\n\t${columnExpression}\n)\n\t`;
return {
statement: `\n(\n\t${columnExpression}\n)\n\t`,
hasActiveColumns: true,
};
}

if (_.isArray(indxKey) && !_.isEmpty(indxKey)) {
const indexedColumnsClause = _.map(
indxKey,
({ name, type }) => `${prepareName(name)} ${_.toUpper(type)}`,
).join(',\n\t');
return `\n(\n\t${indexedColumnsClause}\n)\n\t`;
let hasActiveColumns = false;
let statement = '';

if (Array.isArray(index.indxKey) && !_.isEmpty(index.indxKey)) {
const indexedColumns = _.map(index.indxKey, ({ name, type, isActivated }) => {
if (isActivated && !hasActiveColumns) {
hasActiveColumns = true;
}
return {
statement: `${prepareName(name)} ${_.toUpper(type)}`,
isActivated,
};
});

// Do not comment the column when (!index.isActivated || !isParentActivated || !hasActiveColumns) because the whole index will be commented out
const isActivatedByNeed = !index.isActivated || !isParentActivated || !hasActiveColumns;

const indexedColumnsClause = indexedColumns
.map(column =>
commentIfDeactivated(column.statement, { isActivated: isActivatedByNeed || column.isActivated }),
)
.join(',\n\t');
statement = `\n(\n\t${indexedColumnsClause}\n)\n\t`;
}

return '';
return {
statement,
hasActiveColumns,
};
};

/**
Expand Down
40 changes: 35 additions & 5 deletions forward_engineering/ddlProvider/ddlHelpers/viewHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('lodash');
const { commentIfDeactivated } = require('../../utils/general');

module.exports = ({ prepareName }) => {
const getViewType = ({ editioning, editionable }) => {
Expand All @@ -17,37 +18,66 @@ module.exports = ({ prepareName }) => {
}
};

const getViewData = keys => {
const getViewData = ({ keys, isViewActivated }) => {
if (!Array.isArray(keys)) {
return { tables: [], columns: [] };
}

return keys.reduce(
let hasActiveColumns = false;

const { columns, tables } = keys.reduce(
(result, key) => {
// Keys without a tableName are plain columns that don't reference any table/view column.
// They exist for documentation purposes only and can't take part in the SELECT statement.
if (!key.tableName) {
return result;
}

const tableName = `${prepareName(key.dbName)}.${prepareName(key.tableName)}`;
let tableName = `${prepareName(key.dbName)}.${prepareName(key.tableName)}`;

if (!result.tables.includes(tableName)) {
result.tables.push(tableName);
const addedTable = result.tables.find(t => t.statement === tableName);
if (addedTable) {
addedTable.isActivated = addedTable.isActivated || key.isActivated;
} else {
result.tables.push({
statement: tableName,
isActivated: key.isActivated,
});
}

result.columns.push({
statement: `${tableName}.${getKeyWithAlias(key)}`,
isActivated: key.isActivated,
});

if (key.isActivated && !hasActiveColumns) {
hasActiveColumns = true;
}

return result;
},
{
tables: [],
columns: [],
},
);

const join = items =>
items
.map(
// Do not comment the column or table when (!isViewActivated || !hasActiveColumns) because the whole view will be commented out
item =>
commentIfDeactivated(item.statement, {
isActivated: !isViewActivated || !hasActiveColumns || item.isActivated,
}),
)
.join(',\n\t\t');

return {
hasActiveColumns,
columnsAsString: join(columns),
tablesAsString: join(tables),
};
};

return {
Expand Down
Loading