mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-27 20:11:34 +03:00
Created command to run migrations with a lock
This commit is contained in:
parent
1341d4fe57
commit
c04f0af56f
7 changed files with 54 additions and 6 deletions
|
@ -270,6 +270,7 @@ Available commands:
|
||||||
config:generate-secret [DEPRECATED] Generates a random secret string that can be used for JWT token encryption
|
config:generate-secret [DEPRECATED] Generates a random secret string that can be used for JWT token encryption
|
||||||
db
|
db
|
||||||
db:create Creates the database needed for shlink to work. It will do nothing if the database already exists
|
db:create Creates the database needed for shlink to work. It will do nothing if the database already exists
|
||||||
|
db:migrate Runs database migrations, which will ensure the shlink database is up to date.
|
||||||
short-url
|
short-url
|
||||||
short-url:delete [short-code:delete] Deletes a short URL
|
short-url:delete [short-code:delete] Deletes a short URL
|
||||||
short-url:generate [shortcode:generate|short-code:generate] Generates a short URL for provided long URL and returns it
|
short-url:generate [shortcode:generate|short-code:generate] Generates a short URL for provided long URL and returns it
|
||||||
|
|
|
@ -40,9 +40,9 @@ return [
|
||||||
'db_create_schema' => [
|
'db_create_schema' => [
|
||||||
'command' => 'bin/cli db:create',
|
'command' => 'bin/cli db:create',
|
||||||
],
|
],
|
||||||
// 'db_migrate' => [
|
'db_migrate' => [
|
||||||
// 'command' => 'bin/cli db:migrate',
|
'command' => 'bin/cli db:migrate',
|
||||||
// ],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
|
@ -30,6 +30,7 @@ return [
|
||||||
Command\Tag\DeleteTagsCommand::NAME => Command\Tag\DeleteTagsCommand::class,
|
Command\Tag\DeleteTagsCommand::NAME => Command\Tag\DeleteTagsCommand::class,
|
||||||
|
|
||||||
Command\Db\CreateDatabaseCommand::NAME => Command\Db\CreateDatabaseCommand::class,
|
Command\Db\CreateDatabaseCommand::NAME => Command\Db\CreateDatabaseCommand::class,
|
||||||
|
Command\Db\MigrateDatabaseCommand::NAME => Command\Db\MigrateDatabaseCommand::class,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,7 @@ return [
|
||||||
Command\Tag\DeleteTagsCommand::class => ConfigAbstractFactory::class,
|
Command\Tag\DeleteTagsCommand::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
Command\Db\CreateDatabaseCommand::class => ConfigAbstractFactory::class,
|
Command\Db\CreateDatabaseCommand::class => ConfigAbstractFactory::class,
|
||||||
|
Command\Db\MigrateDatabaseCommand::class => ConfigAbstractFactory::class,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
@ -88,6 +89,11 @@ return [
|
||||||
Connection::class,
|
Connection::class,
|
||||||
NoDbNameConnectionFactory::SERVICE_NAME,
|
NoDbNameConnectionFactory::SERVICE_NAME,
|
||||||
],
|
],
|
||||||
|
Command\Db\MigrateDatabaseCommand::class => [
|
||||||
|
Locker::class,
|
||||||
|
SymfonyCli\Helper\ProcessHelper::class,
|
||||||
|
PhpExecutableFinder::class,
|
||||||
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
|
@ -28,6 +28,6 @@ abstract class AbstractDatabaseCommand extends AbstractLockedCommand
|
||||||
protected function runPhpCommand(OutputInterface $output, array $command): void
|
protected function runPhpCommand(OutputInterface $output, array $command): void
|
||||||
{
|
{
|
||||||
array_unshift($command, $this->phpBinary);
|
array_unshift($command, $this->phpBinary);
|
||||||
$this->processHelper->run($output, $command);
|
$this->processHelper->run($output, $command, null, null, $output->getVerbosity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
40
module/CLI/src/Command/Db/MigrateDatabaseCommand.php
Normal file
40
module/CLI/src/Command/Db/MigrateDatabaseCommand.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\CLI\Command\Db;
|
||||||
|
|
||||||
|
use Shlinkio\Shlink\CLI\Command\Util\LockedCommandConfig;
|
||||||
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
|
class MigrateDatabaseCommand extends AbstractDatabaseCommand
|
||||||
|
{
|
||||||
|
public const NAME = 'db:migrate';
|
||||||
|
public const DOCTRINE_HELPER_SCRIPT = 'vendor/doctrine/migrations/bin/doctrine-migrations.php';
|
||||||
|
public const DOCTRINE_HELPER_COMMAND = 'migrations:migrate';
|
||||||
|
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setName(self::NAME)
|
||||||
|
->setDescription('Runs database migrations, which will ensure the shlink database is up to date.');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function lockedExecute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
|
||||||
|
$io->writeln('<fg=blue>Migrating database...</>');
|
||||||
|
$this->runPhpCommand($output, [self::DOCTRINE_HELPER_SCRIPT, self::DOCTRINE_HELPER_COMMAND]);
|
||||||
|
$io->success('Database properly migrated!');
|
||||||
|
|
||||||
|
return ExitCodes::EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getLockConfig(): LockedCommandConfig
|
||||||
|
{
|
||||||
|
return new LockedCommandConfig($this->getName(), true);
|
||||||
|
}
|
||||||
|
}
|
|
@ -107,7 +107,7 @@ class CreateDatabaseCommandTest extends TestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tablesAreCreatedIfDatabaseIsEMpty(): void
|
public function tablesAreCreatedIfDatabaseIsEmpty(): void
|
||||||
{
|
{
|
||||||
$shlinkDatabase = 'shlink_database';
|
$shlinkDatabase = 'shlink_database';
|
||||||
$getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase);
|
$getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase);
|
||||||
|
@ -119,7 +119,7 @@ class CreateDatabaseCommandTest extends TestCase
|
||||||
'/usr/local/bin/php',
|
'/usr/local/bin/php',
|
||||||
CreateDatabaseCommand::DOCTRINE_HELPER_SCRIPT,
|
CreateDatabaseCommand::DOCTRINE_HELPER_SCRIPT,
|
||||||
CreateDatabaseCommand::DOCTRINE_HELPER_COMMAND,
|
CreateDatabaseCommand::DOCTRINE_HELPER_COMMAND,
|
||||||
]);
|
], Argument::cetera());
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
|
Loading…
Add table
Reference in a new issue