sql export with correct dialect

This commit is contained in:
Jan Prochazka 2021-08-19 13:30:57 +02:00
parent b5c313e517
commit 60545674c5
3 changed files with 28 additions and 6 deletions

View File

@ -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 },

View File

@ -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);

View File

@ -103,4 +103,5 @@ export interface DatabaseInfoObjects {
export interface DatabaseInfo extends DatabaseInfoObjects {
schemas: SchemaInfo[];
engine: string;
}