cockroach fk analyse fix

This commit is contained in:
Jan Prochazka 2021-09-04 21:54:12 +02:00
parent e2ce349a30
commit f4c39bbf3c
6 changed files with 34 additions and 4 deletions

View File

@ -4,6 +4,8 @@ const { testWrapper } = require('../tools');
const t1Sql = 'CREATE TABLE t1 (id int not null primary key, val1 varchar(50) null)'; const t1Sql = 'CREATE TABLE t1 (id int not null primary key, val1 varchar(50) null)';
const ix1Sql = 'CREATE index ix1 ON t1(val1, id)'; const ix1Sql = 'CREATE index ix1 ON t1(val1, id)';
const t2Sql = 'CREATE TABLE t2 (id int not null primary key, val2 varchar(50) null unique)'; const t2Sql = 'CREATE TABLE t2 (id int not null primary key, val2 varchar(50) null unique)';
const t3Sql = 'CREATE TABLE t3 (id int not null primary key, valfk int, foreign key (valfk) references t2(id))';
// const fkSql = 'ALTER TABLE t3 ADD FOREIGN KEY (valfk) REFERENCES t2(id)'
const txMatch = (tname, vcolname, nextcol) => const txMatch = (tname, vcolname, nextcol) =>
expect.objectContaining({ expect.objectContaining({
@ -139,4 +141,24 @@ describe('Table analyse', () => {
expect(t2.uniques[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val2' })); expect(t2.uniques[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val2' }));
}) })
); );
test.each(engines.map(engine => [engine.label, engine]))(
'Foreign key - full analysis - %s',
testWrapper(async (conn, driver, engine) => {
await driver.query(conn, t2Sql);
await driver.query(conn, t3Sql);
// await driver.query(conn, fkSql);
const structure = await driver.analyseFull(conn);
const t3 = structure.tables.find(x => x.pureName == 't3');
console.log('T3', t3.foreignKeys[0].columns);
expect(t3.foreignKeys.length).toEqual(1);
expect(t3.foreignKeys[0].columns.length).toEqual(1);
expect(t3.foreignKeys[0]).toEqual(expect.objectContaining({ refTableName: 't2' }));
expect(t3.foreignKeys[0].columns[0]).toEqual(
expect.objectContaining({ columnName: 'valfk', refColumnName: 'id' })
);
})
);
}); });

View File

@ -34,7 +34,7 @@ const engines = [
}, },
{ {
label: 'PostgreSQL', label: 'PostgreSQL',
skipLocal: true, // skipLocal: true,
connection: { connection: {
engine: 'postgres@dbgate-plugin-postgres', engine: 'postgres@dbgate-plugin-postgres',
password: 'Pwd2020Db', password: 'Pwd2020Db',

View File

@ -429,7 +429,6 @@ export class SqlDumper implements AlterProcessor {
} }
createIndex(ix: IndexInfo) { createIndex(ix: IndexInfo) {
this.put('^create'); this.put('^create');
if (ix.indexType) this.put(' %k', ix.indexType);
if (ix.isUnique) this.put(' ^unique'); if (ix.isUnique) this.put(' ^unique');
this.put(' ^index %i &n^on %f (&>&n', ix.constraintName, ix); this.put(' ^index %i &n^on %f (&>&n', ix.constraintName, ix);
this.putCollection(',&n', ix.columns, col => { this.putCollection(',&n', ix.columns, col => {

View File

@ -41,7 +41,9 @@ class Analyser extends DatabaseAnalyser {
} }
createQuery(resFileName, typeFields) { createQuery(resFileName, typeFields) {
return super.createQuery(sql[resFileName], typeFields); return super
.createQuery(sql[resFileName], typeFields)
.replace('#REFTABLECOND#', this.driver.__analyserInternals.refTableCond);
} }
async _computeSingleObjectId() { async _computeSingleObjectId() {

View File

@ -12,7 +12,7 @@ select
refcol.column_name as "ref_column_name" refcol.column_name as "ref_column_name"
from information_schema.referential_constraints fk 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 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.table_constraints ref on fk.unique_constraint_name = ref.constraint_name and fk.unique_constraint_schema = ref.constraint_schema #REFTABLECOND#
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 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 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 where

View File

@ -33,6 +33,10 @@ const postgresDriverBase = {
showConnectionField: (field, values) => showConnectionField: (field, values) =>
['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field), ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field),
getQuerySplitterOptions: () => postgreSplitterOptions, getQuerySplitterOptions: () => postgreSplitterOptions,
__analyserInternals: {
refTableCond: '',
}
}; };
/** @type {import('dbgate-types').EngineDriver} */ /** @type {import('dbgate-types').EngineDriver} */
@ -59,6 +63,9 @@ const cockroachDriver = {
dropColumnDependencies: ['primaryKey'], dropColumnDependencies: ['primaryKey'],
dropPrimaryKey: false, dropPrimaryKey: false,
}, },
__analyserInternals: {
refTableCond: 'and fk.referenced_table_name = ref.table_name',
}
}; };
/** @type {import('dbgate-types').EngineDriver} */ /** @type {import('dbgate-types').EngineDriver} */