From f256b2cd558213bc132327bf2241f5de1cb31495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Fr=C3=B6mer?= Date: Fri, 29 Jul 2022 13:26:55 +0200 Subject: [PATCH] Fix problem with database names using hyphens This will fix an issue while trying to drop/create a database having hyphens: E.g. `event-store-db` --- src/Console/DoctrineHelper.php | 8 ++++++-- tests/Unit/Console/DoctrineHelperTest.php | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Console/DoctrineHelper.php b/src/Console/DoctrineHelper.php index 7774966a..8050af4f 100644 --- a/src/Console/DoctrineHelper.php +++ b/src/Console/DoctrineHelper.php @@ -60,11 +60,15 @@ public function hasDatabase(Connection $connection, string $databaseName): bool public function createDatabase(Connection $connection, string $databaseName): void { - $connection->createSchemaManager()->createDatabase($databaseName); + $connection->createSchemaManager()->createDatabase( + $connection->getDatabasePlatform()->quoteSingleIdentifier($databaseName) + ); } public function dropDatabase(Connection $connection, string $databaseName): void { - $connection->createSchemaManager()->dropDatabase($databaseName); + $connection->createSchemaManager()->dropDatabase( + $connection->getDatabasePlatform()->quoteSingleIdentifier($databaseName) + ); } } diff --git a/tests/Unit/Console/DoctrineHelperTest.php b/tests/Unit/Console/DoctrineHelperTest.php index 71379aae..f4faaf0d 100644 --- a/tests/Unit/Console/DoctrineHelperTest.php +++ b/tests/Unit/Console/DoctrineHelperTest.php @@ -5,6 +5,7 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Console; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; use InvalidArgumentException; use Patchlevel\EventSourcing\Console\DoctrineHelper; @@ -66,9 +67,10 @@ public function testCreateDatabase(): void $helper = new DoctrineHelper(); $schemaManager = $this->prophesize(AbstractSchemaManager::class); - $schemaManager->createDatabase('test')->shouldBeCalled(); + $schemaManager->createDatabase('`test`')->shouldBeCalled(); $connection = $this->prophesize(Connection::class); + $connection->getDatabasePlatform()->willReturn(new MySQLPlatform()); $connection->createSchemaManager()->willReturn($schemaManager); $helper->createDatabase($connection->reveal(), 'test'); @@ -79,9 +81,10 @@ public function testDropDatabase(): void $helper = new DoctrineHelper(); $schemaManager = $this->prophesize(AbstractSchemaManager::class); - $schemaManager->dropDatabase('test')->shouldBeCalled(); + $schemaManager->dropDatabase('`test`')->shouldBeCalled(); $connection = $this->prophesize(Connection::class); + $connection->getDatabasePlatform()->willReturn(new MySQLPlatform()); $connection->createSchemaManager()->willReturn($schemaManager); $helper->dropDatabase($connection->reveal(), 'test');