Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Command for generation failed_jobs table migration #133

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Console/QueueFailedTableCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrine\Migrations\Console;

use LaravelDoctrine\Migrations\Configuration\DependencyFactoryProvider;
use LaravelDoctrine\Migrations\DoctrineCommand\GenerateFailedJobsCommand;

class QueueFailedTableCommand extends BaseCommand
{
protected $signature = 'doctrine:migrations:queue-failed-table
{--em= : For a specific EntityManager. }
{--table=failed_jobs : Name for the table. }';

protected $description = 'Create a migration for the failed queue jobs database table';

public function handle(DependencyFactoryProvider $provider): int
{
$dependencyFactory = $provider->fromEntityManagerName($this->option('em'));

$command = new GenerateFailedJobsCommand($dependencyFactory);

return $command->run($this->getDoctrineInput($command), $this->output->getOutput());
}
}
108 changes: 108 additions & 0 deletions src/DoctrineCommand/GenerateFailedJobsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrine\Migrations\DoctrineCommand;

use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use LaravelDoctrine\ORM\Queue\FailedJobTable;

class GenerateFailedJobsCommand extends DoctrineCommand
{
/** @var string|null */
protected static $defaultName = 'doctrine:migrations:queue-failed-table';

protected const UP_TEMPLATE = <<<'UPTEMPLATE'
$builder = (new \LaravelDoctrine\Migrations\Schema\Builder($schema));
$builder->create('<tableName>', 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('<tableName>');
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 "<info>%s</info>"', $path),
'',
]);

return 0;
}

protected function upScript($tableName): string
{
return strtr(static::UP_TEMPLATE, [
'<tableName>' => $tableName,
]);
}

protected function downScript($tableName): string
{
return strtr(static::DOWN_TEMPLATE, [
'<tableName>' => $tableName,
]);
}
}
4 changes: 3 additions & 1 deletion src/MigrationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -62,7 +63,8 @@ public function register()
GenerateCommand::class,
SyncMetadataCommand::class,
ListCommand::class,
DumpSchemaCommand::class
DumpSchemaCommand::class,
QueueFailedTableCommand::class,
]);
}

Expand Down