mirror of
https://github.com/knex/knex.git
synced 2025-12-30 00:30:14 +00:00
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:
parent
81d6ffad4a
commit
c4a3abc42d
@ -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 });
|
||||
}
|
||||
|
||||
|
||||
@ -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 () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user