From dfe37496f244e9d5ada85a874d8caf7fea7bc37d Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 26 Aug 2021 13:00:38 +0200 Subject: [PATCH] unique analysis --- .../__tests__/table-analyse.spec.js | 16 +++++++++++++- .../src/backend/Analyser.js | 1 + .../src/backend/Analyser.js | 22 ++++++++++++++----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/integration-tests/__tests__/table-analyse.spec.js b/integration-tests/__tests__/table-analyse.spec.js index a637fe2f..df8e98b8 100644 --- a/integration-tests/__tests__/table-analyse.spec.js +++ b/integration-tests/__tests__/table-analyse.spec.js @@ -3,7 +3,7 @@ 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)'; +const t2Sql = 'CREATE TABLE t2 (id int not null primary key, val2 varchar(50) null unique)'; const txMatch = (tname, vcolname, nextcol) => expect.objectContaining({ @@ -125,4 +125,18 @@ describe('Table analyse', () => { expect(t1.indexes[0].columns[1]).toEqual(expect.objectContaining({ columnName: 'id' })); }) ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Unique - full analysis - %s', + testWrapper(async (conn, driver, engine) => { + await driver.query(conn, t2Sql); + const structure = await driver.analyseFull(conn); + + const t2 = structure.tables.find(x => x.pureName == 't2'); + const indexesAndUniques = [...t2.uniques, ...t2.indexes]; + expect(indexesAndUniques.length).toEqual(1); + expect(indexesAndUniques[0].columns.length).toEqual(1); + expect(indexesAndUniques[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val2' })); + }) + ); }); diff --git a/plugins/dbgate-plugin-postgres/src/backend/Analyser.js b/plugins/dbgate-plugin-postgres/src/backend/Analyser.js index 17271d52..7ddc3b09 100644 --- a/plugins/dbgate-plugin-postgres/src/backend/Analyser.js +++ b/plugins/dbgate-plugin-postgres/src/backend/Analyser.js @@ -120,6 +120,7 @@ class Analyser extends DatabaseAnalyser { })) ), })), + uniques: [], }; }), views: views.rows.map(view => ({ diff --git a/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js b/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js index 42a49fe2..f1c6deed 100644 --- a/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js +++ b/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js @@ -6,11 +6,12 @@ SELECT m.name as tableName, il.name as constraintName, il."unique" as isUnique, - ii.name as columnName + ii.name as columnName, + il.origin FROM sqlite_schema AS m, pragma_index_list(m.name) AS il, pragma_index_info(il.name) AS ii - WHERE m.type='table' AND il.name NOT LIKE 'sqlite_autoindex_%' + WHERE m.type='table' AND il.origin <> 'pk' ORDER BY ii.seqno `; @@ -82,7 +83,9 @@ class Analyser extends DatabaseAnalyser { autoIncrement: tableSqls[tableName].toLowerCase().includes('autoincrement') && !!col.pk, })); - const indexNames = _.uniq(indexcols.rows.filter((x) => x.tableName == tableName).map((x) => x.constraintName)); + const indexNames = _.uniq( + indexcols.rows.filter((x) => x.tableName == tableName && x.origin == 'c').map((x) => x.constraintName) + ); tableObj.indexes = indexNames.map((idx) => ({ constraintName: idx, @@ -92,6 +95,17 @@ class Analyser extends DatabaseAnalyser { .map(({ columnName }) => ({ columnName })), })); + const uniqueNames = _.uniq( + indexcols.rows.filter((x) => x.tableName == tableName && x.origin == 'u').map((x) => x.constraintName) + ); + + tableObj.uniques = uniqueNames.map((idx) => ({ + constraintName: idx, + columns: indexcols.rows + .filter((x) => x.tableName == tableName && x.constraintName == idx) + .map(({ columnName }) => ({ columnName })), + })); + const pkColumns = info.rows .filter((x) => x.pk) .map((col) => ({ @@ -120,8 +134,6 @@ class Analyser extends DatabaseAnalyser { }; return fk; }); - - // console.log(info); } for (const viewName of this.getRequestedObjectPureNames(