Created
April 8, 2014 18:29
EntityManagerMigration.php
This file contains 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 | |
namespace MyApp\Migrations; | |
use Doctrine\DBAL\Migrations\AbstractMigration; | |
use Doctrine\DBAL\Schema\Schema; | |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Until DoctrineMigrationsBundle supports multiple entity managers, provide an abstract class to | |
* conditionally skip a migration based on its entity manager name. | |
* @link https://github.com/doctrine/DoctrineMigrationsBundle/pull/46 | |
*/ | |
abstract class EntityManagerMigration extends AbstractMigration implements ContainerAwareInterface | |
{ | |
/** @var \Symfony\Component\DependencyInjection\Container */ | |
protected $container; | |
public function preUp(Schema $schema) | |
{ | |
$this->skipInvalidDB(); | |
} | |
public function preDown(Schema $schema) | |
{ | |
$this->skipInvalidDB(); | |
} | |
public function setContainer(ContainerInterface $container = null) | |
{ | |
$this->container = $container; | |
} | |
abstract protected function getEntityManagerName(); | |
/** | |
* @return \Doctrine\ORM\EntityManager | |
*/ | |
protected function getEntityManager() | |
{ | |
$name = $this->getEntityManagerName(); | |
return $this->container->get("doctrine.orm.{$name}_entity_manager"); | |
} | |
protected function skipInvalidDB() | |
{ | |
$dbName = $this->getEntityManager()->getConnection()->getDatabase(); | |
$em = $this->getEntityManagerName(); | |
$this->skipIf($this->connection->getDatabase() != $dbName, "Migration can only be executed on '{$dbName}' database (use --em={$em}).'" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment