From b45b9eb6a44782657315ba4918fb33b5282f42dd Mon Sep 17 00:00:00 2001 From: Vincent CHALAMON Date: Wed, 19 Aug 2015 18:01:24 +0200 Subject: [PATCH] Add --shard option to migrations command --- Command/Helper/DoctrineCommandHelper.php | 17 ++++++++++++++++- Command/MigrationsDiffDoctrineCommand.php | 17 ++++++++++++++--- Command/MigrationsExecuteDoctrineCommand.php | 1 + Command/MigrationsGenerateDoctrineCommand.php | 1 + Command/MigrationsLatestDoctrineCommand.php | 1 + Command/MigrationsMigrateDoctrineCommand.php | 1 + Command/MigrationsStatusDoctrineCommand.php | 1 + Command/MigrationsVersionDoctrineCommand.php | 1 + 8 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Command/Helper/DoctrineCommandHelper.php b/Command/Helper/DoctrineCommandHelper.php index 18fffec..0a5a626 100644 --- a/Command/Helper/DoctrineCommandHelper.php +++ b/Command/Helper/DoctrineCommandHelper.php @@ -15,6 +15,7 @@ namespace Doctrine\Bundle\MigrationsBundle\Command\Helper; use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper as BaseDoctrineCommandHelper; +use Doctrine\DBAL\Sharding\PoolingShardConnection; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\InputInterface; @@ -28,7 +29,8 @@ abstract class DoctrineCommandHelper extends BaseDoctrineCommandHelper { public static function setApplicationHelper(Application $application, InputInterface $input) { - $doctrine = $application->getKernel()->getContainer()->get('doctrine'); + $container = $application->getKernel()->getContainer(); + $doctrine = $container->get('doctrine'); $managerNames = $doctrine->getManagerNames(); if (empty($managerNames)) { @@ -36,5 +38,18 @@ public static function setApplicationHelper(Application $application, InputInter } else { self::setApplicationEntityManager($application, $input->getOption('em')); } + + if ($input->getOption('shard')) { + $connection = $application->getHelperSet()->get('db')->getConnection(); + if (!$connection instanceof PoolingShardConnection) { + if (empty($managerNames)) { + throw new \LogicException(sprintf("Connection '%s' must implement shards configuration.", $input->getOption('db'))); + } else { + throw new \LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $input->getOption('em'))); + } + } + + $connection->connect($input->getOption('shard')); + } } } diff --git a/Command/MigrationsDiffDoctrineCommand.php b/Command/MigrationsDiffDoctrineCommand.php index 034a000..9c0e376 100644 --- a/Command/MigrationsDiffDoctrineCommand.php +++ b/Command/MigrationsDiffDoctrineCommand.php @@ -14,11 +14,12 @@ namespace Doctrine\Bundle\MigrationsBundle\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\InputOption; use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper; use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand; +use Doctrine\DBAL\Sharding\PoolingShardConnection; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; /** * Command for generate migration classes by comparing your current database schema @@ -36,6 +37,7 @@ protected function configure() $this ->setName('doctrine:migrations:diff') ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.') + ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ; } @@ -43,6 +45,15 @@ public function execute(InputInterface $input, OutputInterface $output) { DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em')); + if ($input->getOption('shard')) { + $connection = $this->getApplication()->getHelperSet()->get('db')->getConnection(); + if (!$connection instanceof PoolingShardConnection) { + throw new \LogicException(sprintf("Connection of EntityManager '%s' must implements shards configuration.", $input->getOption('em'))); + } + + $connection->connect($input->getOption('shard')); + } + $configuration = $this->getMigrationConfiguration($input, $output); DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration); diff --git a/Command/MigrationsExecuteDoctrineCommand.php b/Command/MigrationsExecuteDoctrineCommand.php index 84faa2c..15492f3 100644 --- a/Command/MigrationsExecuteDoctrineCommand.php +++ b/Command/MigrationsExecuteDoctrineCommand.php @@ -35,6 +35,7 @@ protected function configure() ->setName('doctrine:migrations:execute') ->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.') ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') + ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ; } diff --git a/Command/MigrationsGenerateDoctrineCommand.php b/Command/MigrationsGenerateDoctrineCommand.php index 6437c4e..2e65815 100644 --- a/Command/MigrationsGenerateDoctrineCommand.php +++ b/Command/MigrationsGenerateDoctrineCommand.php @@ -35,6 +35,7 @@ protected function configure() ->setName('doctrine:migrations:generate') ->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.') ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') + ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ; } diff --git a/Command/MigrationsLatestDoctrineCommand.php b/Command/MigrationsLatestDoctrineCommand.php index b0e4082..bc0c2ce 100644 --- a/Command/MigrationsLatestDoctrineCommand.php +++ b/Command/MigrationsLatestDoctrineCommand.php @@ -35,6 +35,7 @@ protected function configure() ->setName('doctrine:migrations:latest') ->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.') ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') + ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ; } diff --git a/Command/MigrationsMigrateDoctrineCommand.php b/Command/MigrationsMigrateDoctrineCommand.php index 4aa70c1..3d34b8e 100644 --- a/Command/MigrationsMigrateDoctrineCommand.php +++ b/Command/MigrationsMigrateDoctrineCommand.php @@ -35,6 +35,7 @@ protected function configure() ->setName('doctrine:migrations:migrate') ->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.') ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') + ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ; } diff --git a/Command/MigrationsStatusDoctrineCommand.php b/Command/MigrationsStatusDoctrineCommand.php index c07003a..7b83a27 100644 --- a/Command/MigrationsStatusDoctrineCommand.php +++ b/Command/MigrationsStatusDoctrineCommand.php @@ -35,6 +35,7 @@ protected function configure() ->setName('doctrine:migrations:status') ->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.') ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') + ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ; } diff --git a/Command/MigrationsVersionDoctrineCommand.php b/Command/MigrationsVersionDoctrineCommand.php index 4ec9972..998f5d3 100644 --- a/Command/MigrationsVersionDoctrineCommand.php +++ b/Command/MigrationsVersionDoctrineCommand.php @@ -35,6 +35,7 @@ protected function configure() ->setName('doctrine:migrations:version') ->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.') ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') + ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ; }