Skip to content

Commit 49b3db5

Browse files
authored
Quote database names in PostgreSQL and SQL Server adapters (#1002)
* 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. * 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
1 parent 9ca3cad commit 49b3db5

10 files changed

Lines changed: 33 additions & 20 deletions

File tree

src/BaseMigration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public function table(string $tableName, array $options = []): Table
431431
/**
432432
* Create a new ForeignKey object.
433433
*
434-
* @params string|string[] $columns Columns
434+
* @param string|string[] $columns Columns
435435
* @return \Migrations\Db\Table\ForeignKey
436436
*/
437437
public function foreignKey(string|array $columns): ForeignKey
@@ -442,7 +442,7 @@ public function foreignKey(string|array $columns): ForeignKey
442442
/**
443443
* Create a new Index object.
444444
*
445-
* @params string|string[] $columns Columns
445+
* @param string|string[] $columns Columns
446446
* @return \Migrations\Db\Table\Index
447447
*/
448448
public function index(string|array $columns): Index

src/Db/Adapter/PostgresAdapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,11 @@ protected function getDropCheckConstraintInstructions(string $tableName, string
823823
public function createDatabase(string $name, array $options = []): void
824824
{
825825
$charset = $options['charset'] ?? 'utf8';
826-
$this->execute(sprintf("CREATE DATABASE %s WITH ENCODING = '%s'", $name, $charset));
826+
$this->execute(sprintf(
827+
'CREATE DATABASE %s WITH ENCODING = %s',
828+
$this->quoteSchemaName($name),
829+
$this->quoteString($charset),
830+
));
827831
}
828832

829833
/**
@@ -849,7 +853,7 @@ public function hasDatabase(string $name): bool
849853
public function dropDatabase($name): void
850854
{
851855
$this->disconnect();
852-
$this->execute(sprintf('DROP DATABASE IF EXISTS %s', $name));
856+
$this->execute(sprintf('DROP DATABASE IF EXISTS %s', $this->quoteSchemaName($name)));
853857
$this->createdTables = [];
854858
$this->connect();
855859
}

src/Db/Adapter/SqlserverAdapter.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,17 @@ protected function getDropForeignKeyByColumnsInstructions(string $tableName, arr
767767
*/
768768
public function createDatabase(string $name, array $options = []): void
769769
{
770+
$quotedName = $this->quoteSchemaName($name);
770771
if (isset($options['collation'])) {
771-
$this->execute(sprintf('CREATE DATABASE [%s] COLLATE [%s]', $name, $options['collation']));
772+
$this->execute(sprintf(
773+
'CREATE DATABASE %s COLLATE %s',
774+
$quotedName,
775+
$this->quoteSchemaName($options['collation']),
776+
));
772777
} else {
773-
$this->execute(sprintf('CREATE DATABASE [%s]', $name));
778+
$this->execute(sprintf('CREATE DATABASE %s', $quotedName));
774779
}
775-
$this->execute(sprintf('USE [%s]', $name));
780+
$this->execute(sprintf('USE %s', $quotedName));
776781
}
777782

778783
/**
@@ -794,12 +799,16 @@ public function hasDatabase(string $name): bool
794799
*/
795800
public function dropDatabase(string $name): void
796801
{
797-
$sql = <<<SQL
798-
USE master;
799-
IF EXISTS(select * from sys.databases where name=N'$name')
800-
ALTER DATABASE [$name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
801-
DROP DATABASE [$name];
802-
SQL;
802+
$quotedName = $this->quoteSchemaName($name);
803+
$sql = sprintf(
804+
'USE master;
805+
IF EXISTS(select * from sys.databases where name=%s)
806+
ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
807+
DROP DATABASE %s;',
808+
$this->quoteString($name),
809+
$quotedName,
810+
$quotedName,
811+
);
803812
$this->execute($sql);
804813
$this->createdTables = [];
805814
}

tests/test_app/config/AltSeeds/AnotherNumbersSeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Migrations\BaseSeed;
44

55
/**
6-
* NumbersSeed seed.
6+
* AnotherNumbersSeed seed.
77
*/
88
class AnotherNumbersSeed extends BaseSeed
99
{

tests/test_app/config/AltSeeds/NumbersAltSeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Migrations\BaseSeed;
44

55
/**
6-
* NumbersSeed seed.
6+
* NumbersAltSeed seed.
77
*/
88
class NumbersAltSeed extends BaseSeed
99
{

tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Migrations\BaseSeed;
44

55
/**
6-
* NumbersSeed seed.
6+
* MigrationSeedNumbers seed.
77
*/
88
class MigrationSeedNumbers extends BaseSeed
99
{

tests/test_app/config/CallSeeds/DatabaseSeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Migrations\BaseSeed;
44

55
/**
6-
* NumbersSeed seed.
6+
* DatabaseSeed seed.
77
*/
88
class DatabaseSeed extends BaseSeed
99
{

tests/test_app/config/CallSeeds/LettersSeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Migrations\BaseSeed;
44

55
/**
6-
* NumbersSeed seed.
6+
* LettersSeed seed.
77
*/
88
class LettersSeed extends BaseSeed
99
{

tests/test_app/config/CallSeeds/NumbersCallSeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Migrations\BaseSeed;
44

55
/**
6-
* NumbersSeed seed.
6+
* NumbersCallSeed seed.
77
*/
88
class NumbersCallSeed extends BaseSeed
99
{

tests/test_app/config/Seeds/StoresSeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Migrations\BaseSeed;
66

77
/**
8-
* NumbersSeed seed.
8+
* StoresSeed seed.
99
*/
1010
class StoresSeed extends BaseSeed
1111
{

0 commit comments

Comments
 (0)