mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-17 22:36:01 +00:00
postgre, mysql analysers - refactor
This commit is contained in:
@@ -21,9 +21,9 @@ class DatabaseAnalyser {
|
||||
|
||||
/** @returns {Promise<import('@dbgate/types').DatabaseModification[]>} */
|
||||
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() {
|
||||
@@ -34,6 +34,11 @@ class DatabaseAnalyser {
|
||||
this.structure = structure;
|
||||
|
||||
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;
|
||||
console.log('DB modifications detected:', this.modifications);
|
||||
return this._runAnalysis();
|
||||
|
||||
@@ -109,11 +109,11 @@ const driver = {
|
||||
},
|
||||
async analyseFull(pool) {
|
||||
const analyser = new MsSqlAnalyser(pool, this);
|
||||
return await analyser.fullAnalysis();
|
||||
return analyser.fullAnalysis();
|
||||
},
|
||||
async analyseIncremental(pool, structure) {
|
||||
const analyser = new MsSqlAnalyser(pool, this);
|
||||
return await analyser.incrementalAnalysis(structure);
|
||||
return analyser.incrementalAnalysis(structure);
|
||||
},
|
||||
createDumper() {
|
||||
return new MsSqlDumper(this);
|
||||
|
||||
@@ -15,25 +15,27 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
||||
res = res.replace('#DATABASE#', this.pool._database_name);
|
||||
return res;
|
||||
}
|
||||
async runAnalysis() {
|
||||
async _runAnalysis() {
|
||||
const tables = await this.driver.query(this.pool, this.createQuery('tables'));
|
||||
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 fkColumns = await this.driver.query(this.pool, this.createQuery('foreign_keys.sql'));
|
||||
|
||||
this.result.tables = tables.rows.map((table) => ({
|
||||
...table,
|
||||
columns: columns.rows
|
||||
.filter((col) => col.pureName == table.pureName)
|
||||
.map(({ isNullable, extra, ...col }) => ({
|
||||
...col,
|
||||
notNull: !isNullable,
|
||||
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
|
||||
})),
|
||||
foreignKeys: [],
|
||||
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
||||
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
||||
}));
|
||||
return this.mergeAnalyseResult({
|
||||
tables: tables.rows.map((table) => ({
|
||||
...table,
|
||||
columns: columns.rows
|
||||
.filter((col) => col.pureName == table.pureName)
|
||||
.map(({ isNullable, extra, ...col }) => ({
|
||||
...col,
|
||||
notNull: !isNullable,
|
||||
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
|
||||
})),
|
||||
foreignKeys: [],
|
||||
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
||||
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,8 +42,11 @@ const driver = {
|
||||
},
|
||||
async analyseFull(pool) {
|
||||
const analyser = new MySqlAnalyser(pool, this);
|
||||
await analyser.runAnalysis();
|
||||
return analyser.result;
|
||||
return analyser.fullAnalysis();
|
||||
},
|
||||
async analyseIncremental(pool, structure) {
|
||||
const analyser = new MySqlAnalyser(pool, this);
|
||||
return analyser.incrementalAnalysis(structure);
|
||||
},
|
||||
async listDatabases(connection) {
|
||||
const { rows } = await this.query(connection, "show databases");
|
||||
|
||||
@@ -14,24 +14,26 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
||||
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
|
||||
return res;
|
||||
}
|
||||
async runAnalysis() {
|
||||
async _runAnalysis() {
|
||||
const tables = await this.driver.query(this.pool, this.createQuery('tableModifications'));
|
||||
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 fkColumns = await this.driver.query(this.pool, this.createQuery('foreign_keys.sql'));
|
||||
|
||||
this.result.tables = tables.rows.map((table) => ({
|
||||
...table,
|
||||
columns: columns.rows
|
||||
.filter((col) => col.pureName == table.pureName && col.schemaName == table.schemaName)
|
||||
.map(({ isNullable, ...col }) => ({
|
||||
...col,
|
||||
notNull: !isNullable,
|
||||
})),
|
||||
foreignKeys: [],
|
||||
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
||||
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
||||
}));
|
||||
return this.mergeAnalyseResult({
|
||||
tables: tables.rows.map((table) => ({
|
||||
...table,
|
||||
columns: columns.rows
|
||||
.filter((col) => col.pureName == table.pureName && col.schemaName == table.schemaName)
|
||||
.map(({ isNullable, ...col }) => ({
|
||||
...col,
|
||||
notNull: !isNullable,
|
||||
})),
|
||||
foreignKeys: [],
|
||||
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
||||
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,8 +29,11 @@ const driver = {
|
||||
},
|
||||
async analyseFull(pool) {
|
||||
const analyser = new PostgreAnalyser(pool, this);
|
||||
await analyser.runAnalysis();
|
||||
return analyser.result;
|
||||
return analyser.fullAnalysis();
|
||||
},
|
||||
async analyseIncremental(pool, structure) {
|
||||
const analyser = new PostgreAnalyser(pool, this);
|
||||
return analyser.incrementalAnalysis(structure);
|
||||
},
|
||||
createDumper() {
|
||||
return new PostgreDumper(this);
|
||||
|
||||
2
packages/types/dbinfo.d.ts
vendored
2
packages/types/dbinfo.d.ts
vendored
@@ -1,4 +1,4 @@
|
||||
import { DbType } from './DbType';
|
||||
import { DbType } from './dbtypes';
|
||||
|
||||
export interface NamedObjectInfo {
|
||||
pureName: string;
|
||||
|
||||
Reference in New Issue
Block a user