mirror of
https://github.com/dbgate/dbgate
synced 2024-11-22 16:27:18 +00:00
cockroach fk analyse fix
This commit is contained in:
parent
e2ce349a30
commit
f4c39bbf3c
@ -4,6 +4,8 @@ const { testWrapper } = require('../tools');
|
||||
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 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) =>
|
||||
expect.objectContaining({
|
||||
@ -139,4 +141,24 @@ describe('Table analyse', () => {
|
||||
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' })
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
@ -34,7 +34,7 @@ const engines = [
|
||||
},
|
||||
{
|
||||
label: 'PostgreSQL',
|
||||
skipLocal: true,
|
||||
// skipLocal: true,
|
||||
connection: {
|
||||
engine: 'postgres@dbgate-plugin-postgres',
|
||||
password: 'Pwd2020Db',
|
||||
|
@ -429,7 +429,6 @@ export class SqlDumper implements AlterProcessor {
|
||||
}
|
||||
createIndex(ix: IndexInfo) {
|
||||
this.put('^create');
|
||||
if (ix.indexType) this.put(' %k', ix.indexType);
|
||||
if (ix.isUnique) this.put(' ^unique');
|
||||
this.put(' ^index %i &n^on %f (&>&n', ix.constraintName, ix);
|
||||
this.putCollection(',&n', ix.columns, col => {
|
||||
|
@ -41,7 +41,9 @@ class Analyser extends DatabaseAnalyser {
|
||||
}
|
||||
|
||||
createQuery(resFileName, typeFields) {
|
||||
return super.createQuery(sql[resFileName], typeFields);
|
||||
return super
|
||||
.createQuery(sql[resFileName], typeFields)
|
||||
.replace('#REFTABLECOND#', this.driver.__analyserInternals.refTableCond);
|
||||
}
|
||||
|
||||
async _computeSingleObjectId() {
|
||||
|
@ -12,7 +12,7 @@ select
|
||||
refcol.column_name as "ref_column_name"
|
||||
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.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 refcol on ref.table_name = refcol.table_name and ref.constraint_name = refcol.constraint_name and basecol.ordinal_position = refcol.ordinal_position
|
||||
where
|
||||
|
@ -33,6 +33,10 @@ const postgresDriverBase = {
|
||||
showConnectionField: (field, values) =>
|
||||
['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase'].includes(field),
|
||||
getQuerySplitterOptions: () => postgreSplitterOptions,
|
||||
|
||||
__analyserInternals: {
|
||||
refTableCond: '',
|
||||
}
|
||||
};
|
||||
|
||||
/** @type {import('dbgate-types').EngineDriver} */
|
||||
@ -59,6 +63,9 @@ const cockroachDriver = {
|
||||
dropColumnDependencies: ['primaryKey'],
|
||||
dropPrimaryKey: false,
|
||||
},
|
||||
__analyserInternals: {
|
||||
refTableCond: 'and fk.referenced_table_name = ref.table_name',
|
||||
}
|
||||
};
|
||||
|
||||
/** @type {import('dbgate-types').EngineDriver} */
|
||||
|
Loading…
Reference in New Issue
Block a user