From a35f32961e99d101130a711d52a86365869327d6 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 28 Feb 2024 17:37:13 +0200 Subject: [PATCH 1/8] - started to work on index alter script --- .../alterScript/alterScriptFromDeltaHelper.js | 2 +- .../alterScriptHelpers/alterEntityHelper.js | 2 +- .../entityHelpers/indexesHelper.js | 166 ++++++++++++++++++ .../alterScript/types/AlterCollectionDto.js | 10 ++ .../alterScript/types/AlterIndexDto.js | 82 +++++++++ .../ddlProvider/ddlProvider.js | 28 +++ forward_engineering/utils/general.js | 9 +- 7 files changed, 295 insertions(+), 4 deletions(-) create mode 100644 forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js create mode 100644 forward_engineering/alterScript/types/AlterIndexDto.js diff --git a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js index 7a1f9e5..36ac18e 100644 --- a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js +++ b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js @@ -102,7 +102,7 @@ const getAlterCollectionsScriptDtos = ({ .concat(collection.properties?.entities?.properties?.modified?.items) .filter(Boolean) .map(item => Object.values(item.properties)[0]) - .flatMap(getModifyCollectionScriptDtos(app)); + .flatMap(getModifyCollectionScriptDtos({app, dbVersion})); const addColumnScriptDtos = [] .concat(collection.properties?.entities?.properties?.added?.items) .filter(Boolean) diff --git a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js index fd85bdf..057807b 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js @@ -75,7 +75,7 @@ const getDeleteCollectionScriptDto = app => collection => { /** * @return {(collection: AlterCollectionDto) => AlterScriptDto[]} * */ -const getModifyCollectionScriptDtos = (app) => (collection) => { +const getModifyCollectionScriptDtos = ({app, dbVersion}) => (collection) => { const _ = app.require('lodash'); const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app); diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js new file mode 100644 index 0000000..16d3a30 --- /dev/null +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js @@ -0,0 +1,166 @@ +const {AlterCollectionDto} = require('../../types/AlterCollectionDto'); +const {AlterIndexDto} = require('../../types/AlterIndexDto'); +const {AlterScriptDto} = require("../../types/AlterScriptDto"); + +/** + * @typedef {{ + * oldIndex: AlterIndexDto, + * newIndex: AlterIndexDto, + * }} ModifiedIndexDto + * */ + +/** + * @return {({ columnId: string, collection: AlterCollectionDto }) => string | undefined} + * */ +const getColumnNameById = ({_}) => ({columnId, collection}) => { + const nameToSchemaInProperties = _.toPairs(collection.role.properties || {}) + .find(([fieldName, fieldJsonSchema]) => { + return fieldJsonSchema.GUID === columnId; + }); + if (nameToSchemaInProperties?.length) { + return nameToSchemaInProperties[0]; + } + return undefined; +} + +/** + * @return {({ index: AlterIndexDto, collection: AlterCollectionDto }) => Object} + * */ +const mapIndexToFeIndexDto = ({_}) => ({ + index, collection, + }) => { + const {getSchemaNameFromCollection} = require('../../../utils/general')(_); + + const schemaName = getSchemaNameFromCollection(collection); + + const columnsWithNamesSet = (index.columns || []) + .map(indexedColumn => { + return { + ...indexedColumn, name: getColumnNameById({_})({columnId: indexedColumn.keyId, collection}), + } + }) + .filter(indexedColumn => Boolean(indexedColumn.name)); + + return { + ...index, schemaName, columns: columnsWithNamesSet, + } +} + +/** + * @return {({ oldIndex: AlterIndexDto, newIndex: AlterIndexDto }) => boolean} + * */ +const wasIndexModified = ({_}) => ({oldIndex, newIndex}) => { + if (oldIndex.id === newIndex.id) { + return _.isEqual(newIndex, oldIndex); + } + // Case for removing and re-creating an index + if (oldIndex.indxName === newIndex.indxName) { + const oldIndexNoId = { + ...oldIndex, id: undefined, + }; + const newIndexNoId = { + ...newIndex, id: undefined, + }; + return _.isEqual(newIndexNoId, oldIndexNoId); + } + // It means "old index" and "new index" dtos describe 2 different indexes + // Hence the "new" index was not modified + return false; +} + +/** + * @param oldIndex {AlterIndexDto} + * @param newIndex {AlterIndexDto} + * @return {boolean} + * */ +const areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex = ({oldIndex, newIndex}) => { + return (oldIndex.id === newIndex.id) || (oldIndex.indxName === oldIndex.indxName); +} + +/** + * @return {({ + * collection: AlterCollectionDto, + * additionalDataForDdlProvider: Object, + * }) => Array} + * */ +const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { + const { dbData, tableName, isParentActivated, } = additionalDataForDdlProvider; + + const newIndexes = collection?.role?.compMod?.Indxs?.new || []; + const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; + + return newIndexes + .filter(newIndex => { + const correspondingOldIndex = oldIndexes.find(oldIndex => areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex({ + oldIndex, newIndex + })); + return !Boolean(correspondingOldIndex); + }) + .map(newIndex => mapIndexToFeIndexDto({_})({index: newIndex, collection})) + .map(newIndex => { + const script = ddlProvider.createIndex(tableName, newIndex, dbData, isParentActivated); + const isIndexActivated = newIndex.isActivated && isParentActivated; + return AlterScriptDto.getInstance([script], isIndexActivated, false); + }) + .filter(Boolean); +} + +/** + * @return {({ + * collection: AlterCollectionDto, + * additionalDataForDdlProvider: Object, + * }) => Array} + * */ +const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { + const { dbData, tableName, isParentActivated, } = additionalDataForDdlProvider; + + const newIndexes = collection?.role?.compMod?.Indxs?.new || []; + const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; + + return oldIndexes + .filter(oldIndex => { + const correspondingNewIndex = newIndexes.find(newIndex => areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex({ + oldIndex, newIndex + })); + return !Boolean(correspondingNewIndex); + }); +} + +/** + * @return {({ collection: AlterCollectionDto }) => Array} + * */ +const getModifiedIndexes = ({_}) => ({collection}) => { + const newIndexes = collection?.role?.compMod?.Indxs?.new || []; + const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; + + return newIndexes + .map(newIndex => { + const correspondingOldIndex = oldIndexes.find(oldIndex => areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex({ + oldIndex, newIndex + })); + if (correspondingOldIndex) { + return { + newIndex, + oldIndex: correspondingOldIndex, + }; + } + return undefined; + }) + .filter(Boolean); +} + +/** + * @return {({ collection: AlterCollectionDto, dbVersion: string }) => Array} + * */ +const getModifyIndexesScriptDtos = ({_, ddlProvider}) => ({collection, dbVersion}) => { + const additionalDataForDdlProvider = { + dbData: {dbVersion}, + tableName: collection?.compMod?.collectionName?.new || '', + isParentActivated: collection.isActivated, + } + + return [] + .filter(Boolean); +} + +module.exports = {} diff --git a/forward_engineering/alterScript/types/AlterCollectionDto.js b/forward_engineering/alterScript/types/AlterCollectionDto.js index 61c237d..33f4a8f 100644 --- a/forward_engineering/alterScript/types/AlterCollectionDto.js +++ b/forward_engineering/alterScript/types/AlterCollectionDto.js @@ -1,3 +1,5 @@ +const {AlterIndexDto} = require('./AlterIndexDto'); + class ColumnCompModField { /** @@ -253,6 +255,14 @@ class AlterCollectionRoleCompModDto { * }>} */ newProperties + + /** + * @type {{ + * new: Array, + * old: Array, + * }} + * */ + Indxs } class AlterCollectionRoleDto { diff --git a/forward_engineering/alterScript/types/AlterIndexDto.js b/forward_engineering/alterScript/types/AlterIndexDto.js new file mode 100644 index 0000000..7f7c20d --- /dev/null +++ b/forward_engineering/alterScript/types/AlterIndexDto.js @@ -0,0 +1,82 @@ +class AlterIndexColumnDto { + + /** + * @type {any | undefined} + */ + sortOrder + + /** + * @type {any | undefined} + */ + nullsOrder + + /** + * @type {boolean} + */ + isActivated + + /** + * @type {any | undefined} + */ + keyId + + /** + * @type {string | undefined} + */ + collation + + /** + * @type {string | undefined} + */ + opclass +} + + +class AlterIndexDto { + + /** + * @type {string} + */ + id + + /** + * @type {boolean} + */ + isActivated + + /** + * @type {string} + */ + index_method + + /** + * @type {boolean} + */ + ifNotExist + + /** + * @type {boolean} + */ + only + + /** + * @type {string} + */ + index_tablespace_name + + /** + * @type {string} + */ + indxName + + /** + * @type {Array | undefined} + * */ + columns + +} + +module.exports = { + AlterIndexDto, + AlterIndexColumnDto +} diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index ee7a7c4..6f86efb 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -289,6 +289,34 @@ module.exports = (baseProvider, options, app) => { ); }, + /** + * @param tableName {string} + * @param dbData {{ + * dbVersion: string, + * }} + * @param isParentActivated {boolean} + * @param index {{ + * unique?: boolean, + * index_method?: string, + * indxName?: string, + * schemaName?: string, + * concurrently?: boolean, + * ifNotExist?: boolean, + * only?: boolean, + * nullsDistinct?: string, + * columns?: Array<{ + * sortOrder?: any, + * nullsOrder?: any, + * isActivated?: boolean, + * name: string, + * collation?: string, + * opclass?: string, + * }>, + * isActivated?: boolean, + * }} + * + * @return {string} + * */ createIndex(tableName, index, dbData, isParentActivated = true) { const isUnique = index.unique && index.index_method === 'btree'; const name = wrapInQuotes(index.indxName); diff --git a/forward_engineering/utils/general.js b/forward_engineering/utils/general.js index 08ec368..b2db776 100644 --- a/forward_engineering/utils/general.js +++ b/forward_engineering/utils/general.js @@ -125,10 +125,14 @@ module.exports = _ => { const getFullTableName = (collection) => { const collectionSchema = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; const tableName = getEntityName(collectionSchema); - const schemaName = collectionSchema.compMod?.keyspaceName; + const schemaName = getSchemaNameFromCollection(collection); return getNamePrefixedWithSchemaName(tableName, schemaName); } + const getSchemaNameFromCollection = ({collection}) => { + return collection.compMod?.keyspaceName; + } + const getFullColumnName = (collection, columnName) => { const {wrapInQuotes} = require('../utils/general')(_); @@ -269,6 +273,7 @@ module.exports = _ => { getColumnsList, getViewData, getSchemaOfAlterCollection, - getFullCollectionName + getFullCollectionName, + getSchemaNameFromCollection, }; }; From 35e010904737f0777a4ca2bf2b1ab724d17c6d86 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 29 Feb 2024 16:16:29 +0200 Subject: [PATCH 2/8] - Added support for adding indexes with Alter Script --- .../alterScriptHelpers/alterEntityHelper.js | 5 +- .../entityHelpers/indexesHelper.js | 86 +++++++++++++++---- .../alterScript/types/AlterIndexDto.js | 16 +++- .../ddlProvider/ddlProvider.js | 13 ++- forward_engineering/ddlProvider/templates.js | 2 + forward_engineering/utils/general.js | 2 +- 6 files changed, 103 insertions(+), 21 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js index 057807b..068f664 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/alterEntityHelper.js @@ -7,6 +7,7 @@ const {getRenameColumnScriptDtos} = require("./columnHelpers/renameColumnHelper" const {AlterScriptDto} = require("../types/AlterScriptDto"); const {AlterCollectionDto} = require('../types/AlterCollectionDto'); const {getModifyPkConstraintsScriptDtos} = require("./entityHelpers/primaryKeyHelper"); +const {getModifyIndexesScriptDtos} = require("./entityHelpers/indexesHelper"); /** @@ -82,10 +83,12 @@ const getModifyCollectionScriptDtos = ({app, dbVersion}) => (collection) => { const modifyCheckConstraintScriptDtos = getModifyCheckConstraintScriptDtos(_, ddlProvider)(collection); const modifyCommentScriptDtos = getModifyEntityCommentsScriptDtos(_, ddlProvider)(collection); const modifyPKConstraintDtos = getModifyPkConstraintsScriptDtos(_, ddlProvider)(collection); + const modifyIndexesScriptDtos = getModifyIndexesScriptDtos({ _, ddlProvider })({ collection, dbVersion }); return [ ...modifyCheckConstraintScriptDtos, ...modifyCommentScriptDtos, - ...modifyPKConstraintDtos + ...modifyPKConstraintDtos, + ...modifyIndexesScriptDtos, ].filter(Boolean); } diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js index 16d3a30..fd43dad 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js @@ -23,6 +23,22 @@ const getColumnNameById = ({_}) => ({columnId, collection}) => { return undefined; } +/** + * @return {({ + * columns?: Array<{keyId: string}>, + * collection: AlterCollectionDto + * }) => Object} + * */ +const setNamesToColumns = ({_}) => ({columns, collection}) => { + return (columns || []) + .map(column => { + return { + ...column, name: getColumnNameById({_})({columnId: column.keyId, collection}), + } + }) + .filter(column => Boolean(column.name)); +} + /** * @return {({ index: AlterIndexDto, collection: AlterCollectionDto }) => Object} * */ @@ -31,18 +47,23 @@ const mapIndexToFeIndexDto = ({_}) => ({ }) => { const {getSchemaNameFromCollection} = require('../../../utils/general')(_); - const schemaName = getSchemaNameFromCollection(collection); + const schemaName = getSchemaNameFromCollection({collection}); - const columnsWithNamesSet = (index.columns || []) - .map(indexedColumn => { - return { - ...indexedColumn, name: getColumnNameById({_})({columnId: indexedColumn.keyId, collection}), - } - }) - .filter(indexedColumn => Boolean(indexedColumn.name)); + const columnsWithNamesSet = setNamesToColumns({_})({ + columns: index.columns, + collection + }); + + const includeColumnsWithNameSet = setNamesToColumns({_})({ + columns: index.include, + collection + }); return { - ...index, schemaName, columns: columnsWithNamesSet, + ...index, + schemaName, + columns: columnsWithNamesSet, + include: includeColumnsWithNameSet, } } @@ -81,11 +102,12 @@ const areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex = ({oldIndex, newI * @return {({ * collection: AlterCollectionDto, * additionalDataForDdlProvider: Object, - * }) => Array} + * }) => Array} * */ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { - const { dbData, tableName, isParentActivated, } = additionalDataForDdlProvider; + const {getNamePrefixedWithSchemaName} = require('../../../utils/general')(_); + const {dbData, tableName, isParentActivated, schemaName} = additionalDataForDdlProvider; const newIndexes = collection?.role?.compMod?.Indxs?.new || []; const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; @@ -98,7 +120,15 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional }) .map(newIndex => mapIndexToFeIndexDto({_})({index: newIndex, collection})) .map(newIndex => { - const script = ddlProvider.createIndex(tableName, newIndex, dbData, isParentActivated); + const fullIndexName = getNamePrefixedWithSchemaName(newIndex.indxName, schemaName); + const fullTableName = getNamePrefixedWithSchemaName(tableName, schemaName); + + const newIndexWithFullName = { + ...newIndex, + indxName: fullIndexName, + } + + const script = ddlProvider.createIndex(fullTableName, newIndexWithFullName, dbData, isParentActivated); const isIndexActivated = newIndex.isActivated && isParentActivated; return AlterScriptDto.getInstance([script], isIndexActivated, false); }) @@ -109,11 +139,12 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional * @return {({ * collection: AlterCollectionDto, * additionalDataForDdlProvider: Object, - * }) => Array} + * }) => Array} * */ const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { - const { dbData, tableName, isParentActivated, } = additionalDataForDdlProvider; + const {getNamePrefixedWithSchemaName} = require('../../../utils/general')(_); + const {schemaName, isParentActivated} = additionalDataForDdlProvider; const newIndexes = collection?.role?.compMod?.Indxs?.new || []; const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; @@ -123,7 +154,14 @@ const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, addition oldIndex, newIndex })); return !Boolean(correspondingNewIndex); - }); + }) + .map(oldIndex => { + const fullIndexName = getNamePrefixedWithSchemaName(oldIndex.indxName, schemaName); + const script = ddlProvider.dropIndex({indexName: fullIndexName}); + const isIndexActivated = oldIndex.isActivated && isParentActivated; + return AlterScriptDto.getInstance([script], isIndexActivated, true); + }) + .filter(Boolean); } /** @@ -153,14 +191,28 @@ const getModifiedIndexes = ({_}) => ({collection}) => { * @return {({ collection: AlterCollectionDto, dbVersion: string }) => Array} * */ const getModifyIndexesScriptDtos = ({_, ddlProvider}) => ({collection, dbVersion}) => { + const {getSchemaNameFromCollection} = require('../../../utils/general')(_); const additionalDataForDdlProvider = { dbData: {dbVersion}, tableName: collection?.compMod?.collectionName?.new || '', + schemaName: getSchemaNameFromCollection({collection}) || '', isParentActivated: collection.isActivated, } - return [] + const deletedIndexesScriptDtos = getDeletedIndexesScriptDtos({_, ddlProvider})({ + collection, additionalDataForDdlProvider + }); + const addedIndexesScriptDtos = getAddedIndexesScriptDtos({_, ddlProvider})({ + collection, additionalDataForDdlProvider + }) + + return [ + ...deletedIndexesScriptDtos, + ...addedIndexesScriptDtos, + ] .filter(Boolean); } -module.exports = {} +module.exports = { + getModifyIndexesScriptDtos, +} diff --git a/forward_engineering/alterScript/types/AlterIndexDto.js b/forward_engineering/alterScript/types/AlterIndexDto.js index 7f7c20d..7215553 100644 --- a/forward_engineering/alterScript/types/AlterIndexDto.js +++ b/forward_engineering/alterScript/types/AlterIndexDto.js @@ -31,6 +31,14 @@ class AlterIndexColumnDto { opclass } +class AlterIndexIncludeColumnDto { + + /** + * @type {string} + * */ + keyId + +} class AlterIndexDto { @@ -74,9 +82,15 @@ class AlterIndexDto { * */ columns + /** + * @type {Array | undefined} + * */ + include + } module.exports = { AlterIndexDto, - AlterIndexColumnDto + AlterIndexColumnDto, + AlterIndexIncludeColumnDto, } diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 6f86efb..ccc4fee 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -1224,6 +1224,17 @@ module.exports = (baseProvider, options, app) => { createSchemaSequences({ schemaName, sequences }) { return getSequencesScript(schemaName, sequences); - } + }, + + /** + * @param indexName {string} + * @return {string} + * */ + dropIndex({ indexName }) { + const templatesConfig = { + indexName + }; + return assignTemplates(templates.dropIndex, templatesConfig); + }, }; }; diff --git a/forward_engineering/ddlProvider/templates.js b/forward_engineering/ddlProvider/templates.js index 0c4654d..fb0b5e6 100644 --- a/forward_engineering/ddlProvider/templates.js +++ b/forward_engineering/ddlProvider/templates.js @@ -79,6 +79,8 @@ module.exports = { 'CREATE${unique} INDEX${concurrently}${ifNotExist} ${name}\n' + ' ON${only} ${tableName}${using}${keys}${nullsDistinct}${options};\n', + dropIndex: 'DROP INDEX IF EXISTS ${indexName};', + createView: 'CREATE${orReplace}${temporary} VIEW ${name}${withOptions}\nAS ${selectStatement}${checkOption};\n\n${comment}\n', diff --git a/forward_engineering/utils/general.js b/forward_engineering/utils/general.js index b2db776..8137ae5 100644 --- a/forward_engineering/utils/general.js +++ b/forward_engineering/utils/general.js @@ -125,7 +125,7 @@ module.exports = _ => { const getFullTableName = (collection) => { const collectionSchema = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; const tableName = getEntityName(collectionSchema); - const schemaName = getSchemaNameFromCollection(collection); + const schemaName = getSchemaNameFromCollection({collection}); return getNamePrefixedWithSchemaName(tableName, schemaName); } From f1d13938c3b676ae4d121d0244c6fb7489b5222a Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 29 Feb 2024 16:26:43 +0200 Subject: [PATCH 3/8] - Fixed a problem with INCLUDE clause when adding indexes - Added ability to delete indexes --- .../alterScriptHelpers/entityHelpers/indexesHelper.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js index fd43dad..c216e84 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js @@ -121,14 +121,12 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional .map(newIndex => mapIndexToFeIndexDto({_})({index: newIndex, collection})) .map(newIndex => { const fullIndexName = getNamePrefixedWithSchemaName(newIndex.indxName, schemaName); - const fullTableName = getNamePrefixedWithSchemaName(tableName, schemaName); - const newIndexWithFullName = { ...newIndex, indxName: fullIndexName, } - const script = ddlProvider.createIndex(fullTableName, newIndexWithFullName, dbData, isParentActivated); + const script = ddlProvider.createIndex(tableName, newIndexWithFullName, dbData, isParentActivated); const isIndexActivated = newIndex.isActivated && isParentActivated; return AlterScriptDto.getInstance([script], isIndexActivated, false); }) @@ -142,7 +140,7 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional * }) => Array} * */ const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { - const {getNamePrefixedWithSchemaName} = require('../../../utils/general')(_); + const {getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../../utils/general')(_); const {schemaName, isParentActivated} = additionalDataForDdlProvider; const newIndexes = collection?.role?.compMod?.Indxs?.new || []; @@ -157,7 +155,8 @@ const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, addition }) .map(oldIndex => { const fullIndexName = getNamePrefixedWithSchemaName(oldIndex.indxName, schemaName); - const script = ddlProvider.dropIndex({indexName: fullIndexName}); + const ddlIndexName = wrapInQuotes(fullIndexName); + const script = ddlProvider.dropIndex({indexName: ddlIndexName}); const isIndexActivated = oldIndex.isActivated && isParentActivated; return AlterScriptDto.getInstance([script], isIndexActivated, true); }) From 39a9f4608013c4c4551769f5dfc0efc55d575729 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 1 Mar 2024 14:38:18 +0200 Subject: [PATCH 4/8] - Added support for index drop-and-recreate modifications --- .../entityHelpers/indexesHelper.js | 149 +++++++++++++----- .../ddlProvider/ddlHelpers/indexHelper.js | 2 +- 2 files changed, 111 insertions(+), 40 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js index c216e84..c545ad7 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js @@ -67,26 +67,43 @@ const mapIndexToFeIndexDto = ({_}) => ({ } } +/** + * @return {({ + * oldIndex: AlterIndexDto, + * newIndex: AlterIndexDto, + * indexPropertiesToCompare: Array, + * }) => boolean} + * */ +const areIndexesDifferent = ({_}) => ({oldIndex, newIndex, indexPropertiesToCompare}) => { + const newIndexWithRelevantProperties = _.pick(newIndex, indexPropertiesToCompare); + const oldIndexWithRelevantProperties = _.pick(oldIndex, indexPropertiesToCompare); + return !_.isEqual(newIndexWithRelevantProperties, oldIndexWithRelevantProperties); +} + /** * @return {({ oldIndex: AlterIndexDto, newIndex: AlterIndexDto }) => boolean} * */ -const wasIndexModified = ({_}) => ({oldIndex, newIndex}) => { - if (oldIndex.id === newIndex.id) { - return _.isEqual(newIndex, oldIndex); - } - // Case for removing and re-creating an index - if (oldIndex.indxName === newIndex.indxName) { - const oldIndexNoId = { - ...oldIndex, id: undefined, - }; - const newIndexNoId = { - ...newIndex, id: undefined, - }; - return _.isEqual(newIndexNoId, oldIndexNoId); - } - // It means "old index" and "new index" dtos describe 2 different indexes - // Hence the "new" index was not modified - return false; +const shouldDropAndRecreateIndex = ({_}) => ({oldIndex, newIndex}) => { + const indexPropertiesToCompare = [ + 'index_method', + 'columns', + 'include', + 'nullsDistinct', + 'where', + ]; + return areIndexesDifferent({_})({ oldIndex, newIndex, indexPropertiesToCompare }); +} + +/** + * @return {({ oldIndex: AlterIndexDto, newIndex: AlterIndexDto }) => boolean} + * */ +const shouldAlterIndex = ({_}) => ({oldIndex, newIndex}) => { + const indexPropertiesToCompare = [ + 'indxName', + 'index_tablespace_name', + 'index_storage_parameter' + ]; + return areIndexesDifferent({_})({ oldIndex, newIndex, indexPropertiesToCompare }); } /** @@ -100,14 +117,35 @@ const areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex = ({oldIndex, newI /** * @return {({ + * index: AlterIndexDto, * collection: AlterCollectionDto, * additionalDataForDdlProvider: Object, - * }) => Array} + * }) => AlterScriptDto | undefined} * */ -const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { +const getCreateIndexScriptDto = ({ _, ddlProvider }) => ({ index, collection, additionalDataForDdlProvider }) => { const {getNamePrefixedWithSchemaName} = require('../../../utils/general')(_); const {dbData, tableName, isParentActivated, schemaName} = additionalDataForDdlProvider; + + const indexForFeScript = mapIndexToFeIndexDto({_})({index, collection}); + const fullIndexName = getNamePrefixedWithSchemaName(indexForFeScript.indxName, schemaName); + const indexWithFullName = { + ...indexForFeScript, + indxName: fullIndexName, + } + + const script = ddlProvider.createIndex(tableName, indexWithFullName, dbData, isParentActivated); + const isIndexActivated = indexForFeScript.isActivated && isParentActivated; + return AlterScriptDto.getInstance([script], isIndexActivated, false); +} + +/** + * @return {({ + * collection: AlterCollectionDto, + * additionalDataForDdlProvider: Object, + * }) => Array} + * */ +const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { const newIndexes = collection?.role?.compMod?.Indxs?.new || []; const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; @@ -118,21 +156,34 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional })); return !Boolean(correspondingOldIndex); }) - .map(newIndex => mapIndexToFeIndexDto({_})({index: newIndex, collection})) .map(newIndex => { - const fullIndexName = getNamePrefixedWithSchemaName(newIndex.indxName, schemaName); - const newIndexWithFullName = { - ...newIndex, - indxName: fullIndexName, - } - - const script = ddlProvider.createIndex(tableName, newIndexWithFullName, dbData, isParentActivated); - const isIndexActivated = newIndex.isActivated && isParentActivated; - return AlterScriptDto.getInstance([script], isIndexActivated, false); + return getCreateIndexScriptDto({ _, ddlProvider })({ + index: newIndex, + collection, + additionalDataForDdlProvider + }); }) .filter(Boolean); } +/** + * @return {({ + * index: AlterIndexDto, + * additionalDataForDdlProvider: Object, + * }) => AlterScriptDto | undefined} + * */ +const getDeleteIndexScriptDto = ({ _, ddlProvider }) => ({ index, additionalDataForDdlProvider }) => { + const {getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../../utils/general')(_); + + const {isParentActivated, schemaName} = additionalDataForDdlProvider; + + const fullIndexName = getNamePrefixedWithSchemaName(index.indxName, schemaName); + const ddlIndexName = wrapInQuotes(fullIndexName); + const script = ddlProvider.dropIndex({indexName: ddlIndexName}); + const isIndexActivated = index.isActivated && isParentActivated; + return AlterScriptDto.getInstance([script], isIndexActivated, true); +} + /** * @return {({ * collection: AlterCollectionDto, @@ -140,9 +191,6 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional * }) => Array} * */ const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { - const {getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../../utils/general')(_); - - const {schemaName, isParentActivated} = additionalDataForDdlProvider; const newIndexes = collection?.role?.compMod?.Indxs?.new || []; const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; @@ -154,19 +202,18 @@ const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, addition return !Boolean(correspondingNewIndex); }) .map(oldIndex => { - const fullIndexName = getNamePrefixedWithSchemaName(oldIndex.indxName, schemaName); - const ddlIndexName = wrapInQuotes(fullIndexName); - const script = ddlProvider.dropIndex({indexName: ddlIndexName}); - const isIndexActivated = oldIndex.isActivated && isParentActivated; - return AlterScriptDto.getInstance([script], isIndexActivated, true); + return getDeleteIndexScriptDto({_, ddlProvider})({ index: oldIndex, additionalDataForDdlProvider }); }) .filter(Boolean); } /** - * @return {({ collection: AlterCollectionDto }) => Array} + * @return {({ + * collection: AlterCollectionDto, + * additionalDataForDdlProvider: Object, + * }) => Array} * */ -const getModifiedIndexes = ({_}) => ({collection}) => { +const getModifiedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additionalDataForDdlProvider}) => { const newIndexes = collection?.role?.compMod?.Indxs?.new || []; const oldIndexes = collection?.role?.compMod?.Indxs?.old || []; @@ -183,6 +230,26 @@ const getModifiedIndexes = ({_}) => ({collection}) => { } return undefined; }) + .filter(Boolean) + .flatMap(({ newIndex, oldIndex }) => { + const shouldDropAndRecreate = shouldDropAndRecreateIndex({ _ })({ newIndex, oldIndex }); + if (shouldDropAndRecreate) { + const deleteIndexScriptDto = getDeleteIndexScriptDto({ _, ddlProvider })({ index: oldIndex, additionalDataForDdlProvider }); + const createIndexScriptDto = getCreateIndexScriptDto({ _, ddlProvider })({ + index: newIndex, + collection, + additionalDataForDdlProvider + }); + return [deleteIndexScriptDto, createIndexScriptDto]; + } + + const shouldAlter = shouldAlterIndex({_})({ oldIndex, newIndex }); + if (shouldAlter) { + return []; + } + + return []; + }) .filter(Boolean); } @@ -203,11 +270,15 @@ const getModifyIndexesScriptDtos = ({_, ddlProvider}) => ({collection, dbVersion }); const addedIndexesScriptDtos = getAddedIndexesScriptDtos({_, ddlProvider})({ collection, additionalDataForDdlProvider + }); + const modifyIndexesScriptDtos = getModifiedIndexesScriptDtos({ _, ddlProvider })({ + collection, additionalDataForDdlProvider }) return [ ...deletedIndexesScriptDtos, ...addedIndexesScriptDtos, + ...modifyIndexesScriptDtos, ] .filter(Boolean); } diff --git a/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js b/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js index 3c27e32..b24769f 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js @@ -61,7 +61,7 @@ module.exports = ({ _, wrapInQuotes, checkAllKeysDeactivated, getColumnsList }) return _.chain(config) .toPairs() .map(([keyInModel, postgresKey]) => { - const value = index.index_storage_parameter[keyInModel]; + const value = (index.index_storage_parameter || {})[keyInModel]; if (_.isNil(value) || value === '') { return; From ea97633621850970f912a3a071f3d4c57f2e20b9 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 1 Mar 2024 16:06:38 +0200 Subject: [PATCH 5/8] - Added support for index modifications with ALTER INDEX syntax --- .../entityHelpers/indexesHelper.js | 96 ++++++++++++++++--- .../alterScript/types/AlterIndexDto.js | 5 + .../ddlProvider/ddlHelpers/indexHelper.js | 1 + .../ddlProvider/ddlProvider.js | 54 ++++++++++- forward_engineering/ddlProvider/templates.js | 10 +- 5 files changed, 151 insertions(+), 15 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js index c545ad7..6caa2f4 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js @@ -91,7 +91,7 @@ const shouldDropAndRecreateIndex = ({_}) => ({oldIndex, newIndex}) => { 'nullsDistinct', 'where', ]; - return areIndexesDifferent({_})({ oldIndex, newIndex, indexPropertiesToCompare }); + return areIndexesDifferent({_})({oldIndex, newIndex, indexPropertiesToCompare}); } /** @@ -103,7 +103,7 @@ const shouldAlterIndex = ({_}) => ({oldIndex, newIndex}) => { 'index_tablespace_name', 'index_storage_parameter' ]; - return areIndexesDifferent({_})({ oldIndex, newIndex, indexPropertiesToCompare }); + return areIndexesDifferent({_})({oldIndex, newIndex, indexPropertiesToCompare}); } /** @@ -122,7 +122,7 @@ const areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex = ({oldIndex, newI * additionalDataForDdlProvider: Object, * }) => AlterScriptDto | undefined} * */ -const getCreateIndexScriptDto = ({ _, ddlProvider }) => ({ index, collection, additionalDataForDdlProvider }) => { +const getCreateIndexScriptDto = ({_, ddlProvider}) => ({index, collection, additionalDataForDdlProvider}) => { const {getNamePrefixedWithSchemaName} = require('../../../utils/general')(_); const {dbData, tableName, isParentActivated, schemaName} = additionalDataForDdlProvider; @@ -157,7 +157,7 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional return !Boolean(correspondingOldIndex); }) .map(newIndex => { - return getCreateIndexScriptDto({ _, ddlProvider })({ + return getCreateIndexScriptDto({_, ddlProvider})({ index: newIndex, collection, additionalDataForDdlProvider @@ -172,7 +172,7 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional * additionalDataForDdlProvider: Object, * }) => AlterScriptDto | undefined} * */ -const getDeleteIndexScriptDto = ({ _, ddlProvider }) => ({ index, additionalDataForDdlProvider }) => { +const getDeleteIndexScriptDto = ({_, ddlProvider}) => ({index, additionalDataForDdlProvider}) => { const {getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../../utils/general')(_); const {isParentActivated, schemaName} = additionalDataForDdlProvider; @@ -202,11 +202,74 @@ const getDeletedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, addition return !Boolean(correspondingNewIndex); }) .map(oldIndex => { - return getDeleteIndexScriptDto({_, ddlProvider})({ index: oldIndex, additionalDataForDdlProvider }); + return getDeleteIndexScriptDto({_, ddlProvider})({index: oldIndex, additionalDataForDdlProvider}); }) .filter(Boolean); } +/** + * @return {({ + * additionalDataForDdlProvider: Object, + * newIndex: AlterIndexDto, + * oldIndex: AlterIndexDto, + * }) => Array} + * */ +const getAlterIndexScriptDtos = ({_, ddlProvider}) => ({ + newIndex, + oldIndex, + additionalDataForDdlProvider + }) => { + const {getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../../utils/general')(_); + const alterIndexScriptDtos = []; + + const {isParentActivated, schemaName} = additionalDataForDdlProvider; + const isNewIndexActivated = newIndex.isActivated && isParentActivated; + const fullNewIndexName = getNamePrefixedWithSchemaName(newIndex.indxName, schemaName); + const newIndexNameForDdl = wrapInQuotes(fullNewIndexName); + + const shouldRename = !_.isEqual(newIndex.indxName, oldIndex.indxName); + if (shouldRename) { + const fullOldIndexName = getNamePrefixedWithSchemaName(oldIndex.indxName, schemaName); + const oldIndexNameForDdl = wrapInQuotes(fullOldIndexName); + + const script = ddlProvider.alterIndexRename({ + oldIndexName: oldIndexNameForDdl, + newIndexName: newIndexNameForDdl, + }); + const renameScriptDto = AlterScriptDto.getInstance([script], isNewIndexActivated, false); + alterIndexScriptDtos.push(renameScriptDto); + } + + const shouldUpdateTablespace = !_.isEqual(newIndex.index_tablespace_name, oldIndex.index_tablespace_name); + if (shouldUpdateTablespace) { + const script = ddlProvider.alterIndexTablespace({ + indexName: newIndexNameForDdl, + tablespaceName: newIndex.index_tablespace_name, + }); + const changeTablespaceScriptDto = AlterScriptDto.getInstance([script], isNewIndexActivated, false); + alterIndexScriptDtos.push(changeTablespaceScriptDto); + } + + const shouldUpdateStorageParams = !_.isEqual(newIndex.index_storage_parameter, oldIndex.index_storage_parameter); + if (shouldUpdateStorageParams) { + const updateStorageParamsScript = ddlProvider.alterIndexStorageParams({ + indexName: newIndexNameForDdl, + index: newIndex, + }); + const updateStorageParamsScriptDto = AlterScriptDto.getInstance([updateStorageParamsScript], isNewIndexActivated, false); + alterIndexScriptDtos.push(updateStorageParamsScriptDto); + + const reindexScript = ddlProvider.reindexIndex({ + indexName: newIndexNameForDdl, + }); + const reindexScriptDto = AlterScriptDto.getInstance([reindexScript], isNewIndexActivated, false); + alterIndexScriptDtos.push(reindexScriptDto); + } + + return alterIndexScriptDtos + .filter(Boolean); +}; + /** * @return {({ * collection: AlterCollectionDto, @@ -231,11 +294,14 @@ const getModifiedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additio return undefined; }) .filter(Boolean) - .flatMap(({ newIndex, oldIndex }) => { - const shouldDropAndRecreate = shouldDropAndRecreateIndex({ _ })({ newIndex, oldIndex }); + .flatMap(({newIndex, oldIndex}) => { + const shouldDropAndRecreate = shouldDropAndRecreateIndex({_})({newIndex, oldIndex}); if (shouldDropAndRecreate) { - const deleteIndexScriptDto = getDeleteIndexScriptDto({ _, ddlProvider })({ index: oldIndex, additionalDataForDdlProvider }); - const createIndexScriptDto = getCreateIndexScriptDto({ _, ddlProvider })({ + const deleteIndexScriptDto = getDeleteIndexScriptDto({_, ddlProvider})({ + index: oldIndex, + additionalDataForDdlProvider + }); + const createIndexScriptDto = getCreateIndexScriptDto({_, ddlProvider})({ index: newIndex, collection, additionalDataForDdlProvider @@ -243,9 +309,13 @@ const getModifiedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additio return [deleteIndexScriptDto, createIndexScriptDto]; } - const shouldAlter = shouldAlterIndex({_})({ oldIndex, newIndex }); + const shouldAlter = shouldAlterIndex({_})({oldIndex, newIndex}); if (shouldAlter) { - return []; + return getAlterIndexScriptDtos({_, ddlProvider})({ + oldIndex, + newIndex, + additionalDataForDdlProvider, + }); } return []; @@ -271,7 +341,7 @@ const getModifyIndexesScriptDtos = ({_, ddlProvider}) => ({collection, dbVersion const addedIndexesScriptDtos = getAddedIndexesScriptDtos({_, ddlProvider})({ collection, additionalDataForDdlProvider }); - const modifyIndexesScriptDtos = getModifiedIndexesScriptDtos({ _, ddlProvider })({ + const modifyIndexesScriptDtos = getModifiedIndexesScriptDtos({_, ddlProvider})({ collection, additionalDataForDdlProvider }) diff --git a/forward_engineering/alterScript/types/AlterIndexDto.js b/forward_engineering/alterScript/types/AlterIndexDto.js index 7215553..00a80a1 100644 --- a/forward_engineering/alterScript/types/AlterIndexDto.js +++ b/forward_engineering/alterScript/types/AlterIndexDto.js @@ -87,6 +87,11 @@ class AlterIndexDto { * */ include + /** + * @type {Object} + * */ + index_storage_parameter + } module.exports = { diff --git a/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js b/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js index b24769f..a194bbc 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/indexHelper.js @@ -85,5 +85,6 @@ module.exports = ({ _, wrapInQuotes, checkAllKeysDeactivated, getColumnsList }) return { getIndexKeys, getIndexOptions, + getWithOptions, }; }; diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index ccc4fee..a3cf20e 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -1,6 +1,7 @@ const defaultTypes = require('../configs/defaultTypes'); const descriptors = require('../configs/descriptors'); const templates = require('./templates'); +const assignTemplates = require("../utils/assignTemplates"); module.exports = (baseProvider, options, app) => { @@ -78,7 +79,7 @@ module.exports = (baseProvider, options, app) => { wrapComment, }); - const { getIndexKeys, getIndexOptions } = require('./ddlHelpers/indexHelper')({ + const { getIndexKeys, getIndexOptions, getWithOptions } = require('./ddlHelpers/indexHelper')({ _, wrapInQuotes, checkAllKeysDeactivated, @@ -1236,5 +1237,56 @@ module.exports = (baseProvider, options, app) => { }; return assignTemplates(templates.dropIndex, templatesConfig); }, + + /** + * @param oldIndexName {string} + * @param newIndexName {string} + * @return {string} + * */ + alterIndexRename({ oldIndexName, newIndexName }) { + const templatesConfig = { + oldIndexName, newIndexName, + }; + return assignTemplates(templates.alterIndexRename, templatesConfig); + }, + + /** + * @param indexName {string} + * @param tablespaceName {string} + * @return {string} + * */ + alterIndexTablespace({ indexName, tablespaceName }) { + const templatesConfig = { + indexName, + tablespaceName, + }; + return assignTemplates(templates.alterIndexTablespace, templatesConfig); + }, + + /** + * @param indexName {string} + * @param index {Object} + * @return {string} + * */ + alterIndexStorageParams({ indexName, index }) { + const ddlIndexStorageParameters = getWithOptions(index); + const templatesConfig = { + indexName, + options: ddlIndexStorageParameters, + } + return assignTemplates(templates.alterIndexStorageParams, templatesConfig); + }, + + /** + * @param indexName {string} + * @return {string} + * */ + reindexIndex({ indexName }){ + const templatesConfig = { + indexName, + } + return assignTemplates(templates.reindexIndex, templatesConfig); + }, + }; }; diff --git a/forward_engineering/ddlProvider/templates.js b/forward_engineering/ddlProvider/templates.js index fb0b5e6..5a680ab 100644 --- a/forward_engineering/ddlProvider/templates.js +++ b/forward_engineering/ddlProvider/templates.js @@ -79,7 +79,15 @@ module.exports = { 'CREATE${unique} INDEX${concurrently}${ifNotExist} ${name}\n' + ' ON${only} ${tableName}${using}${keys}${nullsDistinct}${options};\n', - dropIndex: 'DROP INDEX IF EXISTS ${indexName};', + dropIndex: 'DROP INDEX IF EXISTS ${indexName};\n', + + alterIndexRename: 'ALTER INDEX IF EXISTS ${oldIndexName} RENAME TO ${newIndexName};\n', + + alterIndexTablespace: 'ALTER INDEX IF EXISTS ${indexName} SET TABLESPACE ${tablespaceName};\n', + + alterIndexStorageParams: 'ALTER INDEX IF EXISTS ${indexName} SET (\n\t${options}\n);\n', + + reindexIndex: 'REINDEX INDEX ${indexName};\n', createView: 'CREATE${orReplace}${temporary} VIEW ${name}${withOptions}\nAS ${selectStatement}${checkOption};\n\n${comment}\n', From 8c396ffaf94056512e0ee8171d1125c942890a45 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 1 Mar 2024 16:07:09 +0200 Subject: [PATCH 6/8] - Removed unused import --- forward_engineering/ddlProvider/ddlProvider.js | 1 - 1 file changed, 1 deletion(-) diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index a3cf20e..04072fb 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -1,7 +1,6 @@ const defaultTypes = require('../configs/defaultTypes'); const descriptors = require('../configs/descriptors'); const templates = require('./templates'); -const assignTemplates = require("../utils/assignTemplates"); module.exports = (baseProvider, options, app) => { From 07d7723d2ba6ae28d35e79341d1d84943f37de22 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 4 Mar 2024 13:05:04 +0200 Subject: [PATCH 7/8] - Fixed a lot of issues with index name formatting --- .../entityHelpers/indexesHelper.js | 36 +++++++------------ .../ddlProvider/ddlProvider.js | 34 +++++++++++++----- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js index 6caa2f4..be739cb 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/indexesHelper.js @@ -123,18 +123,11 @@ const areOldIndexDtoAndNewIndexDtoDescribingSameDatabaseIndex = ({oldIndex, newI * }) => AlterScriptDto | undefined} * */ const getCreateIndexScriptDto = ({_, ddlProvider}) => ({index, collection, additionalDataForDdlProvider}) => { - const {getNamePrefixedWithSchemaName} = require('../../../utils/general')(_); - - const {dbData, tableName, isParentActivated, schemaName} = additionalDataForDdlProvider; + const {dbData, tableName, isParentActivated} = additionalDataForDdlProvider; const indexForFeScript = mapIndexToFeIndexDto({_})({index, collection}); - const fullIndexName = getNamePrefixedWithSchemaName(indexForFeScript.indxName, schemaName); - const indexWithFullName = { - ...indexForFeScript, - indxName: fullIndexName, - } - const script = ddlProvider.createIndex(tableName, indexWithFullName, dbData, isParentActivated); + const script = ddlProvider.createIndex(tableName, indexForFeScript, dbData, isParentActivated); const isIndexActivated = indexForFeScript.isActivated && isParentActivated; return AlterScriptDto.getInstance([script], isIndexActivated, false); } @@ -173,13 +166,12 @@ const getAddedIndexesScriptDtos = ({_, ddlProvider}) => ({collection, additional * }) => AlterScriptDto | undefined} * */ const getDeleteIndexScriptDto = ({_, ddlProvider}) => ({index, additionalDataForDdlProvider}) => { - const {getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../../utils/general')(_); + const {getNamePrefixedWithSchemaName} = require('../../../utils/general')(_); const {isParentActivated, schemaName} = additionalDataForDdlProvider; const fullIndexName = getNamePrefixedWithSchemaName(index.indxName, schemaName); - const ddlIndexName = wrapInQuotes(fullIndexName); - const script = ddlProvider.dropIndex({indexName: ddlIndexName}); + const script = ddlProvider.dropIndex({indexName: fullIndexName}); const isIndexActivated = index.isActivated && isParentActivated; return AlterScriptDto.getInstance([script], isIndexActivated, true); } @@ -219,22 +211,17 @@ const getAlterIndexScriptDtos = ({_, ddlProvider}) => ({ oldIndex, additionalDataForDdlProvider }) => { - const {getNamePrefixedWithSchemaName, wrapInQuotes} = require('../../../utils/general')(_); const alterIndexScriptDtos = []; const {isParentActivated, schemaName} = additionalDataForDdlProvider; const isNewIndexActivated = newIndex.isActivated && isParentActivated; - const fullNewIndexName = getNamePrefixedWithSchemaName(newIndex.indxName, schemaName); - const newIndexNameForDdl = wrapInQuotes(fullNewIndexName); const shouldRename = !_.isEqual(newIndex.indxName, oldIndex.indxName); if (shouldRename) { - const fullOldIndexName = getNamePrefixedWithSchemaName(oldIndex.indxName, schemaName); - const oldIndexNameForDdl = wrapInQuotes(fullOldIndexName); - const script = ddlProvider.alterIndexRename({ - oldIndexName: oldIndexNameForDdl, - newIndexName: newIndexNameForDdl, + schemaName, + oldIndexName: oldIndex.indxName, + newIndexName: newIndex.indxName, }); const renameScriptDto = AlterScriptDto.getInstance([script], isNewIndexActivated, false); alterIndexScriptDtos.push(renameScriptDto); @@ -243,7 +230,8 @@ const getAlterIndexScriptDtos = ({_, ddlProvider}) => ({ const shouldUpdateTablespace = !_.isEqual(newIndex.index_tablespace_name, oldIndex.index_tablespace_name); if (shouldUpdateTablespace) { const script = ddlProvider.alterIndexTablespace({ - indexName: newIndexNameForDdl, + schemaName, + indexName: newIndex.indxName, tablespaceName: newIndex.index_tablespace_name, }); const changeTablespaceScriptDto = AlterScriptDto.getInstance([script], isNewIndexActivated, false); @@ -253,14 +241,16 @@ const getAlterIndexScriptDtos = ({_, ddlProvider}) => ({ const shouldUpdateStorageParams = !_.isEqual(newIndex.index_storage_parameter, oldIndex.index_storage_parameter); if (shouldUpdateStorageParams) { const updateStorageParamsScript = ddlProvider.alterIndexStorageParams({ - indexName: newIndexNameForDdl, + schemaName, + indexName: newIndex.indxName, index: newIndex, }); const updateStorageParamsScriptDto = AlterScriptDto.getInstance([updateStorageParamsScript], isNewIndexActivated, false); alterIndexScriptDtos.push(updateStorageParamsScriptDto); const reindexScript = ddlProvider.reindexIndex({ - indexName: newIndexNameForDdl, + schemaName, + indexName: newIndex.indxName, }); const reindexScriptDto = AlterScriptDto.getInstance([reindexScript], isNewIndexActivated, false); alterIndexScriptDtos.push(reindexScriptDto); diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 04072fb..b328922 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -1238,51 +1238,69 @@ module.exports = (baseProvider, options, app) => { }, /** + * @param schemaName {string} * @param oldIndexName {string} * @param newIndexName {string} * @return {string} * */ - alterIndexRename({ oldIndexName, newIndexName }) { + alterIndexRename({ schemaName, oldIndexName, newIndexName }) { + const ddlSchemaName = wrapInQuotes(schemaName); + const ddlOldIndexName = getNamePrefixedWithSchemaName(wrapInQuotes(oldIndexName), ddlSchemaName); + const ddlNewIndexName = wrapInQuotes(newIndexName); + const templatesConfig = { - oldIndexName, newIndexName, + oldIndexName: ddlOldIndexName, + newIndexName: ddlNewIndexName, }; return assignTemplates(templates.alterIndexRename, templatesConfig); }, /** + * @param schemaName {string} * @param indexName {string} * @param tablespaceName {string} * @return {string} * */ - alterIndexTablespace({ indexName, tablespaceName }) { + alterIndexTablespace({ schemaName, indexName, tablespaceName }) { + const ddlSchemaName = wrapInQuotes(schemaName); + const ddlIndexName = getNamePrefixedWithSchemaName(wrapInQuotes(indexName), ddlSchemaName); + const templatesConfig = { - indexName, + indexName: ddlIndexName, tablespaceName, }; return assignTemplates(templates.alterIndexTablespace, templatesConfig); }, /** + * @param schemaName {string} * @param indexName {string} * @param index {Object} * @return {string} * */ - alterIndexStorageParams({ indexName, index }) { + alterIndexStorageParams({ schemaName, indexName, index }) { + const ddlSchemaName = wrapInQuotes(schemaName); + const ddlIndexName = getNamePrefixedWithSchemaName(wrapInQuotes(indexName), ddlSchemaName); + const ddlIndexStorageParameters = getWithOptions(index); const templatesConfig = { - indexName, + indexName: ddlIndexName, options: ddlIndexStorageParameters, } return assignTemplates(templates.alterIndexStorageParams, templatesConfig); }, /** + * @param schemaName {string} * @param indexName {string} * @return {string} * */ - reindexIndex({ indexName }){ + reindexIndex({ schemaName, indexName }){ + const ddlSchemaName = wrapInQuotes(schemaName); + const ddlIndexName = getNamePrefixedWithSchemaName(wrapInQuotes(indexName), ddlSchemaName); + const templatesConfig = { - indexName, + indexName: ddlIndexName, } return assignTemplates(templates.reindexIndex, templatesConfig); }, From 2833af1e90df3515ad1c79f98bf29523c521022e Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 Mar 2024 12:44:08 +0200 Subject: [PATCH 8/8] - Fixed a regression with generating names for DDL --- forward_engineering/utils/general.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forward_engineering/utils/general.js b/forward_engineering/utils/general.js index 8137ae5..769f1ac 100644 --- a/forward_engineering/utils/general.js +++ b/forward_engineering/utils/general.js @@ -125,7 +125,7 @@ module.exports = _ => { const getFullTableName = (collection) => { const collectionSchema = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; const tableName = getEntityName(collectionSchema); - const schemaName = getSchemaNameFromCollection({collection}); + const schemaName = getSchemaNameFromCollection({collection: collectionSchema}); return getNamePrefixedWithSchemaName(tableName, schemaName); }