diff --git a/packages/api/src/shell/deployDb.js b/packages/api/src/shell/deployDb.js index 5702eaa39..6d47d179d 100644 --- a/packages/api/src/shell/deployDb.js +++ b/packages/api/src/shell/deployDb.js @@ -17,6 +17,7 @@ async function deployDb({ dbdiffOptionsExtra, ignoreNameRegex = '', targetSchema = null, + maxMissingTablesRatio = undefined, }) { if (!driver) driver = requireEngineDriver(connection); const dbhan = systemConnection || (await connectUtility(driver, connection, 'read')); @@ -41,6 +42,7 @@ async function deployDb({ dbdiffOptionsExtra, ignoreNameRegex, targetSchema, + maxMissingTablesRatio, }); // console.log('RUNNING DEPLOY SCRIPT:', sql); await executeQuery({ connection, systemConnection: dbhan, driver, sql, logScriptItems: true }); diff --git a/packages/api/src/shell/generateDeploySql.js b/packages/api/src/shell/generateDeploySql.js index f9c98db05..eb9dde32e 100644 --- a/packages/api/src/shell/generateDeploySql.js +++ b/packages/api/src/shell/generateDeploySql.js @@ -26,6 +26,7 @@ async function generateDeploySql({ dbdiffOptionsExtra = {}, ignoreNameRegex = '', targetSchema = null, + maxMissingTablesRatio = undefined, }) { if (!driver) driver = requireEngineDriver(connection); @@ -80,6 +81,17 @@ async function generateDeploySql({ const currentModelPaired = matchPairedObjects(deployedModel, currentModel, opts); const currentModelPairedPreloaded = await enrichWithPreloadedRows(deployedModel, currentModelPaired, dbhan, driver); + if (maxMissingTablesRatio != null) { + const missingTables = currentModelPaired.tables.filter( + x => !deployedModel.tables.find(y => y.pairingId == x.pairingId) + ); + const missingTableCount = missingTables.length; + const missingTablesRatio = missingTableCount / (currentModelPaired.tables.length || 1); + if (missingTablesRatio > maxMissingTablesRatio) { + throw new Error(`Too many missing tables (${missingTablesRatio * 100}%), aborting deploy`); + } + } + // console.log('currentModelPairedPreloaded', currentModelPairedPreloaded.tables[0]); // console.log('deployedModel', deployedModel.tables[0]); // console.log('currentModel', currentModel.tables[0]); diff --git a/packages/tools/src/diffTools.ts b/packages/tools/src/diffTools.ts index 48d5c31b2..d15d0a8d4 100644 --- a/packages/tools/src/diffTools.ts +++ b/packages/tools/src/diffTools.ts @@ -742,8 +742,12 @@ export function createAlterDatabasePlan( } } else { if (opts.deletedSqlObjectPrefix && hasDeletedPrefix(oldobj.pureName, opts, opts.deletedSqlObjectPrefix)) { - plan.dropSqlObject(oldobj); - plan.createSqlObject(newobj); + if (driver.dialect.renameSqlObject && testEqualSqlObjects(oldobj, newobj, opts)) { + plan.renameSqlObject(oldobj, newobj.pureName); + } else { + plan.dropSqlObject(oldobj); + plan.createSqlObject(newobj); + } } else if (!testEqualSqlObjects(oldobj, newobj, opts)) { plan.recreates.sqlObjects += 1; plan.dropSqlObject(oldobj); @@ -819,7 +823,7 @@ export function getAlterDatabaseScript( }; } -export function matchPairedObjects(db1: DatabaseInfo, db2: DatabaseInfo, opts: DbDiffOptions) { +export function matchPairedObjects(db1: DatabaseInfo, db2: DatabaseInfo, opts: DbDiffOptions): DatabaseInfo { if (!db1 || !db2) return null; const res = _cloneDeep(db2);