Do not use sys.tables to find if a table exists (#2328)

Co-authored-by: Olivier Cavadenti <olivier.cavadenti@gmail.com>
This commit is contained in:
TJ (Thomas J.) Biddle 2022-02-01 21:54:46 +08:00 committed by GitHub
parent 81d6ffad4a
commit c4a3abc42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 8 deletions

View File

@ -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 });
}

View File

@ -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 () {
@ -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 () {