diff --git a/integration-tests/__tests__/table-analyse.spec.js b/integration-tests/__tests__/table-analyse.spec.js index 1985aef3b..5814236b2 100644 --- a/integration-tests/__tests__/table-analyse.spec.js +++ b/integration-tests/__tests__/table-analyse.spec.js @@ -2,6 +2,7 @@ const engines = require('../engines'); 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)'; const t2Sql = 'CREATE TABLE t2 (id int not null primary key, val2 varchar(50) null)'; const txMatch = (tname, vcolname, nextcol) => @@ -98,7 +99,7 @@ describe('Table analyse', () => { const structure1 = await driver.analyseFull(conn); if (engine.dbSnapshotBySeconds) await new Promise(resolve => setTimeout(resolve, 1100)); - + await driver.query(conn, 'ALTER TABLE t2 ADD nextcol varchar(50)'); const structure2 = await driver.analyseIncremental(conn, structure1); @@ -109,4 +110,16 @@ describe('Table analyse', () => { expect(structure2.tables.find(x => x.pureName == 't2')).toEqual(t2NextColMatch); }) ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Index - full analysis - %s', + testWrapper(async (conn, driver, engine) => { + await driver.query(conn, t1Sql); + await driver.query(conn, ix1Sql); + const structure = await driver.analyseFull(conn); + + const t1 = structure.tables.find(x => x.pureName == 't1'); + expect(t1.indexes.length).toEqual(1); + }) + ); }); diff --git a/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js b/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js index 2f1117d89..c60746c24 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js +++ b/plugins/dbgate-plugin-mssql/src/backend/MsSqlAnalyser.js @@ -117,6 +117,9 @@ class MsSqlAnalyser extends DatabaseAnalyser { createSql: getCreateSql(row), })); + const indexesRows = await this.driver.query(this.pool, this.createQuery('indexes', ['tables'])); + const indexcolsRows = await this.driver.query(this.pool, this.createQuery('indexesindexcols', ['tables'])); + return { tables, views, diff --git a/plugins/dbgate-plugin-mssql/src/backend/sql/index.js b/plugins/dbgate-plugin-mssql/src/backend/sql/index.js index cb40e88a5..9fdffd700 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/sql/index.js +++ b/plugins/dbgate-plugin-mssql/src/backend/sql/index.js @@ -8,6 +8,8 @@ const views = require('./views'); const programmables = require('./programmables'); const viewColumns = require('./viewColumns'); const getSchemas = require('./getSchemas'); +const indexes = require('./indexes'); +const indexcols = require('./indexcols'); module.exports = { columns, @@ -20,4 +22,6 @@ module.exports = { programmables, viewColumns, getSchemas, + indexes, + indexcols, }; diff --git a/plugins/dbgate-plugin-mssql/src/backend/sql/indexcols.js b/plugins/dbgate-plugin-mssql/src/backend/sql/indexcols.js new file mode 100644 index 000000000..e3eca23b7 --- /dev/null +++ b/plugins/dbgate-plugin-mssql/src/backend/sql/indexcols.js @@ -0,0 +1,7 @@ +module.exports = ` +select c.object_id, c.index_id, c.column_id, c.is_descending_key, c.is_included_column from sys.index_columns c + +where c.object_id =OBJECT_ID_CONDITION +order by c.key_ordinal + +`; diff --git a/plugins/dbgate-plugin-mssql/src/backend/sql/indexes.js b/plugins/dbgate-plugin-mssql/src/backend/sql/indexes.js new file mode 100644 index 000000000..0915f1d71 --- /dev/null +++ b/plugins/dbgate-plugin-mssql/src/backend/sql/indexes.js @@ -0,0 +1,14 @@ +module.exports = ` +select i.object_id as objectId, i.name as constraintName, i.type_desc, i.is_unique as isUnique,i.index_id, i.is_unique_constraint from sys.indexes i +where i.is_primary_key=0 +and i.is_hypothetical=0 and indexproperty(i.object_id, i.name, 'IsStatistics') = 0 +and objectproperty(i.object_id, 'IsUserTable') = 1 +and i.index_id between 1 and 254 + +--and i.name not in +-- (select o.name from sysobjects o +-- where o.parent_obj = i.object_id +-- and objectproperty(o.id, N'isConstraint') = 1.0) + + and i.object_id =OBJECT_ID_CONDITION +`;