From e2ce349a3028969284a117a04e3467500609cc41 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sat, 4 Sep 2021 21:06:42 +0200 Subject: [PATCH] alter processor fixes --- .../__tests__/alter-processor.spec.js | 6 +-- integration-tests/engines.js | 13 ++++-- integration-tests/package.json | 3 ++ packages/tools/src/alterPlan.ts | 42 ++++++++++++++++++- .../src/frontend/Dumper.js | 2 +- .../src/frontend/drivers.js | 1 + .../src/frontend/Dumper.js | 4 ++ 7 files changed, 63 insertions(+), 8 deletions(-) diff --git a/integration-tests/__tests__/alter-processor.spec.js b/integration-tests/__tests__/alter-processor.spec.js index a2585823..4d1e1b7c 100644 --- a/integration-tests/__tests__/alter-processor.spec.js +++ b/integration-tests/__tests__/alter-processor.spec.js @@ -47,7 +47,7 @@ async function testTableDiff(conn, driver, mangle) { const sql = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver); console.log('RUNNING ALTER SQL', driver.engine, ':', sql); - await driver.query(conn, sql); + await driver.script(conn, sql); const structure2Real = extendDatabaseInfo(await driver.analyseFull(conn)); @@ -56,8 +56,8 @@ async function testTableDiff(conn, driver, mangle) { } // const TESTED_COLUMNS = ['col_pk', 'col_std', 'col_def', 'col_fk', 'col_ref', 'col_idx', 'col_uq']; -// const TESTED_COLUMNS = ['col_pk']; -const TESTED_COLUMNS = ['col_idx']; +const TESTED_COLUMNS = ['col_pk']; +// const TESTED_COLUMNS = ['col_idx']; // const TESTED_COLUMNS = ['col_fk']; // const TESTED_COLUMNS = ['col_std']; diff --git a/integration-tests/engines.js b/integration-tests/engines.js index 8d93555b..54b299cd 100644 --- a/integration-tests/engines.js +++ b/integration-tests/engines.js @@ -16,6 +16,7 @@ const matviews = { const engines = [ { label: 'MySQL', + skipLocal: true, connection: { engine: 'mysql@dbgate-plugin-mysql', password: 'Pwd2020Db', @@ -33,6 +34,7 @@ const engines = [ }, { label: 'PostgreSQL', + skipLocal: true, connection: { engine: 'postgres@dbgate-plugin-postgres', password: 'Pwd2020Db', @@ -56,8 +58,10 @@ const engines = [ }, { type: 'functions', - create1: 'CREATE FUNCTION obj1() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t1; return res; end; $$', - create2: 'CREATE FUNCTION obj2() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t2; return res; end; $$', + create1: + 'CREATE FUNCTION obj1() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t1; return res; end; $$', + create2: + 'CREATE FUNCTION obj2() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t2; return res; end; $$', drop1: 'DROP FUNCTION obj1', drop2: 'DROP FUNCTION obj2', }, @@ -65,6 +69,7 @@ const engines = [ }, { label: 'SQL Server', + skipLocal: true, connection: { engine: 'mssql@dbgate-plugin-mssql', password: 'Pwd2020Db', @@ -89,6 +94,7 @@ const engines = [ }, { label: 'SQLite', + skipLocal: true, generateDbFile: true, connection: { engine: 'sqlite@dbgate-plugin-sqlite', @@ -97,6 +103,7 @@ const engines = [ }, { label: 'CockroachDB', + // skipLocal: true, connection: { engine: 'cockroach@dbgate-plugin-postgres', user: 'root', @@ -112,4 +119,4 @@ const engines = [ }, ]; -module.exports = process.env.CITEST ? engines.filter(x => !x.skipOnCI) : engines; +module.exports = process.env.CITEST ? engines.filter(x => !x.skipOnCI) : engines.filter(x => !x.skipLocal); diff --git a/integration-tests/package.json b/integration-tests/package.json index d0fc1057..17a5df22 100644 --- a/integration-tests/package.json +++ b/integration-tests/package.json @@ -17,6 +17,9 @@ "run:local": "docker-compose down && docker-compose up -d && yarn wait:local && yarn test:local" }, + "jest": { + "testTimeout": 5000 + }, "devDependencies": { "cross-env": "^7.0.3", "jest": "^27.0.1" diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index b8e12753..453fdda4 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -207,7 +207,7 @@ export class AlterPlan { ...(this.dialect.dropColumnDependencies?.includes('uniques') ? table.uniques : []), ]).filter(cnt => cnt.columns.find(col => col.columnName == op.oldObject.columnName)); - console.log('deletedConstraints', deletedConstraints); + // console.log('deletedConstraints', deletedConstraints); const res: AlterOperation[] = [ ...[...deletedFks, ...deletedConstraints].map(oldObject => { @@ -283,9 +283,47 @@ export class AlterPlan { return null; } + _groupTableRecreations(): AlterOperation[] { + const res = []; + const recreates = {}; + for (const op of this.operations) { + if (op.operationType == 'recreateTable') { + const recreate = { + ...op, + operations: [...op.operations], + }; + res.push(recreate); + recreates[`${op.table.schemaName}||${op.table.pureName}`] = recreate; + } else { + // @ts-ignore + const oldObject: TableInfo = op.oldObject; + if (oldObject) { + const recreated = recreates[`${oldObject.schemaName}||${oldObject.pureName}`]; + if (recreated) { + recreated.operations.push(op); + continue; + } + } + res.push(op); + } + } + return res; + } + transformPlan() { + // console.log('*****************OPERATIONS0', this.operations); + this.operations = this._addLogicalDependencies(); + + // console.log('*****************OPERATIONS1', this.operations); + this.operations = this._transformToImplementedOps(); + + // console.log('*****************OPERATIONS2', this.operations); + + this.operations = this._groupTableRecreations(); + + // console.log('*****************OPERATIONS3', this.operations); } } @@ -327,6 +365,8 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor) const newDb = DatabaseAnalyser.createEmptyStructure(); newDb.tables.push(newTable); op.operations.forEach(child => runAlterOperation(child, new DatabaseInfoAlterProcessor(newDb))); + // console.log('////////////////////////////op.table', op.table); + // console.log('////////////////////////////newTable', newTable); processor.recreateTable(op.table, newTable); } break; diff --git a/plugins/dbgate-plugin-postgres/src/frontend/Dumper.js b/plugins/dbgate-plugin-postgres/src/frontend/Dumper.js index c69d7060..b98c2c37 100644 --- a/plugins/dbgate-plugin-postgres/src/frontend/Dumper.js +++ b/plugins/dbgate-plugin-postgres/src/frontend/Dumper.js @@ -41,7 +41,7 @@ class Dumper extends SqlDumper { dropTable(obj, options = {}) { this.put('^drop ^table'); if (options.testIfExists) this.put(' ^if ^exists'); - this.put(' %f', obj.FullName); + this.put(' %f', obj); this.endCommand(); } diff --git a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js index c040812d..7de58ece 100644 --- a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js @@ -57,6 +57,7 @@ const cockroachDriver = { ...dialect, materializedViews: true, dropColumnDependencies: ['primaryKey'], + dropPrimaryKey: false, }, }; diff --git a/plugins/dbgate-plugin-sqlite/src/frontend/Dumper.js b/plugins/dbgate-plugin-sqlite/src/frontend/Dumper.js index c0df840f..d320e61a 100644 --- a/plugins/dbgate-plugin-sqlite/src/frontend/Dumper.js +++ b/plugins/dbgate-plugin-sqlite/src/frontend/Dumper.js @@ -4,6 +4,10 @@ class Dumper extends SqlDumper { renameColumn(column, newcol) { this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', column, column.columnName, newcol); } + + renameTable(obj, newname) { + this.putCmd('^alter ^table %f ^rename ^to %i', obj, newname); + } } module.exports = Dumper;