Skip to content

Commit

Permalink
Do not use sys.tables to find if a table exists (#2328)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Cavadenti <olivier.cavadenti@gmail.com>
  • Loading branch information
thomasbiddle and OlivierCavadenti committed Feb 1, 2022
1 parent 81d6ffa commit c4a3abc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions lib/dialects/mssql/schema/mssql-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ class SchemaCompiler_MSSQL extends SchemaCompiler {
// Check whether a table exists on the query.
hasTable(tableName) {
const formattedTable = this.client.parameter(
this.formatter.wrap(prefixedTableName(this.schema, tableName)),
prefixedTableName(this.schema, tableName),
this.builder,
this.bindingsHolder
);

const sql =
`select object_id from sys.tables ` +
`where object_id = object_id(${formattedTable})`;
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ` +
`WHERE TABLE_NAME = ${formattedTable}`;
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
}

Expand Down
8 changes: 4 additions & 4 deletions test/unit/schema-builder/mssql.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,9 @@ describe('MSSQL SchemaBuilder', function () {

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.tables where object_id = object_id(?)'
'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?'
);
expect(tableSql[0].bindings[0]).to.equal('[users]');
expect(tableSql[0].bindings[0]).to.equal('users');
});

it('test has table with schema', function () {
Expand All @@ -511,9 +511,9 @@ describe('MSSQL SchemaBuilder', function () {

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.tables where object_id = object_id(?)'
'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?'
);
expect(tableSql[0].bindings[0]).to.equal('[schema].[users]');
expect(tableSql[0].bindings[0]).to.equal('schema.users');
});

it('test rename table with schema', function () {
Expand Down

0 comments on commit c4a3abc

Please sign in to comment.