diff --git a/module/CLI/src/Command/Db/CreateDatabaseCommand.php b/module/CLI/src/Command/Db/CreateDatabaseCommand.php index 7b316cc3..c507a601 100644 --- a/module/CLI/src/Command/Db/CreateDatabaseCommand.php +++ b/module/CLI/src/Command/Db/CreateDatabaseCommand.php @@ -68,9 +68,11 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand private function checkDbExists(): void { - $shlinkDatabase = $this->conn->getDatabase(); + // In order to create the new database, we have to use a connection where the dbname was not set. + // Otherwise, it will fail to connect and will not be able to create the new database $schemaManager = $this->noDbNameConn->getSchemaManager(); $databases = $schemaManager->listDatabases(); + $shlinkDatabase = $this->conn->getDatabase(); if (! contains($databases, $shlinkDatabase)) { $schemaManager->createDatabase($shlinkDatabase); diff --git a/module/Common/test/Doctrine/NoDbNameConnectionFactoryTest.php b/module/Common/test/Doctrine/NoDbNameConnectionFactoryTest.php new file mode 100644 index 00000000..d8f58f8e --- /dev/null +++ b/module/Common/test/Doctrine/NoDbNameConnectionFactoryTest.php @@ -0,0 +1,56 @@ +container = $this->prophesize(ContainerInterface::class); + $this->originalConn = $this->prophesize(Connection::class); + $this->container->get(Connection::class)->willReturn($this->originalConn->reveal()); + + $this->factory = new NoDbNameConnectionFactory(); + } + + /** @test */ + public function createsNewConnectionRemovingDbNameFromOriginalConnectionParams(): void + { + $params = [ + 'username' => 'foo', + 'password' => 'bar', + 'dbname' => 'something', + ]; + $getOriginalParams = $this->originalConn->getParams()->willReturn($params); + $getOriginalDriver = $this->originalConn->getDriver()->willReturn($this->prophesize(Driver::class)->reveal()); + $getOriginalConfig = $this->originalConn->getConfiguration()->willReturn(null); + $getOriginalEvents = $this->originalConn->getEventManager()->willReturn(null); + + $conn = ($this->factory)($this->container->reveal()); + + $this->assertEquals([ + 'username' => 'foo', + 'password' => 'bar', + ], $conn->getParams()); + $getOriginalParams->shouldHaveBeenCalledOnce(); + $getOriginalDriver->shouldHaveBeenCalledOnce(); + $getOriginalConfig->shouldHaveBeenCalledOnce(); + $getOriginalEvents->shouldHaveBeenCalledOnce(); + $this->container->get(Connection::class)->shouldHaveBeenCalledOnce(); + } +}