postgre, mysql analysers - refactor

This commit is contained in:
Jan Prochazka
2020-04-12 10:16:33 +02:00
parent fae97a8b4a
commit 6da41bcf4a
7 changed files with 51 additions and 36 deletions

View File

@@ -21,9 +21,9 @@ class DatabaseAnalyser {
/** @returns {Promise<import('@dbgate/types').DatabaseModification[]>} */ /** @returns {Promise<import('@dbgate/types').DatabaseModification[]>} */
async getModifications() { async getModifications() {
if (this.structure != null) throw new Error('DatabaseAnalyse.getModifications - structure must not be filled'); if (this.structure == null) throw new Error('DatabaseAnalyse.getModifications - structure must be filled');
return []; return null;
} }
async fullAnalysis() { async fullAnalysis() {
@@ -34,6 +34,11 @@ class DatabaseAnalyser {
this.structure = structure; this.structure = structure;
this.modifications = await this.getModifications(); this.modifications = await this.getModifications();
if (this.modifications == null) {
// modifications not implemented, perform full analysis
this.structure = null;
return 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._runAnalysis(); return this._runAnalysis();

View File

@@ -109,11 +109,11 @@ const driver = {
}, },
async analyseFull(pool) { async analyseFull(pool) {
const analyser = new MsSqlAnalyser(pool, this); const analyser = new MsSqlAnalyser(pool, this);
return await analyser.fullAnalysis(); return analyser.fullAnalysis();
}, },
async analyseIncremental(pool, structure) { async analyseIncremental(pool, structure) {
const analyser = new MsSqlAnalyser(pool, this); const analyser = new MsSqlAnalyser(pool, this);
return await analyser.incrementalAnalysis(structure); return analyser.incrementalAnalysis(structure);
}, },
createDumper() { createDumper() {
return new MsSqlDumper(this); return new MsSqlDumper(this);

View File

@@ -15,25 +15,27 @@ class MySqlAnalyser extends DatabaseAnalayser {
res = res.replace('#DATABASE#', this.pool._database_name); res = res.replace('#DATABASE#', this.pool._database_name);
return res; return res;
} }
async runAnalysis() { async _runAnalysis() {
const tables = await this.driver.query(this.pool, this.createQuery('tables')); const tables = await this.driver.query(this.pool, this.createQuery('tables'));
const columns = await this.driver.query(this.pool, this.createQuery('columns')); const columns = await this.driver.query(this.pool, this.createQuery('columns'));
// const pkColumns = await this.driver.query(this.pool, this.createQuery('primary_keys.sql')); // const pkColumns = await this.driver.query(this.pool, this.createQuery('primary_keys.sql'));
// const fkColumns = await this.driver.query(this.pool, this.createQuery('foreign_keys.sql')); // const fkColumns = await this.driver.query(this.pool, this.createQuery('foreign_keys.sql'));
this.result.tables = tables.rows.map((table) => ({ return this.mergeAnalyseResult({
...table, tables: tables.rows.map((table) => ({
columns: columns.rows ...table,
.filter((col) => col.pureName == table.pureName) columns: columns.rows
.map(({ isNullable, extra, ...col }) => ({ .filter((col) => col.pureName == table.pureName)
...col, .map(({ isNullable, extra, ...col }) => ({
notNull: !isNullable, ...col,
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'), notNull: !isNullable,
})), autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
foreignKeys: [], })),
// primaryKey: extractPrimaryKeys(table, pkColumns.rows), foreignKeys: [],
// foreignKeys: extractForeignKeys(table, fkColumns.rows), // primaryKey: extractPrimaryKeys(table, pkColumns.rows),
})); // foreignKeys: extractForeignKeys(table, fkColumns.rows),
})),
});
} }
} }

View File

@@ -42,8 +42,11 @@ const driver = {
}, },
async analyseFull(pool) { async analyseFull(pool) {
const analyser = new MySqlAnalyser(pool, this); const analyser = new MySqlAnalyser(pool, this);
await analyser.runAnalysis(); return analyser.fullAnalysis();
return analyser.result; },
async analyseIncremental(pool, structure) {
const analyser = new MySqlAnalyser(pool, this);
return analyser.incrementalAnalysis(structure);
}, },
async listDatabases(connection) { async listDatabases(connection) {
const { rows } = await this.query(connection, "show databases"); const { rows } = await this.query(connection, "show databases");

View File

@@ -14,24 +14,26 @@ class MySqlAnalyser extends DatabaseAnalayser {
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null'); res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
return res; return res;
} }
async runAnalysis() { async _runAnalysis() {
const tables = await this.driver.query(this.pool, this.createQuery('tableModifications')); const tables = await this.driver.query(this.pool, this.createQuery('tableModifications'));
const columns = await this.driver.query(this.pool, this.createQuery('columns')); const columns = await this.driver.query(this.pool, this.createQuery('columns'));
// const pkColumns = await this.driver.query(this.pool, this.createQuery('primary_keys.sql')); // const pkColumns = await this.driver.query(this.pool, this.createQuery('primary_keys.sql'));
// const fkColumns = await this.driver.query(this.pool, this.createQuery('foreign_keys.sql')); // const fkColumns = await this.driver.query(this.pool, this.createQuery('foreign_keys.sql'));
this.result.tables = tables.rows.map((table) => ({ return this.mergeAnalyseResult({
...table, tables: tables.rows.map((table) => ({
columns: columns.rows ...table,
.filter((col) => col.pureName == table.pureName && col.schemaName == table.schemaName) columns: columns.rows
.map(({ isNullable, ...col }) => ({ .filter((col) => col.pureName == table.pureName && col.schemaName == table.schemaName)
...col, .map(({ isNullable, ...col }) => ({
notNull: !isNullable, ...col,
})), notNull: !isNullable,
foreignKeys: [], })),
// primaryKey: extractPrimaryKeys(table, pkColumns.rows), foreignKeys: [],
// foreignKeys: extractForeignKeys(table, fkColumns.rows), // primaryKey: extractPrimaryKeys(table, pkColumns.rows),
})); // foreignKeys: extractForeignKeys(table, fkColumns.rows),
})),
});
} }
} }

View File

@@ -29,8 +29,11 @@ const driver = {
}, },
async analyseFull(pool) { async analyseFull(pool) {
const analyser = new PostgreAnalyser(pool, this); const analyser = new PostgreAnalyser(pool, this);
await analyser.runAnalysis(); return analyser.fullAnalysis();
return analyser.result; },
async analyseIncremental(pool, structure) {
const analyser = new PostgreAnalyser(pool, this);
return analyser.incrementalAnalysis(structure);
}, },
createDumper() { createDumper() {
return new PostgreDumper(this); return new PostgreDumper(this);

View File

@@ -1,4 +1,4 @@
import { DbType } from './DbType'; import { DbType } from './dbtypes';
export interface NamedObjectInfo { export interface NamedObjectInfo {
pureName: string; pureName: string;