Skip to content

Commit

Permalink
Merge pull request #2286 from cakephp/fix-database-escaped-characters
Browse files Browse the repository at this point in the history
Fix quoting DB name when creating/dropping
  • Loading branch information
dereuromark authored Jun 4, 2024
2 parents a31003d + 606797e commit b1e128c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1301,13 +1301,13 @@ public function createDatabase(string $name, array $options = []): void

if (isset($options['collation'])) {
$this->execute(sprintf(
'CREATE DATABASE `%s` DEFAULT CHARACTER SET `%s` COLLATE `%s`',
$name,
'CREATE DATABASE %s DEFAULT CHARACTER SET `%s` COLLATE `%s`',
$this->quoteColumnName($name),
$charset,
$options['collation']
));
} else {
$this->execute(sprintf('CREATE DATABASE `%s` DEFAULT CHARACTER SET `%s`', $name, $charset));
$this->execute(sprintf('CREATE DATABASE %s DEFAULT CHARACTER SET `%s`', $this->quoteColumnName($name), $charset));
}
}

Expand Down Expand Up @@ -1337,7 +1337,7 @@ public function hasDatabase(string $name): bool
*/
public function dropDatabase(string $name): void
{
$this->execute(sprintf('DROP DATABASE IF EXISTS `%s`', $name));
$this->execute(sprintf('DROP DATABASE IF EXISTS %s', $this->quoteColumnName($name)));
$this->createdTables = [];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ public function getPhinxType(string $sqlType): 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->quoteColumnName($name), $charset));
}

/**
Expand All @@ -1198,7 +1198,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->quoteColumnName($name)));
$this->createdTables = [];
$this->connect();
}
Expand Down
7 changes: 4 additions & 3 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1174,12 +1174,13 @@ public function getPhinxType(string $sqlType): string
*/
public function createDatabase(string $name, array $options = []): void
{
$databaseName = $this->quoteColumnName($name);
if (isset($options['collation'])) {
$this->execute(sprintf('CREATE DATABASE [%s] COLLATE [%s]', $name, $options['collation']));
$this->execute(sprintf('CREATE DATABASE %s COLLATE [%s]', $databaseName, $options['collation']));
} else {
$this->execute(sprintf('CREATE DATABASE [%s]', $name));
$this->execute(sprintf('CREATE DATABASE %s', $databaseName));
}
$this->execute(sprintf('USE [%s]', $name));
$this->execute(sprintf('USE %s', $databaseName));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,24 @@ public function testDropDatabase()
$this->adapter->dropDatabase('phinx_temp_database');
}

public function testDatabaseNameWithEscapedCharacter()
{
$databaseName = MYSQL_DB_CONFIG['name'] . '-`test`';
$this->adapter->dropDatabase($databaseName);
$this->adapter->createDatabase($databaseName);
$this->assertTrue($this->adapter->hasDatabase($databaseName));
$this->adapter->dropDatabase($databaseName);
}

public function testDatabaseNameWithEscapedCharacterWithCollation()
{
$databaseName = MYSQL_DB_CONFIG['name'] . '-`test`';
$this->adapter->dropDatabase($databaseName);
$this->adapter->createDatabase($databaseName, ['charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci']);
$this->assertTrue($this->adapter->hasDatabase($databaseName));
$this->adapter->dropDatabase($databaseName);
}

public function testAddColumnWithComment()
{
$table = new Table('table1', [], $this->adapter);
Expand Down
9 changes: 9 additions & 0 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,15 @@ public function testDropDatabase()
$this->adapter->dropDatabase('phinx_temp_database');
}

public function testDatabaseNameWithEscapedCharacter()
{
$databaseName = PGSQL_DB_CONFIG['name'] . '-test';
$this->adapter->dropDatabase($databaseName);
$this->adapter->createDatabase($databaseName);
$this->assertTrue($this->adapter->hasDatabase($databaseName));
$this->adapter->dropDatabase($databaseName);
}

public function testCreateSchema()
{
$this->adapter->createSchema('foo');
Expand Down
18 changes: 18 additions & 0 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,24 @@ public function testHasNamedForeignKey()
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), [], 'my_constraint2'));
}

public function testDatabaseNameWithEscapedCharacter()
{
$databaseName = SQLSRV_DB_CONFIG['name'] . '-[test]';
$this->adapter->dropDatabase($databaseName);
$this->adapter->createDatabase($databaseName);
$this->assertTrue($this->adapter->hasDatabase($databaseName));
$this->adapter->dropDatabase($databaseName);
}

public function testDatabaseNameWithEscapedCharacterWithCollation()
{
$databaseName = SQLSRV_DB_CONFIG['name'] . '-[test]';
$this->adapter->dropDatabase($databaseName);
$this->adapter->createDatabase($databaseName, ['collation' => 'SQL_Latin1_General_CP1_CS_AS']);
$this->assertTrue($this->adapter->hasDatabase($databaseName));
$this->adapter->dropDatabase($databaseName);
}

public function testHasDatabase()
{
$this->assertFalse($this->adapter->hasDatabase('fake_database_name'));
Expand Down

0 comments on commit b1e128c

Please sign in to comment.