From e568adc82573a5fe0f4941e5c652646e4c9f42e7 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 9 Sep 2021 08:51:38 +0200 Subject: [PATCH] refactor --- packages/tools/src/alterPlan.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index b705b811..c409344d 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -190,27 +190,28 @@ export class AlterPlan { } } + _getDependendColumnConstraints(column: ColumnInfo, dependencyDefinition) { + const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName); + const fks = dependencyDefinition?.includes('dependencies') + ? table.dependencies.filter(fk => fk.columns.find(col => col.refColumnName == column.columnName)) + : []; + const constraints = _.compact([ + dependencyDefinition?.includes('primaryKey') ? table.primaryKey : null, + ...(dependencyDefinition?.includes('foreignKeys') ? table.foreignKeys : []), + ...(dependencyDefinition?.includes('indexes') ? table.indexes : []), + ...(dependencyDefinition?.includes('uniques') ? table.uniques : []), + ]).filter(cnt => cnt.columns.find(col => col.columnName == column.columnName)); + + return [...fks, ...constraints]; + } + _addLogicalDependencies(): AlterOperation[] { const lists = this.operations.map(op => { if (op.operationType == 'dropColumn') { - const table = this.db.tables.find( - x => x.pureName == op.oldObject.pureName && x.schemaName == op.oldObject.schemaName - ); - const deletedFks = this.dialect.dropColumnDependencies?.includes('dependencies') - ? table.dependencies.filter(fk => fk.columns.find(col => col.refColumnName == op.oldObject.columnName)) - : []; - - const deletedConstraints = _.compact([ - this.dialect.dropColumnDependencies?.includes('primaryKey') ? table.primaryKey : null, - ...(this.dialect.dropColumnDependencies?.includes('foreignKeys') ? table.foreignKeys : []), - ...(this.dialect.dropColumnDependencies?.includes('indexes') ? table.indexes : []), - ...(this.dialect.dropColumnDependencies?.includes('uniques') ? table.uniques : []), - ]).filter(cnt => cnt.columns.find(col => col.columnName == op.oldObject.columnName)); - - // console.log('deletedConstraints', deletedConstraints); + const constraints = this._getDependendColumnConstraints(op.oldObject, this.dialect.dropColumnDependencies); const res: AlterOperation[] = [ - ...[...deletedFks, ...deletedConstraints].map(oldObject => { + ...constraints.map(oldObject => { const opRes: AlterOperation = { operationType: 'dropConstraint', oldObject,