From 561b7eaa19b342d21e336baa1dec05c28c88e063 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Wed, 28 Feb 2024 15:03:44 +0200 Subject: [PATCH 01/11] RE: add handling of schema sequences --- reverse_engineering/api.js | 6 +++-- .../helpers/postgresHelpers/sequenceHelper.js | 27 +++++++++++++++++++ .../helpers/postgresService.js | 8 +++++- reverse_engineering/helpers/queryConstants.js | 19 +++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 reverse_engineering/helpers/postgresHelpers/sequenceHelper.js diff --git a/reverse_engineering/api.js b/reverse_engineering/api.js index 5550bb4..c6b394a 100644 --- a/reverse_engineering/api.js +++ b/reverse_engineering/api.js @@ -132,7 +132,7 @@ module.exports = { data.includePartitions, data.ignoreUdfUdpTriggers, ); - const { functions, procedures, triggers } = await postgresService.retrieveSchemaLevelData( + const { functions, procedures, triggers, sequences } = await postgresService.retrieveSchemaLevelData( schemaName, data.ignoreUdfUdpTriggers, ); @@ -147,6 +147,7 @@ module.exports = { procedures, modelDefinitions, triggers, + sequences, }; }), ) @@ -156,11 +157,12 @@ module.exports = { .flat(); const packages = schemaData.flatMap( - ({ schemaName, tables, views, functions, procedures, triggers, modelDefinitions }) => { + ({ schemaName, tables, views, functions, procedures, triggers, modelDefinitions, sequences }) => { const bucketInfo = { UDFs: functions, Procedures: procedures, triggers, + sequences, }; const tablePackages = tables diff --git a/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js new file mode 100644 index 0000000..db51ff0 --- /dev/null +++ b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js @@ -0,0 +1,27 @@ + + +const getOwnedByColumn = (sequence) => { + if (!sequence.column_name) { + return []; + } + return [{ entity: sequence.table_name, name: sequence.column_name }]; +}; + +const mapSequenceData = (sequence) => { + return { + sequenceName: sequence.sequence_name, + increment: sequence.increment, + start: sequence.start_value, + dataType: sequence.data_type, + maxValue: sequence.maximum_value, + minValue: sequence.minimum_value, + cycle: sequence.cycle_option === 'YES', + ownedByColumn: getOwnedByColumn(sequence), + ownedByNone: !sequence.column_name, + + }; +}; + +module.exports = { + mapSequenceData, +}; diff --git a/reverse_engineering/helpers/postgresService.js b/reverse_engineering/helpers/postgresService.js index 460bf50..16cd3e8 100644 --- a/reverse_engineering/helpers/postgresService.js +++ b/reverse_engineering/helpers/postgresService.js @@ -46,6 +46,7 @@ const { const { setDependencies: setDependenciesInTriggerHelper, getTriggers } = require('./postgresHelpers/triggerHelper'); const queryConstants = require('./queryConstants'); const { reorganizeConstraints } = require('./postgresHelpers/reorganizeConstraints'); +const { mapSequenceData } = require('./postgresHelpers/sequenceHelper'); let currentSshTunnel = null; let _ = null; @@ -223,7 +224,12 @@ module.exports = { return mapProcedureData(functionData, functionArgs, additionalData); }); - return { functions: userDefinedFunctions, procedures: userDefinedProcedures }; + const sequencesData = await db.queryTolerant(queryConstants.GET_SEQUENCES, [ + schemaName, + ]); + const sequences = sequencesData.map(mapSequenceData); + + return { functions: userDefinedFunctions, procedures: userDefinedProcedures, sequences }; }, async _retrieveUserDefinedTypes(schemaName) { diff --git a/reverse_engineering/helpers/queryConstants.js b/reverse_engineering/helpers/queryConstants.js index d9634c9..3a69e9d 100644 --- a/reverse_engineering/helpers/queryConstants.js +++ b/reverse_engineering/helpers/queryConstants.js @@ -394,6 +394,25 @@ const queryConstants = { LEFT JOIN pg_class AS inher_child ON (inher_child.oid = pg_inherits.inhrelid) LEFT JOIN pg_class AS inher_parent ON (inher_parent.oid = pg_inherits.inhparent) WHERE inher_parent.relnamespace = $1;`, + GET_SEQUENCES: ` + SELECT + sequence_name, + data_type, + start_value, + minimum_value, + maximum_value, + "increment", + cycle_option, + inner_pg_class.relname AS table_name, + pg_attribute.attname AS column_name + FROM information_schema."sequences" + JOIN pg_class ON pg_class.relname = information_schema."sequences".sequence_name + JOIN pg_depend ON pg_depend.objid = pg_class.oid + LEFT JOIN pg_class AS inner_pg_class ON pg_depend.refobjid = inner_pg_class.oid + LEFT JOIN pg_attribute ON (pg_depend.refobjid, pg_depend.refobjsubid) = (pg_attribute.attrelid, pg_attribute.attnum) + WHERE pg_class.relkind = 'S' + AND information_schema."sequences".sequence_schema = $1; + `, }; const getQueryName = query => { From fc62f777f7778c5775fcfc41983fa5d690161679 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Wed, 28 Feb 2024 15:04:19 +0200 Subject: [PATCH 02/11] FE: add alter script generation for modified schema sequences --- .../alterScript/alterScriptFromDeltaHelper.js | 36 +++++++ .../containerHelpers/sequencesHelper.js | 47 +++++++++ .../ddlProvider/ddlHelpers/sequenceHelper.js | 99 ++++++++++++++++--- .../ddlProvider/ddlProvider.js | 18 +++- forward_engineering/ddlProvider/templates.js | 16 ++- forward_engineering/utils/general.js | 26 ++++- 6 files changed, 221 insertions(+), 21 deletions(-) create mode 100644 forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js diff --git a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js index 7a1f9e5..e80a77c 100644 --- a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js +++ b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js @@ -31,6 +31,7 @@ const { const {AlterScriptDto, ModificationScript} = require("./types/AlterScriptDto"); const {App, CoreData} = require("../types/coreApplicationTypes"); const {InternalDefinitions, ModelDefinitions, ExternalDefinitions} = require("../types/coreApplicationDataTypes"); +const { getModifyContainerSequencesScriptDtos } = require('./alterScriptHelpers/containerHelpers/sequencesHelper'); /** @@ -300,6 +301,39 @@ const prettifyAlterScriptDto = (dto) => { } } +/** + * @param {{ + * collection: Object, + * app: App, + * }} dto + * @return {AlterScriptDto[]} + * */ +const getAlterContainersSequencesScriptDtos = ({collection, app}) => { + const addedContainers = collection.properties?.containers?.properties?.added?.items; + const deletedContainers = collection.properties?.containers?.properties?.deleted?.items; + const modifiedContainers = collection.properties?.containers?.properties?.modified?.items; + + const addContainersSequencesScriptDtos = [] + .concat(addedContainers) + .filter(Boolean) + .map(container => getAddContainerScriptDto(app)(Object.keys(container.properties)[0])); + const deleteContainersScriptDtos = [] + .concat(deletedContainers) + .filter(Boolean) + .map(container => getDeleteContainerScriptDto(app)(Object.keys(container.properties)[0])); + const modifyContainersScriptDtos = [] + .concat(modifiedContainers) + .filter(Boolean) + .map(container => Object.values(container.properties)[0]) + .flatMap(container => getModifyContainerSequencesScriptDtos(app)(container)) + + return [ + ...addContainersSequencesScriptDtos, + ...deleteContainersScriptDtos, + ...modifyContainersScriptDtos, + ].filter(Boolean); +}; + /** * @param data {CoreData} * @param app {App} @@ -336,11 +370,13 @@ const getAlterScriptDtos = (data, app) => { externalDefinitions, }); const relationshipScriptDtos = getAlterRelationshipsScriptDtos({collection, app}); + const containersSequencesScriptDtos = getAlterContainersSequencesScriptDtos({collection, app}); return [ ...containersScriptDtos, ...modelDefinitionsScriptDtos, ...collectionsScriptDtos, + ...containersSequencesScriptDtos, ...viewScriptDtos, ...relationshipScriptDtos, ] diff --git a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js new file mode 100644 index 0000000..21e052b --- /dev/null +++ b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js @@ -0,0 +1,47 @@ +const {AlterScriptDto} = require("../../types/AlterScriptDto"); +const sequencesCompModKey = 'sequences'; + +/** + * @return {(data: Object) => Array} + * */ +const getModifyContainerSequencesScriptDtos = (app) => (data) => { + const _ = app.require('lodash'); + const ddlProvider = require('../../../ddlProvider')(null, null, app); + const { + getDbName, + getGroupItemsByCompMode, + } = require('../../../utils/general')(_); + + + const schemaName = getDbName([data.role]); + const sequencesCompMod = data?.role?.compMod?.[sequencesCompModKey] || {}; + const {new: newItems = [], old: oldItems = []} = sequencesCompMod; + + const {removed, added, modified} = getGroupItemsByCompMode({ newItems, oldItems }); + + const removedScriptDtos = removed + .map(sequence => { + return ddlProvider.dropSchemaSequence({schemaName, sequence}); + }) + .map(script => AlterScriptDto.getInstance([script], true, true)); + const addedScriptDtos = added + .map(sequence => ddlProvider.createSchemaSequence({schemaName, sequence})) + .map(script => AlterScriptDto.getInstance([script], true, false)); + + const modifiedScriptDtos = modified + .map(sequence => { + const oldSequence = _.find(oldItems, { id: sequence.id }) || {}; + return ddlProvider.alterSchemaSequence({schemaName, sequence, oldSequence }) + }) + .map(script => AlterScriptDto.getInstance([script], true, false)); + + return [ + ...modifiedScriptDtos, + ...removedScriptDtos, + ...addedScriptDtos, + ].filter(Boolean); +}; + +module.exports = { + getModifyContainerSequencesScriptDtos, +} diff --git a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js index c393f07..079d1b4 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js @@ -36,22 +36,70 @@ module.exports = ({ * @returns {string} */ const getSequencesScript = (schemaName, sequences) => { - return _.map(sequences, (sequence) => { - const sequenceSchemaName = sequence.temporary ? '' : schemaName; - const name = getNamePrefixedWithSchemaName( - sequence.sequenceName, - sequenceSchemaName - ); - const ifNotExists = getIfNotExists(sequence); - const sequenceType = getSequenceType(sequence); - const options = getSequenceOptions(sequence); - return assignTemplates(templates.createSequence, { - name, - ifNotExists, - sequenceType, - options, - }); - }).join('\n'); + return _.map(sequences, (sequence) => createSequenceScript(schemaName, sequence)).join('\n'); + }; + + /** + * @param {string} schemaName + * @param {Sequence} sequence + * @returns {string} + */ + const createSequenceScript = (schemaName, sequence) => { + const sequenceSchemaName = sequence.temporary ? '' : schemaName; + const name = getNamePrefixedWithSchemaName( + sequence.sequenceName, + sequenceSchemaName + ); + const ifNotExists = getIfNotExists(sequence); + const sequenceType = getSequenceType(sequence); + const options = getSequenceOptions(sequence); + + return assignTemplates(templates.createSequence, { + name, + ifNotExists, + sequenceType, + options, + }); + }; + + const alterSequenceScript = (schemaName, sequence, oldSequence) => { + const sequenceSchemaName = sequence.temporary ? '' : schemaName; + const sequenceName = oldSequence.sequenceName || sequence.sequenceName; + const name = getNamePrefixedWithSchemaName( + sequenceName, + sequenceSchemaName + ); + const modifiedSequence = _.omitBy(sequence, (value, key) => _.isEqual(value, oldSequence[key])); + const options = getSequenceOptions(modifiedSequence); + const sequenceType = getAlterSequenceType(modifiedSequence); + const newName = modifiedSequence.sequenceName; + /** + * @type {Array} + */ + const configs = [ + { key: 'options', value: options, template: templates.alterSequence }, + { key: 'sequenceType', value: sequenceType, template: templates.setSequenceType }, + { key: 'newName', value: newName, template: templates.renameSequence } + ]; + + return configs.filter(config => config.value).map(config => assignTemplates(config.template, { [config.key]: config.value, name })).join('\n'); + }; + + /** + * @param {string} schemaName + * @param {Sequence} sequence + * @returns {string} + */ + const dropSequenceScript = (schemaName, sequence) => { + const sequenceSchemaName = sequence.temporary ? '' : schemaName; + const name = getNamePrefixedWithSchemaName( + sequence.sequenceName, + sequenceSchemaName + ); + + return assignTemplates(templates.dropSequence, { + name, + }); }; /** @@ -130,6 +178,22 @@ module.exports = ({ return ''; }; + /** + * @param {Sequence} sequence + * @returns {string} + */ + const getAlterSequenceType = (sequence) => { + if (sequence.unlogged === true) { + return 'UNLOGGED'; + } + + if (sequence.unlogged === false) { + return 'LOGGED'; + } + + return ''; + }; + /** * @param {{ sequence: Sequence }} param0 * @returns {string} @@ -171,5 +235,8 @@ module.exports = ({ return { getSequencesScript, + createSequenceScript, + dropSequenceScript, + alterSequenceScript, }; }; diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index ee7a7c4..48b6d14 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -56,7 +56,7 @@ module.exports = (baseProvider, options, app) => { getNamePrefixedWithSchemaName, }); - const { getSequencesScript } = require('./ddlHelpers/sequenceHelper')({ + const { getSequencesScript, createSequenceScript, dropSequenceScript, alterSequenceScript } = require('./ddlHelpers/sequenceHelper')({ _, templates, assignTemplates, @@ -1196,6 +1196,18 @@ module.exports = (baseProvider, options, app) => { createSchemaSequences({ schemaName, sequences }) { return getSequencesScript(schemaName, sequences); - } - }; + }, + + createSchemaSequence({ schemaName, sequence }) { + return createSequenceScript(schemaName, sequence); + }, + + dropSchemaSequence({ schemaName, sequence }) { + return dropSequenceScript(schemaName, sequence); + }, + + alterSchemaSequence({ schemaName, sequence, oldSequence }) { + return alterSequenceScript(schemaName, sequence, oldSequence); + } +}; }; diff --git a/forward_engineering/ddlProvider/templates.js b/forward_engineering/ddlProvider/templates.js index 0c4654d..32b760e 100644 --- a/forward_engineering/ddlProvider/templates.js +++ b/forward_engineering/ddlProvider/templates.js @@ -114,5 +114,19 @@ module.exports = { createSequence: 'CREATE${sequenceType} SEQUENCE${ifNotExists} ${name}' + '${options}' + - ';\n' + ';\n', + + dropSequence: + 'DROP SEQUENCE IF EXISTS ${name};\n', + + renameSequence: + 'ALTER SEQUENCE IF EXISTS ${name} RENAME TO ${newName};\n', + + setSequenceType: + 'ALTER SEQUENCE IF EXISTS ${name} SET ${sequenceType};\n', + + alterSequence: + 'ALTER SEQUENCE IF EXISTS ${name}' + + '${options}' + + ';\n', }; diff --git a/forward_engineering/utils/general.js b/forward_engineering/utils/general.js index 08ec368..97ea70f 100644 --- a/forward_engineering/utils/general.js +++ b/forward_engineering/utils/general.js @@ -242,6 +242,29 @@ module.exports = _ => { ); }; + const getGroupItemsByCompMode = ({ newItems = [], oldItems = [] }) => { + const addedItems = newItems.filter(newItem => !oldItems.some(item => item.id === newItem.id)); + const removedItems = []; + const modifiedItems = []; + + oldItems.forEach(oldItem => { + const newItem = newItems.find(item => item.id === oldItem.id); + if (!newItem) { + removedItems.push(oldItem); + } else { + if (!_.isEqual(newItem, oldItem)) { + modifiedItems.push(newItem); + } + } + }); + + return { + added: addedItems, + removed: removedItems, + modified: modifiedItems, + }; +}; + return { getDbName, getDbData, @@ -269,6 +292,7 @@ module.exports = _ => { getColumnsList, getViewData, getSchemaOfAlterCollection, - getFullCollectionName + getFullCollectionName, + getGroupItemsByCompMode, }; }; From e328c1bbfa0d657a566d515a52fec424b72c1890 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Wed, 28 Feb 2024 15:16:32 +0200 Subject: [PATCH 03/11] FE: FE: add alter script generation for deleted schema sequences --- .../alterScript/alterScriptFromDeltaHelper.js | 5 +- .../containerHelpers/sequencesHelper.js | 98 ++++++++++++------- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js index e80a77c..d99278a 100644 --- a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js +++ b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js @@ -31,7 +31,7 @@ const { const {AlterScriptDto, ModificationScript} = require("./types/AlterScriptDto"); const {App, CoreData} = require("../types/coreApplicationTypes"); const {InternalDefinitions, ModelDefinitions, ExternalDefinitions} = require("../types/coreApplicationDataTypes"); -const { getModifyContainerSequencesScriptDtos } = require('./alterScriptHelpers/containerHelpers/sequencesHelper'); +const { getModifyContainerSequencesScriptDtos, getDeleteContainerSequencesScriptDtos } = require('./alterScriptHelpers/containerHelpers/sequencesHelper'); /** @@ -320,7 +320,8 @@ const getAlterContainersSequencesScriptDtos = ({collection, app}) => { const deleteContainersScriptDtos = [] .concat(deletedContainers) .filter(Boolean) - .map(container => getDeleteContainerScriptDto(app)(Object.keys(container.properties)[0])); + .map(container => Object.values(container.properties)[0]) + .flatMap(container => getDeleteContainerSequencesScriptDtos(app)(container)) const modifyContainersScriptDtos = [] .concat(modifiedContainers) .filter(Boolean) diff --git a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js index 21e052b..babbd7f 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js @@ -1,47 +1,69 @@ -const {AlterScriptDto} = require("../../types/AlterScriptDto"); -const sequencesCompModKey = 'sequences'; +const { AlterScriptDto } = require("../../types/AlterScriptDto"); +const sequencesCompModKey = "sequences"; /** * @return {(data: Object) => Array} * */ const getModifyContainerSequencesScriptDtos = (app) => (data) => { - const _ = app.require('lodash'); - const ddlProvider = require('../../../ddlProvider')(null, null, app); - const { - getDbName, - getGroupItemsByCompMode, - } = require('../../../utils/general')(_); - - - const schemaName = getDbName([data.role]); - const sequencesCompMod = data?.role?.compMod?.[sequencesCompModKey] || {}; - const {new: newItems = [], old: oldItems = []} = sequencesCompMod; - - const {removed, added, modified} = getGroupItemsByCompMode({ newItems, oldItems }); - - const removedScriptDtos = removed - .map(sequence => { - return ddlProvider.dropSchemaSequence({schemaName, sequence}); - }) - .map(script => AlterScriptDto.getInstance([script], true, true)); - const addedScriptDtos = added - .map(sequence => ddlProvider.createSchemaSequence({schemaName, sequence})) - .map(script => AlterScriptDto.getInstance([script], true, false)); - - const modifiedScriptDtos = modified - .map(sequence => { - const oldSequence = _.find(oldItems, { id: sequence.id }) || {}; - return ddlProvider.alterSchemaSequence({schemaName, sequence, oldSequence }) - }) - .map(script => AlterScriptDto.getInstance([script], true, false)); - - return [ - ...modifiedScriptDtos, - ...removedScriptDtos, - ...addedScriptDtos, - ].filter(Boolean); + const _ = app.require("lodash"); + const ddlProvider = require("../../../ddlProvider")(null, null, app); + const { getDbName, getGroupItemsByCompMode } = + require("../../../utils/general")(_); + + const schemaName = getDbName([data.role]); + const sequencesCompMod = data.role?.compMod?.[sequencesCompModKey] || {}; + const { new: newItems = [], old: oldItems = [] } = sequencesCompMod; + + const { removed, added, modified } = getGroupItemsByCompMode({ + newItems, + oldItems, + }); + + const removedScriptDtos = removed + .map((sequence) => { + return ddlProvider.dropSchemaSequence({ schemaName, sequence }); + }) + .map((script) => AlterScriptDto.getInstance([script], true, true)); + const addedScriptDtos = added + .map((sequence) => + ddlProvider.createSchemaSequence({ schemaName, sequence }) + ) + .map((script) => AlterScriptDto.getInstance([script], true, false)); + + const modifiedScriptDtos = modified + .map((sequence) => { + const oldSequence = _.find(oldItems, { id: sequence.id }) || {}; + return ddlProvider.alterSchemaSequence({ + schemaName, + sequence, + oldSequence, + }); + }) + .map((script) => AlterScriptDto.getInstance([script], true, false)); + + return [ + ...modifiedScriptDtos, + ...removedScriptDtos, + ...addedScriptDtos, + ].filter(Boolean); +}; + +/** + * @return {(containerName: string) => AlterScriptDto | undefined} + * */ +const getDeleteContainerSequencesScriptDtos = (app) => (data) => { + const _ = app.require("lodash"); + const ddlProvider = require("../../../ddlProvider")(null, null, app); + const { getDbName } = require("../../../utils/general")(_); + const schemaName = getDbName([data.role]); + + return (data.role?.sequences || []) + .map((sequence) => ddlProvider.dropSchemaSequence({ schemaName, sequence })) + .map((script) => AlterScriptDto.getInstance([script], true, true)) + .filter(Boolean); }; module.exports = { getModifyContainerSequencesScriptDtos, -} + getDeleteContainerSequencesScriptDtos, +}; From 2bf9a341c1368abb87ba395de136c7729d1bff5f Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Wed, 28 Feb 2024 15:20:49 +0200 Subject: [PATCH 04/11] FE: FE: add alter script generation for added schema sequences --- .../alterScript/alterScriptFromDeltaHelper.js | 5 +++-- .../containerHelpers/sequencesHelper.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js index d99278a..3d7cf64 100644 --- a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js +++ b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js @@ -31,7 +31,7 @@ const { const {AlterScriptDto, ModificationScript} = require("./types/AlterScriptDto"); const {App, CoreData} = require("../types/coreApplicationTypes"); const {InternalDefinitions, ModelDefinitions, ExternalDefinitions} = require("../types/coreApplicationDataTypes"); -const { getModifyContainerSequencesScriptDtos, getDeleteContainerSequencesScriptDtos } = require('./alterScriptHelpers/containerHelpers/sequencesHelper'); +const { getModifyContainerSequencesScriptDtos, getDeleteContainerSequencesScriptDtos, getAddContainerSequencesScriptDtos } = require('./alterScriptHelpers/containerHelpers/sequencesHelper'); /** @@ -316,7 +316,8 @@ const getAlterContainersSequencesScriptDtos = ({collection, app}) => { const addContainersSequencesScriptDtos = [] .concat(addedContainers) .filter(Boolean) - .map(container => getAddContainerScriptDto(app)(Object.keys(container.properties)[0])); + .map(container => Object.values(container.properties)[0]) + .flatMap(container => getAddContainerSequencesScriptDtos(app)(container)) const deleteContainersScriptDtos = [] .concat(deletedContainers) .filter(Boolean) diff --git a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js index babbd7f..95cd2cd 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js @@ -1,6 +1,21 @@ const { AlterScriptDto } = require("../../types/AlterScriptDto"); const sequencesCompModKey = "sequences"; +/** + * @return {(containerName: string) => AlterScriptDto | undefined} + * */ +const getAddContainerSequencesScriptDtos = (app) => (data) => { + const _ = app.require("lodash"); + const ddlProvider = require("../../../ddlProvider")(null, null, app); + const { getDbName } = require("../../../utils/general")(_); + const schemaName = getDbName([data.role]); + + return (data.role?.sequences || []) + .map((sequence) => ddlProvider.createSchemaSequence({ schemaName, sequence })) + .map((script) => AlterScriptDto.getInstance([script], true, false)) + .filter(Boolean); +}; + /** * @return {(data: Object) => Array} * */ @@ -64,6 +79,7 @@ const getDeleteContainerSequencesScriptDtos = (app) => (data) => { }; module.exports = { + getAddContainerSequencesScriptDtos, getModifyContainerSequencesScriptDtos, getDeleteContainerSequencesScriptDtos, }; From a17801b15c911c271d783d3b4f98c42a175e200b Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Wed, 28 Feb 2024 17:15:09 +0200 Subject: [PATCH 05/11] FE: define types for sequence helper --- .../alterScript/alterScriptFromDeltaHelper.js | 2 +- .../containerHelpers/sequencesHelper.js | 124 +++++++++--------- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js index 3d7cf64..cc42277 100644 --- a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js +++ b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js @@ -308,7 +308,7 @@ const prettifyAlterScriptDto = (dto) => { * }} dto * @return {AlterScriptDto[]} * */ -const getAlterContainersSequencesScriptDtos = ({collection, app}) => { +const getAlterContainersSequencesScriptDtos = ({ collection, app }) => { const addedContainers = collection.properties?.containers?.properties?.added?.items; const deletedContainers = collection.properties?.containers?.properties?.deleted?.items; const modifiedContainers = collection.properties?.containers?.properties?.modified?.items; diff --git a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js index 95cd2cd..606b88e 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js @@ -1,85 +1,89 @@ -const { AlterScriptDto } = require("../../types/AlterScriptDto"); -const sequencesCompModKey = "sequences"; +const { AlterScriptDto } = require('../../types/AlterScriptDto'); +const { App } = require('../../../types/coreApplicationTypes'); +const sequencesCompModKey = 'sequences'; /** - * @return {(containerName: string) => AlterScriptDto | undefined} + * @param {App} app + * @return {(data: object) => AlterScriptDto[]} * */ const getAddContainerSequencesScriptDtos = (app) => (data) => { - const _ = app.require("lodash"); - const ddlProvider = require("../../../ddlProvider")(null, null, app); - const { getDbName } = require("../../../utils/general")(_); - const schemaName = getDbName([data.role]); + const _ = app.require('lodash'); + const ddlProvider = require('../../../ddlProvider')(null, null, app); + const { getDbName } = require('../../../utils/general')(_); + const schemaName = getDbName([data.role]); - return (data.role?.sequences || []) - .map((sequence) => ddlProvider.createSchemaSequence({ schemaName, sequence })) - .map((script) => AlterScriptDto.getInstance([script], true, false)) - .filter(Boolean); + return (data.role?.sequences || []) + .map((sequence) => ddlProvider.createSchemaSequence({ schemaName, sequence })) + .map((script) => AlterScriptDto.getInstance([script], true, false)) + .filter(Boolean); }; /** - * @return {(data: Object) => Array} + * @param {App} app + * @return {(data: object) => AlterScriptDto[]} * */ const getModifyContainerSequencesScriptDtos = (app) => (data) => { - const _ = app.require("lodash"); - const ddlProvider = require("../../../ddlProvider")(null, null, app); - const { getDbName, getGroupItemsByCompMode } = - require("../../../utils/general")(_); + const _ = app.require('lodash'); + const ddlProvider = require('../../../ddlProvider')(null, null, app); + const { getDbName, getGroupItemsByCompMode } = + require('../../../utils/general')(_); - const schemaName = getDbName([data.role]); - const sequencesCompMod = data.role?.compMod?.[sequencesCompModKey] || {}; - const { new: newItems = [], old: oldItems = [] } = sequencesCompMod; + const schemaName = getDbName([data.role]); + const sequencesCompMod = data.role?.compMod?.[sequencesCompModKey] || {}; + const { new: newItems = [], old: oldItems = [] } = sequencesCompMod; - const { removed, added, modified } = getGroupItemsByCompMode({ - newItems, - oldItems, - }); + const { removed, added, modified } = getGroupItemsByCompMode({ + newItems, + oldItems, + }); - const removedScriptDtos = removed - .map((sequence) => { - return ddlProvider.dropSchemaSequence({ schemaName, sequence }); - }) - .map((script) => AlterScriptDto.getInstance([script], true, true)); - const addedScriptDtos = added - .map((sequence) => - ddlProvider.createSchemaSequence({ schemaName, sequence }) - ) - .map((script) => AlterScriptDto.getInstance([script], true, false)); + const removedScriptDtos = removed + .map((sequence) => { + return ddlProvider.dropSchemaSequence({ schemaName, sequence }); + }) + .map((script) => AlterScriptDto.getInstance([script], true, true)); + const addedScriptDtos = added + .map((sequence) => + ddlProvider.createSchemaSequence({ schemaName, sequence }) + ) + .map((script) => AlterScriptDto.getInstance([script], true, false)); - const modifiedScriptDtos = modified - .map((sequence) => { - const oldSequence = _.find(oldItems, { id: sequence.id }) || {}; - return ddlProvider.alterSchemaSequence({ - schemaName, - sequence, - oldSequence, - }); - }) - .map((script) => AlterScriptDto.getInstance([script], true, false)); + const modifiedScriptDtos = modified + .map((sequence) => { + const oldSequence = _.find(oldItems, { id: sequence.id }) || {}; + return ddlProvider.alterSchemaSequence({ + schemaName, + sequence, + oldSequence, + }); + }) + .map((script) => AlterScriptDto.getInstance([script], true, false)); - return [ - ...modifiedScriptDtos, - ...removedScriptDtos, - ...addedScriptDtos, - ].filter(Boolean); + return [ + ...modifiedScriptDtos, + ...removedScriptDtos, + ...addedScriptDtos, + ].filter(Boolean); }; /** - * @return {(containerName: string) => AlterScriptDto | undefined} + * @param {App} app + * @return {(data: object) => AlterScriptDto[]} * */ const getDeleteContainerSequencesScriptDtos = (app) => (data) => { - const _ = app.require("lodash"); - const ddlProvider = require("../../../ddlProvider")(null, null, app); - const { getDbName } = require("../../../utils/general")(_); - const schemaName = getDbName([data.role]); + const _ = app.require('lodash'); + const ddlProvider = require('../../../ddlProvider')(null, null, app); + const { getDbName } = require('../../../utils/general')(_); + const schemaName = getDbName([data.role]); - return (data.role?.sequences || []) - .map((sequence) => ddlProvider.dropSchemaSequence({ schemaName, sequence })) - .map((script) => AlterScriptDto.getInstance([script], true, true)) - .filter(Boolean); + return (data.role?.sequences || []) + .map((sequence) => ddlProvider.dropSchemaSequence({ schemaName, sequence })) + .map((script) => AlterScriptDto.getInstance([script], true, true)) + .filter(Boolean); }; module.exports = { - getAddContainerSequencesScriptDtos, - getModifyContainerSequencesScriptDtos, - getDeleteContainerSequencesScriptDtos, + getAddContainerSequencesScriptDtos, + getModifyContainerSequencesScriptDtos, + getDeleteContainerSequencesScriptDtos, }; From 375be3de66228803d68f365853c56398197b5ea2 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Wed, 28 Feb 2024 17:16:09 +0200 Subject: [PATCH 06/11] FE: fix changing of sequence type --- forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js index 079d1b4..7f3f8a1 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js @@ -183,6 +183,10 @@ module.exports = ({ * @returns {string} */ const getAlterSequenceType = (sequence) => { + if (sequence.temporary) { + return ''; + } + if (sequence.unlogged === true) { return 'UNLOGGED'; } From 2d8a070de8a0044345f8edb639b19e8c8c4867ae Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Wed, 28 Feb 2024 17:31:58 +0200 Subject: [PATCH 07/11] FE: add schema name for sequence owned by option column name --- .../ddlProvider/ddlHelpers/sequenceHelper.js | 21 +++++++++---------- .../ddlProvider/ddlProvider.js | 1 + 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js index 7f3f8a1..6d30ffa 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js @@ -29,6 +29,7 @@ module.exports = ({ templates, assignTemplates, getNamePrefixedWithSchemaName, + wrapInQuotes, }) => { /** * @param {string} schemaName @@ -52,7 +53,7 @@ module.exports = ({ ); const ifNotExists = getIfNotExists(sequence); const sequenceType = getSequenceType(sequence); - const options = getSequenceOptions(sequence); + const options = getSequenceOptions(sequence, schemaName); return assignTemplates(templates.createSequence, { name, @@ -70,7 +71,7 @@ module.exports = ({ sequenceSchemaName ); const modifiedSequence = _.omitBy(sequence, (value, key) => _.isEqual(value, oldSequence[key])); - const options = getSequenceOptions(modifiedSequence); + const options = getSequenceOptions(modifiedSequence, schemaName); const sequenceType = getAlterSequenceType(modifiedSequence); const newName = modifiedSequence.sequenceName; /** @@ -103,10 +104,11 @@ module.exports = ({ }; /** - * @param {Sequence} sequence + * @param {Sequence} sequence + * @param {string} schemaName * @returns {string} */ - const getSequenceOptions = (sequence) => { + const getSequenceOptions = (sequence, schemaName) => { /** * @type {Array} */ @@ -122,7 +124,7 @@ module.exports = ({ ]; const options = optionConfigs - .map((config) => wrapOption(config.getOption({ sequence, config }))) + .map((config) => wrapOption(config.getOption({ sequence, schemaName, config }))) .filter(Boolean) .join(''); @@ -215,10 +217,10 @@ module.exports = ({ }; /** - * @param {{ sequence: Sequence }} param0 + * @param {{ sequence: Sequence, schemaName: string }} param0 * @returns {string} */ - const getOwnedBy = ({ sequence }) => { + const getOwnedBy = ({ sequence, schemaName }) => { if (sequence.ownedByNone) { return 'OWNED BY NONE'; } @@ -227,10 +229,7 @@ module.exports = ({ if (ownedColumn) { const [tableName, columnName] = ownedColumn.name?.split('.') || []; - const ownedColumnName = getNamePrefixedWithSchemaName( - columnName, - tableName - ); + const ownedColumnName = [schemaName, tableName, columnName].filter(Boolean).map(wrapInQuotes).join('.'); return `OWNED BY ${ownedColumnName}`; } diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 48b6d14..cca599b 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -61,6 +61,7 @@ module.exports = (baseProvider, options, app) => { templates, assignTemplates, getNamePrefixedWithSchemaName, + wrapInQuotes, }); const { getTableTemporaryValue, getTableOptions } = require('./ddlHelpers/tableHelper')({ From 5f104544817635eb97b2feabfc901fed35984496 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Thu, 29 Feb 2024 16:54:33 +0200 Subject: [PATCH 08/11] RE: fix duplicating of sequences in SELECT query --- reverse_engineering/helpers/queryConstants.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/reverse_engineering/helpers/queryConstants.js b/reverse_engineering/helpers/queryConstants.js index 3a69e9d..d232380 100644 --- a/reverse_engineering/helpers/queryConstants.js +++ b/reverse_engineering/helpers/queryConstants.js @@ -395,7 +395,7 @@ const queryConstants = { LEFT JOIN pg_class AS inher_parent ON (inher_parent.oid = pg_inherits.inhparent) WHERE inher_parent.relnamespace = $1;`, GET_SEQUENCES: ` - SELECT + SELECT DISTINCT ON (sequence_name) sequence_name, data_type, start_value, @@ -411,7 +411,8 @@ const queryConstants = { LEFT JOIN pg_class AS inner_pg_class ON pg_depend.refobjid = inner_pg_class.oid LEFT JOIN pg_attribute ON (pg_depend.refobjid, pg_depend.refobjsubid) = (pg_attribute.attrelid, pg_attribute.attnum) WHERE pg_class.relkind = 'S' - AND information_schema."sequences".sequence_schema = $1; + AND information_schema."sequences".sequence_schema = $1 + ORDER BY sequence_name, column_name; `, }; From 6c1487ab3ccc87477d240bb1f46dea91ee51768c Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Thu, 29 Feb 2024 17:18:16 +0200 Subject: [PATCH 09/11] RE: update passing arguments in functions --- .../alterScript/alterScriptFromDeltaHelper.js | 6 +- .../containerHelpers/sequencesHelper.js | 30 ++++---- .../ddlProvider/ddlHelpers/sequenceHelper.js | 69 ++++++++++--------- .../ddlProvider/ddlProvider.js | 8 +-- .../helpers/postgresHelpers/sequenceHelper.js | 4 +- .../helpers/postgresService.js | 2 +- 6 files changed, 62 insertions(+), 57 deletions(-) diff --git a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js index cc42277..77f4874 100644 --- a/forward_engineering/alterScript/alterScriptFromDeltaHelper.js +++ b/forward_engineering/alterScript/alterScriptFromDeltaHelper.js @@ -317,17 +317,17 @@ const getAlterContainersSequencesScriptDtos = ({ collection, app }) => { .concat(addedContainers) .filter(Boolean) .map(container => Object.values(container.properties)[0]) - .flatMap(container => getAddContainerSequencesScriptDtos(app)(container)) + .flatMap(container => getAddContainerSequencesScriptDtos({ app })({ container })) const deleteContainersScriptDtos = [] .concat(deletedContainers) .filter(Boolean) .map(container => Object.values(container.properties)[0]) - .flatMap(container => getDeleteContainerSequencesScriptDtos(app)(container)) + .flatMap(container => getDeleteContainerSequencesScriptDtos({ app })({ container })) const modifyContainersScriptDtos = [] .concat(modifiedContainers) .filter(Boolean) .map(container => Object.values(container.properties)[0]) - .flatMap(container => getModifyContainerSequencesScriptDtos(app)(container)) + .flatMap(container => getModifyContainerSequencesScriptDtos({ app })({ container })) return [ ...addContainersSequencesScriptDtos, diff --git a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js index 606b88e..338aed4 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js @@ -3,33 +3,33 @@ const { App } = require('../../../types/coreApplicationTypes'); const sequencesCompModKey = 'sequences'; /** - * @param {App} app - * @return {(data: object) => AlterScriptDto[]} + * @param {{ app: App }} + * @return {({ container }: { container: object }) => AlterScriptDto[]} * */ -const getAddContainerSequencesScriptDtos = (app) => (data) => { +const getAddContainerSequencesScriptDtos = ({ app }) => ({ container }) => { const _ = app.require('lodash'); const ddlProvider = require('../../../ddlProvider')(null, null, app); const { getDbName } = require('../../../utils/general')(_); - const schemaName = getDbName([data.role]); + const schemaName = getDbName([container.role]); - return (data.role?.sequences || []) + return (container.role?.sequences || []) .map((sequence) => ddlProvider.createSchemaSequence({ schemaName, sequence })) .map((script) => AlterScriptDto.getInstance([script], true, false)) .filter(Boolean); }; /** - * @param {App} app - * @return {(data: object) => AlterScriptDto[]} + * @param {{ app: App }} + * @return {({ container }: { container: object }) => AlterScriptDto[]} * */ -const getModifyContainerSequencesScriptDtos = (app) => (data) => { +const getModifyContainerSequencesScriptDtos = ({ app }) => ({ container }) => { const _ = app.require('lodash'); const ddlProvider = require('../../../ddlProvider')(null, null, app); const { getDbName, getGroupItemsByCompMode } = require('../../../utils/general')(_); - const schemaName = getDbName([data.role]); - const sequencesCompMod = data.role?.compMod?.[sequencesCompModKey] || {}; + const schemaName = getDbName([container.role]); + const sequencesCompMod = container.role?.compMod?.[sequencesCompModKey] || {}; const { new: newItems = [], old: oldItems = [] } = sequencesCompMod; const { removed, added, modified } = getGroupItemsByCompMode({ @@ -67,16 +67,16 @@ const getModifyContainerSequencesScriptDtos = (app) => (data) => { }; /** - * @param {App} app - * @return {(data: object) => AlterScriptDto[]} + * @param {{ app: App }} + * @return {({ container }: { container: object }) => AlterScriptDto[]} * */ -const getDeleteContainerSequencesScriptDtos = (app) => (data) => { +const getDeleteContainerSequencesScriptDtos = ({ app }) => ({ container }) => { const _ = app.require('lodash'); const ddlProvider = require('../../../ddlProvider')(null, null, app); const { getDbName } = require('../../../utils/general')(_); - const schemaName = getDbName([data.role]); + const schemaName = getDbName([container.role]); - return (data.role?.sequences || []) + return (container.role?.sequences || []) .map((sequence) => ddlProvider.dropSchemaSequence({ schemaName, sequence })) .map((script) => AlterScriptDto.getInstance([script], true, true)) .filter(Boolean); diff --git a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js index 6d30ffa..8ce53ff 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js @@ -31,29 +31,28 @@ module.exports = ({ getNamePrefixedWithSchemaName, wrapInQuotes, }) => { + /** - * @param {string} schemaName - * @param {Sequence[]} sequences + * @param {{ schemaName: string, sequences: Sequence[] }} * @returns {string} */ - const getSequencesScript = (schemaName, sequences) => { - return _.map(sequences, (sequence) => createSequenceScript(schemaName, sequence)).join('\n'); + const getSequencesScript = ({ schemaName, sequences }) => { + return _.map(sequences, (sequence) => createSequenceScript({ schemaName, sequence })).join('\n'); }; /** - * @param {string} schemaName - * @param {Sequence} sequence + * @param {{ schemaName: string, sequence: Sequence }} * @returns {string} */ - const createSequenceScript = (schemaName, sequence) => { + const createSequenceScript = ({ schemaName, sequence }) => { const sequenceSchemaName = sequence.temporary ? '' : schemaName; const name = getNamePrefixedWithSchemaName( sequence.sequenceName, sequenceSchemaName ); - const ifNotExists = getIfNotExists(sequence); - const sequenceType = getSequenceType(sequence); - const options = getSequenceOptions(sequence, schemaName); + const ifNotExists = getIfNotExists({ sequence }); + const sequenceType = getSequenceType({ sequence }); + const options = getSequenceOptions({ sequence, schemaName }); return assignTemplates(templates.createSequence, { name, @@ -63,7 +62,12 @@ module.exports = ({ }); }; - const alterSequenceScript = (schemaName, sequence, oldSequence) => { + /** + * + * @param {{ schemaName: string, sequence: Sequence, oldSequence: Sequence }} + * @returns {string} + */ + const alterSequenceScript = ({ schemaName, sequence, oldSequence }) => { const sequenceSchemaName = sequence.temporary ? '' : schemaName; const sequenceName = oldSequence.sequenceName || sequence.sequenceName; const name = getNamePrefixedWithSchemaName( @@ -71,8 +75,8 @@ module.exports = ({ sequenceSchemaName ); const modifiedSequence = _.omitBy(sequence, (value, key) => _.isEqual(value, oldSequence[key])); - const options = getSequenceOptions(modifiedSequence, schemaName); - const sequenceType = getAlterSequenceType(modifiedSequence); + const options = getSequenceOptions({ schemaName, sequence: modifiedSequence }); + const sequenceType = getAlterSequenceType({ sequence: modifiedSequence }); const newName = modifiedSequence.sequenceName; /** * @type {Array} @@ -87,11 +91,10 @@ module.exports = ({ }; /** - * @param {string} schemaName - * @param {Sequence} sequence + * @param {{ schemaName: string, sequence: Sequence }} * @returns {string} */ - const dropSequenceScript = (schemaName, sequence) => { + const dropSequenceScript = ({ schemaName, sequence }) => { const sequenceSchemaName = sequence.temporary ? '' : schemaName; const name = getNamePrefixedWithSchemaName( sequence.sequenceName, @@ -104,11 +107,10 @@ module.exports = ({ }; /** - * @param {Sequence} sequence - * @param {string} schemaName + * @param {{ schemaName: string, sequence: Sequence }} * @returns {string} */ - const getSequenceOptions = (sequence, schemaName) => { + const getSequenceOptions = ({ schemaName, sequence }) => { /** * @type {Array} */ @@ -124,11 +126,14 @@ module.exports = ({ ]; const options = optionConfigs - .map((config) => wrapOption(config.getOption({ sequence, schemaName, config }))) + .map((config) => { + const option = config.getOption({ sequence, schemaName, config }); + return wrapOption({ option }); + }) .filter(Boolean) .join(''); - return options ? wrapOptionsBlock(options) : options; + return options ? wrapOptionsBlock({ options }) : options; }; /** @@ -141,34 +146,34 @@ module.exports = ({ }; /** - * @param {string} option + * @param {{ option: string }} * @returns {string} */ - const wrapOption = (option) => { + const wrapOption = ({ option }) => { return option ? `\t${option}\n` : ''; }; /** - * @param {string} option + * @param {{ options: string }} * @returns {string} */ - const wrapOptionsBlock = (option) => { - return '\n' + option.replace(/\n$/, ''); + const wrapOptionsBlock = ({ options }) => { + return '\n' + options.replace(/\n$/, ''); }; /** - * @param {Sequence} sequence + * @param {{ sequence: Sequence }} * @returns {string} */ - const getIfNotExists = (sequence) => { + const getIfNotExists = ({ sequence }) => { return sequence.ifNotExist ? ' IF NOT EXISTS' : ''; }; /** - * @param {Sequence} sequence + * @param {{ sequence: Sequence }} * @returns {string} */ - const getSequenceType = (sequence) => { + const getSequenceType = ({ sequence }) => { if (sequence.temporary) { return ' TEMPORARY'; } @@ -181,10 +186,10 @@ module.exports = ({ }; /** - * @param {Sequence} sequence + * @param {{ sequence: Sequence }} * @returns {string} */ - const getAlterSequenceType = (sequence) => { + const getAlterSequenceType = ({ sequence }) => { if (sequence.temporary) { return ''; } diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index cca599b..f59f467 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -1196,19 +1196,19 @@ module.exports = (baseProvider, options, app) => { }, createSchemaSequences({ schemaName, sequences }) { - return getSequencesScript(schemaName, sequences); + return getSequencesScript({ schemaName, sequences }); }, createSchemaSequence({ schemaName, sequence }) { - return createSequenceScript(schemaName, sequence); + return createSequenceScript({ schemaName, sequence }); }, dropSchemaSequence({ schemaName, sequence }) { - return dropSequenceScript(schemaName, sequence); + return dropSequenceScript({ schemaName, sequence }); }, alterSchemaSequence({ schemaName, sequence, oldSequence }) { - return alterSequenceScript(schemaName, sequence, oldSequence); + return alterSequenceScript({ schemaName, sequence, oldSequence }); } }; }; diff --git a/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js index db51ff0..5af97ae 100644 --- a/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js +++ b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js @@ -1,13 +1,13 @@ -const getOwnedByColumn = (sequence) => { +const getOwnedByColumn = ({ sequence }) => { if (!sequence.column_name) { return []; } return [{ entity: sequence.table_name, name: sequence.column_name }]; }; -const mapSequenceData = (sequence) => { +const mapSequenceData = ({ sequence }) => { return { sequenceName: sequence.sequence_name, increment: sequence.increment, diff --git a/reverse_engineering/helpers/postgresService.js b/reverse_engineering/helpers/postgresService.js index 16cd3e8..323b8ab 100644 --- a/reverse_engineering/helpers/postgresService.js +++ b/reverse_engineering/helpers/postgresService.js @@ -227,7 +227,7 @@ module.exports = { const sequencesData = await db.queryTolerant(queryConstants.GET_SEQUENCES, [ schemaName, ]); - const sequences = sequencesData.map(mapSequenceData); + const sequences = sequencesData.map(sequence => mapSequenceData({ sequence })); return { functions: userDefinedFunctions, procedures: userDefinedProcedures, sequences }; }, From 0ce878c54cb02647449d9b5a2047294b80f190f2 Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Thu, 29 Feb 2024 17:55:08 +0200 Subject: [PATCH 10/11] Add missed types --- .../containerHelpers/sequencesHelper.js | 3 +- .../ddlProvider/ddlHelpers/sequenceHelper.js | 22 +--- .../ddlProvider/ddlProvider.js | 17 +++ .../types/schemaSequenceTypes.js | 118 ++++++++++++++++++ .../helpers/postgresHelpers/sequenceHelper.js | 9 ++ 5 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 forward_engineering/types/schemaSequenceTypes.js diff --git a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js index 338aed4..da35525 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/containerHelpers/sequencesHelper.js @@ -25,8 +25,7 @@ const getAddContainerSequencesScriptDtos = ({ app }) => ({ container }) => { const getModifyContainerSequencesScriptDtos = ({ app }) => ({ container }) => { const _ = app.require('lodash'); const ddlProvider = require('../../../ddlProvider')(null, null, app); - const { getDbName, getGroupItemsByCompMode } = - require('../../../utils/general')(_); + const { getDbName, getGroupItemsByCompMode } = require('../../../utils/general')(_); const schemaName = getDbName([container.role]); const sequencesCompMod = container.role?.compMod?.[sequencesCompModKey] || {}; diff --git a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js index 8ce53ff..b9b4b29 100644 --- a/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js +++ b/forward_engineering/ddlProvider/ddlHelpers/sequenceHelper.js @@ -1,29 +1,13 @@ /** - * @typedef {'bigint' | 'integer' | 'smallint'} DataType - * - * @typedef {{ - * cache?: number; - * cycle: boolean; - * dataType: DataType; - * ifNotExist: boolean; - * increment?: number; - * maxValue?: number; - * minValue?: number; - * ownedByColumn: object[]; - * ownedByNone: boolean; - * sequenceName: string; - * start?: number; - * temporary: boolean; - * unlogged: boolean; - * }} Sequence - * * @typedef {{ * key: keyof Sequence; * clause: string; - * getOption: ({ sequence: Sequence, config: OptionConfig }) => {} + * getOption: ({ sequence, config } : { sequence: Sequence, config: OptionConfig }) => string * }} OptionConfig */ +const { Sequence } = require('../../types/schemaSequenceTypes'); + module.exports = ({ _, templates, diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index f59f467..dd4c19a 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 { Sequence } =require('../types/schemaSequenceTypes'); module.exports = (baseProvider, options, app) => { @@ -1195,18 +1196,34 @@ module.exports = (baseProvider, options, app) => { return assignTemplates(templates.dropConstraint, templatesConfig); }, + /** + * @param {{ schemaName: string, sequences: Sequence[] }} + * @returns {string} + */ createSchemaSequences({ schemaName, sequences }) { return getSequencesScript({ schemaName, sequences }); }, + /** + * @param {{ schemaName: string, sequence: Sequence }} + * @returns {string} + */ createSchemaSequence({ schemaName, sequence }) { return createSequenceScript({ schemaName, sequence }); }, + /** + * @param {{ schemaName: string, sequence: Sequence }} + * @returns {string} + */ dropSchemaSequence({ schemaName, sequence }) { return dropSequenceScript({ schemaName, sequence }); }, + /** + * @param {{ schemaName: string, sequence: Sequence, oldSequence: Sequence }} + * @returns {string} + */ alterSchemaSequence({ schemaName, sequence, oldSequence }) { return alterSequenceScript({ schemaName, sequence, oldSequence }); } diff --git a/forward_engineering/types/schemaSequenceTypes.js b/forward_engineering/types/schemaSequenceTypes.js new file mode 100644 index 0000000..66286e1 --- /dev/null +++ b/forward_engineering/types/schemaSequenceTypes.js @@ -0,0 +1,118 @@ +class Sequence { + /** + *@type {number | undefined} + */ + cache + + /** + * @type {boolean} + */ + cycle + + /** + * @type {'bigint' | 'integer' | 'smallint'} + */ + dataType + + /** + * @type {boolean} + */ + ifNotExist + + /** + * @type {number | undefined} + */ + increment + + /** + * @type {number | undefined} + */ + maxValue + + /** + * @type {number | undefined} + */ + minValue + + /** + * @type {object[]} + */ + ownedByColumn + + /** + * @type {boolean} + */ + ownedByNone + + /** + * @type {string} + */ + sequenceName + + /** + * @type {number | undefined} + */ + start + + /** + * @type {boolean} + */ + temporary + + /** + * @type {boolean} + */ + unlogged +} + +class SequenceDto { + /** + * @type {string} + */ + sequence_name + + /** + * @type {number} + */ + increment + + /** + * @type {number} + */ + start + + /** + * @type {'bigint' | 'integer' | 'smallint'} + */ + data_type + + /** + * @type {number} + */ + maximum_value + + /** + * @type {number} + */ + minimum_value + + /** + * @type {'YES' | 'NO'} + */ + cycle_option + + /** + * @type {string | null} + */ + column_name + + /** + * @type {string | null} + */ + table_name +} + +module.exports = { + Sequence, + SequenceDto, +}; diff --git a/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js index 5af97ae..af6c053 100644 --- a/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js +++ b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js @@ -1,5 +1,10 @@ +const { Sequence, SequenceDto } = require('../../../forward_engineering/types/schemaSequenceTypes'); +/** + * @param {{sequence: SequenceDto }} + * @returns {object[]} + */ const getOwnedByColumn = ({ sequence }) => { if (!sequence.column_name) { return []; @@ -7,6 +12,10 @@ const getOwnedByColumn = ({ sequence }) => { return [{ entity: sequence.table_name, name: sequence.column_name }]; }; +/** + * @param {{ sequence: SequenceDto }} + * @returns {Sequence} + */ const mapSequenceData = ({ sequence }) => { return { sequenceName: sequence.sequence_name, From a51f3c970e4eadcb7fe2ee49e4074477a0b0e49a Mon Sep 17 00:00:00 2001 From: Serhii Filonenko Date: Tue, 5 Mar 2024 12:27:43 +0200 Subject: [PATCH 11/11] RE: fix incorrect argument passing --- reverse_engineering/helpers/postgresHelpers/sequenceHelper.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js index af6c053..3723a42 100644 --- a/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js +++ b/reverse_engineering/helpers/postgresHelpers/sequenceHelper.js @@ -25,9 +25,8 @@ const mapSequenceData = ({ sequence }) => { maxValue: sequence.maximum_value, minValue: sequence.minimum_value, cycle: sequence.cycle_option === 'YES', - ownedByColumn: getOwnedByColumn(sequence), + ownedByColumn: getOwnedByColumn({ sequence }), ownedByNone: !sequence.column_name, - }; };