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 stream = require('stream');
const path = require('path'); const path = require('path');
const { driverBase } = require('dbgate-tools'); const { driverBase } = require('dbgate-tools');
const requireEngineDriver = require('../utility/requireEngineDriver');
class SqlizeStream extends stream.Transform { class SqlizeStream extends stream.Transform {
constructor({ fileName }) { constructor({ fileName }) {
super({ objectMode: true }); super({ objectMode: true });
this.wasHeader = false; this.wasHeader = false;
this.tableName = path.parse(fileName).name; this.tableName = path.parse(fileName).name;
this.driver = driverBase;
} }
_transform(chunk, encoding, done) { _transform(chunk, encoding, done) {
let skip = false; let skip = false;
@@ -19,11 +21,15 @@ class SqlizeStream extends stream.Transform {
) { ) {
skip = true; skip = true;
this.tableName = chunk.pureName; this.tableName = chunk.pureName;
if (chunk.engine) {
// @ts-ignore
this.driver = requireEngineDriver(chunk.engine) || driverBase;
}
} }
this.wasHeader = true; this.wasHeader = true;
} }
if (!skip) { if (!skip) {
const dmp = driverBase.createDumper(); const dmp = this.driver.createDumper();
dmp.put( dmp.put(
'^insert ^into %f (%,i) ^values (%,v);\n', '^insert ^into %f (%,i) ^values (%,v);\n',
{ pureName: this.tableName }, { pureName: this.tableName },

View File

@@ -4,7 +4,10 @@ import _groupBy from 'lodash/groupBy';
import _pick from 'lodash/pick'; import _pick from 'lodash/pick';
import _compact from 'lodash/compact'; import _compact from 'lodash/compact';
const STRUCTURE_FIELDS = ['tables', 'collections', 'views', 'matviews', 'functions', 'procedures', 'triggers'];
const fp_pick = arg => array => _pick(array, arg); const fp_pick = arg => array => _pick(array, arg);
export class DatabaseAnalyser { export class DatabaseAnalyser {
structure: DatabaseInfo; structure: DatabaseInfo;
modifications: DatabaseModification[]; modifications: DatabaseModification[];
@@ -23,8 +26,20 @@ export class DatabaseAnalyser {
async _computeSingleObjectId() {} 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() { async fullAnalysis() {
const res = await this._runAnalysis(); const res = this.addEngineField(await this._runAnalysis());
// console.log('FULL ANALYSIS', res); // console.log('FULL ANALYSIS', res);
return res; return res;
} }
@@ -33,7 +48,7 @@ export class DatabaseAnalyser {
// console.log('Analysing SINGLE OBJECT', name, typeField); // console.log('Analysing SINGLE OBJECT', name, typeField);
this.singleObjectFilter = { ...name, typeField }; this.singleObjectFilter = { ...name, typeField };
await this._computeSingleObjectId(); await this._computeSingleObjectId();
const res = await this._runAnalysis(); const res = this.addEngineField(await this._runAnalysis());
// console.log('SINGLE OBJECT RES', res); // console.log('SINGLE OBJECT RES', res);
const obj = const obj =
res[typeField]?.length == 1 res[typeField]?.length == 1
@@ -50,11 +65,11 @@ export class DatabaseAnalyser {
if (this.modifications == null) { if (this.modifications == null) {
// modifications not implemented, perform full analysis // modifications not implemented, perform full analysis
this.structure = null; this.structure = null;
return this._runAnalysis(); return this.addEngineField(await this._runAnalysis());
} }
if (this.modifications.length == 0) return null; if (this.modifications.length == 0) return null;
console.log('DB modifications detected:', this.modifications); console.log('DB modifications detected:', this.modifications);
return this.mergeAnalyseResult(await this._runAnalysis()); return this.addEngineField(this.mergeAnalyseResult(await this._runAnalysis()));
} }
mergeAnalyseResult(newlyAnalysed) { mergeAnalyseResult(newlyAnalysed) {
@@ -66,7 +81,7 @@ export class DatabaseAnalyser {
} }
const res = {}; const res = {};
for (const field of ['tables', 'collections', 'views', 'matviews', 'functions', 'procedures', 'triggers']) { for (const field of STRUCTURE_FIELDS) {
const removedIds = this.modifications const removedIds = this.modifications
.filter(x => x.action == 'remove' && x.objectTypeField == field) .filter(x => x.action == 'remove' && x.objectTypeField == field)
.map(x => x.objectId); .map(x => x.objectId);

View File

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