diff --git a/packages/api/src/shell/sqlDataWriter.js b/packages/api/src/shell/sqlDataWriter.js index 9783f3883..50324db03 100644 --- a/packages/api/src/shell/sqlDataWriter.js +++ b/packages/api/src/shell/sqlDataWriter.js @@ -2,12 +2,14 @@ const fs = require('fs'); const stream = require('stream'); const path = require('path'); const { driverBase } = require('dbgate-tools'); +const requireEngineDriver = require('../utility/requireEngineDriver'); class SqlizeStream extends stream.Transform { constructor({ fileName }) { super({ objectMode: true }); this.wasHeader = false; this.tableName = path.parse(fileName).name; + this.driver = driverBase; } _transform(chunk, encoding, done) { let skip = false; @@ -19,11 +21,15 @@ class SqlizeStream extends stream.Transform { ) { skip = true; this.tableName = chunk.pureName; + if (chunk.engine) { + // @ts-ignore + this.driver = requireEngineDriver(chunk.engine) || driverBase; + } } this.wasHeader = true; } if (!skip) { - const dmp = driverBase.createDumper(); + const dmp = this.driver.createDumper(); dmp.put( '^insert ^into %f (%,i) ^values (%,v);\n', { pureName: this.tableName }, diff --git a/packages/tools/src/DatabaseAnalyser.ts b/packages/tools/src/DatabaseAnalyser.ts index 4b5ee4eda..45d8d7baa 100644 --- a/packages/tools/src/DatabaseAnalyser.ts +++ b/packages/tools/src/DatabaseAnalyser.ts @@ -4,7 +4,10 @@ import _groupBy from 'lodash/groupBy'; import _pick from 'lodash/pick'; import _compact from 'lodash/compact'; +const STRUCTURE_FIELDS = ['tables', 'collections', 'views', 'matviews', 'functions', 'procedures', 'triggers']; + const fp_pick = arg => array => _pick(array, arg); + export class DatabaseAnalyser { structure: DatabaseInfo; modifications: DatabaseModification[]; @@ -23,8 +26,20 @@ export class DatabaseAnalyser { async _computeSingleObjectId() {} + addEngineField(db: DatabaseInfo) { + if (!this.driver?.engine) return; + for (const field of STRUCTURE_FIELDS) { + if (!db[field]) continue; + for (const item of db[field]) { + item.engine = this.driver.engine; + } + } + db.engine = this.driver.engine; + return db; + } + async fullAnalysis() { - const res = await this._runAnalysis(); + const res = this.addEngineField(await this._runAnalysis()); // console.log('FULL ANALYSIS', res); return res; } @@ -33,7 +48,7 @@ export class DatabaseAnalyser { // console.log('Analysing SINGLE OBJECT', name, typeField); this.singleObjectFilter = { ...name, typeField }; await this._computeSingleObjectId(); - const res = await this._runAnalysis(); + const res = this.addEngineField(await this._runAnalysis()); // console.log('SINGLE OBJECT RES', res); const obj = res[typeField]?.length == 1 @@ -50,11 +65,11 @@ export class DatabaseAnalyser { if (this.modifications == null) { // modifications not implemented, perform full analysis this.structure = null; - return this._runAnalysis(); + return this.addEngineField(await this._runAnalysis()); } if (this.modifications.length == 0) return null; console.log('DB modifications detected:', this.modifications); - return this.mergeAnalyseResult(await this._runAnalysis()); + return this.addEngineField(this.mergeAnalyseResult(await this._runAnalysis())); } mergeAnalyseResult(newlyAnalysed) { @@ -66,7 +81,7 @@ export class DatabaseAnalyser { } const res = {}; - for (const field of ['tables', 'collections', 'views', 'matviews', 'functions', 'procedures', 'triggers']) { + for (const field of STRUCTURE_FIELDS) { const removedIds = this.modifications .filter(x => x.action == 'remove' && x.objectTypeField == field) .map(x => x.objectId); diff --git a/packages/types/dbinfo.d.ts b/packages/types/dbinfo.d.ts index faa33f8ad..4e4a45194 100644 --- a/packages/types/dbinfo.d.ts +++ b/packages/types/dbinfo.d.ts @@ -103,4 +103,5 @@ export interface DatabaseInfoObjects { export interface DatabaseInfo extends DatabaseInfoObjects { schemas: SchemaInfo[]; + engine: string; }