Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/hck 4566 bbva sequences definition for postgresql re fro #91

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, getDeleteContainerSequencesScriptDtos, getAddContainerSequencesScriptDtos } = require('./alterScriptHelpers/containerHelpers/sequencesHelper');


/**
Expand Down Expand Up @@ -300,6 +301,41 @@ 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 => Object.values(container.properties)[0])
.flatMap(container => getAddContainerSequencesScriptDtos({ app })({ container }))
const deleteContainersScriptDtos = []
.concat(deletedContainers)
.filter(Boolean)
.map(container => Object.values(container.properties)[0])
.flatMap(container => getDeleteContainerSequencesScriptDtos({ app })({ container }))
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}
Expand Down Expand Up @@ -336,11 +372,13 @@ const getAlterScriptDtos = (data, app) => {
externalDefinitions,
});
const relationshipScriptDtos = getAlterRelationshipsScriptDtos({collection, app});
const containersSequencesScriptDtos = getAlterContainersSequencesScriptDtos({collection, app});

return [
...containersScriptDtos,
...modelDefinitionsScriptDtos,
...collectionsScriptDtos,
...containersSequencesScriptDtos,
...viewScriptDtos,
...relationshipScriptDtos,
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const { App } = require('../../../types/coreApplicationTypes');
const sequencesCompModKey = 'sequences';

/**
* @param {{ app: App }}
* @return {({ container }: { container: object }) => AlterScriptDto[]}
* */
const getAddContainerSequencesScriptDtos = ({ app }) => ({ container }) => {
const _ = app.require('lodash');
const ddlProvider = require('../../../ddlProvider')(null, null, app);
const { getDbName } = require('../../../utils/general')(_);
const schemaName = getDbName([container.role]);

return (container.role?.sequences || [])
.map((sequence) => ddlProvider.createSchemaSequence({ schemaName, sequence }))
.map((script) => AlterScriptDto.getInstance([script], true, false))
.filter(Boolean);
};

/**
* @param {{ app: App }}
* @return {({ container }: { container: object }) => AlterScriptDto[]}
* */
const getModifyContainerSequencesScriptDtos = ({ app }) => ({ container }) => {
const _ = app.require('lodash');
const ddlProvider = require('../../../ddlProvider')(null, null, app);
const { getDbName, getGroupItemsByCompMode } = require('../../../utils/general')(_);

const schemaName = getDbName([container.role]);
const sequencesCompMod = container.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);
};

/**
* @param {{ app: App }}
* @return {({ container }: { container: object }) => AlterScriptDto[]}
* */
const getDeleteContainerSequencesScriptDtos = ({ app }) => ({ container }) => {
const _ = app.require('lodash');
const ddlProvider = require('../../../ddlProvider')(null, null, app);
const { getDbName } = require('../../../utils/general')(_);
const schemaName = getDbName([container.role]);

return (container.role?.sequences || [])
.map((sequence) => ddlProvider.dropSchemaSequence({ schemaName, sequence }))
.map((script) => AlterScriptDto.getInstance([script], true, true))
.filter(Boolean);
};

module.exports = {
getAddContainerSequencesScriptDtos,
getModifyContainerSequencesScriptDtos,
getDeleteContainerSequencesScriptDtos,
};
serhii-filonenko marked this conversation as resolved.
Show resolved Hide resolved
Loading