Skip to content
13 changes: 11 additions & 2 deletions forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
getDeleteContainerSequencesScriptDtos,
getAddContainerSequencesScriptDtos,
} = require('./alterScriptHelpers/containerHelpers/sequencesHelper');
const { isObjectInDeltaModelActivated } = require('../utils/general');

/**
* @param dto {{
Expand All @@ -53,12 +54,20 @@ const getAlterContainersScriptDtos = ({ collection }) => {
const addContainersScriptDtos = []
.concat(addedContainers)
.filter(Boolean)
.map(container => getAddContainerScriptDto(Object.keys(container.properties)[0]));
.map(container => {
const [containerName, containerData] = Object.entries(container.properties)[0];
const isActivated = isObjectInDeltaModelActivated(containerData);
return getAddContainerScriptDto(containerName, isActivated);
});

const deleteContainersScriptDtos = []
.concat(deletedContainers)
.filter(Boolean)
.map(container => getDeleteContainerScriptDto(Object.keys(container.properties)[0]));
.map(container => {
const [containerName, containerData] = Object.entries(container.properties)[0];
const isActivated = isObjectInDeltaModelActivated(containerData);
return getDeleteContainerScriptDto(containerName, isActivated);
});

const modifyContainersScriptDtos = []
.concat(modifiedContainers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ const dropSchema = schemaName => {

/**
* @param {string} containerName
* @param {boolean} isActivated
* @return {AlterScriptDto | undefined}
* */
const getAddContainerScriptDto = containerName => {
const getAddContainerScriptDto = (containerName, isActivated = true) => {
const script = createSchemaOnly(wrapInQuotes(containerName));
return AlterScriptDto.getInstance([script], true, false);
return AlterScriptDto.getInstance([script], isActivated, false);
};

/**
* @param {string} containerName
* @param {boolean} isActivated
* @return {AlterScriptDto | undefined}
* */
const getDeleteContainerScriptDto = containerName => {
const getDeleteContainerScriptDto = (containerName, isActivated = true) => {
const script = dropSchema(wrapInQuotes(containerName));
return AlterScriptDto.getInstance([script], true, true);
return AlterScriptDto.getInstance([script], isActivated, true);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ const {
getAdditionalDataForDdlProvider,
} = require('./entityHelpers/indexesHelper');
const { getModifiedDefaultColumnValueScriptDtos } = require('./columnHelpers/defaultValueHelper');
const { getEntityName, getFullTableName, getNamePrefixedWithSchemaName, wrapInQuotes } = require('../../utils/general');
const {
getEntityName,
getFullTableName,
getNamePrefixedWithSchemaName,
wrapInQuotes,
isParentContainerActivated,
isObjectInDeltaModelActivated,
} = require('../../utils/general');

/**
* @return {(collection: AlterCollectionDto) => AlterScriptDto | undefined}
Expand Down Expand Up @@ -124,6 +131,9 @@ const getAddColumnsByConditionScriptDtos =
const fullName = getNamePrefixedWithSchemaName(tableName, schemaName);
const schemaData = { schemaName, dbVersion };

const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);

const scripts = _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => predicate([name, jsonSchema]))
.map(([name, jsonSchema]) => {
Expand All @@ -134,18 +144,22 @@ const getAddColumnsByConditionScriptDtos =
externalDefinitions,
});

return createColumnDefinitionBySchema({
const columnDefinition = createColumnDefinitionBySchema({
name,
jsonSchema,
parentJsonSchema: collectionSchema,
ddlProvider,
schemaData,
definitionJsonSchema,
});
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { columnDefinition, isActivated };
})
.map(ddlProvider.convertColumnDefinition)
.map(columnDefinition => ddlProvider.addColumn(fullName, columnDefinition))
.map(addColumnScript => AlterScriptDto.getInstance([addColumnScript], true, false));
.map(({ columnDefinition, isActivated }) => ({
script: ddlProvider.addColumn(fullName, ddlProvider.convertColumnDefinition(columnDefinition)),
isActivated,
}))
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, false));

return scripts.filter(Boolean);
};
Expand Down Expand Up @@ -204,13 +218,17 @@ const getDeleteColumnsByConditionScriptDtos = app => (collection, predicate) =>
const schemaName = collectionSchema.compMod?.keyspaceName;
const fullTableName = getNamePrefixedWithSchemaName(tableName, schemaName);

const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => predicate([name, jsonSchema]))
.map(([name]) => {
.map(([name, jsonSchema]) => {
const columnNameForDDL = wrapInQuotes(name);
return ddlProvider.dropColumn(fullTableName, columnNameForDDL);
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: ddlProvider.dropColumn(fullTableName, columnNameForDDL), isActivated };
})
.map(dropColumnScript => AlterScriptDto.getInstance([dropColumnScript], true, true))
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, true))
.filter(Boolean);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const _ = require('lodash');
const { AlterScriptDto } = require('../types/AlterScriptDto');
const { getUdtName, wrapInQuotes, checkFieldPropertiesChanged } = require('../../utils/general');
const {
getUdtName,
wrapInQuotes,
checkFieldPropertiesChanged,
isObjectInDeltaModelActivated,
} = require('../../utils/general');

/**
* @return {(jsonSchema: Object) => AlterScriptDto | undefined}
Expand Down Expand Up @@ -44,7 +49,7 @@ const getCreateUdtScriptDto =
const udt = { ...updatedUdt, properties: columnDefinitions };

const script = ddlProvider.createUdt(udt);
return AlterScriptDto.getInstance([script], true, false);
return AlterScriptDto.getInstance([script], jsonSchema.isActivated, false);
};

/**
Expand All @@ -56,10 +61,10 @@ const getDeleteUdtScriptDto = app => udt => {
const ddlUdtName = wrapInQuotes(getUdtName(udt));
if (udt.type === 'domain') {
const script = ddlProvider.dropDomain(ddlUdtName);
return AlterScriptDto.getInstance([script], true, true);
return AlterScriptDto.getInstance([script], udt.isActivated, true);
} else {
const script = ddlProvider.dropType(ddlUdtName);
return AlterScriptDto.getInstance([script], true, true);
return AlterScriptDto.getInstance([script], udt.isActivated, true);
}
};

Expand Down Expand Up @@ -97,7 +102,7 @@ const getAddColumnToTypeScriptDtos =
})
.map(ddlProvider.convertColumnDefinition)
.map(columnDefinition => ddlProvider.alterTypeAddAttribute(fullName, columnDefinition))
.map(script => AlterScriptDto.getInstance([script], true, false))
.map(script => AlterScriptDto.getInstance([script], udt.isActivated, false))
.filter(Boolean);
};

Expand All @@ -108,11 +113,12 @@ const getDeleteColumnFromTypeScriptDtos = app => udt => {
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);

const fullName = wrapInQuotes(udt.code || udt.name);
const isActivated = isObjectInDeltaModelActivated(udt);

return _.toPairs(udt.properties)
.filter(([name, jsonSchema]) => !jsonSchema.compMod)
.map(([name]) => ddlProvider.alterTypeDropAttribute(fullName, wrapInQuotes(name)))
.map(script => AlterScriptDto.getInstance([script], true, true))
.map(script => AlterScriptDto.getInstance([script], isActivated, true))
.filter(Boolean);
};

Expand All @@ -123,6 +129,7 @@ const getModifyColumnOfTypeScriptDtos = app => udt => {
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);

const fullName = wrapInQuotes(udt.code || udt.name);
const isActivated = isObjectInDeltaModelActivated(udt);

const renameColumnScripts = _.values(udt.properties)
.filter(jsonSchema => checkFieldPropertiesChanged(jsonSchema.compMod, ['name']))
Expand All @@ -131,7 +138,7 @@ const getModifyColumnOfTypeScriptDtos = app => udt => {
const newAttributeDDLName = wrapInQuotes(jsonSchema.compMod.newField.name);
return ddlProvider.alterTypeRenameAttribute(fullName, oldAttributeDDLName, newAttributeDDLName);
})
.map(script => AlterScriptDto.getInstance([script], true, false));
.map(script => AlterScriptDto.getInstance([script], isActivated, false));

const changeTypeScripts = _.toPairs(udt.properties)
.filter(([name, jsonSchema]) => checkFieldPropertiesChanged(jsonSchema.compMod, ['type', 'mode']))
Expand All @@ -140,7 +147,7 @@ const getModifyColumnOfTypeScriptDtos = app => udt => {
const newDataType = jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type;
return ddlProvider.alterTypeChangeAttributeType(fullName, attributeDDLName, newDataType);
})
.map(script => AlterScriptDto.getInstance([script], true, false));
.map(script => AlterScriptDto.getInstance([script], isActivated, false));

return [...renameColumnScripts, ...changeTypeScripts].filter(Boolean);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const _ = require('lodash');
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const { checkFieldPropertiesChanged, getFullTableName, wrapInQuotes } = require('../../../utils/general');
const {
checkFieldPropertiesChanged,
getFullTableName,
wrapInQuotes,
isObjectInDeltaModelActivated,
isParentContainerActivated,
} = require('../../../utils/general');
const assignTemplates = require('../../../utils/assignTemplates');
const templates = require('../../../ddlProvider/templates');

Expand Down Expand Up @@ -63,6 +69,8 @@ const hasPrecisionOrScaleChanged = (collection, oldFieldName, currentJsonSchema)
* */
const getUpdateTypesScriptDtos = collection => {
const fullTableName = getFullTableName(collection);
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
Expand All @@ -79,9 +87,10 @@ const getUpdateTypesScriptDtos = collection => {
const typeName = jsonSchema.compMod.newField.mode || jsonSchema.compMod.newField.type;
const columnName = wrapInQuotes(name);
const typeConfig = _.pick(jsonSchema, ['length', 'precision', 'scale']);
return alterColumnType(fullTableName, columnName, typeName, typeConfig);
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: alterColumnType(fullTableName, columnName, typeName, typeConfig), isActivated };
})
.map(script => AlterScriptDto.getInstance([script], true, false));
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, false));
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const _ = require('lodash');
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const { getFullColumnName, wrapComment } = require('../../../utils/general');
const {
getFullColumnName,
wrapComment,
isObjectInDeltaModelActivated,
isParentContainerActivated,
} = require('../../../utils/general');
const assignTemplates = require('../../../utils/assignTemplates');
const templates = require('../../../ddlProvider/templates');

Expand All @@ -22,6 +27,8 @@ const updateColumnComment = (columnName, comment) => {
* @return {AlterScriptDto[]}
* */
const getUpdatedCommentOnColumnScriptDtos = collection => {
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);
return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
const newComment = jsonSchema.description;
Expand All @@ -33,9 +40,10 @@ const getUpdatedCommentOnColumnScriptDtos = collection => {
const newComment = jsonSchema.description;
const ddlComment = wrapComment(newComment);
const columnName = getFullColumnName(collection, name);
return updateColumnComment(columnName, ddlComment);
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: updateColumnComment(columnName, ddlComment), isActivated };
})
.map(script => AlterScriptDto.getInstance([script], true, false));
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, false));
};

/**
Expand All @@ -55,6 +63,8 @@ const dropColumnComment = columnName => {
* @return {AlterScriptDto[]}
* */
const getDeletedCommentOnColumnScriptDtos = collection => {
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);
return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
const newComment = jsonSchema.description;
Expand All @@ -64,9 +74,10 @@ const getDeletedCommentOnColumnScriptDtos = collection => {
})
.map(([name, jsonSchema]) => {
const columnName = getFullColumnName(collection, name);
return dropColumnComment(columnName);
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: dropColumnComment(columnName), isActivated };
})
.map(script => AlterScriptDto.getInstance([script], true, true));
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, true));
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const { toPairs } = require('lodash');
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const { getFullTableName, wrapInQuotes } = require('../../../utils/general');
const {
getFullTableName,
wrapInQuotes,
isObjectInDeltaModelActivated,
isParentContainerActivated,
} = require('../../../utils/general');
const { decorateDefault } = require('../../../ddlProvider/ddlHelpers/columnDefinitionHelper');
const assignTemplates = require('../../../utils/assignTemplates');
const templates = require('../../../ddlProvider/templates');
Expand All @@ -26,8 +31,11 @@ const updateColumnDefaultValue = ({ tableName, columnName, defaultValue }) => {
* @param {Object} props.collection
* @returns { Array<AlterScriptDto> }
* */
const getUpdatedDefaultColumnValueScriptDtos = ({ collection }) =>
toPairs(collection.properties)
const getUpdatedDefaultColumnValueScriptDtos = ({ collection }) => {
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);

return toPairs(collection.properties)
.filter(([_name, jsonSchema]) => {
const newDefault = jsonSchema.default;
const oldName = jsonSchema.compMod.oldField.name;
Expand All @@ -43,10 +51,12 @@ const getUpdatedDefaultColumnValueScriptDtos = ({ collection }) =>
columnName: wrapInQuotes(columnName),
defaultValue: decorateDefault(type, newDefaultValue, isArrayType),
};
return updateColumnDefaultValue(scriptGenerationConfig);
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: updateColumnDefaultValue(scriptGenerationConfig), isActivated };
})
.map(script => AlterScriptDto.getInstance([script], true, false))
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, false))
.filter(Boolean);
};

/**
* @param {Object} props
Expand All @@ -67,8 +77,11 @@ const dropColumnDefaultValue = ({ tableName, columnName }) => {
* @param {Object} props.collection
* @returns { Array<AlterScriptDto> }
* */
const getDeletedDefaultColumnValueScriptDtos = ({ collection }) =>
toPairs(collection.properties)
const getDeletedDefaultColumnValueScriptDtos = ({ collection }) => {
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);

return toPairs(collection.properties)
.filter(([_name, jsonSchema]) => {
const newDefault = jsonSchema.default;
const oldName = jsonSchema.compMod.oldField.name;
Expand All @@ -77,15 +90,17 @@ const getDeletedDefaultColumnValueScriptDtos = ({ collection }) =>
const hasNewValue = newDefault !== undefined;
return hasPrevValue && !hasNewValue;
})
.map(([columnName]) => {
.map(([columnName, jsonSchema]) => {
const scriptGenerationConfig = {
tableName: getFullTableName(collection),
columnName: wrapInQuotes(columnName),
};
return dropColumnDefaultValue(scriptGenerationConfig);
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: dropColumnDefaultValue(scriptGenerationConfig), isActivated };
})
.map(script => AlterScriptDto.getInstance([script], true, true))
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, true))
.filter(Boolean);
};

/**
* @param {Object} props
Expand Down
Loading