mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 23:13:57 +00:00
postgre analyser
This commit is contained in:
@@ -83,12 +83,15 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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', ['tables']));
|
||||||
const columns = await this.driver.query(this.pool, this.createQuery('columns'));
|
const columns = await this.driver.query(this.pool, this.createQuery('columns', ['tables', 'views']));
|
||||||
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys'));
|
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables']));
|
||||||
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys'));
|
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables']));
|
||||||
const views = await this.driver.query(this.pool, this.createQuery('views'));
|
const views = await this.driver.query(this.pool, this.createQuery('views', ['views']));
|
||||||
const programmables = await this.driver.query(this.pool, this.createQuery('programmables'));
|
const programmables = await this.driver.query(
|
||||||
|
this.pool,
|
||||||
|
this.createQuery('programmables', ['procedures', 'functions'])
|
||||||
|
);
|
||||||
|
|
||||||
const viewTexts = await this.getViewTexts(views.rows.map((x) => x.pureName));
|
const viewTexts = await this.getViewTexts(views.rows.map((x) => x.pureName));
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ function getColumnInfo({
|
|||||||
return {
|
return {
|
||||||
columnName,
|
columnName,
|
||||||
dataType: fullDataType,
|
dataType: fullDataType,
|
||||||
notNull: !isNullable,
|
notNull: !isNullable || isNullable == 'NO' || isNullable == 'no',
|
||||||
autoIncrement: !!isIdentity,
|
autoIncrement: !!isIdentity,
|
||||||
defaultValue,
|
defaultValue,
|
||||||
};
|
};
|
||||||
@@ -40,16 +40,17 @@ class PostgreAnalyser extends DatabaseAnalayser {
|
|||||||
super(pool, driver);
|
super(pool, driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
createQuery(resFileName, tables = false, views = false, procedures = false, functions = false, triggers = false) {
|
createQuery(resFileName, typeFields) {
|
||||||
let res = sql[resFileName];
|
let res = sql[resFileName];
|
||||||
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', ['tables']));
|
||||||
const columns = await this.driver.query(this.pool, this.createQuery('columns'));
|
const columns = await this.driver.query(this.pool, this.createQuery('columns', ['tables']));
|
||||||
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys'));
|
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables']));
|
||||||
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys'));
|
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);
|
// console.log('PG fkColumns', fkColumns.rows);
|
||||||
|
|
||||||
return this.mergeAnalyseResult({
|
return this.mergeAnalyseResult({
|
||||||
@@ -61,6 +62,12 @@ class PostgreAnalyser extends DatabaseAnalayser {
|
|||||||
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
||||||
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.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),
|
||||||
|
})),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ const columns = require('./columns');
|
|||||||
const tableModifications = require('./tableModifications');
|
const tableModifications = require('./tableModifications');
|
||||||
const primaryKeys = require('./primaryKeys');
|
const primaryKeys = require('./primaryKeys');
|
||||||
const foreignKeys = require('./foreignKeys');
|
const foreignKeys = require('./foreignKeys');
|
||||||
|
const views = require('./views');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
columns,
|
columns,
|
||||||
tableModifications,
|
tableModifications,
|
||||||
primaryKeys,
|
primaryKeys,
|
||||||
foreignKeys,
|
foreignKeys,
|
||||||
|
views,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ SELECT oid as "objectId", nspname as "schemaName", relname as "pureName",
|
|||||||
' ' || column_name || ' ' || type || ' '|| not_null
|
' ' || column_name || ' ' || type || ' '|| not_null
|
||||||
)
|
)
|
||||||
, E',\\n'
|
, 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
|
from
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
|
|||||||
9
packages/engines/postgres/sql/views.js
Normal file
9
packages/engines/postgres/sql/views.js
Normal 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'
|
||||||
|
`;
|
||||||
1
packages/types/dbinfo.d.ts
vendored
1
packages/types/dbinfo.d.ts
vendored
@@ -45,6 +45,7 @@ export interface DatabaseObjectInfo extends NamedObjectInfo {
|
|||||||
objectId?: string;
|
objectId?: string;
|
||||||
createDate?: string;
|
createDate?: string;
|
||||||
modifyDate?: string;
|
modifyDate?: string;
|
||||||
|
hashCode?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SqlObjectInfo extends DatabaseObjectInfo {
|
export interface SqlObjectInfo extends DatabaseObjectInfo {
|
||||||
|
|||||||
Reference in New Issue
Block a user