diff --git a/src/Console/QueueFailedTableCommand.php b/src/Console/QueueFailedTableCommand.php new file mode 100644 index 0000000..64ed9e6 --- /dev/null +++ b/src/Console/QueueFailedTableCommand.php @@ -0,0 +1,26 @@ +fromEntityManagerName($this->option('em')); + + $command = new GenerateFailedJobsCommand($dependencyFactory); + + return $command->run($this->getDoctrineInput($command), $this->output->getOutput()); + } +} \ No newline at end of file diff --git a/src/DoctrineCommand/GenerateFailedJobsCommand.php b/src/DoctrineCommand/GenerateFailedJobsCommand.php new file mode 100644 index 0000000..6b57454 --- /dev/null +++ b/src/DoctrineCommand/GenerateFailedJobsCommand.php @@ -0,0 +1,108 @@ +create('', function (\LaravelDoctrine\Migrations\Schema\Table $table) { + $table->increments('id'); + $table->string('uuid'); + $table->string('connection'); + $table->string('queue'); + $table->text('payload'); + $table->dateTime('failed_at'); + $table->text('exception')->setNotnull(false); + $table->unique(['uuid'], 'uuid_unique'); +}); +UPTEMPLATE; + + protected const DOWN_TEMPLATE = <<<'DOWN_TEMPLATE' +$schema->dropTable(''); +DOWN_TEMPLATE; + + protected function configure(): void + { + $this + ->setAliases(['failed-table']) + ->setDescription('Create a migration for the failed queue jobs database table') + ->addOption( + 'table', + null, + InputOption::VALUE_REQUIRED, + 'Table name', + 'failed_jobs' + ) + ->addOption( + 'namespace', + null, + InputOption::VALUE_REQUIRED, + 'The namespace to use for the migration (must be in the list of configured namespaces)' + ); + + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $configuration = $this->getDependencyFactory()->getConfiguration(); + + $migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator(); + + $namespace = $input->getOption('namespace'); + if ($namespace === '') { + $namespace = null; + } + + $dirs = $configuration->getMigrationDirectories(); + if ($namespace === null) { + $namespace = key($dirs); + } elseif (! isset($dirs[$namespace])) { + throw new Exception(sprintf('Path not defined for the namespace %s', $namespace)); + } + + $tableName = $input->getOption('table'); + + assert(is_string($namespace)); + assert(is_string($tableName)); + + $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace); + + $up = $this->upScript($tableName); + $down = $this->downScript($tableName); + $path = $migrationGenerator->generateMigration($fqcn, $up, $down); + + $this->io->text([ + sprintf('Generated new migration class to "%s"', $path), + '', + ]); + + return 0; + } + + protected function upScript($tableName): string + { + return strtr(static::UP_TEMPLATE, [ + '' => $tableName, + ]); + } + + protected function downScript($tableName): string + { + return strtr(static::DOWN_TEMPLATE, [ + '' => $tableName, + ]); + } +} diff --git a/src/MigrationsServiceProvider.php b/src/MigrationsServiceProvider.php index a3c70b7..25a04e9 100644 --- a/src/MigrationsServiceProvider.php +++ b/src/MigrationsServiceProvider.php @@ -20,6 +20,7 @@ use LaravelDoctrine\Migrations\Console\StatusCommand; use LaravelDoctrine\Migrations\Console\SyncMetadataCommand; use LaravelDoctrine\Migrations\Console\VersionCommand; +use LaravelDoctrine\Migrations\Console\QueueFailedTableCommand; class MigrationsServiceProvider extends ServiceProvider { @@ -62,7 +63,8 @@ public function register() GenerateCommand::class, SyncMetadataCommand::class, ListCommand::class, - DumpSchemaCommand::class + DumpSchemaCommand::class, + QueueFailedTableCommand::class, ]); }