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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const {
wrapInQuotes,
getId,
} = require('../../../utils/general');
const { dropIndex, createIndex, getWithOptions } = require('../../../ddlProvider/ddlHelpers/indexHelper');
const {
dropIndex,
createIndex,
getWithOptions,
getCommentOnIndexStatement,
} = require('../../../ddlProvider/ddlHelpers/indexHelper');
const assignTemplates = require('../../../utils/assignTemplates');
const templates = require('../../../ddlProvider/templates');

Expand Down Expand Up @@ -359,6 +364,41 @@ const getAlterIndexScriptDtos = ({ newIndex, oldIndex, additionalDataForDdlProvi
return alterIndexScriptDtos.filter(Boolean);
};

/**
* Comment changes stay on the existing index. A comment-only change must not drop and recreate it.
*
* @param {AlterIndexDto} newIndex
* @param {AlterIndexDto} oldIndex
* @param {Object} additionalDataForDdlProvider
* @param {AlterCollectionDto} collection
* @return {AlterScriptDto | null}
*/
const getModifyIndexCommentScriptDto = ({ newIndex, oldIndex, additionalDataForDdlProvider, collection }) => {
const oldComment = oldIndex.indexComment || '';
const newComment = newIndex.indexComment || '';

if (newComment === oldComment) {
return null;
}

const { isParentActivated, schemaName } = additionalDataForDdlProvider;
const isRemoval = !newComment;
const script = getCommentOnIndexStatement({
indexName: newIndex.indxName,
schemaName,
comment: newComment,
isRemoved: isRemoval,
});

return AlterScriptDto.getInstance(
script,
newIndex.isActivated && isParentActivated,
isRemoval,
SCRIPT_TYPE.alterEntityIndex,
getId(collection),
);
};

/**
* @param {AlterCollectionDto} collection
* @param {Object} additionalDataForDdlProvider
Expand Down Expand Up @@ -401,17 +441,22 @@ const getModifiedIndexesScriptDtos = ({ collection, additionalDataForDdlProvider
return [deleteIndexScriptDto, createIndexScriptDto];
}

const shouldAlter = shouldAlterIndex({ oldIndex, newIndex });
if (shouldAlter) {
return getAlterIndexScriptDtos({
oldIndex,
newIndex,
collection,
additionalDataForDdlProvider,
});
}
const alterScriptDtos = shouldAlterIndex({ oldIndex, newIndex })
? getAlterIndexScriptDtos({
oldIndex,
newIndex,
collection,
additionalDataForDdlProvider,
})
: [];
const commentScriptDto = getModifyIndexCommentScriptDto({
newIndex,
oldIndex,
collection,
additionalDataForDdlProvider,
});

return [];
return alterScriptDtos.concat(commentScriptDto);
})
.filter(Boolean);
};
Expand Down
5 changes: 5 additions & 0 deletions forward_engineering/alterScript/types/AlterIndexDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class AlterIndexDto {
* @type {Object}
* */
index_storage_parameter;

/**
* @type {string | undefined}
*/
indexComment;
}

module.exports = {
Expand Down
56 changes: 39 additions & 17 deletions forward_engineering/ddlProvider/ddlHelpers/indexHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
getNamePrefixedWithSchemaName,
getDbVersion,
commentIfDeactivated,
wrapComment,
} = require('../../utils/general');
const assignTemplates = require('../../utils/assignTemplates');
const templates = require('../templates');
Expand Down Expand Up @@ -102,6 +103,22 @@ const getValue = value => {
return value;
};

/**
* @param {{ indexName?: string, schemaName?: string, comment?: string, isRemoved?: boolean }} params
* @returns {string}
*/
const getCommentOnIndexStatement = ({ indexName, schemaName, comment, isRemoved = false }) => {
if (!indexName || (!comment && !isRemoved)) {
return '';
}

return assignTemplates(templates.comment, {
object: 'INDEX',
objectName: getNamePrefixedWithSchemaName(indexName, schemaName),
comment: isRemoved ? 'NULL' : wrapComment(comment),
});
};

const createIndex = (tableName, index, dbData, isParentActivated = true) => {
const isNameEmpty = !index.indxName && index.ifNotExist;
const indexExpressions = getIndexExpressions({ index });
Expand Down Expand Up @@ -134,24 +151,28 @@ const createIndex = (tableName, index, dbData, isParentActivated = true) => {
isAllColumnsDeactivated,
});
const options = getIndexOptions(index, isParentActivated);
const createIndexStatement = assignTemplates(templates.index, {
unique,
concurrently,
ifNotExist,
name,
only,
using,
keys,
options,
nullsDistinct,
tableName: getNamePrefixedWithSchemaName(tableName, index.schemaName),
});
const commentStatement = getCommentOnIndexStatement({
indexName: index.indxName,
schemaName: index.schemaName,
comment: index.indexComment,
});
const statement = commentStatement ? `${createIndexStatement}\n${commentStatement}` : createIndexStatement;

return commentIfDeactivated(
assignTemplates(templates.index, {
unique,
concurrently,
ifNotExist,
name,
only,
using,
keys,
options,
nullsDistinct,
tableName: getNamePrefixedWithSchemaName(tableName, index.schemaName),
}),
{
isActivated: index.isActivated && isParentActivated && !isAllColumnsDeactivated,
},
);
return commentIfDeactivated(statement, {
isActivated: index.isActivated && isParentActivated && !isAllColumnsDeactivated,
});
};

/**
Expand All @@ -168,6 +189,7 @@ const dropIndex = ({ indexName }) => {
module.exports = {
createIndex,
dropIndex,
getCommentOnIndexStatement,
getIndexKeys,
getIndexOptions,
getWithOptions,
Expand Down
1 change: 1 addition & 0 deletions forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ module.exports = (baseProvider, options, app) => {
* ifNotExist?: boolean,
* only?: boolean,
* nullsDistinct?: string,
* indexComment?: string,
* columns?: Array<{
* sortOrder?: any,
* nullsOrder?: any,
Expand Down
1 change: 1 addition & 0 deletions reverse_engineering/helpers/postgresHelpers/tableHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ const prepareTableIndexes = tableIndexesResult => {
? columns
: _.map(columns, column => _.omit(column, 'sortOrder', 'nullsOrder')),
...(nullsDistinct && { nullsDistinct }),
...(indexData.index_comment && { indexComment: indexData.index_comment }),
};

return clearEmptyPropertiesInObject(index);
Expand Down
7 changes: 5 additions & 2 deletions reverse_engineering/helpers/queryConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const getGET_TABLE_INDEXES = postgresVersion => {
array_agg(nulls_first
ORDER BY ord) AS nulls_first,
reloptions AS storage_parameters,
tablespace_name
tablespace_name,
index_comment
FROM
(SELECT ct.oid AS table_oid,
c.relname AS indexname,
Expand All @@ -30,6 +31,7 @@ const getGET_TABLE_INDEXES = postgresVersion => {
attribute.attname,
c.reloptions,
tablespace_t.spcname AS tablespace_name,
pg_catalog.obj_description(c.oid, 'pg_class') AS index_comment,
indexes.${postgresVersion === 10 ? 'indnatts' : 'indnkeyatts'} AS number_of_keys,
pg_catalog.pg_get_expr(indpred, indrelid) AS where_expression,
CASE
Expand Down Expand Up @@ -71,7 +73,8 @@ const getGET_TABLE_INDEXES = postgresVersion => {
reloptions,
number_of_keys,
where_expression,
tablespace_name;
tablespace_name,
index_comment;
`;
};

Expand Down