mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 04:03:12 +08:00
Remove AbstractDatabaseCommand
This commit is contained in:
@@ -1,33 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\CLI\Command\Db;
|
|
||||||
|
|
||||||
use Shlinkio\Shlink\CLI\Command\Util\CommandUtils;
|
|
||||||
use Shlinkio\Shlink\CLI\Command\Util\LockConfig;
|
|
||||||
use Symfony\Component\Console\Command\Command;
|
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
||||||
use Symfony\Component\Lock\LockFactory;
|
|
||||||
|
|
||||||
abstract class AbstractDatabaseCommand extends Command
|
|
||||||
{
|
|
||||||
public function __construct(private readonly LockFactory $locker)
|
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
final protected function execute(InputInterface $input, OutputInterface $output): int
|
|
||||||
{
|
|
||||||
return CommandUtils::executeWithLock(
|
|
||||||
$this->locker,
|
|
||||||
LockConfig::blocking($this->getName() ?? static::class),
|
|
||||||
new SymfonyStyle($input, $output),
|
|
||||||
fn () => $this->lockedExecute($input, $output),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract protected function lockedExecute(InputInterface $input, OutputInterface $output): int;
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,10 @@ namespace Shlinkio\Shlink\CLI\Command\Db;
|
|||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
|
use Shlinkio\Shlink\CLI\Command\Util\CommandUtils;
|
||||||
|
use Shlinkio\Shlink\CLI\Command\Util\LockConfig;
|
||||||
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
@@ -18,7 +21,7 @@ use function array_map;
|
|||||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||||
use function Shlinkio\Shlink\Core\ArrayUtils\some;
|
use function Shlinkio\Shlink\Core\ArrayUtils\some;
|
||||||
|
|
||||||
class CreateDatabaseCommand extends AbstractDatabaseCommand
|
class CreateDatabaseCommand extends Command
|
||||||
{
|
{
|
||||||
private readonly Connection $regularConn;
|
private readonly Connection $regularConn;
|
||||||
|
|
||||||
@@ -27,13 +30,13 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
|
|||||||
public const string COMMAND = 'orm:schema-tool:create';
|
public const string COMMAND = 'orm:schema-tool:create';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
LockFactory $locker,
|
private readonly LockFactory $locker,
|
||||||
private readonly ProcessRunnerInterface $processRunner,
|
private readonly ProcessRunnerInterface $processRunner,
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
private readonly Connection $noDbNameConn,
|
private readonly Connection $noDbNameConn,
|
||||||
) {
|
) {
|
||||||
$this->regularConn = $this->em->getConnection();
|
$this->regularConn = $this->em->getConnection();
|
||||||
parent::__construct($locker);
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure(): void
|
protected function configure(): void
|
||||||
@@ -46,10 +49,19 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function lockedExecute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
return CommandUtils::executeWithLock(
|
||||||
|
$this->locker,
|
||||||
|
LockConfig::blocking(self::NAME),
|
||||||
|
$io,
|
||||||
|
fn () => $this->executeCommand($io),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function executeCommand(SymfonyStyle $io): int
|
||||||
|
{
|
||||||
if ($this->databaseTablesExist()) {
|
if ($this->databaseTablesExist()) {
|
||||||
$io->success('Database already exists. Run "db:migrate" command to make sure it is up to date.');
|
$io->success('Database already exists. Run "db:migrate" command to make sure it is up to date.');
|
||||||
return self::SUCCESS;
|
return self::SUCCESS;
|
||||||
@@ -57,7 +69,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
|
|||||||
|
|
||||||
// Create database
|
// Create database
|
||||||
$io->writeln('<fg=blue>Creating database tables...</>');
|
$io->writeln('<fg=blue>Creating database tables...</>');
|
||||||
$this->processRunner->run($output, [self::SCRIPT, self::COMMAND, '--no-interaction']);
|
$this->processRunner->run($io, [self::SCRIPT, self::COMMAND, '--no-interaction']);
|
||||||
$io->success('Database properly created!');
|
$io->success('Database properly created!');
|
||||||
|
|
||||||
return self::SUCCESS;
|
return self::SUCCESS;
|
||||||
|
|||||||
@@ -4,23 +4,26 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\CLI\Command\Db;
|
namespace Shlinkio\Shlink\CLI\Command\Db;
|
||||||
|
|
||||||
|
use Shlinkio\Shlink\CLI\Command\Util\CommandUtils;
|
||||||
|
use Shlinkio\Shlink\CLI\Command\Util\LockConfig;
|
||||||
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
use Symfony\Component\Lock\LockFactory;
|
use Symfony\Component\Lock\LockFactory;
|
||||||
|
|
||||||
class MigrateDatabaseCommand extends AbstractDatabaseCommand
|
class MigrateDatabaseCommand extends Command
|
||||||
{
|
{
|
||||||
public const string NAME = 'db:migrate';
|
public const string NAME = 'db:migrate';
|
||||||
public const string SCRIPT = 'vendor/doctrine/migrations/bin/doctrine-migrations.php';
|
public const string SCRIPT = 'vendor/doctrine/migrations/bin/doctrine-migrations.php';
|
||||||
public const string COMMAND = 'migrations:migrate';
|
public const string COMMAND = 'migrations:migrate';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
LockFactory $locker,
|
private readonly LockFactory $locker,
|
||||||
private readonly ProcessRunnerInterface $processRunner,
|
private readonly ProcessRunnerInterface $processRunner,
|
||||||
) {
|
) {
|
||||||
parent::__construct($locker);
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure(): void
|
protected function configure(): void
|
||||||
@@ -31,12 +34,21 @@ class MigrateDatabaseCommand extends AbstractDatabaseCommand
|
|||||||
->setDescription('Runs database migrations, which will ensure the shlink database is up to date.');
|
->setDescription('Runs database migrations, which will ensure the shlink database is up to date.');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function lockedExecute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
return CommandUtils::executeWithLock(
|
||||||
|
$this->locker,
|
||||||
|
LockConfig::blocking(self::NAME),
|
||||||
|
$io,
|
||||||
|
fn () => $this->executeCommand($io),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function executeCommand(SymfonyStyle $io): int
|
||||||
|
{
|
||||||
$io->writeln('<fg=blue>Migrating database...</>');
|
$io->writeln('<fg=blue>Migrating database...</>');
|
||||||
$this->processRunner->run($output, [self::SCRIPT, self::COMMAND, '--no-interaction']);
|
$this->processRunner->run($io, [self::SCRIPT, self::COMMAND, '--no-interaction']);
|
||||||
$io->success('Database properly migrated!');
|
$io->success('Database properly migrated!');
|
||||||
|
|
||||||
return self::SUCCESS;
|
return self::SUCCESS;
|
||||||
|
|||||||
Reference in New Issue
Block a user