Skip to content

Commit

Permalink
[8.x] SqlServer Grammar: Bugfixes for hasTable and dropIfExists / sup…
Browse files Browse the repository at this point in the history
…port for using schema names in these functions (#37280)

* - Bugfixes for dropIfExists and hasTable when using a schema in table name in SqlServer.
- Shorter version for compileColumnListing

* Adjusted tests

Co-authored-by: Arjen van Oostrum <[email protected]>
  • Loading branch information
arjenvanoostrum and performation-arjen authored May 6, 2021
1 parent 8f603f1 commit cf3fe08
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function compileDropDatabaseIfExists($name)
*/
public function compileTableExists()
{
return "select * from sysobjects where type = 'U' and name = ?";
return "select * from sys.sysobjects where id = object_id(?, 'U')";
}

/**
Expand All @@ -75,9 +75,7 @@ public function compileTableExists()
*/
public function compileColumnListing($table)
{
return "select col.name from sys.columns as col
join sys.objects as obj on col.object_id = obj.object_id
where obj.type = 'U' and obj.object_id = object_id('$table')";
return "select name from sys.columns where object_id = object_id('$table', 'U')";
}

/**
Expand Down Expand Up @@ -194,7 +192,7 @@ public function compileDrop(Blueprint $blueprint, Fluent $command)
*/
public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
{
return sprintf('if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = %s) drop table %s',
return sprintf('if exists (select * from sys.sysobjects where id = object_id(%s, \'U\')) drop table %s',
"'".str_replace("'", "''", $this->getTablePrefix().$blueprint->getTable())."'",
$this->wrapTable($blueprint)
);
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public function testDropTableIfExists()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = \'users\') drop table "users"', $statements[0]);
$this->assertSame('if exists (select * from sys.sysobjects where id = object_id(\'users\', \'U\')) drop table "users"', $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->dropIfExists();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()->setTablePrefix('prefix_'));

$this->assertCount(1, $statements);
$this->assertSame('if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = \'prefix_users\') drop table "prefix_users"', $statements[0]);
$this->assertSame('if exists (select * from sys.sysobjects where id = object_id(\'prefix_users\', \'U\')) drop table "prefix_users"', $statements[0]);
}

public function testDropColumn()
Expand Down

0 comments on commit cf3fe08

Please sign in to comment.