From e715a95cc04e935c9a5ad6e8c7c4e0998bb1d5c8 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 16 Sep 2021 10:48:46 +0200 Subject: [PATCH] recreate object warning --- .../__tests__/alter-database.spec.js | 2 +- .../__tests__/alter-table.spec.js | 2 +- packages/tools/src/alterPlan.ts | 13 ++++++ packages/tools/src/diffTools.ts | 15 ++++-- packages/web/src/forms/FormSubmit.svelte | 8 +++- .../web/src/modals/ConfirmSqlModal.svelte | 46 +++++++++++++++---- .../web/src/tabs/TableStructureTab.svelte | 3 +- .../web/src/utility/alterDatabaseTools.ts | 5 +- 8 files changed, 75 insertions(+), 19 deletions(-) diff --git a/integration-tests/__tests__/alter-database.spec.js b/integration-tests/__tests__/alter-database.spec.js index b1f7d0fb..0df93c44 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, {}, 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 e1db13bd..c3df192e 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), {}, structure2, driver); console.log('RUNNING ALTER SQL', driver.engine, ':', sql); await driver.script(conn, sql); diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index 2bbb4d12..d94b40db 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -104,6 +104,12 @@ type AlterOperation = | AlterOperation_RecreateTable; export class AlterPlan { + recreates = { + tables: 0, + constraints: 0, + sqlObjects: 0, + }; + public operations: AlterOperation[] = []; constructor(public db: DatabaseInfo, public dialect: SqlDialect) {} @@ -209,6 +215,7 @@ export class AlterPlan { table, operations, }); + this.recreates.tables += 1; } run(processor: AlterProcessor) { @@ -270,6 +277,10 @@ export class AlterPlan { return opRes; }), ]; + + if (constraints.length > 0) { + this.recreates.constraints += 1; + } return res; } @@ -289,6 +300,7 @@ export class AlterPlan { } if (op.operationType == 'changeConstraint') { + this.recreates.constraints += 1; const opDrop: AlterOperation = { operationType: 'dropConstraint', oldObject: op.oldObject, @@ -356,6 +368,7 @@ export class AlterPlan { const table = this.db.tables.find( x => x.pureName == op[objectField].pureName && x.schemaName == op[objectField].schemaName ); + this.recreates.tables += 1; return [ { operationType: 'recreateTable', diff --git a/packages/tools/src/diffTools.ts b/packages/tools/src/diffTools.ts index 31dc6d66..137650d4 100644 --- a/packages/tools/src/diffTools.ts +++ b/packages/tools/src/diffTools.ts @@ -348,6 +348,7 @@ export function createAlterDatabasePlan( } else { if (newobj == null) plan.dropSqlObject(oldobj); else if (newobj.createSql != oldobj.createSql) { + plan.recreates.sqlObjects += 1; plan.dropSqlObject(oldobj); plan.createSqlObject(newobj); } @@ -372,13 +373,16 @@ export function getAlterTableScript( opts: DbDiffOptions, db: DatabaseInfo, driver: EngineDriver -): string { +) { const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver); const dmp = driver.createDumper(); if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction(); plan.run(dmp); if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction(); - return dmp.s; + return { + sql: dmp.s, + recreates: plan.recreates, + }; } export function getAlterDatabaseScript( @@ -387,11 +391,14 @@ export function getAlterDatabaseScript( opts: DbDiffOptions, db: DatabaseInfo, driver: EngineDriver -): string { +) { const plan = createAlterDatabasePlan(oldDb, newDb, opts, db, driver); const dmp = driver.createDumper(); if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction(); plan.run(dmp); if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction(); - return dmp.s; + return { + sql: dmp.s, + recreates: plan.recreates, + }; } diff --git a/packages/web/src/forms/FormSubmit.svelte b/packages/web/src/forms/FormSubmit.svelte index d7643970..3c4aa3b0 100644 --- a/packages/web/src/forms/FormSubmit.svelte +++ b/packages/web/src/forms/FormSubmit.svelte @@ -3,6 +3,8 @@ import { getFormContext } from './FormProviderCore.svelte'; import { createEventDispatcher } from 'svelte'; + export let disabled; + const dispatch = createEventDispatcher(); const { submitActionRef } = getFormContext(); @@ -13,8 +15,10 @@ } submitActionRef.set(() => { - handleClick(); + if (!disabled) { + handleClick(); + } }); - + diff --git a/packages/web/src/modals/ConfirmSqlModal.svelte b/packages/web/src/modals/ConfirmSqlModal.svelte index 168c1f46..1ec61367 100644 --- a/packages/web/src/modals/ConfirmSqlModal.svelte +++ b/packages/web/src/modals/ConfirmSqlModal.svelte @@ -1,7 +1,11 @@ @@ -20,15 +29,32 @@ + {#if isRecreated} +
+
+ This operation is not directly supported by SQL engine. DbGate can emulate it, but + please check the generated SQL script. +
+ +
+ {/if} +
- { - closeCurrentModal(); - onConfirm(); - }} - /> - + + { + closeCurrentModal(); + onConfirm(); + }} + /> + +
@@ -39,4 +65,8 @@ height: 30vh; width: 40vw; } + + .form-margin { + margin: var(--dim-large-form-margin); + } diff --git a/packages/web/src/tabs/TableStructureTab.svelte b/packages/web/src/tabs/TableStructureTab.svelte index edb376be..572aab2c 100644 --- a/packages/web/src/tabs/TableStructureTab.svelte +++ b/packages/web/src/tabs/TableStructureTab.svelte @@ -102,7 +102,7 @@ function doSave(createTableName) { const driver = findEngineDriver($connection, $extensions); - const sql = getAlterTableScript( + const { sql, recreates } = getAlterTableScript( $editorValue.base, fillConstraintNames($editorValue.current, driver.dialect), {}, @@ -112,6 +112,7 @@ showModal(ConfirmSqlModal, { sql, + recreates, onConfirm: () => { handleConfirmSql(sql, createTableName); }, diff --git a/packages/web/src/utility/alterDatabaseTools.ts b/packages/web/src/utility/alterDatabaseTools.ts index 7346d012..5eec5455 100644 --- a/packages/web/src/utility/alterDatabaseTools.ts +++ b/packages/web/src/utility/alterDatabaseTools.ts @@ -1,4 +1,4 @@ -import _ from 'lodash' +import _ from 'lodash'; import { findEngineDriver, generateDbPairingId, getAlterDatabaseScript } from 'dbgate-tools'; import InputTextModal from '../modals/InputTextModal.svelte'; import { showModal } from '../modals/modalTools'; @@ -15,10 +15,11 @@ export async function alterDatabaseDialog(conid, database, updateFunc) { const dbUpdated = _.cloneDeep(db); updateFunc(dbUpdated); - const sql = getAlterDatabaseScript(db, dbUpdated, {}, db, driver); + const { sql, recreates } = getAlterDatabaseScript(db, dbUpdated, {}, db, driver); showModal(ConfirmSqlModal, { sql, + recreates, onConfirm: async () => { const resp = await axiosInstance.request({ url: 'database-connections/run-script',