Skip to content

Commit

Permalink
HCK-7614: column DEFAULT (#113)
Browse files Browse the repository at this point in the history
* feat: added column DEFAULT prop handler

* fix: drop DEFAULT statement params

* fix: bump pg version

* fix: adjusted statements with fail-safe check IF EXISTS

* fix: handle _fasly_ values properly

* fix: handle drop statement for empty values

* fix: appropriately generate drop statement

* chore: simplify
  • Loading branch information
chulanovskyi-bs authored Aug 13, 2024
1 parent 76c6383 commit 4a60513
Show file tree
Hide file tree
Showing 24 changed files with 460 additions and 401 deletions.
2 changes: 1 addition & 1 deletion forward_engineering/alterScript/alterScriptBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { AlterScriptDto } = require('./types/AlterScriptDto');
* @return {(dtos: AlterScriptDto[], shouldApplyDropStatements: boolean) => string}
* */
const joinAlterScriptDtosIntoScript = _ => (dtos, shouldApplyDropStatements) => {
const { commentIfDeactivated } = require('../utils/general')(_);
const { commentIfDeactivated } = require('../utils/general');
return dtos
.map(dto => {
if (dto.isActivated === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { AlterScriptDto } = require('../types/AlterScriptDto');
const getAddContainerScriptDto = app => containerName => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { wrapInQuotes } = require('../../utils/general')(_);
const { wrapInQuotes } = require('../../utils/general');
const script = ddlProvider.createSchemaOnly(wrapInQuotes(containerName));
return AlterScriptDto.getInstance([script], true, false);
};
Expand All @@ -18,7 +18,7 @@ const getAddContainerScriptDto = app => containerName => {
const getDeleteContainerScriptDto = app => containerName => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { wrapInQuotes } = require('../../utils/general')(_);
const { wrapInQuotes } = require('../../utils/general');

const script = ddlProvider.dropSchema(wrapInQuotes(containerName));
return AlterScriptDto.getInstance([script], true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
getAddedIndexesScriptDtos,
getAdditionalDataForDdlProvider,
} = require('./entityHelpers/indexesHelper');
const { getModifiedDefaultColumnValueScriptDtos } = require('./columnHelpers/defaultValueHelper');

/**
* @return {(collection: AlterCollectionDto) => AlterScriptDto | undefined}
Expand All @@ -21,7 +22,7 @@ const getAddCollectionScriptDto =
({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }) =>
collection => {
const _ = app.require('lodash');
const { getEntityName } = require('../../utils/general')(_);
const { getEntityName } = require('../../utils/general');
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(app);
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { getDefinitionByReference } = app.require('@hackolade/ddl-fe-utils');
Expand Down Expand Up @@ -76,7 +77,7 @@ const getAddCollectionScriptDto =
const getDeleteCollectionScriptDto = app => collection => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { getFullTableName } = require('../../utils/general')(_);
const { getFullTableName } = require('../../utils/general');

const fullName = getFullTableName(collection);
const script = ddlProvider.dropTable(fullName);
Expand Down Expand Up @@ -116,7 +117,7 @@ const getAddColumnsByConditionScriptDtos =
({ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions }) =>
(collection, predicate) => {
const _ = app.require('lodash');
const { getEntityName, getNamePrefixedWithSchemaName } = require('../../utils/general')(_);
const { getEntityName, getNamePrefixedWithSchemaName } = require('../../utils/general');
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(app);
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { getDefinitionByReference } = app.require('@hackolade/ddl-fe-utils');
Expand Down Expand Up @@ -204,7 +205,7 @@ const getAddColumnScriptDtos =
const getDeleteColumnsByConditionScriptDtos = app => (collection, predicate) => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { getEntityName, getNamePrefixedWithSchemaName, wrapInQuotes } = require('../../utils/general')(_);
const { getEntityName, getNamePrefixedWithSchemaName, wrapInQuotes } = require('../../utils/general');

const collectionSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const tableName = getEntityName(collectionSchema);
Expand Down Expand Up @@ -294,12 +295,17 @@ const getModifyColumnScriptDtos =
const updateTypeScriptDtos = getUpdateTypesScriptDtos(_, ddlProvider)(collection);
const modifyNotNullScriptDtos = getModifyNonNullColumnsScriptDtos(_, ddlProvider)(collection);
const modifyCommentScriptDtos = getModifiedCommentOnColumnScriptDtos(_, ddlProvider)(collection);
const modifyDefaultColumnValueScriptDtos = getModifiedDefaultColumnValueScriptDtos({
ddlProvider,
collection,
});

return [
...renameColumnScriptDtos,
...updateTypeScriptDtos,
...modifyNotNullScriptDtos,
...modifyCommentScriptDtos,
...modifyDefaultColumnValueScriptDtos,
].filter(Boolean);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getRelationshipName = relationship => {
* @return {(relationship: AlterRelationshipDto) => string}
* */
const getFullChildTableName = _ => relationship => {
const { getNamePrefixedWithSchemaName } = require('../../utils/general')(_);
const { getNamePrefixedWithSchemaName } = require('../../utils/general');
const compMod = relationship.role.compMod;

const childBucketName = compMod.child.bucket.name;
Expand Down Expand Up @@ -88,7 +88,7 @@ const getAddForeignKeyScriptDtos = (ddlProvider, _) => addedRelationships => {
* }}
* */
const getDeleteSingleForeignKeyStatementDto = (ddlProvider, _) => relationship => {
const { wrapInQuotes } = require('../../utils/general')(_);
const { wrapInQuotes } = require('../../utils/general');
const compMod = relationship.role.compMod;

const ddlChildEntityName = getFullChildTableName(_)(relationship);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const getDeleteUdtScriptDto = app => udt => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);

const { getUdtName, wrapInQuotes } = require('../../utils/general')(_);
const { getUdtName, wrapInQuotes } = require('../../utils/general');

const ddlUdtName = wrapInQuotes(getUdtName(udt));
if (udt.type === 'domain') {
Expand All @@ -73,7 +73,7 @@ const getAddColumnToTypeScriptDtos =
udt => {
const _ = app.require('lodash');
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(app);
const { getUdtName, wrapInQuotes } = require('../../utils/general')(_);
const { getUdtName, wrapInQuotes } = require('../../utils/general');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { getDefinitionByReference } = app.require('@hackolade/ddl-fe-utils');

Expand Down Expand Up @@ -112,7 +112,7 @@ const getDeleteColumnFromTypeScriptDtos = app => udt => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);

const { wrapInQuotes } = require('../../utils/general')(_);
const { wrapInQuotes } = require('../../utils/general');

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

Expand All @@ -130,7 +130,7 @@ const getModifyColumnOfTypeScriptDtos = app => udt => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);

const { checkFieldPropertiesChanged, wrapInQuotes } = require('../../utils/general')(_);
const { checkFieldPropertiesChanged, wrapInQuotes } = require('../../utils/general');

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getAddViewScriptDto = app => view => {
const getDeleteViewScriptDto = app => view => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider/ddlProvider')(null, null, app);
const { wrapInQuotes } = require('../../utils/general')(_);
const { wrapInQuotes } = require('../../utils/general');
const viewName = wrapInQuotes(view.code || view.name);

const script = ddlProvider.dropView(viewName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const hasPrecisionOrScaleChanged = (collection, oldFieldName, currentJsonSchema)
* @return {(collection: Object) => AlterScriptDto[]}
* */
const getUpdateTypesScriptDtos = (_, ddlProvider) => collection => {
const { checkFieldPropertiesChanged, getFullTableName, wrapInQuotes } = require('../../../utils/general')(_);
const { checkFieldPropertiesChanged, getFullTableName, wrapInQuotes } = require('../../../utils/general');
const fullTableName = getFullTableName(collection);

return _.toPairs(collection.properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { AlterScriptDto } = require('../../types/AlterScriptDto');
* @return {(collection: Object) => AlterScriptDto[]}
* */
const getUpdatedCommentOnColumnScriptDtos = (_, ddlProvider) => collection => {
const { getFullColumnName, wrapComment } = require('../../../utils/general')(_);
const { getFullColumnName, wrapComment } = require('../../../utils/general');
return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
const newComment = jsonSchema.description;
Expand All @@ -25,7 +25,7 @@ const getUpdatedCommentOnColumnScriptDtos = (_, ddlProvider) => collection => {
* @return {(collection: Object) => AlterScriptDto[]}
* */
const getDeletedCommentOnColumnScriptDtos = (_, ddlProvider) => collection => {
const { getFullColumnName } = require('../../../utils/general')(_);
const { getFullColumnName } = require('../../../utils/general');

return _.toPairs(collection.properties)
.filter(([name, jsonSchema]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { toPairs } = require('lodash');
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const { getFullTableName, wrapInQuotes, wrapInSingleQuotes } = require('../../../utils/general');

/**
* @param { ({ ddlProvider: Object, collection: Object }) }
* @returns { Array<AlterScriptDto> }
* */
const getUpdatedDefaultColumnValueScriptDtos = ({ ddlProvider, collection }) =>
toPairs(collection.properties)
.filter(([_name, jsonSchema]) => {
const newDefault = jsonSchema.default;
const oldName = jsonSchema.compMod.oldField.name;
const oldDefault = collection.role.properties[oldName]?.default;
return newDefault !== undefined && (!oldDefault || newDefault !== oldDefault);
})
.map(([columnName, jsonSchema]) => {
const newDefaultValue = jsonSchema.default;
const scriptGenerationConfig = {
tableName: getFullTableName(collection),
columnName: wrapInQuotes(columnName),
defaultValue: wrapInSingleQuotes({ name: newDefaultValue }),
};
return ddlProvider.updateColumnDefaultValue(scriptGenerationConfig);
})
.map(script => AlterScriptDto.getInstance([script], true, false))
.filter(Boolean);

/**
* @param { ({ ddlProvider: Object, collection: Object }) }
* @returns { Array<AlterScriptDto> }
* */
const getDeletedDefaultColumnValueScriptDtos = ({ ddlProvider, collection }) =>
toPairs(collection.properties)
.filter(([_name, jsonSchema]) => {
const newDefault = jsonSchema.default;
const oldName = jsonSchema.compMod.oldField.name;
const oldDefault = collection.role.properties[oldName]?.default;
const hasPrevValue = oldDefault !== undefined;
const hasNewValue = newDefault !== undefined;
return hasPrevValue && !hasNewValue;
})
.map(([columnName]) => {
const scriptGenerationConfig = {
tableName: getFullTableName(collection),
columnName: wrapInQuotes(columnName),
};
return ddlProvider.dropColumnDefaultValue(scriptGenerationConfig);
})
.map(script => AlterScriptDto.getInstance([script], true, true))
.filter(Boolean);

/**
* @param { ({ ddlProvider: Object, collection: Object }) }
* @returns { Array<AlterScriptDto> }
* */
const getModifiedDefaultColumnValueScriptDtos = ({ ddlProvider, collection }) => {
const updatedDefaultValuesScriptDtos = getUpdatedDefaultColumnValueScriptDtos({ ddlProvider, collection });
const dropDefaultValuesScriptDtos = getDeletedDefaultColumnValueScriptDtos({ ddlProvider, collection });
return [...updatedDefaultValuesScriptDtos, ...dropDefaultValuesScriptDtos];
};

module.exports = {
getModifiedDefaultColumnValueScriptDtos,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { AlterScriptDto } = require('../../types/AlterScriptDto');
* @return {(collection: Object) => AlterScriptDto[]}
* */
const getModifyNonNullColumnsScriptDtos = (_, ddlProvider) => collection => {
const { getFullTableName, wrapInQuotes } = require('../../../utils/general')(_);
const { getFullTableName, wrapInQuotes } = require('../../../utils/general');
const fullTableName = getFullTableName(collection);

const currentRequiredColumnNames = collection.required || [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { AlterScriptDto } = require('../../types/AlterScriptDto');
* @return {(collection: Object) => AlterScriptDto[]}
* */
const getRenameColumnScriptDtos = (_, ddlProvider) => collection => {
const { checkFieldPropertiesChanged, getFullTableName, wrapInQuotes } = require('../../../utils/general')(_);
const { checkFieldPropertiesChanged, getFullTableName, wrapInQuotes } = require('../../../utils/general');
const fullTableName = getFullTableName(collection);

return _.values(collection.properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const extractDescription = container => {
* @return {(collection: Object) => AlterScriptDto | undefined}
* */
const getUpsertCommentsScriptDto = (_, ddlProvider) => container => {
const { wrapComment, wrapInQuotes } = require('../../../utils/general')(_);
const { wrapComment, wrapInQuotes } = require('../../../utils/general');

const description = extractDescription(container);
if (description.new && description.new !== description.old) {
Expand All @@ -24,7 +24,7 @@ const getUpsertCommentsScriptDto = (_, ddlProvider) => container => {
* @return {(collection: Object) => AlterScriptDto | undefined}
* */
const getDropCommentsScriptDto = (_, ddlProvider) => container => {
const { wrapInQuotes } = require('../../../utils/general')(_);
const { wrapInQuotes } = require('../../../utils/general');

const description = extractDescription(container);
if (description.old && !description.new) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getAddContainerSequencesScriptDtos =
({ container }) => {
const _ = app.require('lodash');
const ddlProvider = require('../../../ddlProvider')(null, null, app);
const { getDbName } = require('../../../utils/general')(_);
const { getDbName } = require('../../../utils/general');
const schemaName = getDbName([container.role]);

return (container.role?.sequences || [])
Expand All @@ -29,7 +29,7 @@ const getModifyContainerSequencesScriptDtos =
({ 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] || {};
Expand Down Expand Up @@ -72,7 +72,7 @@ const getDeleteContainerSequencesScriptDtos =
({ container }) => {
const _ = app.require('lodash');
const ddlProvider = require('../../../ddlProvider')(null, null, app);
const { getDbName } = require('../../../utils/general')(_);
const { getDbName } = require('../../../utils/general');
const schemaName = getDbName([container.role]);

return (container.role?.sequences || [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const mapCheckConstraintNamesToChangeHistory = _ => collection => {
* @return {(constraintHistory: Array<CheckConstraintHistoryEntry>, fullTableName: string) => Array<AlterScriptDto>}
* */
const getDropCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistory, fullTableName) => {
const { wrapInQuotes } = require('../../../utils/general')(_);
const { wrapInQuotes } = require('../../../utils/general');

return constraintHistory
.filter(historyEntry => historyEntry.old && !historyEntry.new)
Expand All @@ -56,7 +56,7 @@ const getDropCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistory,
* @return {(constraintHistory: Array<CheckConstraintHistoryEntry>, fullTableName: string) => Array<AlterScriptDto>}
* */
const getAddCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistory, fullTableName) => {
const { wrapInQuotes } = require('../../../utils/general')(_);
const { wrapInQuotes } = require('../../../utils/general');

return constraintHistory
.filter(historyEntry => historyEntry.new && !historyEntry.old)
Expand All @@ -71,7 +71,7 @@ const getAddCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistory,
* @return {(constraintHistory: Array<CheckConstraintHistoryEntry>, fullTableName: string) => Array<AlterScriptDto>}
* */
const getUpdateCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistory, fullTableName) => {
const { wrapInQuotes } = require('../../../utils/general')(_);
const { wrapInQuotes } = require('../../../utils/general');

return constraintHistory
.filter(historyEntry => {
Expand Down Expand Up @@ -105,7 +105,7 @@ const getUpdateCheckConstraintScriptDtos = (_, ddlProvider) => (constraintHistor
* @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>}
* */
const getModifyCheckConstraintScriptDtos = (_, ddlProvider) => collection => {
const { getFullTableName } = require('../../../utils/general')(_);
const { getFullTableName } = require('../../../utils/general');
const fullTableName = getFullTableName(collection);
const constraintHistory = mapCheckConstraintNamesToChangeHistory(_)(collection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { AlterCollectionDto } = require('../../types/AlterCollectionDto');
* @return {(collection: AlterCollectionDto) => AlterScriptDto}
*/
const getUpdatedCommentOnCollectionScriptDto = (_, ddlProvider) => collection => {
const { getFullTableName, wrapComment } = require('../../../utils/general')(_);
const { getFullTableName, wrapComment } = require('../../../utils/general');

const descriptionInfo = collection?.role.compMod?.description;
if (!descriptionInfo) {
Expand All @@ -28,7 +28,7 @@ const getUpdatedCommentOnCollectionScriptDto = (_, ddlProvider) => collection =>
* @return {(collection: AlterCollectionDto) => AlterScriptDto}
*/
const getDeletedCommentOnCollectionScriptDto = (_, ddlProvider) => collection => {
const { getFullTableName } = require('../../../utils/general')(_);
const { getFullTableName } = require('../../../utils/general');

const descriptionInfo = collection?.role.compMod?.description;
if (!descriptionInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const setNamesToColumns =
const mapIndexToFeIndexDto =
({ _ }) =>
({ index, collection }) => {
const { getSchemaNameFromCollection } = require('../../../utils/general')(_);
const { getSchemaNameFromCollection } = require('../../../utils/general');

const schemaName = getSchemaNameFromCollection({ collection });

Expand Down Expand Up @@ -183,7 +183,7 @@ const getAddedIndexesScriptDtos =
const getDeleteIndexScriptDto =
({ _, ddlProvider }) =>
({ index, additionalDataForDdlProvider }) => {
const { getNamePrefixedWithSchemaName } = require('../../../utils/general')(_);
const { getNamePrefixedWithSchemaName } = require('../../../utils/general');

const { isParentActivated, schemaName } = additionalDataForDdlProvider;

Expand Down Expand Up @@ -369,7 +369,7 @@ const getModifyIndexesScriptDtos =
};

const getAdditionalDataForDdlProvider = ({ _, dbVersion, collection }) => {
const { getSchemaNameFromCollection } = require('../../../utils/general')(_);
const { getSchemaNameFromCollection } = require('../../../utils/general');

return {
dbData: { dbVersion },
Expand Down
Loading

0 comments on commit 4a60513

Please sign in to comment.