Kris White
Kris White

Reputation: 679

Doctrine2 Symfony2 migration on multiple databases

I'm having a bit of trouble doing a db migration on a non-default database with Symfony2 and Doctrine.

I have two dbs and two entity managers I'm working with. I have two bundles, each which works with that respective EM so everything should be quite separated.

Under my DefaultBundle I have migrations with about 20 migration files that all apply to the default db. My SecondBundle has one migration file which applies to the seconddb.

When I attempt to run

   php app/console doctrine:migrations:migrate --no-interaction --em="second"

I'm getting

Migration 20140709212101 failed during Execution. Error There is no table with name 'second_database.users' in the schema.

  [Doctrine\DBAL\Schema\SchemaException]                                  
  There is no table with name 'second_database.users' in the schema.  

Which is totally true, "users" is a table in my primary db. What's happening here is that the migration file it's attempting to run is actually the first of the 20 in the DefaultCore (which are all old and already applied). Because I've specified --em it's crossing migrations I want don't want to run with against the db I'm working with. There doesn't appear to be another commmand line option to specify a bundle like there is for other migration commands like generate. I want to ignore DefaultBundle, and just run my migrations from my SecondBundle.

Upvotes: 3

Views: 1877

Answers (2)

Kris White
Kris White

Reputation: 679

The solution for this is you have to run migrations twice, once on each --em, and then in the migration itself check for which database you're on to see if you should run it or not. You make your migration ContainerAware so you have access to do that. Example:

class Version201501021322 extends AbstractMigration implements ContainerAwareInterface
{
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        $this->skipIf($this->connection->getDatabase() != $this->container->getParameter('second_database_name'), 'Skipping database.');

        $this->addsql("//migration here");
    }


}

Upvotes: 4

Logans
Logans

Reputation: 119

I recently came to this issue too.

The problem came's when doctrine:fixtures:load try's to purge database for this entity manager that you pass there, but it fails because he can't find specific table which stores in another database.

To avoid this problem, you can use key --append. For example:

$ php app/console doctrine:fixtures:load --em=second --append

This key mean that you'r database wont be purged so fixtures will load over existing data.

This works only if fixtures load data only for second connection.

I think you can avoid this by passing into --fixtures key, fixtures directory (but i'm not sure).


In my case i have 2 databases and 2 connection (default, not_default) but i have only fixtures for default connection so this works for me fine.

Upvotes: 0

Related Questions