From 3fa2b68d6593b4b96d4477b8918a0e53ae0dfb99 Mon Sep 17 00:00:00 2001 From: mscherer Date: Mon, 19 Jan 2026 10:29:20 +0100 Subject: [PATCH 1/2] Quote database names in PostgreSQL and SQL Server adapters MySQL adapter properly quotes database names in createDatabase/dropDatabase using quoteTableName(), but PostgreSQL and SQL Server did not. - PostgreSQL: Use quoteSchemaName() for database name and quoteString() for charset - SQL Server: Use quoteSchemaName() for database name and quoteString() for string comparisons This ensures consistent identifier quoting across all database adapters. --- src/Db/Adapter/PostgresAdapter.php | 8 ++++++-- src/Db/Adapter/SqlserverAdapter.php | 27 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Db/Adapter/PostgresAdapter.php b/src/Db/Adapter/PostgresAdapter.php index 8720b9c5..4df4e6e4 100644 --- a/src/Db/Adapter/PostgresAdapter.php +++ b/src/Db/Adapter/PostgresAdapter.php @@ -823,7 +823,11 @@ protected function getDropCheckConstraintInstructions(string $tableName, string public function createDatabase(string $name, array $options = []): void { $charset = $options['charset'] ?? 'utf8'; - $this->execute(sprintf("CREATE DATABASE %s WITH ENCODING = '%s'", $name, $charset)); + $this->execute(sprintf( + 'CREATE DATABASE %s WITH ENCODING = %s', + $this->quoteSchemaName($name), + $this->quoteString($charset), + )); } /** @@ -849,7 +853,7 @@ public function hasDatabase(string $name): bool public function dropDatabase($name): void { $this->disconnect(); - $this->execute(sprintf('DROP DATABASE IF EXISTS %s', $name)); + $this->execute(sprintf('DROP DATABASE IF EXISTS %s', $this->quoteSchemaName($name))); $this->createdTables = []; $this->connect(); } diff --git a/src/Db/Adapter/SqlserverAdapter.php b/src/Db/Adapter/SqlserverAdapter.php index 14602abc..a6b91efc 100644 --- a/src/Db/Adapter/SqlserverAdapter.php +++ b/src/Db/Adapter/SqlserverAdapter.php @@ -767,12 +767,17 @@ protected function getDropForeignKeyByColumnsInstructions(string $tableName, arr */ public function createDatabase(string $name, array $options = []): void { + $quotedName = $this->quoteSchemaName($name); if (isset($options['collation'])) { - $this->execute(sprintf('CREATE DATABASE [%s] COLLATE [%s]', $name, $options['collation'])); + $this->execute(sprintf( + 'CREATE DATABASE %s COLLATE %s', + $quotedName, + $this->quoteSchemaName($options['collation']), + )); } else { - $this->execute(sprintf('CREATE DATABASE [%s]', $name)); + $this->execute(sprintf('CREATE DATABASE %s', $quotedName)); } - $this->execute(sprintf('USE [%s]', $name)); + $this->execute(sprintf('USE %s', $quotedName)); } /** @@ -794,12 +799,16 @@ public function hasDatabase(string $name): bool */ public function dropDatabase(string $name): void { - $sql = <<quoteSchemaName($name); + $sql = sprintf( + 'USE master; +IF EXISTS(select * from sys.databases where name=%s) +ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE; +DROP DATABASE %s;', + $this->quoteString($name), + $quotedName, + $quotedName, + ); $this->execute($sql); $this->createdTables = []; } From f75c3b8e89851f8465be6e56a28a644d464abb2f Mon Sep 17 00:00:00 2001 From: mscherer Date: Mon, 19 Jan 2026 18:14:17 +0100 Subject: [PATCH 2/2] Fix docblock typos and copy-paste errors - Fix @params typo to @param in BaseMigration.php foreignKey() and index() methods - Fix copy-paste docblock errors in test seed files where "NumbersSeed seed" was used for classes with different names --- src/BaseMigration.php | 4 ++-- tests/test_app/config/AltSeeds/AnotherNumbersSeed.php | 2 +- tests/test_app/config/AltSeeds/NumbersAltSeed.php | 2 +- tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php | 2 +- tests/test_app/config/CallSeeds/DatabaseSeed.php | 2 +- tests/test_app/config/CallSeeds/LettersSeed.php | 2 +- tests/test_app/config/CallSeeds/NumbersCallSeed.php | 2 +- tests/test_app/config/Seeds/StoresSeed.php | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/BaseMigration.php b/src/BaseMigration.php index b5df6c62..2969933a 100644 --- a/src/BaseMigration.php +++ b/src/BaseMigration.php @@ -431,7 +431,7 @@ public function table(string $tableName, array $options = []): Table /** * Create a new ForeignKey object. * - * @params string|string[] $columns Columns + * @param string|string[] $columns Columns * @return \Migrations\Db\Table\ForeignKey */ public function foreignKey(string|array $columns): ForeignKey @@ -442,7 +442,7 @@ public function foreignKey(string|array $columns): ForeignKey /** * Create a new Index object. * - * @params string|string[] $columns Columns + * @param string|string[] $columns Columns * @return \Migrations\Db\Table\Index */ public function index(string|array $columns): Index diff --git a/tests/test_app/config/AltSeeds/AnotherNumbersSeed.php b/tests/test_app/config/AltSeeds/AnotherNumbersSeed.php index f72c0a34..efa478b6 100644 --- a/tests/test_app/config/AltSeeds/AnotherNumbersSeed.php +++ b/tests/test_app/config/AltSeeds/AnotherNumbersSeed.php @@ -3,7 +3,7 @@ use Migrations\BaseSeed; /** - * NumbersSeed seed. + * AnotherNumbersSeed seed. */ class AnotherNumbersSeed extends BaseSeed { diff --git a/tests/test_app/config/AltSeeds/NumbersAltSeed.php b/tests/test_app/config/AltSeeds/NumbersAltSeed.php index 4c9e3c6d..1455134e 100644 --- a/tests/test_app/config/AltSeeds/NumbersAltSeed.php +++ b/tests/test_app/config/AltSeeds/NumbersAltSeed.php @@ -3,7 +3,7 @@ use Migrations\BaseSeed; /** - * NumbersSeed seed. + * NumbersAltSeed seed. */ class NumbersAltSeed extends BaseSeed { diff --git a/tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php b/tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php index d12df269..5558fb59 100644 --- a/tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php +++ b/tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php @@ -3,7 +3,7 @@ use Migrations\BaseSeed; /** - * NumbersSeed seed. + * MigrationSeedNumbers seed. */ class MigrationSeedNumbers extends BaseSeed { diff --git a/tests/test_app/config/CallSeeds/DatabaseSeed.php b/tests/test_app/config/CallSeeds/DatabaseSeed.php index 90954c90..e8e69f01 100644 --- a/tests/test_app/config/CallSeeds/DatabaseSeed.php +++ b/tests/test_app/config/CallSeeds/DatabaseSeed.php @@ -3,7 +3,7 @@ use Migrations\BaseSeed; /** - * NumbersSeed seed. + * DatabaseSeed seed. */ class DatabaseSeed extends BaseSeed { diff --git a/tests/test_app/config/CallSeeds/LettersSeed.php b/tests/test_app/config/CallSeeds/LettersSeed.php index 300d6688..a2591b6f 100644 --- a/tests/test_app/config/CallSeeds/LettersSeed.php +++ b/tests/test_app/config/CallSeeds/LettersSeed.php @@ -3,7 +3,7 @@ use Migrations\BaseSeed; /** - * NumbersSeed seed. + * LettersSeed seed. */ class LettersSeed extends BaseSeed { diff --git a/tests/test_app/config/CallSeeds/NumbersCallSeed.php b/tests/test_app/config/CallSeeds/NumbersCallSeed.php index a6843abb..24f56bde 100644 --- a/tests/test_app/config/CallSeeds/NumbersCallSeed.php +++ b/tests/test_app/config/CallSeeds/NumbersCallSeed.php @@ -3,7 +3,7 @@ use Migrations\BaseSeed; /** - * NumbersSeed seed. + * NumbersCallSeed seed. */ class NumbersCallSeed extends BaseSeed { diff --git a/tests/test_app/config/Seeds/StoresSeed.php b/tests/test_app/config/Seeds/StoresSeed.php index 961bd42e..e9ac5175 100644 --- a/tests/test_app/config/Seeds/StoresSeed.php +++ b/tests/test_app/config/Seeds/StoresSeed.php @@ -5,7 +5,7 @@ use Migrations\BaseSeed; /** - * NumbersSeed seed. + * StoresSeed seed. */ class StoresSeed extends BaseSeed {