Skip to content

Commit

Permalink
Fix escaping database name for SqlServerAdapter::dropDatabase (cakeph…
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin authored Jun 5, 2024
1 parent b1e128c commit ef38e41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function quoteTableName(string $tableName): string
*/
public function quoteColumnName(string $columnName): string
{
return '[' . str_replace(']', '\]', $columnName) . ']';
return '[' . str_replace(']', ']]', $columnName) . ']';
}

/**
Expand Down Expand Up @@ -1176,7 +1176,7 @@ public function createDatabase(string $name, array $options = []): void
{
$databaseName = $this->quoteColumnName($name);
if (isset($options['collation'])) {
$this->execute(sprintf('CREATE DATABASE %s COLLATE [%s]', $databaseName, $options['collation']));
$this->execute(sprintf('CREATE DATABASE %s COLLATE %s', $databaseName, $options['collation']));
} else {
$this->execute(sprintf('CREATE DATABASE %s', $databaseName));
}
Expand Down Expand Up @@ -1204,12 +1204,13 @@ public function hasDatabase(string $name): bool
*/
public function dropDatabase(string $name): void
{
$sql = <<<SQL
USE master;
IF EXISTS(select * from sys.databases where name=N'$name')
ALTER DATABASE [$name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [$name];
SQL;
$sql = sprintf(
'USE master;
IF EXISTS(select * from sys.databases where name=N\'' . $name . '\')
ALTER DATABASE %1$s SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE %1$s;',
$this->quoteColumnName($name),
);
$this->execute($sql);
$this->createdTables = [];
}
Expand Down
15 changes: 13 additions & 2 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,20 @@ public function testQuoteTableName()
$this->assertEquals('[test_table]', $this->adapter->quoteTableName('test_table'));
}

public function testQuoteColumnName()
public function columnNameDataProvider(): array
{
$this->assertEquals('[test_column]', $this->adapter->quoteColumnName('test_column'));
return [
['test_column', '[test_column]'],
['test_col[u]mn', '[test_col[u]]mn]'],
];
}

/**
* @dataProvider columnNameDataProvider
*/
public function testQuoteColumnName(string $columnName, string $expected)
{
$this->assertEquals($expected, $this->adapter->quoteColumnName($columnName));
}

public function testCreateTable()
Expand Down

0 comments on commit ef38e41

Please sign in to comment.