Updated CreateDatabaseCommand to create the empty database if it does not exist

This commit is contained in:
Alejandro Celaya 2019-08-05 10:08:59 +02:00
parent 3916b06e7c
commit f78fa58cf1
7 changed files with 132 additions and 22 deletions

View File

@ -36,4 +36,13 @@ return [
], ],
], ],
'installation_commands' => [
'db_create_schema' => [
'command' => 'bin/cli db:create',
],
// 'db_migrate' => [
// 'command' => 'bin/cli db:migrate',
// ],
],
]; ];

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI; namespace Shlinkio\Shlink\CLI;
use Doctrine\DBAL\Connection;
use GeoIp2\Database\Reader; use GeoIp2\Database\Reader;
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater;
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater; use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater;
@ -83,6 +84,7 @@ return [
Locker::class, Locker::class,
SymfonyCli\Helper\ProcessHelper::class, SymfonyCli\Helper\ProcessHelper::class,
PhpExecutableFinder::class, PhpExecutableFinder::class,
Connection::class,
], ],
], ],

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Db;
use Shlinkio\Shlink\CLI\Command\Util\AbstractLockedCommand;
use Symfony\Component\Console\Helper\ProcessHelper;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Lock\Factory as Locker;
use Symfony\Component\Process\PhpExecutableFinder;
use function array_unshift;
abstract class AbstractDatabaseCommand extends AbstractLockedCommand
{
/** @var ProcessHelper */
private $processHelper;
/** @var string */
private $phpBinary;
public function __construct(Locker $locker, ProcessHelper $processHelper, PhpExecutableFinder $phpFinder)
{
parent::__construct($locker);
$this->processHelper = $processHelper;
$this->phpBinary = $phpFinder->find(false) ?: 'php';
}
protected function runPhpCommand(OutputInterface $output, array $command): void
{
array_unshift($command, $this->phpBinary);
$this->processHelper->run($output, $command);
}
}

View File

@ -3,7 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Db; namespace Shlinkio\Shlink\CLI\Command\Db;
use Shlinkio\Shlink\CLI\Command\Util\AbstractLockedCommand; use Doctrine\DBAL\Connection;
use Shlinkio\Shlink\CLI\Command\Util\LockedCommandConfig; use Shlinkio\Shlink\CLI\Command\Util\LockedCommandConfig;
use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Symfony\Component\Console\Helper\ProcessHelper; use Symfony\Component\Console\Helper\ProcessHelper;
@ -13,22 +13,25 @@ use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Lock\Factory as Locker; use Symfony\Component\Lock\Factory as Locker;
use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\PhpExecutableFinder;
class CreateDatabaseCommand extends AbstractLockedCommand use function Functional\contains;
class CreateDatabaseCommand extends AbstractDatabaseCommand
{ {
public const NAME = 'db:create'; public const NAME = 'db:create';
private const DOCTRINE_HELPER_SCRIPT = 'vendor/doctrine/orm/bin/doctrine.php'; private const DOCTRINE_HELPER_SCRIPT = 'vendor/doctrine/orm/bin/doctrine.php';
private const DOCTRINE_HELPER_COMMAND = 'orm:schema-tool:create'; private const DOCTRINE_HELPER_COMMAND = 'orm:schema-tool:create';
/** @var ProcessHelper */ /** @var Connection */
private $processHelper; private $conn;
/** @var string */
private $phpBinary;
public function __construct(Locker $locker, ProcessHelper $processHelper, PhpExecutableFinder $phpFinder) public function __construct(
{ Locker $locker,
parent::__construct($locker); ProcessHelper $processHelper,
$this->processHelper = $processHelper; PhpExecutableFinder $phpFinder,
$this->phpBinary = $phpFinder->find(false) ?: 'php'; Connection $conn
) {
parent::__construct($locker, $processHelper, $phpFinder);
$this->conn = $conn;
} }
protected function configure(): void protected function configure(): void
@ -44,34 +47,34 @@ class CreateDatabaseCommand extends AbstractLockedCommand
{ {
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
if ($this->dbExistsAndIsPopulated()) { $this->checkDbExists();
if ($this->schemaExists()) {
$io->success('Database already exists.'); $io->success('Database already exists.');
return ExitCodes::EXIT_SUCCESS; return ExitCodes::EXIT_SUCCESS;
} }
if (! $this->schemaExists()) {
// TODO Create empty database
}
// Create database // Create database
$io->writeln('Creating database tables...'); $io->writeln('Creating database tables...');
$command = [$this->phpBinary, self::DOCTRINE_HELPER_SCRIPT, self::DOCTRINE_HELPER_COMMAND]; $this->runPhpCommand($output, [self::DOCTRINE_HELPER_SCRIPT, self::DOCTRINE_HELPER_COMMAND]);
$this->processHelper->run($output, $command);
$io->success('Database properly created!'); $io->success('Database properly created!');
return ExitCodes::EXIT_SUCCESS; return ExitCodes::EXIT_SUCCESS;
} }
private function dbExistsAndIsPopulated(): bool private function checkDbExists(): void
{ {
// TODO Implement $schemaManager = $this->conn->getSchemaManager();
return false; $databases = $schemaManager->listDatabases();
if (! contains($databases, '')) {
$schemaManager->createDatabase($this->conn->getDatabase());
}
} }
private function schemaExists(): bool private function schemaExists(): bool
{ {
// TODO Implement // TODO Implement
return true; return false;
} }
protected function getLockConfig(): LockedCommandConfig protected function getLockConfig(): LockedCommandConfig

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common; namespace Shlinkio\Shlink\Common;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
return [ return [
@ -18,6 +19,7 @@ return [
'dependencies' => [ 'dependencies' => [
'factories' => [ 'factories' => [
EntityManager::class => Doctrine\EntityManagerFactory::class, EntityManager::class => Doctrine\EntityManagerFactory::class,
Connection::class => Doctrine\ConnectionFactory::class,
], ],
'aliases' => [ 'aliases' => [
'em' => EntityManager::class, 'em' => EntityManager::class,

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Doctrine;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;
class ConnectionFactory
{
public function __invoke(ContainerInterface $container): Connection
{
$em = $container->get(EntityManager::class);
return $em->getConnection();
}
}

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Doctrine;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Doctrine\ConnectionFactory;
class ConnectionFactoryTest extends TestCase
{
/** @var ConnectionFactory */
private $factory;
/** @var ObjectProphecy */
private $container;
/** @var ObjectProphecy */
private $em;
public function setUp(): void
{
$this->container = $this->prophesize(ContainerInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->container->get(EntityManager::class)->willReturn($this->em->reveal());
$this->factory = new ConnectionFactory();
}
/** @test */
public function properServiceFallbackOccursWhenInvoked(): void
{
$connection = $this->prophesize(Connection::class)->reveal();
$getConnection = $this->em->getConnection()->willReturn($connection);
$result = ($this->factory)($this->container->reveal());
$this->assertSame($connection, $result);
$getConnection->shouldHaveBeenCalledOnce();
$this->container->get(EntityManager::class)->shouldHaveBeenCalledOnce();
}
}