From c49452b92078f53fbea72e3e7c911a2570bfc204 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 4 Jun 2024 16:11:34 -0400 Subject: [PATCH 1/4] Fix escaping database name for SqlServerAdapter::dropDatabase Signed-off-by: Matthew Peveler --- src/Phinx/Db/Adapter/SqlServerAdapter.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index b2e268b37..88cc35371 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -1204,12 +1204,13 @@ public function hasDatabase(string $name): bool */ public function dropDatabase(string $name): void { - $sql = <<quoteColumnName($name), + ); $this->execute($sql); $this->createdTables = []; } From 90414ae30b9f5171c5f6406b8d9a7d8082a0d340 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 4 Jun 2024 22:32:16 -0400 Subject: [PATCH 2/4] tweaks --- src/Phinx/Db/Adapter/SqlServerAdapter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index 88cc35371..5904c9a06 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -211,7 +211,7 @@ public function quoteTableName(string $tableName): string */ public function quoteColumnName(string $columnName): string { - return '[' . str_replace(']', '\]', $columnName) . ']'; + return '[' . str_replace(']', ']]', $columnName) . ']'; } /** @@ -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)); } @@ -1206,7 +1206,7 @@ public function dropDatabase(string $name): void { $sql = sprintf( 'USE master; - IF EXISTS(select * from sys.databases where name=N\'$name\') + 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), From 93f4f99c7259e02ecb6dbfda88abd1eb63ae96c0 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 4 Jun 2024 22:36:05 -0400 Subject: [PATCH 3/4] add testcase --- tests/Phinx/Db/Adapter/SqlServerAdapterTest.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index ac3c6f74f..8a07bd85f 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -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() From 8dd0ce0486a82ce1d57754986b8c3e346f4ed3b6 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 4 Jun 2024 22:37:05 -0400 Subject: [PATCH 4/4] style --- src/Phinx/Db/Adapter/SqlServerAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index 5904c9a06..cd9e104ee 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -1206,7 +1206,7 @@ public function dropDatabase(string $name): void { $sql = sprintf( 'USE master; - IF EXISTS(select * from sys.databases where name=N\'' . $name .'\') + 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),