Created new service which is the database connection but without the dbname, and used in in create db command

This commit is contained in:
Alejandro Celaya 2019-08-05 18:48:33 +02:00
parent 1aba77c752
commit a575f2eced
4 changed files with 33 additions and 5 deletions

View File

@ -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,
],
],

View File

@ -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('<fg=blue>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);

View File

@ -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,

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Doctrine;
use Doctrine\DBAL\Connection;
use Psr\Container\ContainerInterface;
class NoDbNameConnectionFactory
{
public const SERVICE_NAME = 'Shlinkio\Shlink\Common\Doctrine\NoDbNameConnection';
public function __invoke(ContainerInterface $container): Connection
{
$conn = $container->get(Connection::class);
$params = $conn->getParams();
unset($params['dbname']);
return new Connection($params, $conn->getDriver(), $conn->getConfiguration(), $conn->getEventManager());
}
}