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

Add --shard option to migrations command #132

Merged
merged 1 commit into from
Nov 4, 2015
Merged
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
17 changes: 16 additions & 1 deletion Command/Helper/DoctrineCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,13 +29,27 @@ 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)) {
self::setApplicationConnection($application, $input->getOption('db'));
} 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'));
}
}
}
17 changes: 14 additions & 3 deletions Command/MigrationsDiffDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,13 +37,23 @@ 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.')
;
}

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);

Expand Down
1 change: 1 addition & 0 deletions Command/MigrationsExecuteDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
;
}

Expand Down
1 change: 1 addition & 0 deletions Command/MigrationsGenerateDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
;
}

Expand Down
1 change: 1 addition & 0 deletions Command/MigrationsLatestDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
;
}

Expand Down
1 change: 1 addition & 0 deletions Command/MigrationsMigrateDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
;
}

Expand Down
1 change: 1 addition & 0 deletions Command/MigrationsStatusDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
;
}

Expand Down
1 change: 1 addition & 0 deletions Command/MigrationsVersionDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
;
}

Expand Down