-
-
Save MushuLeDragon/8af923cb5a3e9f23af7fe04c3a24ed53 to your computer and use it in GitHub Desktop.
Proxy commands for Doctrine Migrations bundle with multiple entity managers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Doctrine\Common\Persistence\ManagerRegistry; | |
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager; | |
use Doctrine\Migrations\Configuration\Migration\YamlFile; | |
use Doctrine\Migrations\DependencyFactory; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
abstract class AbstractMigrationCommand extends Command | |
{ | |
private ManagerRegistry $registry; | |
public function __construct(ManagerRegistry $registry) | |
{ | |
parent::__construct(); | |
$this->registry = $registry; | |
} | |
protected function configure(): void | |
{ | |
$this->addArgument('em', InputArgument::REQUIRED, 'Name of the Entity Manager to handle'); | |
} | |
protected function getDependencyFactory(InputInterface $input): DependencyFactory | |
{ | |
$em = $this->registry->getManager($input->getArgument('em')); | |
$config = new YamlFile(__DIR__ . '/../../../../../config/doctrine_migrations/' . $input->getArgument('em') . '.yaml'); | |
return DependencyFactory::fromEntityManager($config, new ExistingEntityManager($em)); | |
} | |
protected function getManager(InputInterface $input): ManagerRegistry | |
{ | |
$em = $this->registry->getManager($input->getArgument('em')); | |
return $this->registry; | |
} | |
/** | |
* Get the value of registry | |
*/ | |
public function getRegistry(): ManagerRegistry | |
{ | |
return $this->registry; | |
} | |
/** | |
* Set the value of registry | |
* | |
* @return self | |
*/ | |
public function setRegistry(ManagerRegistry $registry) | |
{ | |
$this->registry = $registry; | |
return $this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class CreateDatabaseDoctrineCommand extends AbstractDoctrineCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('custom:database:create') | |
->setDescription('Wrapper to launch doctrine:database:create command as it would require a "configuration" option') | |
->addOption('connection', 'c', InputOption::VALUE_OPTIONAL, 'The connection to use for this command') | |
->addOption('if-not-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database already exists') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([ | |
'--connection' => $input->getOption('connection'), | |
'--if-not-exists' => $input->getOption('if-not-exists'), | |
]); | |
$otherCommand = new \Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand($this->getRegistry()); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class DiffCommand extends AbstractMigrationCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('app:migrations:diff') | |
->setDescription('Wrapper to launch doctrine:migrations:diff command as it would require a "configuration" option') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([]); | |
$newInput->setInteractive($input->isInteractive()); | |
$otherCommand = new \Doctrine\Migrations\Tools\Console\Command\DiffCommand($this->getDependencyFactory($input)); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class DropDatabaseDoctrineCommand extends AbstractDoctrineCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('custom:database:drop') | |
->setDescription('Wrapper to launch doctrine:database:drop command as it would require a "configuration" option') | |
->addOption('connection', 'c', InputOption::VALUE_OPTIONAL, 'The connection to use for this command') | |
->addOption('if-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist') | |
->addOption('force', 'f', InputOption::VALUE_NONE, 'Set this parameter to execute this action') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([ | |
'--connection' => $input->getOption('connection'), | |
'--if-exists' => $input->getOption('if-exists'), | |
'--force' => $input->getOption('force'), | |
]); | |
$newInput->setInteractive($input->isInteractive()); | |
$otherCommand = new \Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand($this->getRegistry()); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class GenerateCommand extends AbstractMigrationCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('app:migrations:generate') | |
->setDescription('Wrapper to launch doctrine:migrations:generate command as it would require a "configuration" option') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([]); | |
$newInput->setInteractive($input->isInteractive()); | |
$otherCommand = new \Doctrine\Migrations\Tools\Console\Command\GenerateCommand($this->getDependencyFactory($input)); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class MigrateCommand extends AbstractMigrationCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('app:migrations:migrate') | |
->setDescription('Wrapper to launch doctrine:migrations:migrate command as it would require a "configuration" option') | |
->addArgument('version', InputArgument::OPTIONAL, 'The version number (YYYYMMDDHHMMSS) or alias (first, prev, next, latest) to migrate to.', 'latest') | |
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the migration as a dry run.') | |
->addOption('query-time', null, InputOption::VALUE_NONE, 'Time all the queries individually.') | |
->addOption('allow-no-migration', null, InputOption::VALUE_NONE, 'Don\'t throw an exception if no migration is available (CI).') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([ | |
'version' => $input->getArgument('version'), | |
'--dry-run' => $input->getOption('dry-run'), | |
'--query-time' => $input->getOption('query-time'), | |
'--allow-no-migration' => $input->getOption('allow-no-migration'), | |
]); | |
$newInput->setInteractive($input->isInteractive()); | |
$otherCommand = new \Doctrine\Migrations\Tools\Console\Command\MigrateCommand($this->getDependencyFactory($input)); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class StatusCommand extends AbstractMigrationCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('app:migrations:status') | |
->setDescription('Wrapper to launch doctrine:migrations:status command as it would require a "configuration" option') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([]); | |
$otherCommand = new \Doctrine\Migrations\Tools\Console\Command\StatusCommand($this->getDependencyFactory($input)); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class SyncMetadataStorageCommand extends AbstractMigrationCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('app:migrations:sync-metadata-storage') | |
->setDescription('Wrapper to launch doctrine:migrations:sync-metadata-storage command as it would require a "configuration" option') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([]); | |
$otherCommand = new SyncMetadataCommand($this->getDependencyFactory($input)); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Command\Migration; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class VersionCommand extends AbstractMigrationCommand | |
{ | |
protected function configure(): void | |
{ | |
parent::configure(); | |
$this | |
->setName('app:migrations:version') | |
->setDescription('Wrapper to launch doctrine:migrations:version command as it would require a "configuration" option') | |
->addArgument('version', InputArgument::OPTIONAL, 'The version to add or delete.', null) | |
->addOption('add', null, InputOption::VALUE_NONE, 'Add the specified version.') | |
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete the specified version.') | |
->addOption('all', null, InputOption::VALUE_NONE, 'Apply to all the versions.') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$newInput = new ArrayInput([ | |
'version' => $input->getArgument('version'), | |
'--add' => $input->getOption('add'), | |
'--delete' => $input->getOption('delete'), | |
'--all' => $input->getOption('all'), | |
]); | |
$newInput->setInteractive($input->isInteractive()); | |
$otherCommand = new \Doctrine\Migrations\Tools\Console\Command\VersionCommand($this->getDependencyFactory($input)); | |
$otherCommand->run($newInput, $output); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment