Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
103 changes: 103 additions & 0 deletions forward_engineering/alterScript/alterScriptBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const {getAlterScriptDtos} = require("./alterScriptFromDeltaHelper");

const {AlterScriptDto} = require('./types/AlterScriptDto');

/**
* @return {(dtos: AlterScriptDto[], shouldApplyDropStatements: boolean) => string}
* */
const joinAlterScriptDtosIntoScript = (_) => (dtos, shouldApplyDropStatements) => {
const {commentIfDeactivated} = require('../utils/general')(_);
return dtos.map((dto) => {
if (dto.isActivated === false) {
return dto.scripts
.map((scriptDto) => commentIfDeactivated(scriptDto.script, {
isActivated: false,
isPartOfLine: false,
}));
}
if (!shouldApplyDropStatements) {
return dto.scripts
.map((scriptDto) => commentIfDeactivated(scriptDto.script, {
isActivated: !scriptDto.isDropScript,
isPartOfLine: false,
}));
}
return dto.scripts.map((scriptDto) => scriptDto.script);
})
.flat()
.filter(Boolean)
.map((scriptLine) => scriptLine.trim())
.filter(Boolean)
.join('\n\n');
}

/**
* @param data {CoreData}
* @param app {App}
* @return {string}
* */
const buildEntityLevelAlterScript = (data, app) => {
const _ = app.require('lodash');
const alterScriptDtos = getAlterScriptDtos(data, app);
const shouldApplyDropStatements = data.options?.additionalOptions?.some(
option => option.id === 'applyDropStatements' && option.value,
);

return joinAlterScriptDtosIntoScript(_)(alterScriptDtos, shouldApplyDropStatements);
}

/**
* @param data {CoreData}
* @param app {App}
* @return { boolean}
* */
const doesEntityLevelAlterScriptContainDropStatements = (data, app) => {
const alterScriptDtos = getAlterScriptDtos(data, app);
return alterScriptDtos
.some(alterScriptDto => alterScriptDto.isActivated && alterScriptDto
.scripts.some(scriptModificationDto => scriptModificationDto.isDropScript));
}

const mapCoreDataForContainerLevelScripts = (data) => {
return {
...data,
jsonSchema: data.collections[0],
internalDefinitions: Object.values(data.internalDefinitions)[0],
}
}

/**
* @param data {CoreData}
* @param app {App}
* @return {string}
* */
const buildContainerLevelAlterScript = (data, app) => {
const preparedData = mapCoreDataForContainerLevelScripts(data);
const _ = app.require('lodash');
const alterScriptDtos = getAlterScriptDtos(preparedData, app);
const shouldApplyDropStatements = preparedData.options?.additionalOptions?.some(
option => option.id === 'applyDropStatements' && option.value,
);

return joinAlterScriptDtosIntoScript(_)(alterScriptDtos, shouldApplyDropStatements);
}

/**
* @param data {CoreData}
* @param app {App}
* @return { boolean}
* */
const doesContainerLevelAlterScriptContainDropStatements = (data, app) => {
const preparedData = mapCoreDataForContainerLevelScripts(data);
const alterScriptDtos = getAlterScriptDtos(preparedData, app);
return alterScriptDtos
.some(alterScriptDto => alterScriptDto.isActivated && alterScriptDto
.scripts.some(scriptModificationDto => scriptModificationDto.isDropScript));
}

module.exports = {
buildEntityLevelAlterScript,
doesEntityLevelAlterScriptContainDropStatements,
buildContainerLevelAlterScript,
doesContainerLevelAlterScriptContainDropStatements
}
Loading