From b366a7d4518cc929354706ec3a66ea53a21fa5b5 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Fri, 13 Sep 2024 14:23:37 +0200 Subject: [PATCH] alter table fixes WIP --- integration-tests/engines.js | 2 +- packages/tools/src/alterPlan.ts | 64 ++++++++++--------- packages/types/dialect.d.ts | 1 + .../src/frontend/driver.js | 4 +- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/integration-tests/engines.js b/integration-tests/engines.js index 070e1bce..958ee2b9 100644 --- a/integration-tests/engines.js +++ b/integration-tests/engines.js @@ -139,7 +139,7 @@ const engines = [ local: { databaseUrl: 'http://localhost:15005', }, - // skipOnCI: true, + skipOnCI: true, objects: [views], skipDataModifications: true, skipReferences: true, diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index 9edf0ada..65c43206 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -315,35 +315,40 @@ export class AlterPlan { return res; } - if (op.operationType == 'changeColumn') { - const constraints = this._getDependendColumnConstraints(op.oldObject, this.dialect.changeColumnDependencies); + for (const [testedOperationType, testedDependencies, testedObject] of [ + ['changeColumn', this.dialect.changeColumnDependencies, (op as AlterOperation_ChangeColumn).oldObject], + ['renameColumn', this.dialect.renameColumnDependencies, (op as AlterOperation_RenameColumn).object], + ]) { + if (op.operationType == testedOperationType) { + const constraints = this._getDependendColumnConstraints(testedObject as ColumnInfo, testedDependencies); - if (constraints.length > 0 && this.opts.noDropConstraint) { - return []; + if (constraints.length > 0 && this.opts.noDropConstraint) { + return []; + } + + const res: AlterOperation[] = [ + ...constraints.map(oldObject => { + const opRes: AlterOperation = { + operationType: 'dropConstraint', + oldObject, + }; + return opRes; + }), + op, + ..._.reverse([...constraints]).map(newObject => { + const opRes: AlterOperation = { + operationType: 'createConstraint', + newObject, + }; + return opRes; + }), + ]; + + if (constraints.length > 0) { + this.recreates.constraints += 1; + } + return res; } - - const res: AlterOperation[] = [ - ...constraints.map(oldObject => { - const opRes: AlterOperation = { - operationType: 'dropConstraint', - oldObject, - }; - return opRes; - }), - op, - ..._.reverse([...constraints]).map(newObject => { - const opRes: AlterOperation = { - operationType: 'createConstraint', - newObject, - }; - return opRes; - }), - ]; - - if (constraints.length > 0) { - this.recreates.constraints += 1; - } - return res; } if (op.operationType == 'dropTable') { @@ -392,7 +397,8 @@ export class AlterPlan { this._testTableRecreate(op, 'dropColumn', this.dialect.dropColumn, 'oldObject') || this._testTableRecreate(op, 'createConstraint', obj => this._canCreateConstraint(obj), 'newObject') || this._testTableRecreate(op, 'dropConstraint', obj => this._canDropConstraint(obj), 'oldObject') || - this._testTableRecreate(op, 'changeColumn', this.dialect.changeColumn, 'newObject') || [op] + this._testTableRecreate(op, 'changeColumn', this.dialect.changeColumn, 'newObject') || + this._testTableRecreate(op, 'renameColumn', true, 'object') || [op] ); }); @@ -473,7 +479,7 @@ export class AlterPlan { } } else { // @ts-ignore - const oldObject: TableInfo = op.oldObject; + const oldObject: TableInfo = op.oldObject || op.object; if (oldObject) { const recreated = recreates[`${oldObject.schemaName}||${oldObject.pureName}`]; if (recreated) { diff --git a/packages/types/dialect.d.ts b/packages/types/dialect.d.ts index 1c8c30bf..94705d7c 100644 --- a/packages/types/dialect.d.ts +++ b/packages/types/dialect.d.ts @@ -17,6 +17,7 @@ export interface SqlDialect { dropColumnDependencies?: string[]; changeColumnDependencies?: string[]; + renameColumnDependencies?: string[]; dropIndexContainsTableSpec?: boolean; diff --git a/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js b/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js index d27a7342..bf97cb9c 100644 --- a/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js +++ b/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js @@ -91,7 +91,9 @@ const dialect = { rangeSelect: true, stringEscapeChar: "'", fallbackDataType: 'String', - + dropColumnDependencies: ['primaryKey', 'sortingKey'], + changeColumnDependencies: ['primaryKey', 'sortingKey'], + renameColumnDependencies: ['primaryKey', 'sortingKey'], createColumn: true, dropColumn: true, changeColumn: true,