Skip to content

Instantly share code, notes, and snippets.

@cyk
Created April 8, 2014 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyk/10167795 to your computer and use it in GitHub Desktop.
Save cyk/10167795 to your computer and use it in GitHub Desktop.
EntityManagerMigration.php
<?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