|
| 1 | +const _ = require('lodash'); |
| 2 | +const { AlterScriptDto } = require('../../types/AlterScriptDto'); |
| 3 | +const { |
| 4 | + getFullTableName, |
| 5 | + wrapInQuotes, |
| 6 | + isObjectInDeltaModelActivated, |
| 7 | + isParentContainerActivated, |
| 8 | +} = require('../../../utils/general'); |
| 9 | +const assignTemplates = require('../../../utils/assignTemplates'); |
| 10 | +const templates = require('../../../ddlProvider/templates'); |
| 11 | + |
| 12 | +/** |
| 13 | + * @typedef {{ |
| 14 | + * name: string, |
| 15 | + * expression: string, |
| 16 | + * noInherit?: boolean, |
| 17 | + * }} ColumnCheckConstraint |
| 18 | + * |
| 19 | + * @typedef {{ |
| 20 | + * old?: ColumnCheckConstraint, |
| 21 | + * new?: ColumnCheckConstraint, |
| 22 | + * columnName: string, |
| 23 | + * isActivated: boolean, |
| 24 | + * }} ColumnCheckConstraintHistoryEntry |
| 25 | + * */ |
| 26 | + |
| 27 | +/** |
| 28 | + * |
| 29 | + * @param {string} [constraintName] |
| 30 | + * @param {string} columnName |
| 31 | + * @param {string} tableName |
| 32 | + * @returns {string} |
| 33 | + */ |
| 34 | +const getConstraintName = (constraintName, columnName, tableName) => { |
| 35 | + return wrapInQuotes(constraintName || `chk_${tableName}.${columnName}`); |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * @param {string} tableName |
| 40 | + * @param {string} constraintName |
| 41 | + * @return string |
| 42 | + * */ |
| 43 | +const dropConstraint = (tableName, constraintName) => { |
| 44 | + const templateConfig = { |
| 45 | + tableName, |
| 46 | + constraintName, |
| 47 | + }; |
| 48 | + return assignTemplates(templates.dropConstraint, templateConfig); |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * @param tableName {string} |
| 53 | + * @param constraintName {string} |
| 54 | + * @param expression {string} |
| 55 | + * @param noInherit {boolean} |
| 56 | + * @return string |
| 57 | + * */ |
| 58 | +const addCheckConstraint = (tableName, constraintName, expression, noInherit = false) => { |
| 59 | + const templateConfig = { |
| 60 | + tableName, |
| 61 | + constraintName, |
| 62 | + expression, |
| 63 | + noInherit: noInherit ? ' NO INHERIT' : '', |
| 64 | + }; |
| 65 | + return assignTemplates(templates.addCheckConstraint, templateConfig); |
| 66 | +}; |
| 67 | + |
| 68 | +/** |
| 69 | + * @param {Object} collection |
| 70 | + * @return {ColumnCheckConstraintHistoryEntry[]} |
| 71 | + * */ |
| 72 | +const mapColumnCheckConstraintsToChangeHistory = collection => { |
| 73 | + const history = []; |
| 74 | + |
| 75 | + _.toPairs(collection.properties).forEach(([columnName, jsonSchema]) => { |
| 76 | + const oldColumnName = jsonSchema.compMod?.oldField?.name || columnName; |
| 77 | + const newCheckConstraint = !_.isEmpty(jsonSchema.checkConstraint) |
| 78 | + ? _.omit(_.first(jsonSchema.checkConstraint), 'id') |
| 79 | + : undefined; |
| 80 | + |
| 81 | + const oldCheckConstraintValue = collection.role.properties?.[oldColumnName]?.checkConstraint; |
| 82 | + const oldCheckConstraint = !_.isEmpty(oldCheckConstraintValue) |
| 83 | + ? _.omit(_.first(oldCheckConstraintValue), 'id') |
| 84 | + : undefined; |
| 85 | + |
| 86 | + if (!newCheckConstraint && !oldCheckConstraint) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + history.push({ |
| 91 | + columnName, |
| 92 | + old: oldCheckConstraint, |
| 93 | + new: newCheckConstraint, |
| 94 | + isActivated: jsonSchema.isActivated, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + return history; |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * @param {ColumnCheckConstraintHistoryEntry[]} constraintHistory |
| 103 | + * @param {string} fullTableName |
| 104 | + * @return {AlterScriptDto[]} |
| 105 | + * */ |
| 106 | +const getDropColumnCheckConstraintScriptDtos = (constraintHistory, fullTableName) => { |
| 107 | + return constraintHistory |
| 108 | + .filter(historyEntry => historyEntry.old && !historyEntry.new) |
| 109 | + .map(historyEntry => { |
| 110 | + const wrappedConstraintName = getConstraintName( |
| 111 | + historyEntry.old.name, |
| 112 | + historyEntry.columnName, |
| 113 | + fullTableName, |
| 114 | + ); |
| 115 | + const script = dropConstraint(fullTableName, wrappedConstraintName); |
| 116 | + return AlterScriptDto.getInstance([script], historyEntry.isActivated, true); |
| 117 | + }); |
| 118 | +}; |
| 119 | + |
| 120 | +/** |
| 121 | + * @param {ColumnCheckConstraintHistoryEntry[]} constraintHistory |
| 122 | + * @param {string} fullTableName |
| 123 | + * @return {AlterScriptDto[]} |
| 124 | + * */ |
| 125 | +const getAddColumnCheckConstraintScriptDtos = (constraintHistory, fullTableName) => { |
| 126 | + return constraintHistory |
| 127 | + .filter(historyEntry => historyEntry.new && !historyEntry.old) |
| 128 | + .map(historyEntry => { |
| 129 | + const { name, expression, noInherit } = historyEntry.new; |
| 130 | + const constraintName = getConstraintName(name, historyEntry.columnName, fullTableName); |
| 131 | + |
| 132 | + const script = addCheckConstraint(fullTableName, constraintName, expression, noInherit); |
| 133 | + return AlterScriptDto.getInstance([script], historyEntry.isActivated); |
| 134 | + }); |
| 135 | +}; |
| 136 | + |
| 137 | +/** |
| 138 | + * @param {ColumnCheckConstraintHistoryEntry[]} constraintHistory |
| 139 | + * @param {string} fullTableName |
| 140 | + * @return {AlterScriptDto[]} |
| 141 | + * */ |
| 142 | +const getUpdateColumnCheckConstraintScriptDtos = (constraintHistory, fullTableName) => { |
| 143 | + return constraintHistory |
| 144 | + .filter(historyEntry => { |
| 145 | + if (historyEntry.old && historyEntry.new) { |
| 146 | + const oldExpression = historyEntry.old.expression; |
| 147 | + const newExpression = historyEntry.new.expression; |
| 148 | + const oldNoInherit = historyEntry.old.noInherit; |
| 149 | + const newNoInherit = historyEntry.new.noInherit; |
| 150 | + const oldName = historyEntry.old.name; |
| 151 | + const newName = historyEntry.new.name; |
| 152 | + return oldExpression !== newExpression || oldNoInherit !== newNoInherit || oldName !== newName; |
| 153 | + } |
| 154 | + return false; |
| 155 | + }) |
| 156 | + .map(historyEntry => { |
| 157 | + const { name: oldConstrainName } = historyEntry.old; |
| 158 | + const wrappedOldConstraintName = getConstraintName( |
| 159 | + oldConstrainName, |
| 160 | + historyEntry.columnName, |
| 161 | + fullTableName, |
| 162 | + ); |
| 163 | + const dropConstraintScript = dropConstraint(fullTableName, wrappedOldConstraintName); |
| 164 | + |
| 165 | + const { |
| 166 | + name: newConstrainName, |
| 167 | + expression: newConstraintExpression, |
| 168 | + noInherit: newNoInherit, |
| 169 | + } = historyEntry.new; |
| 170 | + const addConstraintScript = addCheckConstraint( |
| 171 | + fullTableName, |
| 172 | + getConstraintName(newConstrainName, historyEntry.columnName, fullTableName), |
| 173 | + newConstraintExpression, |
| 174 | + newNoInherit, |
| 175 | + ); |
| 176 | + |
| 177 | + return [ |
| 178 | + AlterScriptDto.getInstance([dropConstraintScript], historyEntry.isActivated, true), |
| 179 | + AlterScriptDto.getInstance([addConstraintScript], historyEntry.isActivated, false), |
| 180 | + ]; |
| 181 | + }) |
| 182 | + .flat(); |
| 183 | +}; |
| 184 | + |
| 185 | +/** |
| 186 | + * @param {Object} collection |
| 187 | + * @return {AlterScriptDto[]} |
| 188 | + * */ |
| 189 | +const getModifyColumnCheckConstraintScriptDtos = collection => { |
| 190 | + const fullTableName = getFullTableName(collection); |
| 191 | + const constraintHistory = mapColumnCheckConstraintsToChangeHistory(collection); |
| 192 | + |
| 193 | + const isContainerActivated = isParentContainerActivated(collection); |
| 194 | + const isCollectionActivated = isObjectInDeltaModelActivated(collection); |
| 195 | + |
| 196 | + const addCheckConstraintScripts = getAddColumnCheckConstraintScriptDtos(constraintHistory, fullTableName); |
| 197 | + const dropCheckConstraintScripts = getDropColumnCheckConstraintScriptDtos(constraintHistory, fullTableName); |
| 198 | + const updateCheckConstraintScripts = getUpdateColumnCheckConstraintScriptDtos(constraintHistory, fullTableName); |
| 199 | + |
| 200 | + return [...addCheckConstraintScripts, ...dropCheckConstraintScripts, ...updateCheckConstraintScripts].map(dto => ({ |
| 201 | + ...dto, |
| 202 | + isActivated: isContainerActivated && isCollectionActivated && dto.isActivated, |
| 203 | + })); |
| 204 | +}; |
| 205 | + |
| 206 | +module.exports = { |
| 207 | + getModifyColumnCheckConstraintScriptDtos, |
| 208 | +}; |
0 commit comments