|
| 1 | +const { |
| 2 | + getFullEntityName, |
| 3 | + generateFullEntityName, |
| 4 | + getEntityProperties, |
| 5 | + getContainerName, |
| 6 | + getEntityData, |
| 7 | + getEntityName, |
| 8 | + prepareScript, |
| 9 | + hydrateProperty, |
| 10 | +} = require('./generalHelper'); |
| 11 | +const { replaceSpaceWithUnderscore, prepareName, commentDeactivatedStatements } = require('../generalHelper'); |
| 12 | + |
| 13 | +const templates = require('./config/templates'); |
| 14 | +const { getItems } = require('./common'); |
| 15 | + |
| 16 | +const getRelationshipName = relationship => { |
| 17 | + return relationship.role.code || relationship.role.name; |
| 18 | +}; |
| 19 | + |
| 20 | +const getFullParentTableName = relationship => { |
| 21 | + const compMod = relationship.role.compMod; |
| 22 | + |
| 23 | + const parentDBName = replaceSpaceWithUnderscore(prepareName(compMod.parent.bucket.name)); |
| 24 | + const parentEntityName = replaceSpaceWithUnderscore(compMod.parent.collection.name); |
| 25 | + |
| 26 | + return getFullEntityName(parentDBName, parentEntityName); |
| 27 | +}; |
| 28 | + |
| 29 | +const getFullChildTableName = relationship => { |
| 30 | + const compMod = relationship.role.compMod; |
| 31 | + |
| 32 | + const childDBName = replaceSpaceWithUnderscore(prepareName(compMod.child.bucket.name)); |
| 33 | + const childEntityName = replaceSpaceWithUnderscore(compMod.child.collection.name); |
| 34 | + return getFullEntityName(childDBName, childEntityName); |
| 35 | +}; |
| 36 | + |
| 37 | +const getAddSingleForeignKeyScript = provider => relationship => { |
| 38 | + const compMod = relationship.role.compMod; |
| 39 | + const parentTableName = getFullParentTableName(relationship); |
| 40 | + const childTableName = getFullChildTableName(relationship); |
| 41 | + |
| 42 | + const relationshipName = compMod.code?.new || compMod.name?.new || getRelationshipName(relationship) || ''; |
| 43 | + const constraintName = relationshipName.includes(' ') ? `\`${relationshipName}\`` : relationshipName; |
| 44 | + const childColumns = compMod.child.collection.fkFields.map(field => prepareName(field.name)); |
| 45 | + const parentColumns = compMod.parent.collection.fkFields.map(field => prepareName(field.name)); |
| 46 | + const disableNoValidate = relationship.role?.compMod?.customProperties?.new?.disableNoValidate; |
| 47 | + const disableNoValidateClause = disableNoValidate ? ' DISABLE NOVALIDATE' : ''; |
| 48 | + |
| 49 | + return provider.assignTemplates(templates.addFkConstraint, { |
| 50 | + childTableName, |
| 51 | + constraintName, |
| 52 | + childColumns, |
| 53 | + parentTableName, |
| 54 | + parentColumns, |
| 55 | + disableNoValidate: disableNoValidateClause, |
| 56 | + }); |
| 57 | +}; |
| 58 | + |
| 59 | +const canRelationshipBeAdded = relationship => { |
| 60 | + const compMod = relationship.role.compMod; |
| 61 | + if (!compMod) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + return [ |
| 65 | + compMod.code?.new || compMod.name?.new || getRelationshipName(relationship), |
| 66 | + compMod.parent?.bucket, |
| 67 | + compMod.parent?.collection, |
| 68 | + compMod.parent?.collection?.fkFields?.length, |
| 69 | + compMod.child?.bucket, |
| 70 | + compMod.child?.collection, |
| 71 | + compMod.child?.collection?.fkFields?.length, |
| 72 | + ].every(property => Boolean(property)); |
| 73 | +}; |
| 74 | + |
| 75 | +const getAddForeignKeyScript = provider => relationship => { |
| 76 | + const script = getAddSingleForeignKeyScript(provider)(relationship); |
| 77 | + const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new); |
| 78 | + |
| 79 | + return commentDeactivatedStatements(script, isActivated); |
| 80 | +}; |
| 81 | + |
| 82 | +const getDeleteSingleForeignKeyScript = provider => relationship => { |
| 83 | + const compMod = relationship.role.compMod; |
| 84 | + const tableName = getFullChildTableName(relationship); |
| 85 | + const relationshipName = compMod.code?.old || compMod.name?.old || getRelationshipName(relationship) || ''; |
| 86 | + const constraintName = prepareName(relationshipName); |
| 87 | + |
| 88 | + return provider.assignTemplates(templates.dropConstraint, { |
| 89 | + tableName, |
| 90 | + constraintName, |
| 91 | + }); |
| 92 | +}; |
| 93 | + |
| 94 | +const canRelationshipBeDeleted = relationship => { |
| 95 | + const compMod = relationship.role.compMod; |
| 96 | + if (!compMod) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + return [ |
| 100 | + compMod.code?.old || compMod.name?.old || getRelationshipName(relationship), |
| 101 | + compMod.child?.bucket, |
| 102 | + compMod.child?.collection, |
| 103 | + ].every(property => Boolean(property)); |
| 104 | +}; |
| 105 | + |
| 106 | +const getDeleteForeignKeyScripts = provider => deletedRelationships => { |
| 107 | + return deletedRelationships |
| 108 | + .filter(relationship => canRelationshipBeDeleted(relationship)) |
| 109 | + .map(relationship => { |
| 110 | + const script = getDeleteSingleForeignKeyScript(provider)(relationship); |
| 111 | + const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new); |
| 112 | + |
| 113 | + return commentDeactivatedStatements(script, isActivated); |
| 114 | + }); |
| 115 | +}; |
| 116 | + |
| 117 | +const getModifyForeignKeyScript = provider => relationship => { |
| 118 | + const deleteScript = getDeleteSingleForeignKeyScript(provider)(relationship); |
| 119 | + const addScript = getAddSingleForeignKeyScript(provider)(relationship); |
| 120 | + const isActivated = Boolean(relationship.role?.compMod?.isActivated?.new); |
| 121 | + |
| 122 | + return ( |
| 123 | + commentDeactivatedStatements(deleteScript, isActivated) + commentDeactivatedStatements(addScript, isActivated) |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +const getAlterRelationshipsScripts = (schema, provider, initialSchemaName) => { |
| 128 | + let currentSchemaName = initialSchemaName; |
| 129 | + |
| 130 | + const generateAddFkScripts = (addedRelationships, getScript) => { |
| 131 | + return addedRelationships.filter(relationship => canRelationshipBeAdded(relationship)).flatMap(getScript); |
| 132 | + }; |
| 133 | + |
| 134 | + const generateModifyFkScripts = (modifiedRelationships, getScript) => { |
| 135 | + return modifiedRelationships |
| 136 | + .filter(relationship => canRelationshipBeAdded(relationship) && canRelationshipBeDeleted(relationship)) |
| 137 | + .flatMap(getScript); |
| 138 | + }; |
| 139 | + |
| 140 | + const getRelationshipsScriptsWithUseSchema = (relationships, processRelationships, getScript) => { |
| 141 | + return processRelationships(relationships, relationship => { |
| 142 | + const script = getScript(provider)(relationship); |
| 143 | + |
| 144 | + if (!script) { |
| 145 | + return []; |
| 146 | + } |
| 147 | + |
| 148 | + const schemaName = replaceSpaceWithUnderscore( |
| 149 | + prepareName(relationship.role.compMod.child.bucket?.name || ''), |
| 150 | + ); |
| 151 | + |
| 152 | + if (currentSchemaName === schemaName) { |
| 153 | + return [script]; |
| 154 | + } |
| 155 | + |
| 156 | + currentSchemaName = schemaName; |
| 157 | + |
| 158 | + const useSchemaScript = provider.assignTemplates(templates.useSchema, { schemaName }); |
| 159 | + |
| 160 | + return [useSchemaScript, script]; |
| 161 | + }); |
| 162 | + }; |
| 163 | + |
| 164 | + const deletedRelationships = getItems(schema, 'relationships', 'deleted').filter( |
| 165 | + relationship => relationship.role?.compMod?.deleted, |
| 166 | + ); |
| 167 | + const addedRelationships = getItems(schema, 'relationships', 'added').filter( |
| 168 | + relationship => relationship.role?.compMod?.created, |
| 169 | + ); |
| 170 | + const modifiedRelationships = getItems(schema, 'relationships', 'modified'); |
| 171 | + |
| 172 | + const deleteFkScripts = getDeleteForeignKeyScripts(provider)(deletedRelationships); |
| 173 | + const addFkScripts = getRelationshipsScriptsWithUseSchema( |
| 174 | + addedRelationships, |
| 175 | + generateAddFkScripts, |
| 176 | + getAddForeignKeyScript, |
| 177 | + ); |
| 178 | + const modifiedFkScripts = getRelationshipsScriptsWithUseSchema( |
| 179 | + modifiedRelationships, |
| 180 | + generateModifyFkScripts, |
| 181 | + getModifyForeignKeyScript, |
| 182 | + ); |
| 183 | + return { deleteFkScripts, addFkScripts, modifiedFkScripts }; |
| 184 | +}; |
| 185 | + |
| 186 | +module.exports = { |
| 187 | + getAlterRelationshipsScripts, |
| 188 | +}; |
0 commit comments