diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js index 487219a..19bb315 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js @@ -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'); @@ -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 @@ -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); }; diff --git a/forward_engineering/alterScript/types/AlterIndexDto.js b/forward_engineering/alterScript/types/AlterIndexDto.js index 513ac43..3d1a65e 100644 --- a/forward_engineering/alterScript/types/AlterIndexDto.js +++ b/forward_engineering/alterScript/types/AlterIndexDto.js @@ -87,6 +87,11 @@ class AlterIndexDto { * @type {Object} * */ index_storage_parameter; + + /** + * @type {string | undefined} + */ + indexComment; } module.exports = { diff --git a/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js b/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js index e18b3c5..f9135d5 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js @@ -6,6 +6,7 @@ const { getNamePrefixedWithSchemaName, getDbVersion, commentIfDeactivated, + wrapComment, } = require('../../utils/general'); const assignTemplates = require('../../utils/assignTemplates'); const templates = require('../templates'); @@ -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 }); @@ -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, + }); }; /** @@ -168,6 +189,7 @@ const dropIndex = ({ indexName }) => { module.exports = { createIndex, dropIndex, + getCommentOnIndexStatement, getIndexKeys, getIndexOptions, getWithOptions, diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 1199741..236569e 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -274,6 +274,7 @@ module.exports = (baseProvider, options, app) => { * ifNotExist?: boolean, * only?: boolean, * nullsDistinct?: string, + * indexComment?: string, * columns?: Array<{ * sortOrder?: any, * nullsOrder?: any, diff --git a/reverse_engineering/helpers/postgresHelpers/tableHelper.js b/reverse_engineering/helpers/postgresHelpers/tableHelper.js index eeecc8e..6c6667a 100644 --- a/reverse_engineering/helpers/postgresHelpers/tableHelper.js +++ b/reverse_engineering/helpers/postgresHelpers/tableHelper.js @@ -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); diff --git a/reverse_engineering/helpers/queryConstants.js b/reverse_engineering/helpers/queryConstants.js index 9635b87..135f418 100644 --- a/reverse_engineering/helpers/queryConstants.js +++ b/reverse_engineering/helpers/queryConstants.js @@ -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, @@ -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 @@ -71,7 +73,8 @@ const getGET_TABLE_INDEXES = postgresVersion => { reloptions, number_of_keys, where_expression, - tablespace_name; + tablespace_name, + index_comment; `; };