Remove AbstractDatabaseCommand

This commit is contained in:
Alejandro Celaya
2025-12-16 09:11:53 +01:00
parent 83e373e96a
commit 49daf9fbb6
3 changed files with 34 additions and 43 deletions

View File

@@ -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;
}

View File

@@ -7,7 +7,10 @@ namespace Shlinkio\Shlink\CLI\Command\Db;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
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 Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
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\some;
class CreateDatabaseCommand extends AbstractDatabaseCommand
class CreateDatabaseCommand extends Command
{
private readonly Connection $regularConn;
@@ -27,13 +30,13 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
public const string COMMAND = 'orm:schema-tool:create';
public function __construct(
LockFactory $locker,
private readonly LockFactory $locker,
private readonly ProcessRunnerInterface $processRunner,
private readonly EntityManagerInterface $em,
private readonly Connection $noDbNameConn,
) {
$this->regularConn = $this->em->getConnection();
parent::__construct($locker);
parent::__construct();
}
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);
return CommandUtils::executeWithLock(
$this->locker,
LockConfig::blocking(self::NAME),
$io,
fn () => $this->executeCommand($io),
);
}
private function executeCommand(SymfonyStyle $io): int
{
if ($this->databaseTablesExist()) {
$io->success('Database already exists. Run "db:migrate" command to make sure it is up to date.');
return self::SUCCESS;
@@ -57,7 +69,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
// Create database
$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!');
return self::SUCCESS;

View File

@@ -4,23 +4,26 @@ 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 Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
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;
class MigrateDatabaseCommand extends AbstractDatabaseCommand
class MigrateDatabaseCommand extends Command
{
public const string NAME = 'db:migrate';
public const string SCRIPT = 'vendor/doctrine/migrations/bin/doctrine-migrations.php';
public const string COMMAND = 'migrations:migrate';
public function __construct(
LockFactory $locker,
private readonly LockFactory $locker,
private readonly ProcessRunnerInterface $processRunner,
) {
parent::__construct($locker);
parent::__construct();
}
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.');
}
protected function lockedExecute(InputInterface $input, OutputInterface $output): int
protected function execute(InputInterface $input, OutputInterface $output): int
{
$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...</>');
$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!');
return self::SUCCESS;