diff --git a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js index bc910b4..40b2029 100644 --- a/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js +++ b/forward_engineering/alterScript/alterScriptHelpers/entityHelpers/primaryKeyHelper.js @@ -3,32 +3,210 @@ const { AlterCollectionDto, AlterCollectionColumnDto, AlterCollectionRoleCompModPKDto, - AlterCollectionColumnPrimaryKeyOptionDto + AlterCollectionColumnPrimaryKeyOptionDto, + AlterCollectionRoleCompModPrimaryKey } = require('../../types/AlterCollectionDto'); +const amountOfColumnsInRegularPk = 1; + +class PkTransitionDto { + + /** + * @type {boolean} + * */ + didTransitionHappen + + /** + * @type {boolean | undefined} + * */ + wasPkChangedInTransition + + /** + * @return {PkTransitionDto} + * */ + static noTransition() { + return { + didTransitionHappen: false, + } + } + + /** + * @param wasPkChangedInTransition {boolean} + * @return {PkTransitionDto} + * */ + static transition(wasPkChangedInTransition) { + return { + didTransitionHappen: true, + wasPkChangedInTransition + } + } + +} + +class PkScriptModificationDto { + + /** + * @type string + * */ + script + + /** + * @type boolean + * */ + isDropScript + + /** + * @type {string} + * */ + fullTableName + + /** + * @type {boolean} + * */ + isActivated + + /** + * @param fullTableName {string} + * @param script {string} + * @param isDropScript {boolean} + * @param isActivated {boolean} + * */ + constructor( + script, + fullTableName, + isDropScript, + isActivated + ) { + this.script = script; + this.isDropScript = isDropScript; + this.fullTableName = fullTableName; + this.isActivated = isActivated; + } + +} + +/** + * @param entityName {string} + * @return {string} + * */ +const getDefaultConstraintName = (entityName) => { + return `${entityName}_pkey`; +} + +/** + * @param optionHolder {AlterCollectionColumnPrimaryKeyOptionDto} + * @return {} + * */ +const extractOptionsForComparisonWithRegularPkOptions = (optionHolder) => { + return { + constraintName: optionHolder.constraintName, + indexStorageParameters: optionHolder.indexStorageParameters, + indexTablespace: optionHolder.indexTablespace, + indexInclude: optionHolder.indexInclude, + } +} + +/** + * @param columnJsonSchema {AlterCollectionColumnDto} + * @return {Array>} + * */ +const getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions = (columnJsonSchema) => { + /** + * @type {Array} + * */ + const constraintOptions = columnJsonSchema.primaryKeyOptions || []; + return constraintOptions + .map(option => extractOptionsForComparisonWithRegularPkOptions(option)); +} + /** - * @return {(collection: AlterCollectionDto) => boolean} + * @param compositePk {AlterCollectionRoleCompModPKDto} + * @return {Array>} * */ -const didCompositePkChange = (_) => (collection) => { +const getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions = (compositePk) => { + const optionsForComparison = extractOptionsForComparisonWithRegularPkOptions(compositePk); + return [optionsForComparison] + .filter(o => Object.values(o).some(Boolean)); +} + +/** + * @return {(collection: AlterCollectionDto) => PkTransitionDto} + * */ +const wasCompositePkChangedInTransitionFromCompositeToRegular = (_) => (collection) => { + /** + * @type {AlterCollectionRoleCompModPrimaryKey} + * */ const pkDto = collection?.role?.compMod?.primaryKey || {}; - const newPrimaryKeys = pkDto.new || []; + /** + * @type {AlterCollectionRoleCompModPKDto[]} + * */ const oldPrimaryKeys = pkDto.old || []; - if (newPrimaryKeys.length !== oldPrimaryKeys.length) { - return true; + const idsOfColumns = oldPrimaryKeys.flatMap(pk => pk.compositePrimaryKey.map(dto => dto.keyId)) + if (idsOfColumns.length !== amountOfColumnsInRegularPk) { + // We return false, because it wouldn't count as transition between regular PK and composite PK + // if composite PK did not constraint exactly 1 column + return PkTransitionDto.noTransition(); } - if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) { - return false; + const idOfPkColumn = idsOfColumns[0]; + const newColumnJsonSchema = Object.values(collection.properties) + .find(columnJsonSchema => columnJsonSchema.GUID === idOfPkColumn); + if (!newColumnJsonSchema) { + return PkTransitionDto.noTransition(); } - const areKeyArraysEqual = _(oldPrimaryKeys).differenceWith(newPrimaryKeys, _.isEqual).isEmpty(); - return !areKeyArraysEqual; + const isNewColumnARegularPrimaryKey = newColumnJsonSchema?.primaryKey && !newColumnJsonSchema?.compositePrimaryKey; + if (!isNewColumnARegularPrimaryKey) { + return PkTransitionDto.noTransition(); + } + const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(newColumnJsonSchema); + const areOptionsEqual = oldPrimaryKeys.some((compositePk) => { + if (compositePk.compositePrimaryKey.length !== amountOfColumnsInRegularPk) { + return false; + } + const oldCompositePkAsRegularPkOptions = getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(compositePk); + return _(oldCompositePkAsRegularPkOptions).differenceWith(constraintOptions, _.isEqual).isEmpty(); + }); + + return PkTransitionDto.transition(!areOptionsEqual); } /** - * @param entityName {string} - * @return {string} + * @return {(collection: AlterCollectionDto) => PkTransitionDto} * */ -const getDefaultConstraintName = (entityName) => { - return `${entityName}_pk`; +const wasCompositePkChangedInTransitionFromRegularToComposite = (_) => (collection) => { + /** + * @type {AlterCollectionRoleCompModPrimaryKey} + * */ + const pkDto = collection?.role?.compMod?.primaryKey || {}; + /** + * @type {AlterCollectionRoleCompModPKDto[]} + * */ + const newPrimaryKeys = pkDto.new || []; + const idsOfColumns = newPrimaryKeys.flatMap(pk => pk.compositePrimaryKey.map(dto => dto.keyId)) + if (idsOfColumns.length !== amountOfColumnsInRegularPk) { + // We return false, because it wouldn't count as transition between regular PK and composite PK + // if composite PK does not constraint exactly 1 column + return PkTransitionDto.noTransition(); + } + const idOfPkColumn = idsOfColumns[0]; + const oldColumnJsonSchema = Object.values(collection.role.properties) + .find(columnJsonSchema => columnJsonSchema.GUID === idOfPkColumn); + if (!oldColumnJsonSchema) { + return PkTransitionDto.noTransition(); + } + const isOldColumnARegularPrimaryKey = oldColumnJsonSchema?.primaryKey && !oldColumnJsonSchema?.compositePrimaryKey; + if (!isOldColumnARegularPrimaryKey) { + return PkTransitionDto.noTransition(); + } + const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(oldColumnJsonSchema); + const areOptionsEqual = newPrimaryKeys.some((compositePk) => { + if (compositePk.compositePrimaryKey.length !== amountOfColumnsInRegularPk) { + return false; + } + const oldCompositePkAsRegularPkOptions = getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(compositePk); + return _(oldCompositePkAsRegularPkOptions).differenceWith(constraintOptions, _.isEqual).isEmpty(); + }); + + return PkTransitionDto.transition(!areOptionsEqual); } /** @@ -107,48 +285,56 @@ const getCreateCompositePKDDLProviderConfig = (_) => ( } /** - * @return {(collection: AlterCollectionDto) => Array} + * @return {(collection: AlterCollectionDto) => Array} * */ -const getAddCompositePkScripts = (_, ddlProvider) => (collection) => { +const getAddCompositePkScriptDtos = (_, ddlProvider) => (collection) => { const { getFullCollectionName, getSchemaOfAlterCollection, getEntityName, } = require('../../../utils/general')(_); - const didPkChange = didCompositePkChange(_)(collection); - if (!didPkChange) { - return [] + /** + * @type {AlterCollectionRoleCompModPrimaryKey} + * */ + const pkDto = collection?.role?.compMod?.primaryKey || {}; + const newPrimaryKeys = pkDto.new || []; + const oldPrimaryKeys = pkDto.old || []; + if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) { + return []; + } + const transitionToCompositeDto = wasCompositePkChangedInTransitionFromRegularToComposite(_)(collection); + if (transitionToCompositeDto.didTransitionHappen && !transitionToCompositeDto.wasPkChangedInTransition) { + return []; + } + if (newPrimaryKeys.length === oldPrimaryKeys.length) { + const areKeyArraysEqual = _(oldPrimaryKeys).differenceWith(newPrimaryKeys, _.isEqual).isEmpty(); + if (areKeyArraysEqual) { + return [] + } } const collectionSchema = getSchemaOfAlterCollection(collection); const fullTableName = getFullCollectionName(collectionSchema); const entityName = getEntityName(collectionSchema); - const pkDto = collection?.role?.compMod?.primaryKey || {}; - /** - * @type {Array} - * */ - const newPrimaryKeys = pkDto.new || []; - return newPrimaryKeys .map((newPk) => { const ddlConfig = getCreateCompositePKDDLProviderConfig(_)(newPk, entityName, collection); - return ddlProvider.createKeyConstraint( + const statementDto = ddlProvider.createKeyConstraint( fullTableName, collection.isActivated, ddlConfig ); + return new PkScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated); }) - .filter(Boolean) - .map(scriptDto => AlterScriptDto.getInstance([scriptDto.statement], scriptDto.isActivated, false)) - .filter(Boolean); + .filter(scriptDto => Boolean(scriptDto.script)); } /** - * @return {(collection: AlterCollectionDto) => Array} + * @return {(collection: AlterCollectionDto) => Array} * */ -const getDropCompositePkScripts = (_, ddlProvider) => (collection) => { +const getDropCompositePkScriptDtos = (_, ddlProvider) => (collection) => { const { getFullCollectionName, getSchemaOfAlterCollection, @@ -156,43 +342,50 @@ const getDropCompositePkScripts = (_, ddlProvider) => (collection) => { wrapInQuotes } = require('../../../utils/general')(_); - const didPkChange = didCompositePkChange(_)(collection); - if (!didPkChange) { + const pkDto = collection?.role?.compMod?.primaryKey || {}; + const newPrimaryKeys = pkDto.new || []; + const oldPrimaryKeys = pkDto.old || []; + if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) { return []; } + const transitionToCompositeDto = wasCompositePkChangedInTransitionFromCompositeToRegular(_)(collection); + if (transitionToCompositeDto.didTransitionHappen && !transitionToCompositeDto.wasPkChangedInTransition) { + return []; + } + if (newPrimaryKeys.length === oldPrimaryKeys.length) { + const areKeyArraysEqual = _(oldPrimaryKeys).differenceWith(newPrimaryKeys, _.isEqual).isEmpty(); + if (areKeyArraysEqual) { + return [] + } + } const collectionSchema = getSchemaOfAlterCollection(collection); const fullTableName = getFullCollectionName(collectionSchema); const entityName = getEntityName(collectionSchema); - const pkDto = collection?.role?.compMod?.primaryKey || {}; - /** - * @type {AlterCollectionRoleCompModPKDto[]} - * */ - const oldPrimaryKeys = pkDto.old || []; - return oldPrimaryKeys .map((oldPk) => { - let constraintName = wrapInQuotes(getDefaultConstraintName(entityName)); + let constraintName = getDefaultConstraintName(entityName); if (oldPk.constraintName) { - constraintName = wrapInQuotes(oldPk.constraintName); + constraintName = oldPk.constraintName; } - return ddlProvider.dropPkConstraint(fullTableName, constraintName); + const ddlConstraintName = wrapInQuotes(constraintName); + const script = ddlProvider.dropPkConstraint(fullTableName, ddlConstraintName); + return new PkScriptModificationDto(script, fullTableName, true, collection.isActivated); }) - .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true)) - .filter(Boolean); + .filter(scriptDto => Boolean(scriptDto.script)); } /** - * @return {(collection: AlterCollectionDto) => Array} + * @return {(collection: AlterCollectionDto) => Array} * */ -const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => { - const dropCompositePkScripts = getDropCompositePkScripts(_, ddlProvider)(collection); - const addCompositePkScripts = getAddCompositePkScripts(_, ddlProvider)(collection); +const getModifyCompositePkScriptDtos = (_, ddlProvider) => (collection) => { + const dropCompositePkScriptDtos = getDropCompositePkScriptDtos(_, ddlProvider)(collection); + const addCompositePkScriptDtos = getAddCompositePkScriptDtos(_, ddlProvider)(collection); return [ - ...dropCompositePkScripts, - ...addCompositePkScripts, + ...dropCompositePkScriptDtos, + ...addCompositePkScriptDtos, ].filter(Boolean); } @@ -291,10 +484,104 @@ const getCreateRegularPKDDLProviderConfig = (_) => ( * */ const wasFieldChangedToBeARegularPk = (_) => (columnJsonSchema, collection) => { const oldName = columnJsonSchema.compMod.oldField.name; + const oldColumnJsonSchema = collection.role.properties[oldName]; const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey; - const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey); - return isRegularPrimaryKey && !wasTheFieldAPrimaryKey; + const wasTheFieldAnyPrimaryKey = Boolean(oldColumnJsonSchema?.primaryKey); + + return isRegularPrimaryKey && !wasTheFieldAnyPrimaryKey; +} + +/** + * @return {(columnJsonSchema: AlterCollectionColumnDto, collection: AlterCollectionDto) => PkTransitionDto} + * */ +const wasRegularPkChangedInTransitionFromCompositeToRegular = (_) => (columnJsonSchema, collection) => { + const oldName = columnJsonSchema.compMod.oldField.name; + const oldColumnJsonSchema = collection.role.properties[oldName]; + + const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey; + const wasTheFieldAnyPrimaryKey = Boolean(oldColumnJsonSchema?.primaryKey); + + if (!(isRegularPrimaryKey && wasTheFieldAnyPrimaryKey)) { + return PkTransitionDto.noTransition(); + } + + /** + * @type {AlterCollectionRoleCompModPrimaryKey} + * */ + const pkDto = collection?.role?.compMod?.primaryKey || {}; + const newPrimaryKeys = pkDto.new || []; + /** + * @type {AlterCollectionRoleCompModPKDto[]} + * */ + const oldPrimaryKeys = pkDto.old || []; + const wasTheFieldACompositePrimaryKey = oldPrimaryKeys.some(compPk => compPk.compositePrimaryKey.some((pk) => pk.keyId === oldColumnJsonSchema.GUID)); + const isTheFieldACompositePrimaryKey = newPrimaryKeys.some(compPk => compPk.compositePrimaryKey.some((pk) => pk.keyId === columnJsonSchema.GUID)); + + const wasCompositePkRemoved = wasTheFieldACompositePrimaryKey && !isTheFieldACompositePrimaryKey; + + if (isRegularPrimaryKey && wasCompositePkRemoved) { + // return compare custom properties and amount of columns. + // If there was a transition and amount of composite PK columns is not equal + // to amount of regular pk columns, we must recreate PK + const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(columnJsonSchema); + const areOptionsEqual = oldPrimaryKeys.some((oldCompositePk) => { + if (oldCompositePk.compositePrimaryKey.length !== amountOfColumnsInRegularPk) { + return false; + } + const oldCompositePkAsRegularPkOptions = getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(oldCompositePk); + return _(oldCompositePkAsRegularPkOptions).differenceWith(constraintOptions, _.isEqual).isEmpty(); + }); + return PkTransitionDto.transition(!areOptionsEqual); + } + + return PkTransitionDto.noTransition(); +} + +/** + * @return {(columnJsonSchema: AlterCollectionColumnDto, collection: AlterCollectionDto) => PkTransitionDto} + * */ +const wasRegularPkChangedInTransitionFromRegularToComposite = (_) => (columnJsonSchema, collection) => { + const oldName = columnJsonSchema.compMod.oldField.name; + const oldColumnJsonSchema = collection.role.properties[oldName]; + + const wasRegularPrimaryKey = oldColumnJsonSchema.primaryKey && !oldColumnJsonSchema.compositePrimaryKey; + const isTheFieldAnyPrimaryKey = Boolean(columnJsonSchema?.primaryKey); + + if (!(wasRegularPrimaryKey && isTheFieldAnyPrimaryKey)) { + return PkTransitionDto.noTransition(); + } + + /** + * @type {AlterCollectionRoleCompModPrimaryKey} + * */ + const pkDto = collection?.role?.compMod?.primaryKey || {}; + const newPrimaryKeys = pkDto.new || []; + /** + * @type {AlterCollectionRoleCompModPKDto[]} + * */ + const oldPrimaryKeys = pkDto.old || []; + const wasTheFieldACompositePrimaryKey = oldPrimaryKeys.some(compPk => compPk.compositePrimaryKey.some((pk) => pk.keyId === oldColumnJsonSchema.GUID)); + const isTheFieldACompositePrimaryKey = newPrimaryKeys.some(compPk => compPk.compositePrimaryKey.some((pk) => pk.keyId === columnJsonSchema.GUID)); + + const wasCompositePkAdded = isTheFieldACompositePrimaryKey && !wasTheFieldACompositePrimaryKey; + + if (wasRegularPrimaryKey && wasCompositePkAdded) { + // return compare custom properties and amount of columns. + // If there was a transition and amount of composite PK columns is not equal + // to amount of regular pk columns, we must recreate PK + const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(oldColumnJsonSchema); + const areOptionsEqual = newPrimaryKeys.some((oldCompositePk) => { + if (oldCompositePk.compositePrimaryKey.length !== amountOfColumnsInRegularPk) { + return false; + } + const oldCompositePkAsRegularPkOptions = getCustomPropertiesOfCompositePkForComparisonWithRegularPkOptions(oldCompositePk); + return _(oldCompositePkAsRegularPkOptions).differenceWith(constraintOptions, _.isEqual).isEmpty(); + }); + return PkTransitionDto.transition(!areOptionsEqual); + } + + return PkTransitionDto.noTransition(); } /** @@ -306,8 +593,8 @@ const isFieldNoLongerARegularPk = (_) => (columnJsonSchema, collection) => { const oldJsonSchema = collection.role.properties[oldName]; const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey; - const isNotAPrimaryKey = !columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey; - return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey; + const isNotAnyPrimaryKey = !columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey; + return wasTheFieldARegularPrimaryKey && isNotAnyPrimaryKey; } /** @@ -315,7 +602,7 @@ const isFieldNoLongerARegularPk = (_) => (columnJsonSchema, collection) => { * */ const wasRegularPkModified = (_) => (columnJsonSchema, collection) => { const oldName = columnJsonSchema.compMod.oldField.name; - const oldJsonSchema = collection.role.properties[oldName]; + const oldJsonSchema = collection.role.properties[oldName] || {}; const isRegularPrimaryKey = columnJsonSchema.primaryKey && !columnJsonSchema.compositePrimaryKey; const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey; @@ -323,16 +610,16 @@ const wasRegularPkModified = (_) => (columnJsonSchema, collection) => { if (!(isRegularPrimaryKey && wasTheFieldARegularPrimaryKey)) { return false; } - const constraintOptions = columnJsonSchema.primaryKeyOptions; - const oldConstraintOptions = oldJsonSchema?.primaryKeyOptions; + const constraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(columnJsonSchema); + const oldConstraintOptions = getCustomPropertiesOfRegularPkForComparisonWithRegularPkOptions(oldJsonSchema); const areOptionsEqual = _(oldConstraintOptions).differenceWith(constraintOptions, _.isEqual).isEmpty(); return !areOptionsEqual; } /** - * @return {(collection: AlterCollectionDto) => Array} + * @return {(collection: AlterCollectionDto) => Array} * */ -const getAddPkScripts = (_, ddlProvider) => (collection) => { +const getAddPkScriptDtos = (_, ddlProvider) => (collection) => { const { getFullCollectionName, getSchemaOfAlterCollection, @@ -345,24 +632,31 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => { return _.toPairs(collection.properties) .filter(([name, jsonSchema]) => { - return wasFieldChangedToBeARegularPk(_)(jsonSchema, collection) || wasRegularPkModified(_)(jsonSchema, collection); + if (wasFieldChangedToBeARegularPk(_)(jsonSchema, collection)) { + return true; + } + const transitionToRegularDto = wasRegularPkChangedInTransitionFromCompositeToRegular(_)(jsonSchema, collection); + if (transitionToRegularDto.didTransitionHappen) { + return transitionToRegularDto.wasPkChangedInTransition; + } + return wasRegularPkModified(_)(jsonSchema, collection); }) .map(([name, jsonSchema]) => { const ddlConfig = getCreateRegularPKDDLProviderConfig(_)(name, jsonSchema, entityName, collection); - return ddlProvider.createKeyConstraint( + const statementDto = ddlProvider.createKeyConstraint( fullTableName, collection.isActivated, ddlConfig ); + return new PkScriptModificationDto(statementDto.statement, fullTableName, false, statementDto.isActivated); }) - .map(scriptDto => AlterScriptDto.getInstance([scriptDto.statement], scriptDto.isActivated, false)) - .filter(Boolean); + .filter(scriptDto => Boolean(scriptDto.script)); } /** - * @return {(collection: AlterCollectionDto) => Array} + * @return {(collection: AlterCollectionDto) => Array} * */ -const getDropPkScript = (_, ddlProvider) => (collection) => { +const getDropPkScriptDto = (_, ddlProvider) => (collection) => { const { getFullCollectionName, getSchemaOfAlterCollection, @@ -376,42 +670,74 @@ const getDropPkScript = (_, ddlProvider) => (collection) => { return _.toPairs(collection.properties) .filter(([name, jsonSchema]) => { - return isFieldNoLongerARegularPk(_)(jsonSchema, collection) || wasRegularPkModified(_)(jsonSchema, collection); + if (isFieldNoLongerARegularPk(_)(jsonSchema, collection)) { + return true; + } + const transitionToRegularDto = wasRegularPkChangedInTransitionFromRegularToComposite(_)(jsonSchema, collection); + if (transitionToRegularDto.didTransitionHappen) { + return transitionToRegularDto.wasPkChangedInTransition; + } + return wasRegularPkModified(_)(jsonSchema, collection); }) .map(([name, jsonSchema]) => { const oldName = jsonSchema.compMod.oldField.name; const oldJsonSchema = collection.role.properties[oldName]; - const constraintName = wrapInQuotes(getConstraintNameForRegularPk(oldJsonSchema, entityName)); - return ddlProvider.dropPkConstraint(fullTableName, constraintName); + const ddlConstraintName = wrapInQuotes(getConstraintNameForRegularPk(oldJsonSchema, entityName)); + + const script = ddlProvider.dropPkConstraint(fullTableName, ddlConstraintName); + return new PkScriptModificationDto(script, fullTableName, true, collection.isActivated); }) - .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true)) - .filter(Boolean); + .filter(scriptDto => Boolean(scriptDto.script)); } /** - * @return {(collection: AlterCollectionDto) => Array} + * @return {(collection: AlterCollectionDto) => Array} * */ -const getModifyPkScripts = (_, ddlProvider) => (collection) => { - const dropPkScripts = getDropPkScript(_, ddlProvider)(collection); - const addPkScripts = getAddPkScripts(_, ddlProvider)(collection); +const getModifyPkScriptDtos = (_, ddlProvider) => (collection) => { + const dropPkScriptDtos = getDropPkScriptDto(_, ddlProvider)(collection); + const addPkScriptDtos = getAddPkScriptDtos(_, ddlProvider)(collection); return [ - ...dropPkScripts, - ...addPkScripts, + ...dropPkScriptDtos, + ...addPkScriptDtos, ].filter(Boolean); } +/** + * @param constraintDtos {PkScriptModificationDto[]} + * @return {PkScriptModificationDto[]} + * */ +const sortModifyPkConstraints = (constraintDtos) => { + return constraintDtos.sort((c1, c2) => { + if (c1.fullTableName === c2.fullTableName) { + // Number(true) = 1, Number(false) = 0; + // This ensures that DROP script appears before CREATE script + // if the same table has 2 scripts that drop and recreate PK + return Number(c2.isDropScript) - Number(c1.isDropScript); + } + // This sorts all statements based on full table name, ASC + return c1.fullTableName < c2.fullTableName; + }); +} + /** * @return {(collection: AlterCollectionDto) => Array} * */ const getModifyPkConstraintsScriptDtos = (_, ddlProvider) => (collection) => { - const modifyCompositePkScripts = getModifyCompositePkScripts(_, ddlProvider)(collection); - const modifyPkScripts = getModifyPkScripts(_, ddlProvider)(collection); - - return [ - ...modifyCompositePkScripts, - ...modifyPkScripts, - ].filter(Boolean); + const modifyCompositePkScriptDtos = getModifyCompositePkScriptDtos(_, ddlProvider)(collection); + const modifyPkScriptDtos = getModifyPkScriptDtos(_, ddlProvider)(collection); + + const allDtos = [ + ...modifyCompositePkScriptDtos, + ...modifyPkScriptDtos, + ]; + const sortedAllDtos = sortModifyPkConstraints(allDtos); + + return sortedAllDtos + .map(dto => { + return AlterScriptDto.getInstance([dto.script], dto.isActivated, dto.isDropScript); + }) + .filter(Boolean); } module.exports = { diff --git a/forward_engineering/alterScript/types/AlterCollectionDto.js b/forward_engineering/alterScript/types/AlterCollectionDto.js index 095bff4..61c237d 100644 --- a/forward_engineering/alterScript/types/AlterCollectionDto.js +++ b/forward_engineering/alterScript/types/AlterCollectionDto.js @@ -53,7 +53,8 @@ class AlterCollectionColumnPrimaryKeyOptionDto { /** * @type {Array<{ - * keyId: string + * keyId: string, + * type?: string, * }>} * */ indexInclude @@ -166,6 +167,18 @@ class AlterCollectionRoleCompModPKDto extends AlterCollectionColumnPrimaryKeyOpt } +class AlterCollectionRoleCompModPrimaryKey { + /** + * @type {AlterCollectionRoleCompModPKDto[] | undefined} + * */ + new + /** + * @type {AlterCollectionRoleCompModPKDto[] | undefined} + * */ + old + +} + class AlterCollectionRoleCompModDto { /** * @type {string} @@ -222,10 +235,7 @@ class AlterCollectionRoleCompModDto { on_commit /** - * @type {{ - * new: Array, - * old: Array, - * }} + * @type {AlterCollectionRoleCompModPrimaryKey} */ primaryKey @@ -409,5 +419,6 @@ module.exports = { AlterCollectionDto, AlterCollectionRoleDto, AlterCollectionColumnDto, + AlterCollectionRoleCompModPrimaryKey, AlterCollectionRoleCompModPKDto, } diff --git a/properties_pane/field_level/fieldLevelConfig.json b/properties_pane/field_level/fieldLevelConfig.json index 31a7301..6423763 100644 --- a/properties_pane/field_level/fieldLevelConfig.json +++ b/properties_pane/field_level/fieldLevelConfig.json @@ -2,9 +2,9 @@ * Copyright © 2016-2017 by IntegrIT S.A. dba Hackolade. All rights reserved. * * The copyright to the computer software herein is the property of IntegrIT S.A. -* The software may be used and/or copied only with the written permission of -* IntegrIT S.A. or in accordance with the terms and conditions stipulated in -* the agreement/contract under which the software has been supplied. +* The software may be used and/or copied only with the written permission of +* IntegrIT S.A. or in accordance with the terms and conditions stipulated in +* the agreement/contract under which the software has been supplied. In order to define custom properties for any object's properties pane, you may copy/paste from the following, @@ -71,8 +71,8 @@ making sure that you maintain a proper JSON format. ] }, // “groupInput” can have the following states - 0 items, 1 item, and many items. -// “blockInput” has only 2 states - 0 items or 1 item. -// This gives us an easy way to represent it as an object and not as an array internally which is beneficial for processing +// “blockInput” has only 2 states - 0 items or 1 item. +// This gives us an easy way to represent it as an object and not as an array internally which is beneficial for processing // and forward-engineering in particular. { "propertyName": "Block", @@ -100,7 +100,7 @@ making sure that you maintain a proper JSON format. "propertyKeyword": "keyList", "propertyType": "fieldList", "template": "orderedList" - }, + }, { "propertyName": "List with attribute", "propertyKeyword": "keyListOrder", @@ -193,7 +193,33 @@ making sure that you maintain a proper JSON format. "propertyName": "Not null", "propertyKeyword": "required", "enableForReference": true, - "propertyType": "checkbox" + "propertyType": "checkbox", + "dependency": { + "key": "primaryKey", + "value": true + }, + "disabled": true, + "defaultValue": true + }, + { + "propertyName": "Not null", + "propertyKeyword": "required", + "enableForReference": true, + "propertyType": "checkbox", + "dependency": { + "type": "or", + "values": [ + { + "key": "primaryKey", + "value": false + }, + { + "key": "primaryKey", + "exists": false + } + ] + }, + "defaultValue": false }, "default", { @@ -214,7 +240,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -1123,7 +1149,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -1608,7 +1634,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -2082,7 +2108,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -2518,7 +2544,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -3059,7 +3085,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -3475,7 +3501,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -3912,7 +3938,7 @@ making sure that you maintain a proper JSON format. "tooltip": "This column is part of the table composite primary key definition. Please refer to this definition if you want more information or to update the Primary Key definition", "dependency": { "key": "compositePrimaryKey", - "value": true + "value": true } }, { @@ -4771,4 +4797,4 @@ making sure that you maintain a proper JSON format. } ] } -} \ No newline at end of file +}