diff --git a/integration-tests/__tests__/alter-database.spec.js b/integration-tests/__tests__/alter-database.spec.js index 0df93c44..f5659521 100644 --- a/integration-tests/__tests__/alter-database.spec.js +++ b/integration-tests/__tests__/alter-database.spec.js @@ -30,7 +30,7 @@ async function testDatabaseDiff(conn, driver, mangle, createObject = null) { mangle(structure2); structure2 = extendDatabaseInfo(structure2); - const { sql } = getAlterDatabaseScript(structure1, structure2, {}, structure2, driver); + const { sql } = getAlterDatabaseScript(structure1, structure2, {}, structure1, structure2, driver); console.log('RUNNING ALTER SQL', driver.engine, ':', sql); await driver.script(conn, sql); diff --git a/integration-tests/__tests__/alter-table.spec.js b/integration-tests/__tests__/alter-table.spec.js index c3df192e..f6ae926f 100644 --- a/integration-tests/__tests__/alter-table.spec.js +++ b/integration-tests/__tests__/alter-table.spec.js @@ -46,7 +46,7 @@ async function testTableDiff(conn, driver, mangle) { mangle(tget(structure2)); structure2 = extendDatabaseInfo(structure2); - const { sql } = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver); + const { sql } = getAlterTableScript(tget(structure1), tget(structure2), {}, structure1, structure2, driver); console.log('RUNNING ALTER SQL', driver.engine, ':', sql); await driver.script(conn, sql); diff --git a/integration-tests/engines.js b/integration-tests/engines.js index ed1bcaf0..eebc1175 100644 --- a/integration-tests/engines.js +++ b/integration-tests/engines.js @@ -116,9 +116,9 @@ const engines = [ const filterLocal = [ // filter local testing - '-MySQL', - '-PostgreSQL', - '-SQL Server', + 'MySQL', + 'PostgreSQL', + 'SQL Server', 'SQLite', '-CockroachDB', ]; diff --git a/packages/api/src/shell/generateDeploySql.js b/packages/api/src/shell/generateDeploySql.js index 920e04f2..9e4e428a 100644 --- a/packages/api/src/shell/generateDeploySql.js +++ b/packages/api/src/shell/generateDeploySql.js @@ -42,7 +42,14 @@ async function generateDeploySql({ // console.log('deployedModel', deployedModel.tables[0]); // console.log('currentModel', currentModel.tables[0]); // console.log('currentModelPaired', currentModelPaired.tables[0]); - const res = getAlterDatabaseScript(currentModelPaired, deployedModel, opts, deployedModel, driver); + const res = getAlterDatabaseScript( + currentModelPaired, + deployedModel, + opts, + currentModelPaired, + deployedModel, + driver + ); return res; } diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index eea0f295..5cb64fa9 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -111,7 +111,12 @@ export class AlterPlan { }; public operations: AlterOperation[] = []; - constructor(public db: DatabaseInfo, public dialect: SqlDialect, public opts: DbDiffOptions) {} + constructor( + public wholeOldDb: DatabaseInfo, + public wholeNewDb: DatabaseInfo, + public dialect: SqlDialect, + public opts: DbDiffOptions + ) {} createTable(table: TableInfo) { this.operations.push({ @@ -225,7 +230,7 @@ export class AlterPlan { } _getDependendColumnConstraints(column: ColumnInfo, dependencyDefinition) { - const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName); + const table = this.wholeOldDb.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName); if (!table) return []; const fks = dependencyDefinition?.includes('dependencies') ? table.dependencies.filter(fk => fk.columns.find(col => col.refColumnName == column.columnName)) @@ -385,7 +390,7 @@ export class AlterPlan { return []; } - const table = this.db.tables.find( + const table = this.wholeNewDb.tables.find( x => x.pureName == op[objectField].pureName && x.schemaName == op[objectField].schemaName ); this.recreates.tables += 1; diff --git a/packages/tools/src/computeDiffRows.ts b/packages/tools/src/computeDiffRows.ts index dbe18b18..a5c64b48 100644 --- a/packages/tools/src/computeDiffRows.ts +++ b/packages/tools/src/computeDiffRows.ts @@ -83,7 +83,7 @@ export function computeDbDiffRows( res.push( ..._.sortBy( computeDiffRowsCore(sourceDb[objectTypeField], targetDb[objectTypeField], (a, b) => - defs.test(a, b, opts, targetDb, driver) + defs.test(a, b, opts, sourceDb, targetDb, driver) ).map(row => ({ ...row, sourceSchemaName: row?.source?.schemaName, diff --git a/packages/tools/src/diffTools.ts b/packages/tools/src/diffTools.ts index 4afd75ef..8ca02b8a 100644 --- a/packages/tools/src/diffTools.ts +++ b/packages/tools/src/diffTools.ts @@ -373,10 +373,11 @@ export function testEqualTables( a: TableInfo, b: TableInfo, opts: DbDiffOptions, - db: DatabaseInfo, + wholeOldDb: DatabaseInfo, + wholeNewDb: DatabaseInfo, driver: EngineDriver ) { - const plan = new AlterPlan(db, driver.dialect, opts); + const plan = new AlterPlan(wholeOldDb, wholeNewDb, driver.dialect, opts); planAlterTable(plan, a, b, opts); // console.log('plan.operations', a, b, plan.operations); return plan.operations.length == 0; @@ -390,10 +391,11 @@ export function createAlterTablePlan( oldTable: TableInfo, newTable: TableInfo, opts: DbDiffOptions, - db: DatabaseInfo, + wholeOldDb: DatabaseInfo, + wholeNewDb: DatabaseInfo, driver: EngineDriver ): AlterPlan { - const plan = new AlterPlan(db, driver.dialect, opts); + const plan = new AlterPlan(wholeOldDb, wholeNewDb, driver.dialect, opts); if (oldTable == null) { plan.createTable(newTable); } else if (newTable == null) { @@ -409,10 +411,11 @@ export function createAlterDatabasePlan( oldDb: DatabaseInfo, newDb: DatabaseInfo, opts: DbDiffOptions, - db: DatabaseInfo, + wholeOldDb: DatabaseInfo, + wholeNewDb: DatabaseInfo, driver: EngineDriver ): AlterPlan { - const plan = new AlterPlan(db, driver.dialect, opts); + const plan = new AlterPlan(wholeOldDb, wholeNewDb, driver.dialect, opts); for (const objectTypeField of ['tables', 'views', 'procedures', 'matviews', 'functions']) { for (const oldobj of oldDb[objectTypeField] || []) { @@ -456,14 +459,15 @@ export function getAlterTableScript( oldTable: TableInfo, newTable: TableInfo, opts: DbDiffOptions, - db: DatabaseInfo, + wholeOldDb: DatabaseInfo, + wholeNewDb: DatabaseInfo, driver: EngineDriver ) { if ((!oldTable && !newTable) || !driver) { return { sql: '', recreates: [] }; } - const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver); + const plan = createAlterTablePlan(oldTable, newTable, opts, wholeOldDb, wholeNewDb, driver); const dmp = driver.createDumper(); if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction(); plan.run(dmp); @@ -478,10 +482,11 @@ export function getAlterDatabaseScript( oldDb: DatabaseInfo, newDb: DatabaseInfo, opts: DbDiffOptions, - db: DatabaseInfo, + wholeOldDb: DatabaseInfo, + wholeNewDb: DatabaseInfo, driver: EngineDriver ) { - const plan = createAlterDatabasePlan(oldDb, newDb, opts, db, driver); + const plan = createAlterDatabasePlan(oldDb, newDb, opts, wholeOldDb, wholeNewDb, driver); const dmp = driver.createDumper(); if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction(); plan.run(dmp); diff --git a/packages/web/src/tabs/CompareModelTab.svelte b/packages/web/src/tabs/CompareModelTab.svelte index c73907ea..c6be778d 100644 --- a/packages/web/src/tabs/CompareModelTab.svelte +++ b/packages/web/src/tabs/CompareModelTab.svelte @@ -70,7 +70,7 @@ } if (objectTypeField == 'tables') { - return getAlterTableScript(oldObject, newObject, opts, db, driver); + return getAlterTableScript(oldObject, newObject, opts, db, db, driver); } const dmp = driver.createDumper(); if (oldObject) dmp.dropSqlObject(oldObject); @@ -280,7 +280,7 @@ function getDeploySql() { return diffRows .filter(row => $values[`isChecked_${row.identifier}`]) - .map(row => getAlterTableScript(row?.target, row?.source, dbDiffOptions, targetDb, driver).sql) + .map(row => getAlterTableScript(row?.target, row?.source, dbDiffOptions, sourceDb, targetDb, driver).sql) .join('\n'); } diff --git a/packages/web/src/tabs/TableStructureTab.svelte b/packages/web/src/tabs/TableStructureTab.svelte index 7e4c7da5..82d2996e 100644 --- a/packages/web/src/tabs/TableStructureTab.svelte +++ b/packages/web/src/tabs/TableStructureTab.svelte @@ -115,6 +115,7 @@ extendTableInfo(fillConstraintNames($editorValue.current, driver.dialect)), {}, $dbInfo, + $dbInfo, driver ); diff --git a/packages/web/src/utility/alterDatabaseTools.ts b/packages/web/src/utility/alterDatabaseTools.ts index 5eec5455..4269f6e2 100644 --- a/packages/web/src/utility/alterDatabaseTools.ts +++ b/packages/web/src/utility/alterDatabaseTools.ts @@ -15,7 +15,7 @@ export async function alterDatabaseDialog(conid, database, updateFunc) { const dbUpdated = _.cloneDeep(db); updateFunc(dbUpdated); - const { sql, recreates } = getAlterDatabaseScript(db, dbUpdated, {}, db, driver); + const { sql, recreates } = getAlterDatabaseScript(db, dbUpdated, {}, db, dbUpdated, driver); showModal(ConfirmSqlModal, { sql,