Skip to content

Commit

Permalink
Fix wrapping and escaping in SQL Server dropIfExists() (#16279)
Browse files Browse the repository at this point in the history
* Support table prefix
* Escape single quotes
  • Loading branch information
vlakoff authored and taylorotwell committed Nov 5, 2016
1 parent 384128c commit adf7abc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public function compileDrop(Blueprint $blueprint, Fluent $command)
*/
public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
{
return 'if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = \''.$blueprint->getTable().'\') drop table ['.$blueprint->getTable().']';
$table = "'".str_replace("'", "''", $this->getTablePrefix().$blueprint->getTable())."'";

return 'if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = '.$table.') drop table '.$this->wrapTable($blueprint);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public function testBasicCreateTable()

$this->assertEquals(1, count($statements));
$this->assertEquals('alter table "users" add "id" int identity primary key not null, "email" nvarchar(255) not null', $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->increments('id');
$blueprint->string('email');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()->setTablePrefix('prefix_'));

$this->assertEquals(1, count($statements));
$this->assertEquals('create table "prefix_users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
}

public function testDropTable()
Expand All @@ -38,6 +47,30 @@ public function testDropTable()

$this->assertEquals(1, count($statements));
$this->assertEquals('drop table "users"', $statements[0]);

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

$this->assertEquals(1, count($statements));
$this->assertEquals('drop table "prefix_users"', $statements[0]);
}

public function testDropTableIfExists()
{
$blueprint = new Blueprint('users');
$blueprint->dropIfExists();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertEquals(1, count($statements));
$this->assertEquals('if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = \'users\') drop table "users"', $statements[0]);

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

$this->assertEquals(1, count($statements));
$this->assertEquals('if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = \'prefix_users\') drop table "prefix_users"', $statements[0]);
}

public function testDropColumn()
Expand Down

0 comments on commit adf7abc

Please sign in to comment.