dbgate/packages/engines/postgres/PostgreAnalyser.js

41 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-04-11 18:24:30 +00:00
const fp = require('lodash/fp');
const _ = require('lodash');
const sql = require('./sql');
2020-02-02 19:28:27 +00:00
2020-04-11 18:24:30 +00:00
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
2020-02-02 19:28:27 +00:00
2020-05-16 07:34:28 +00:00
class PostgreAnalyser extends DatabaseAnalayser {
2020-02-02 19:28:27 +00:00
constructor(pool, driver) {
super(pool, driver);
}
2020-04-11 18:24:30 +00:00
createQuery(resFileName, tables = false, views = false, procedures = false, functions = false, triggers = false) {
2020-03-14 09:38:10 +00:00
let res = sql[resFileName];
2020-04-11 18:24:30 +00:00
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
2020-02-02 19:28:27 +00:00
return res;
}
2020-04-12 08:16:33 +00:00
async _runAnalysis() {
2020-04-11 18:24:30 +00:00
const tables = await this.driver.query(this.pool, this.createQuery('tableModifications'));
const columns = await this.driver.query(this.pool, this.createQuery('columns'));
2020-05-16 07:34:28 +00:00
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys'));
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys'));
// console.log('PG fkColumns', fkColumns.rows);
2020-02-02 19:28:27 +00:00
2020-04-12 08:16:33 +00:00
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,
})),
2020-05-16 07:34:28 +00:00
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
2020-04-12 08:16:33 +00:00
})),
});
2020-02-02 19:28:27 +00:00
}
}
2020-05-16 07:34:28 +00:00
module.exports = PostgreAnalyser;