|
1 | 1 | const {checkFieldPropertiesChanged} = require('./common'); |
2 | 2 |
|
| 3 | +/** |
| 4 | + * @typedef {{ |
| 5 | + * id: string, |
| 6 | + * chkConstrName: string, |
| 7 | + * constrExpression: string, |
| 8 | + * }} CheckConstraint |
| 9 | + * |
| 10 | + * @typedef {{ |
| 11 | + * old?: CheckConstraint, |
| 12 | + * new?: CheckConstraint |
| 13 | + * }} CheckConstraintHistoryEntry |
| 14 | + * */ |
| 15 | + |
| 16 | +const getFullTableName = (_) => (collection) => { |
| 17 | + const {getEntityName} = require('../../utils/general')(_); |
| 18 | + const {getNamePrefixedWithSchemaName} = require('../general')({_}); |
| 19 | + |
| 20 | + const collectionSchema = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; |
| 21 | + const tableName = getEntityName(collectionSchema); |
| 22 | + const schemaName = collectionSchema.compMod?.keyspaceName; |
| 23 | + return getNamePrefixedWithSchemaName(tableName, schemaName); |
| 24 | +} |
| 25 | + |
3 | 26 | const getAddCollectionScript = |
4 | 27 | ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => |
5 | 28 | collection => { |
@@ -48,17 +71,117 @@ const getAddCollectionScript = |
48 | 71 |
|
49 | 72 | const getDeleteCollectionScript = app => collection => { |
50 | 73 | const _ = app.require('lodash'); |
51 | | - const {getEntityName} = require('../../utils/general')(_); |
52 | | - const {getNamePrefixedWithSchemaName} = require('../general')({_}); |
53 | | - |
54 | | - const jsonData = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; |
55 | | - const tableName = getEntityName(jsonData); |
56 | | - const schemaName = collection.compMod.keyspaceName; |
57 | | - const fullName = getNamePrefixedWithSchemaName(tableName, schemaName); |
58 | | - |
| 74 | + const fullName = getFullTableName(_)(collection); |
59 | 75 | return `DROP TABLE IF EXISTS ${fullName};`; |
60 | 76 | }; |
61 | 77 |
|
| 78 | +/** |
| 79 | + * @return {(collection: Object) => Array<CheckConstraintHistoryEntry>} |
| 80 | + * */ |
| 81 | +const mapCheckConstraintNamesToChangeHistory = (_) => (collection) => { |
| 82 | + const checkConstraintHistory = collection?.compMod?.chkConstr; |
| 83 | + if (!checkConstraintHistory) { |
| 84 | + return []; |
| 85 | + } |
| 86 | + const newConstraints = checkConstraintHistory.new || []; |
| 87 | + const oldConstraints = checkConstraintHistory.old || []; |
| 88 | + const constrNames = _.chain([ ...newConstraints, ...oldConstraints ]) |
| 89 | + .map(constr => constr.chkConstrName) |
| 90 | + .uniq() |
| 91 | + .value(); |
| 92 | + |
| 93 | + return constrNames.map(chkConstrName => { |
| 94 | + return { |
| 95 | + old: _.find(oldConstraints, { chkConstrName }), |
| 96 | + new: _.find(newConstraints, { chkConstrName }), |
| 97 | + }; |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * @return {(constraintHistory: Array<CheckConstraintHistoryEntry>, fullTableName: string) => Array<string>} |
| 103 | + * */ |
| 104 | +const getDropCheckConstraintScripts = (_, ddlProvider) => (constraintHistory, fullTableName) => { |
| 105 | + const {wrapInQuotes} = require('../general')({_}); |
| 106 | + |
| 107 | + return constraintHistory |
| 108 | + .filter(historyEntry => historyEntry.old && !historyEntry.new) |
| 109 | + .map(historyEntry => { |
| 110 | + const wrappedConstraintName = wrapInQuotes(historyEntry.old.chkConstrName); |
| 111 | + return ddlProvider.dropConstraint(fullTableName, wrappedConstraintName); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * @return {(constraintHistory: Array<CheckConstraintHistoryEntry>, fullTableName: string) => Array<string>} |
| 117 | + * */ |
| 118 | +const getAddCheckConstraintScripts = (_, ddlProvider) => (constraintHistory, fullTableName) => { |
| 119 | + const {wrapInQuotes} = require('../general')({_}); |
| 120 | + |
| 121 | + return constraintHistory |
| 122 | + .filter(historyEntry => historyEntry.new && !historyEntry.old) |
| 123 | + .map(historyEntry => { |
| 124 | + const { chkConstrName, constrExpression } = historyEntry.new; |
| 125 | + return ddlProvider.addCheckConstraint(fullTableName, wrapInQuotes(chkConstrName), constrExpression); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * @return {(constraintHistory: Array<CheckConstraintHistoryEntry>, fullTableName: string) => Array<string>} |
| 131 | + * */ |
| 132 | +const getUpdateCheckConstraintScripts = (_, ddlProvider) => (constraintHistory, fullTableName) => { |
| 133 | + const {wrapInQuotes} = require('../general')({_}); |
| 134 | + |
| 135 | + return constraintHistory |
| 136 | + .filter(historyEntry => { |
| 137 | + if (historyEntry.old && historyEntry.new) { |
| 138 | + const oldExpression = historyEntry.old.constrExpression; |
| 139 | + const newExpression = historyEntry.new.constrExpression; |
| 140 | + return oldExpression !== newExpression; |
| 141 | + } |
| 142 | + return false; |
| 143 | + }) |
| 144 | + .map(historyEntry => { |
| 145 | + const { chkConstrName: oldConstrainName } = historyEntry.old; |
| 146 | + const dropConstraintScript = ddlProvider.dropConstraint(fullTableName, wrapInQuotes(oldConstrainName)); |
| 147 | + |
| 148 | + const { chkConstrName: newConstrainName, constrExpression: newConstraintExpression } = historyEntry.new; |
| 149 | + const addConstraintScript = ddlProvider.addCheckConstraint(fullTableName, wrapInQuotes(newConstrainName), newConstraintExpression); |
| 150 | + |
| 151 | + return [dropConstraintScript, addConstraintScript]; |
| 152 | + }) |
| 153 | + .flat(); |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * @return (collection: Object) => Array<string> |
| 158 | + * */ |
| 159 | +const getModifyCheckConstraintScripts = (_, ddlProvider) => (collection) => { |
| 160 | + const fullTableName = getFullTableName(_)(collection); |
| 161 | + const constraintHistory = mapCheckConstraintNamesToChangeHistory(_)(collection); |
| 162 | + |
| 163 | + const addCheckConstraintScripts = getAddCheckConstraintScripts(_, ddlProvider)(constraintHistory, fullTableName); |
| 164 | + const dropCheckConstraintScripts = getDropCheckConstraintScripts(_, ddlProvider)(constraintHistory, fullTableName); |
| 165 | + const updateCheckConstraintScripts = getUpdateCheckConstraintScripts(_, ddlProvider)(constraintHistory, fullTableName); |
| 166 | + |
| 167 | + return [ |
| 168 | + ...addCheckConstraintScripts, |
| 169 | + ...dropCheckConstraintScripts, |
| 170 | + ...updateCheckConstraintScripts, |
| 171 | + ] |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * @return (collection: Object) => Array<string> |
| 176 | + * */ |
| 177 | +const getModifyCollectionScript = (app) => (collection) => { |
| 178 | + const _ = app.require('lodash'); |
| 179 | + const ddlProvider = require('../../ddlProvider')(null, null, app); |
| 180 | + |
| 181 | + const modifyCheckConstraintScripts = getModifyCheckConstraintScripts(_, ddlProvider)(collection); |
| 182 | + return [...modifyCheckConstraintScripts] |
| 183 | +} |
| 184 | + |
62 | 185 | const getAddColumnScript = |
63 | 186 | ({app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}) => |
64 | 187 | collection => { |
@@ -113,16 +236,6 @@ const getDeleteColumnScript = app => collection => { |
113 | 236 | .map(([name]) => `ALTER TABLE IF EXISTS ${fullName} DROP COLUMN IF EXISTS ${wrapInQuotes(name)};`); |
114 | 237 | }; |
115 | 238 |
|
116 | | -const getFullTableName = (_) => (collection) => { |
117 | | - const {getEntityName} = require('../../utils/general')(_); |
118 | | - const {getNamePrefixedWithSchemaName} = require('../general')({_}); |
119 | | - |
120 | | - const collectionSchema = {...collection, ...(_.omit(collection?.role, 'properties') || {})}; |
121 | | - const tableName = getEntityName(collectionSchema); |
122 | | - const schemaName = collectionSchema.compMod?.keyspaceName; |
123 | | - return getNamePrefixedWithSchemaName(tableName, schemaName); |
124 | | -} |
125 | | - |
126 | 239 | const hasLengthChanged = (collection, oldFieldName, currentJsonSchema) => { |
127 | 240 | const oldProperty = collection.role.properties[oldFieldName]; |
128 | 241 |
|
@@ -231,6 +344,7 @@ const getModifyColumnScript = app => collection => { |
231 | 344 | module.exports = { |
232 | 345 | getAddCollectionScript, |
233 | 346 | getDeleteCollectionScript, |
| 347 | + getModifyCollectionScript, |
234 | 348 | getAddColumnScript, |
235 | 349 | getDeleteColumnScript, |
236 | 350 | getModifyColumnScript, |
|
0 commit comments