Skip to content

Commit 303a576

Browse files
Merge pull request #132 from vincentchalamon/sharding
Add --shard option to migrations command
2 parents 93ec729 + b45b9eb commit 303a576

8 files changed

+36
-4
lines changed

Command/Helper/DoctrineCommandHelper.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Doctrine\Bundle\MigrationsBundle\Command\Helper;
1616

1717
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper as BaseDoctrineCommandHelper;
18+
use Doctrine\DBAL\Sharding\PoolingShardConnection;
1819
use Symfony\Bundle\FrameworkBundle\Console\Application;
1920
use Symfony\Component\Console\Input\InputInterface;
2021

@@ -28,13 +29,27 @@ abstract class DoctrineCommandHelper extends BaseDoctrineCommandHelper
2829
{
2930
public static function setApplicationHelper(Application $application, InputInterface $input)
3031
{
31-
$doctrine = $application->getKernel()->getContainer()->get('doctrine');
32+
$container = $application->getKernel()->getContainer();
33+
$doctrine = $container->get('doctrine');
3234
$managerNames = $doctrine->getManagerNames();
3335

3436
if (empty($managerNames)) {
3537
self::setApplicationConnection($application, $input->getOption('db'));
3638
} else {
3739
self::setApplicationEntityManager($application, $input->getOption('em'));
3840
}
41+
42+
if ($input->getOption('shard')) {
43+
$connection = $application->getHelperSet()->get('db')->getConnection();
44+
if (!$connection instanceof PoolingShardConnection) {
45+
if (empty($managerNames)) {
46+
throw new \LogicException(sprintf("Connection '%s' must implement shards configuration.", $input->getOption('db')));
47+
} else {
48+
throw new \LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $input->getOption('em')));
49+
}
50+
}
51+
52+
$connection->connect($input->getOption('shard'));
53+
}
3954
}
4055
}

Command/MigrationsDiffDoctrineCommand.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414

1515
namespace Doctrine\Bundle\MigrationsBundle\Command;
1616

17-
use Symfony\Component\Console\Input\InputInterface;
18-
use Symfony\Component\Console\Output\OutputInterface;
19-
use Symfony\Component\Console\Input\InputOption;
2017
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
2118
use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand;
19+
use Doctrine\DBAL\Sharding\PoolingShardConnection;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Input\InputOption;
22+
use Symfony\Component\Console\Output\OutputInterface;
2223

2324
/**
2425
* Command for generate migration classes by comparing your current database schema
@@ -36,13 +37,23 @@ protected function configure()
3637
$this
3738
->setName('doctrine:migrations:diff')
3839
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
40+
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
3941
;
4042
}
4143

4244
public function execute(InputInterface $input, OutputInterface $output)
4345
{
4446
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
4547

48+
if ($input->getOption('shard')) {
49+
$connection = $this->getApplication()->getHelperSet()->get('db')->getConnection();
50+
if (!$connection instanceof PoolingShardConnection) {
51+
throw new \LogicException(sprintf("Connection of EntityManager '%s' must implements shards configuration.", $input->getOption('em')));
52+
}
53+
54+
$connection->connect($input->getOption('shard'));
55+
}
56+
4657
$configuration = $this->getMigrationConfiguration($input, $output);
4758
DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
4859

Command/MigrationsExecuteDoctrineCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535
->setName('doctrine:migrations:execute')
3636
->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.')
3737
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
38+
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
3839
;
3940
}
4041

Command/MigrationsGenerateDoctrineCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535
->setName('doctrine:migrations:generate')
3636
->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.')
3737
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
38+
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
3839
;
3940
}
4041

Command/MigrationsLatestDoctrineCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535
->setName('doctrine:migrations:latest')
3636
->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.')
3737
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
38+
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
3839
;
3940
}
4041

Command/MigrationsMigrateDoctrineCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535
->setName('doctrine:migrations:migrate')
3636
->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.')
3737
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
38+
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
3839
;
3940
}
4041

Command/MigrationsStatusDoctrineCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535
->setName('doctrine:migrations:status')
3636
->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.')
3737
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
38+
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
3839
;
3940
}
4041

Command/MigrationsVersionDoctrineCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535
->setName('doctrine:migrations:version')
3636
->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.')
3737
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
38+
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
3839
;
3940
}
4041

0 commit comments

Comments
 (0)