|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Netsells\LaravelMutexMigrations; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Events\Dispatcher; |
| 6 | +use Illuminate\Database\Console\Migrations\MigrateCommand; |
| 7 | +use Illuminate\Database\Migrations\Migrator; |
| 8 | +use Netsells\LaravelMutexMigrations\Processors\MigrationProcessorFactory; |
| 9 | +use Netsells\LaravelMutexMigrations\Processors\MigrationProcessorInterface; |
| 10 | +use Symfony\Component\Console\Command\SignalableCommandInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | + |
| 13 | +class MigrateCommandExtension extends MigrateCommand implements SignalableCommandInterface |
| 14 | +{ |
| 15 | + private const OPTION_DOWN = 'down'; |
| 16 | + |
| 17 | + private const OPTION_MUTEX = 'mutex'; |
| 18 | + |
| 19 | + private MigrationProcessorInterface $processor; |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + Migrator $migrator, |
| 23 | + Dispatcher $dispatcher, |
| 24 | + private readonly MigrationProcessorFactory $factory |
| 25 | + ) { |
| 26 | + $this->extendSignature(); |
| 27 | + |
| 28 | + parent::__construct($migrator, $dispatcher); |
| 29 | + } |
| 30 | + |
| 31 | + public function handle() |
| 32 | + { |
| 33 | + $this->processor = $this->createProcessor(); |
| 34 | + |
| 35 | + try { |
| 36 | + $this->processor->start(); |
| 37 | + |
| 38 | + return parent::handle(); |
| 39 | + } catch (\Throwable $th) { |
| 40 | + $this->components->error($th->getMessage()); |
| 41 | + |
| 42 | + return self::FAILURE; |
| 43 | + } finally { |
| 44 | + $this->processor->terminate(isset($th)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private function createProcessor(): MigrationProcessorInterface |
| 49 | + { |
| 50 | + return $this->factory->create( |
| 51 | + $this->option(self::OPTION_MUTEX), |
| 52 | + $this->option(self::OPTION_DOWN), |
| 53 | + $this, |
| 54 | + $this->components |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function getSubscribedSignals(): array |
| 59 | + { |
| 60 | + return [SIGINT, SIGTERM]; |
| 61 | + } |
| 62 | + |
| 63 | + public function handleSignal(int $signal): void |
| 64 | + { |
| 65 | + $this->processor->terminate(false); |
| 66 | + } |
| 67 | + |
| 68 | + private function extendSignature(): void |
| 69 | + { |
| 70 | + $this->signature = join(PHP_EOL, array_merge( |
| 71 | + [$this->signature], |
| 72 | + array_map(function ($option) { |
| 73 | + return '{--' . join(" : ", [$option[0], $option[3]]) . '}'; |
| 74 | + }, $this->getAdditionalOptions()) |
| 75 | + )); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Additional options to add to the command. |
| 80 | + * |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + private function getAdditionalOptions(): array |
| 84 | + { |
| 85 | + return [ |
| 86 | + [self::OPTION_MUTEX, null, InputOption::VALUE_NONE, 'Run a mutually exclusive migration'], |
| 87 | + [self::OPTION_DOWN, null, InputOption::VALUE_NONE, 'Enable maintenance mode during a migration'] |
| 88 | + ]; |
| 89 | + } |
| 90 | +} |
0 commit comments