Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quoting DB name when creating/dropping #2286

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The upstream patch used $this->quoteTableName which does an escape around any . characters in addition to calling quoteColumnName where it's assuming that it's a separator between schema and table name. Given that database names don't have schema references, better I think to use quoteColumnName directly.

$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
Loading