postgre - analyse foreign keys

This commit is contained in:
Jan Prochazka 2020-05-16 09:34:28 +02:00
parent 7d6008a441
commit 942f22f1a3
6 changed files with 57 additions and 9 deletions

View File

@ -19,7 +19,7 @@ class MySqlAnalyser extends DatabaseAnalayser {
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('foreignCeys'));
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys'));
return this.mergeAnalyseResult({
tables: tables.rows.map((table) => ({

View File

@ -4,7 +4,7 @@ const sql = require('./sql');
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
class MySqlAnalyser extends DatabaseAnalayser {
class PostgreAnalyser extends DatabaseAnalayser {
constructor(pool, driver) {
super(pool, driver);
}
@ -17,8 +17,9 @@ class MySqlAnalyser extends DatabaseAnalayser {
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'));
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);
return this.mergeAnalyseResult({
tables: tables.rows.map((table) => ({
@ -29,12 +30,11 @@ class MySqlAnalyser extends DatabaseAnalayser {
...col,
notNull: !isNullable,
})),
foreignKeys: [],
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
})),
});
}
}
module.exports = MySqlAnalyser;
module.exports = PostgreAnalyser;

View File

@ -0,0 +1,24 @@
module.exports = `
select
fk.constraint_name as "constraintName",
fk.constraint_schema as "constraintSchema",
base.table_name as "pureName",
base.table_schema as "schemaName",
fk.update_rule as "updateAction",
fk.delete_rule as "deleteAction",
ref.table_name as "refTableName",
ref.table_schema as "refSchemaName",
basecol.column_name as "columnName",
refcol.column_name as "refColumnName"
from information_schema.referential_constraints fk
inner join information_schema.table_constraints base on fk.constraint_name = base.constraint_name and fk.constraint_schema = base.constraint_schema
inner join information_schema.table_constraints ref on fk.unique_constraint_name = ref.constraint_name and fk.unique_constraint_schema = ref.constraint_schema
inner join information_schema.key_column_usage basecol on base.table_name = basecol.table_name and base.constraint_name = basecol.constraint_name
inner join information_schema.key_column_usage refcol on ref.table_name = refcol.table_name and ref.constraint_name = refcol.constraint_name and basecol.ordinal_position = refcol.ordinal_position
where
base.table_schema <> 'information_schema'
and base.table_schema <> 'pg_catalog'
and base.table_schema !~ '^pg_toast'
and 'table:' || base.table_schema || '.' || base.table_name =[OBJECT_ID_CONDITION]
order by basecol.ordinal_position
`;

View File

@ -1,7 +1,11 @@
const columns = require('./columns');
const tableModifications = require('./tableModifications');
const primaryKeys = require('./primaryKeys');
const foreignKeys = require('./foreignKeys');
module.exports = {
columns,
tableModifications,
primaryKeys,
foreignKeys,
};

View File

@ -0,0 +1,17 @@
module.exports = `
select
table_constraints.constraint_schema as "constraintSchema",
table_constraints.constraint_name as "constraintName",
table_constraints.table_schema as "schemaName",
table_constraints.table_name as "pureName",
key_column_usage.column_name as "columnName"
from information_schema.table_constraints
inner join information_schema.key_column_usage on table_constraints.table_name = key_column_usage.table_name and table_constraints.constraint_name = key_column_usage.constraint_name
where
table_constraints.table_schema <> 'information_schema'
and table_constraints.table_schema <> 'pg_catalog'
and table_constraints.table_schema !~ '^pg_toast'
and table_constraints.constraint_type = 'PRIMARY KEY'
and 'table:' || table_constraints.table_schema || '.' || table_constraints.table_name =[OBJECT_ID_CONDITION]
order by key_column_usage.ordinal_position
`;

View File

@ -93,7 +93,10 @@ function SqlObjectList({ conid, database }) {
const [filter, setFilter] = React.useState('');
const objectList = _.flatten(
['tables', 'views', 'procedures', 'functions'].map((objectTypeField) =>
((objects || {})[objectTypeField] || []).map((obj) => ({ ...obj, objectTypeField }))
_.sortBy(
((objects || {})[objectTypeField] || []).map((obj) => ({ ...obj, objectTypeField })),
['schemaName', 'pureName']
)
)
);
const inputRef = React.useRef(null);