From a575f2eced04b14b4a116ce039d0a047bf06c0bc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 5 Aug 2019 18:48:33 +0200 Subject: [PATCH] Created new service which is the database connection but without the dbname, and used in in create db command --- module/CLI/config/dependencies.config.php | 2 ++ .../src/Command/Db/CreateDatabaseCommand.php | 14 ++++++++----- module/Common/config/doctrine.config.php | 1 + .../Doctrine/NoDbNameConnectionFactory.php | 21 +++++++++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 module/Common/src/Doctrine/NoDbNameConnectionFactory.php diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 5df6ea4d..69ace4b3 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\CLI; use Doctrine\DBAL\Connection; use GeoIp2\Database\Reader; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater; +use Shlinkio\Shlink\Common\Doctrine\NoDbNameConnectionFactory; use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater; use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; use Shlinkio\Shlink\Common\Service\PreviewGenerator; @@ -85,6 +86,7 @@ return [ SymfonyCli\Helper\ProcessHelper::class, PhpExecutableFinder::class, Connection::class, + NoDbNameConnectionFactory::SERVICE_NAME, ], ], diff --git a/module/CLI/src/Command/Db/CreateDatabaseCommand.php b/module/CLI/src/Command/Db/CreateDatabaseCommand.php index 2703aefb..7b316cc3 100644 --- a/module/CLI/src/Command/Db/CreateDatabaseCommand.php +++ b/module/CLI/src/Command/Db/CreateDatabaseCommand.php @@ -23,15 +23,19 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand /** @var Connection */ private $conn; + /** @var Connection */ + private $noDbNameConn; public function __construct( Locker $locker, ProcessHelper $processHelper, PhpExecutableFinder $phpFinder, - Connection $conn + Connection $conn, + Connection $noDbNameConn ) { parent::__construct($locker, $processHelper, $phpFinder); $this->conn = $conn; + $this->noDbNameConn = $noDbNameConn; } protected function configure(): void @@ -50,12 +54,12 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand $this->checkDbExists(); if ($this->schemaExists()) { - $io->success('Database already exists.'); + $io->success('Database already exists. Run "db:migrate" command to make sure it is up to date.'); return ExitCodes::EXIT_SUCCESS; } // Create database - $io->writeln('Creating database tables...'); + $io->writeln('Creating database tables...'); $this->runPhpCommand($output, [self::DOCTRINE_HELPER_SCRIPT, self::DOCTRINE_HELPER_COMMAND]); $io->success('Database properly created!'); @@ -64,9 +68,9 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand private function checkDbExists(): void { - $schemaManager = $this->conn->getSchemaManager(); - $databases = $schemaManager->listDatabases(); $shlinkDatabase = $this->conn->getDatabase(); + $schemaManager = $this->noDbNameConn->getSchemaManager(); + $databases = $schemaManager->listDatabases(); if (! contains($databases, $shlinkDatabase)) { $schemaManager->createDatabase($shlinkDatabase); diff --git a/module/Common/config/doctrine.config.php b/module/Common/config/doctrine.config.php index 547724f2..0b569043 100644 --- a/module/Common/config/doctrine.config.php +++ b/module/Common/config/doctrine.config.php @@ -20,6 +20,7 @@ return [ 'factories' => [ EntityManager::class => Doctrine\EntityManagerFactory::class, Connection::class => Doctrine\ConnectionFactory::class, + Doctrine\NoDbNameConnectionFactory::SERVICE_NAME => Doctrine\NoDbNameConnectionFactory::class, ], 'aliases' => [ 'em' => EntityManager::class, diff --git a/module/Common/src/Doctrine/NoDbNameConnectionFactory.php b/module/Common/src/Doctrine/NoDbNameConnectionFactory.php new file mode 100644 index 00000000..fdf470e0 --- /dev/null +++ b/module/Common/src/Doctrine/NoDbNameConnectionFactory.php @@ -0,0 +1,21 @@ +get(Connection::class); + $params = $conn->getParams(); + unset($params['dbname']); + + return new Connection($params, $conn->getDriver(), $conn->getConfiguration(), $conn->getEventManager()); + } +}