postgre analyser

This commit is contained in:
Jan Prochazka 2020-06-29 19:49:54 +02:00
parent eaaa7beaa1
commit 3c1be39976
6 changed files with 35 additions and 13 deletions

View File

@ -83,12 +83,15 @@ class MySqlAnalyser extends DatabaseAnalayser {
}
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('primaryKeys'));
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys'));
const views = await this.driver.query(this.pool, this.createQuery('views'));
const programmables = await this.driver.query(this.pool, this.createQuery('programmables'));
const tables = await this.driver.query(this.pool, this.createQuery('tables', ['tables']));
const columns = await this.driver.query(this.pool, this.createQuery('columns', ['tables', 'views']));
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables']));
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables']));
const views = await this.driver.query(this.pool, this.createQuery('views', ['views']));
const programmables = await this.driver.query(
this.pool,
this.createQuery('programmables', ['procedures', 'functions'])
);
const viewTexts = await this.getViewTexts(views.rows.map((x) => x.pureName));

View File

@ -29,7 +29,7 @@ function getColumnInfo({
return {
columnName,
dataType: fullDataType,
notNull: !isNullable,
notNull: !isNullable || isNullable == 'NO' || isNullable == 'no',
autoIncrement: !!isIdentity,
defaultValue,
};
@ -40,16 +40,17 @@ class PostgreAnalyser extends DatabaseAnalayser {
super(pool, driver);
}
createQuery(resFileName, tables = false, views = false, procedures = false, functions = false, triggers = false) {
createQuery(resFileName, typeFields) {
let res = sql[resFileName];
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
return res;
}
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('primaryKeys'));
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys'));
const tables = await this.driver.query(this.pool, this.createQuery('tableModifications', ['tables']));
const columns = await this.driver.query(this.pool, this.createQuery('columns', ['tables']));
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables']));
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables']));
const views = await this.driver.query(this.pool, this.createQuery('views', ['views']));
// console.log('PG fkColumns', fkColumns.rows);
return this.mergeAnalyseResult({
@ -61,6 +62,12 @@ class PostgreAnalyser extends DatabaseAnalayser {
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
})),
views: views.rows.map((view) => ({
...view,
columns: columns.rows
.filter((col) => col.pureName == view.pureName && col.schemaName == view.schemaName)
.map(getColumnInfo),
})),
});
}
}

View File

@ -2,10 +2,12 @@ const columns = require('./columns');
const tableModifications = require('./tableModifications');
const primaryKeys = require('./primaryKeys');
const foreignKeys = require('./foreignKeys');
const views = require('./views');
module.exports = {
columns,
tableModifications,
primaryKeys,
foreignKeys,
views,
};

View File

@ -20,7 +20,7 @@ SELECT oid as "objectId", nspname as "schemaName", relname as "pureName",
' ' || column_name || ' ' || type || ' '|| not_null
)
, E',\\n'
) || E'\\n);\\n' || (select pkey from pkey where pkey.conrelid = oid)) as "hash"
) || E'\\n);\\n' || (select pkey from pkey where pkey.conrelid = oid)) as "hashCode"
from
(
SELECT

View File

@ -0,0 +1,9 @@
module.exports = `
select
table_name as "pureName",
table_schema as "schemaName",
view_definition as "createSql",
md5(view_definition) as "hashCode"
from
information_schema.views where table_schema != 'information_schema' and table_schema != 'pg_catalog'
`;

View File

@ -45,6 +45,7 @@ export interface DatabaseObjectInfo extends NamedObjectInfo {
objectId?: string;
createDate?: string;
modifyDate?: string;
hashCode?: string;
}
export interface SqlObjectInfo extends DatabaseObjectInfo {