|
| 1 | +const { AlterScriptDto } = require('../../types/AlterScriptDto'); |
| 2 | +const {AlterCollectionDto, AlterCollectionColumnDto,AlterCollectionRoleCompModPKDto} = require('../../types/AlterCollectionDto'); |
| 3 | + |
| 4 | + |
| 5 | +/** |
| 6 | + * @return {(collection: AlterCollectionDto, guid: string) => AlterCollectionColumnDto | undefined} |
| 7 | + * */ |
| 8 | +const getPropertyByGuid = (_) => (collection, guid) => { |
| 9 | + const propertyInArray = _.toPairs(collection?.role?.properties) |
| 10 | + .filter(([name, jsonSchema]) => jsonSchema.GUID === guid) |
| 11 | + .map(([name, jsonSchema]) => jsonSchema); |
| 12 | + if (!propertyInArray.length) { |
| 13 | + return undefined; |
| 14 | + } |
| 15 | + return propertyInArray[0]; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @return {(collection: AlterCollectionDto, guids: string[]) => Array<AlterCollectionColumnDto>} |
| 20 | + * */ |
| 21 | +const getPropertiesByGuids = (_) => (collection, guids) => { |
| 22 | + return guids |
| 23 | + .map(guid => getPropertyByGuid(_)(collection, guid)) |
| 24 | + .filter(Boolean); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @return {(collection: AlterCollectionDto) => boolean} |
| 29 | + * */ |
| 30 | +const didCompositePkChange = (_) => (collection) => { |
| 31 | + const pkDto = collection?.role?.compMod?.primaryKey || {}; |
| 32 | + const newPrimaryKeys = pkDto.new || []; |
| 33 | + const oldPrimaryKeys = pkDto.old || []; |
| 34 | + if (newPrimaryKeys.length !== oldPrimaryKeys.length) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + if (newPrimaryKeys.length === 0 && oldPrimaryKeys.length === 0) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + const areKeyArraysEqual = _(oldPrimaryKeys).differenceWith(newPrimaryKeys, _.isEqual).isEmpty(); |
| 41 | + return !areKeyArraysEqual; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>} |
| 46 | + * */ |
| 47 | +const getAddCompositePkScripts = (_, ddlProvider) => (collection) => { |
| 48 | + const didPkChange = didCompositePkChange(_)(collection); |
| 49 | + if (!didPkChange) { |
| 50 | + return [] |
| 51 | + } |
| 52 | + const fullTableName = generateFullEntityName(collection); |
| 53 | + const constraintName = getEntityNameFromCollection(collection) + '_pk'; |
| 54 | + const pkDto = collection?.role?.compMod?.primaryKey || {}; |
| 55 | + const newPrimaryKeys = pkDto.new || []; |
| 56 | + |
| 57 | + return newPrimaryKeys |
| 58 | + .map((newPk) => { |
| 59 | + /** |
| 60 | + * @type {Array<AlterCollectionRoleCompModPKDto>} |
| 61 | + * */ |
| 62 | + const compositePrimaryKey = newPk.compositePrimaryKey || []; |
| 63 | + const guidsOfColumnsInPk = compositePrimaryKey.map((compositePkEntry) => compositePkEntry.keyId); |
| 64 | + const columnsInPk = getPropertiesByGuids(_)(collection, guidsOfColumnsInPk); |
| 65 | + const columnNamesForDDL = columnsInPk.map(column => prepareName(column.compMod.newField.name)); |
| 66 | + if (!columnNamesForDDL.length) { |
| 67 | + return undefined; |
| 68 | + } |
| 69 | + return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL); |
| 70 | + }) |
| 71 | + .filter(Boolean) |
| 72 | + .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false)) |
| 73 | + .filter(Boolean); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>} |
| 78 | + * */ |
| 79 | +const getDropCompositePkScripts = (_, ddlProvider) => (collection) => { |
| 80 | + const didPkChange = didCompositePkChange(_)(collection); |
| 81 | + if (!didPkChange) { |
| 82 | + return [] |
| 83 | + } |
| 84 | + const fullTableName = generateFullEntityName(collection); |
| 85 | + const pkDto = collection?.role?.compMod?.primaryKey || {}; |
| 86 | + const oldPrimaryKeys = pkDto.old || []; |
| 87 | + return oldPrimaryKeys |
| 88 | + .map(oldPk => ddlProvider.dropPkConstraint(fullTableName)) |
| 89 | + .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true)) |
| 90 | + .filter(Boolean); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>} |
| 95 | + * */ |
| 96 | +const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => { |
| 97 | + const dropCompositePkScripts = getDropCompositePkScripts(_, ddlProvider)(collection); |
| 98 | + const addCompositePkScripts = getAddCompositePkScripts(_, ddlProvider)(collection); |
| 99 | + |
| 100 | + return [ |
| 101 | + ...dropCompositePkScripts, |
| 102 | + ...addCompositePkScripts, |
| 103 | + ].filter(Boolean); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>} |
| 108 | + * */ |
| 109 | +const getAddPkScripts = (_, ddlProvider) => (collection) => { |
| 110 | + const { getFullCollectionName, getSchemaOfAlterCollection } = require('../../../utils/general')(_); |
| 111 | + const collectionSchema = getSchemaOfAlterCollection(collection); |
| 112 | + |
| 113 | + const fullTableName = getFullCollectionName(collectionSchema); |
| 114 | + const constraintName = getEntityNameFromCollection(collection) + '_pk'; |
| 115 | + |
| 116 | + return _.toPairs(collection.properties) |
| 117 | + .filter(([name, jsonSchema]) => { |
| 118 | + const isRegularPrimaryKey = jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey; |
| 119 | + const oldName = jsonSchema.compMod.oldField.name; |
| 120 | + const wasTheFieldAPrimaryKey = Boolean(collection.role.properties[oldName]?.primaryKey); |
| 121 | + return isRegularPrimaryKey && !wasTheFieldAPrimaryKey; |
| 122 | + }) |
| 123 | + .map(([name, jsonSchema]) => { |
| 124 | + const nameForDDl = prepareName(name); |
| 125 | + const columnNamesForDDL = [nameForDDl]; |
| 126 | + return ddlProvider.addPkConstraint(fullTableName, constraintName, columnNamesForDDL); |
| 127 | + }) |
| 128 | + .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, false)) |
| 129 | + .filter(Boolean); |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>} |
| 134 | + * */ |
| 135 | +const getDropPkScripts = (_, ddlProvider) => (collection) => { |
| 136 | + const fullTableName = generateFullEntityName(collection); |
| 137 | + |
| 138 | + return _.toPairs(collection.properties) |
| 139 | + .filter(([name, jsonSchema]) => { |
| 140 | + const oldName = jsonSchema.compMod.oldField.name; |
| 141 | + const oldJsonSchema = collection.role.properties[oldName]; |
| 142 | + const wasTheFieldARegularPrimaryKey = oldJsonSchema?.primaryKey && !oldJsonSchema?.compositePrimaryKey; |
| 143 | + |
| 144 | + const isNotAPrimaryKey = !jsonSchema.primaryKey && !jsonSchema.compositePrimaryKey; |
| 145 | + return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey; |
| 146 | + }) |
| 147 | + .map(([name, jsonSchema]) => { |
| 148 | + return ddlProvider.dropConstraint(fullTableName, ); |
| 149 | + }) |
| 150 | + .map(scriptLine => AlterScriptDto.getInstance([scriptLine], collection.isActivated, true)) |
| 151 | + .filter(Boolean); |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>} |
| 156 | + * */ |
| 157 | +const getModifyPkScripts = (_, ddlProvider) => (collection) => { |
| 158 | + const dropPkScripts = getDropPkScripts(_, ddlProvider)(collection); |
| 159 | + const addPkScripts = getAddPkScripts(_, ddlProvider)(collection); |
| 160 | + |
| 161 | + return [ |
| 162 | + ...dropPkScripts, |
| 163 | + ...addPkScripts, |
| 164 | + ].filter(Boolean); |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto>} |
| 169 | + * */ |
| 170 | +const getModifyPkConstraintsScriptDtos = (_, ddlProvider) => (collection) => { |
| 171 | + const modifyCompositePkScripts = getModifyCompositePkScripts(_, ddlProvider)(collection); |
| 172 | + const modifyPkScripts = getModifyPkScripts(_, ddlProvider)(collection); |
| 173 | + |
| 174 | + return [ |
| 175 | + ...modifyCompositePkScripts, |
| 176 | + ...modifyPkScripts, |
| 177 | + ].filter(Boolean); |
| 178 | +} |
| 179 | + |
| 180 | +module.exports = { |
| 181 | + getModifyPkConstraintsScriptDtos, |
| 182 | +} |
0 commit comments