From 0ef5ac04d8b46ea30b9cd541b11dc1ec83706d15 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 5 Sep 2021 10:55:53 +0200 Subject: [PATCH] drop unique column works --- .../__tests__/alter-processor.spec.js | 8 +++-- integration-tests/engines.js | 6 ++-- packages/tools/src/alterPlan.ts | 8 ++++- .../src/database-info-alter-processor.ts | 30 ++++++++++++++++++- packages/types/dialect.d.ts | 4 +++ .../src/frontend/driver.js | 6 +++- .../src/frontend/drivers.js | 4 +++ .../src/frontend/drivers.js | 4 +++ .../src/frontend/driver.js | 2 +- 9 files changed, 62 insertions(+), 10 deletions(-) diff --git a/integration-tests/__tests__/alter-processor.spec.js b/integration-tests/__tests__/alter-processor.spec.js index 4d1e1b7c..f28f25c4 100644 --- a/integration-tests/__tests__/alter-processor.spec.js +++ b/integration-tests/__tests__/alter-processor.spec.js @@ -9,7 +9,9 @@ const { getAlterTableScript, extendDatabaseInfo, generateDbPairingId } = require function pickImportantTableInfo(table) { return { pureName: table.pureName, - columns: table.columns.map(fp.pick(['columnName', 'notNull', 'autoIncrement'])), + columns: table.columns + .filter(x => x.columnName != 'rowid') + .map(fp.pick(['columnName', 'notNull', 'autoIncrement'])), }; } @@ -56,9 +58,9 @@ 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_pk']; // const TESTED_COLUMNS = ['col_idx']; -// const TESTED_COLUMNS = ['col_fk']; +const TESTED_COLUMNS = ['col_uq']; // const TESTED_COLUMNS = ['col_std']; function engines_columns_source() { diff --git a/integration-tests/engines.js b/integration-tests/engines.js index 11414919..1bde9c3d 100644 --- a/integration-tests/engines.js +++ b/integration-tests/engines.js @@ -16,7 +16,7 @@ const matviews = { const engines = [ { label: 'MySQL', - // skipLocal: true, + skipLocal: true, connection: { engine: 'mysql@dbgate-plugin-mysql', password: 'Pwd2020Db', @@ -34,7 +34,7 @@ const engines = [ }, { label: 'PostgreSQL', - // skipLocal: true, + skipLocal: true, connection: { engine: 'postgres@dbgate-plugin-postgres', password: 'Pwd2020Db', @@ -103,7 +103,7 @@ const engines = [ }, { label: 'CockroachDB', - // skipLocal: true, + skipLocal: true, connection: { engine: 'cockroach@dbgate-plugin-postgres', user: 'root', diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index 453fdda4..f86ff64c 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -244,6 +244,8 @@ export class AlterPlan { if (cnt.constraintType == 'primaryKey') return this.dialect.createPrimaryKey; if (cnt.constraintType == 'foreignKey') return this.dialect.createForeignKey; if (cnt.constraintType == 'index') return this.dialect.createIndex; + if (cnt.constraintType == 'unique') return this.dialect.createUnique; + if (cnt.constraintType == 'check') return this.dialect.createCheck; return null; } @@ -251,6 +253,8 @@ export class AlterPlan { if (cnt.constraintType == 'primaryKey') return this.dialect.dropPrimaryKey; if (cnt.constraintType == 'foreignKey') return this.dialect.dropForeignKey; if (cnt.constraintType == 'index') return this.dialect.dropIndex; + if (cnt.constraintType == 'unique') return this.dialect.dropUnique; + if (cnt.constraintType == 'check') return this.dialect.dropCheck; return null; } @@ -364,9 +368,11 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor) const newTable = _.cloneDeep(op.table); const newDb = DatabaseAnalyser.createEmptyStructure(); newDb.tables.push(newTable); + // console.log('////////////////////////////newTable1', newTable); op.operations.forEach(child => runAlterOperation(child, new DatabaseInfoAlterProcessor(newDb))); + // console.log('////////////////////////////op.operations', op.operations); // console.log('////////////////////////////op.table', op.table); - // console.log('////////////////////////////newTable', newTable); + // console.log('////////////////////////////newTable2', newTable); processor.recreateTable(op.table, newTable); } break; diff --git a/packages/tools/src/database-info-alter-processor.ts b/packages/tools/src/database-info-alter-processor.ts index 829db4e7..350339bb 100644 --- a/packages/tools/src/database-info-alter-processor.ts +++ b/packages/tools/src/database-info-alter-processor.ts @@ -1,4 +1,14 @@ -import { ColumnInfo, ConstraintInfo, DatabaseInfo, ForeignKeyInfo, PrimaryKeyInfo, TableInfo } from '../../types'; +import { + ColumnInfo, + ConstraintInfo, + DatabaseInfo, + ForeignKeyInfo, + PrimaryKeyInfo, + TableInfo, + IndexInfo, + CheckInfo, + UniqueInfo, +} from '../../types'; export class DatabaseInfoAlterProcessor { constructor(public db: DatabaseInfo) {} @@ -35,6 +45,15 @@ export class DatabaseInfoAlterProcessor { case 'foreignKey': table.foreignKeys.push(constraint as ForeignKeyInfo); break; + case 'index': + table.indexes.push(constraint as IndexInfo); + break; + case 'unique': + table.uniques.push(constraint as UniqueInfo); + break; + case 'check': + table.checks.push(constraint as CheckInfo); + break; } } @@ -53,6 +72,15 @@ export class DatabaseInfoAlterProcessor { case 'foreignKey': table.foreignKeys = table.foreignKeys.filter(x => x.constraintName != constraint.constraintName); break; + case 'index': + table.indexes = table.indexes.filter(x => x.constraintName != constraint.constraintName); + break; + case 'unique': + table.uniques = table.uniques.filter(x => x.constraintName != constraint.constraintName); + break; + case 'check': + table.checks = table.checks.filter(x => x.constraintName != constraint.constraintName); + break; } } diff --git a/packages/types/dialect.d.ts b/packages/types/dialect.d.ts index 2facb4ff..2be14293 100644 --- a/packages/types/dialect.d.ts +++ b/packages/types/dialect.d.ts @@ -25,4 +25,8 @@ export interface SqlDialect { dropForeignKey?: boolean; createPrimaryKey?: boolean; dropPrimaryKey?: boolean; + createUnique?: boolean; + dropUnique?: boolean; + createCheck?: boolean; + dropCheck?: boolean; } diff --git a/plugins/dbgate-plugin-mssql/src/frontend/driver.js b/plugins/dbgate-plugin-mssql/src/frontend/driver.js index 6076d06d..6162a7ba 100644 --- a/plugins/dbgate-plugin-mssql/src/frontend/driver.js +++ b/plugins/dbgate-plugin-mssql/src/frontend/driver.js @@ -12,7 +12,7 @@ const dialect = { fallbackDataType: 'nvarchar(max)', explicitDropConstraint: false, enableConstraintsPerTable: true, - dropColumnDependencies: ['default', 'dependencies', 'indexes', 'primaryKey'], + dropColumnDependencies: ['default', 'dependencies', 'indexes', 'primaryKey', 'foreignKeys', 'uniques'], changeColumnDependencies: ['indexes'], anonymousPrimaryKey: false, dropIndexContainsTableSpec: true, @@ -28,6 +28,10 @@ const dialect = { dropForeignKey: true, createPrimaryKey: true, dropPrimaryKey: true, + createUnique: true, + dropUnique: true, + createCheck: true, + dropCheck: true, }; /** @type {import('dbgate-types').EngineDriver} */ diff --git a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js index f743e8f7..11eb28f5 100644 --- a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js @@ -23,6 +23,10 @@ const dialect = { createPrimaryKey: true, dropPrimaryKey: true, dropIndexContainsTableSpec: true, + createUnique: true, + dropUnique: true, + createCheck: true, + dropCheck: true, }; const mysqlDriverBase = { diff --git a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js index f36a70ad..6d57cecd 100644 --- a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js @@ -24,6 +24,10 @@ const dialect = { dropForeignKey: true, createPrimaryKey: true, dropPrimaryKey: true, + createUnique: true, + dropUnique: true, + createCheck: true, + dropCheck: true, }; const postgresDriverBase = { diff --git a/plugins/dbgate-plugin-sqlite/src/frontend/driver.js b/plugins/dbgate-plugin-sqlite/src/frontend/driver.js index d8bbda00..25ab833b 100644 --- a/plugins/dbgate-plugin-sqlite/src/frontend/driver.js +++ b/plugins/dbgate-plugin-sqlite/src/frontend/driver.js @@ -17,7 +17,7 @@ const dialect = { explicitDropConstraint: true, stringEscapeChar: "'", fallbackDataType: 'nvarchar(max)', - dropColumnDependencies: ['indexes', 'primaryKey'], + dropColumnDependencies: ['indexes', 'primaryKey', 'uniques'], quoteIdentifier(s) { return `[${s}]`; },