From 6b783027e50ee11292c92612b44b10507a808d4d Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Fri, 17 Feb 2023 10:00:21 +0100 Subject: [PATCH] data duplicator fix --- packages/datalib/src/DataDuplicator.ts | 21 ++++++++++++++----- packages/tools/src/driverBase.ts | 2 ++ packages/types/dialect.d.ts | 1 + .../src/frontend/drivers.js | 1 + 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/datalib/src/DataDuplicator.ts b/packages/datalib/src/DataDuplicator.ts index 043cdc91..938591e6 100644 --- a/packages/datalib/src/DataDuplicator.ts +++ b/packages/datalib/src/DataDuplicator.ts @@ -118,17 +118,28 @@ class DuplicatorItemHolder { skipped += 1; return; } - await runCommandOnDriver(pool, driver, dmp => - dmp.putCmd( + let res = await runQueryOnDriver(pool, driver, dmp => { + dmp.put( '^insert ^into %f (%,i) ^values (%,v)', this.table, Object.keys(insertedObj), Object.values(insertedObj) - ) - ); + ); + + if ( + this.autoColumn && + this.isReferenced && + !this.duplicator.driver.dialect.requireStandaloneSelectForScopeIdentity + ) { + dmp.selectScopeIdentity(this.table); + } + }); inserted += 1; if (this.autoColumn && this.isReferenced) { - const res = await runQueryOnDriver(pool, driver, dmp => dmp.selectScopeIdentity(this.table)); + if (this.duplicator.driver.dialect.requireStandaloneSelectForScopeIdentity) { + res = await runQueryOnDriver(pool, driver, dmp => dmp.selectScopeIdentity(this.table)); + } + // console.log('IDRES', JSON.stringify(res)); const resId = Object.entries(res?.rows?.[0])?.[0]?.[1]; if (resId != null) { this.idMap[chunk[this.autoColumn]] = resId; diff --git a/packages/tools/src/driverBase.ts b/packages/tools/src/driverBase.ts index ac7b312a..626ed0b2 100644 --- a/packages/tools/src/driverBase.ts +++ b/packages/tools/src/driverBase.ts @@ -23,6 +23,7 @@ const dialect = { export async function runCommandOnDriver(pool, driver: EngineDriver, cmd: (dmp: SqlDumper) => void): Promise { const dmp = driver.createDumper(); cmd(dmp as any); + // console.log('CMD:', dmp.s); await driver.query(pool, dmp.s, { discardResult: true }); } @@ -33,6 +34,7 @@ export async function runQueryOnDriver( ): Promise { const dmp = driver.createDumper(); cmd(dmp as any); + // console.log('QUERY:', dmp.s); return await driver.query(pool, dmp.s); } diff --git a/packages/types/dialect.d.ts b/packages/types/dialect.d.ts index 8669b4f6..166d8298 100644 --- a/packages/types/dialect.d.ts +++ b/packages/types/dialect.d.ts @@ -11,6 +11,7 @@ export interface SqlDialect { anonymousPrimaryKey?: boolean; defaultSchemaName?: string; enableConstraintsPerTable?: boolean; + requireStandaloneSelectForScopeIdentity?: boolean; dropColumnDependencies?: string[]; changeColumnDependencies?: string[]; diff --git a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js index 3a98fefa..20106c99 100644 --- a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js @@ -42,6 +42,7 @@ const dialect = { dropCheck: true, dropReferencesWhenDropTable: false, + requireStandaloneSelectForScopeIdentity: true, columnProperties: { columnComment: true,