3
votes

I have two database connections in config.yml and two entity managers. Each are tied to a bundle.

The issue that I'm having is running unit tests, which start by creating a blank db and loading in data. It's creating both databases but i'm getting both sets of tables in each database, instead of one set of entities in one db and one in the other. Since two db connections isn't incredibly common, I'm having trouble finding some help on this.

doctrine:
    dbal:
        default_connection: default
 ...
    connections:
         default:
         (conectioninfo)
         seconddb:
         (connectioninfo)


 orm:
    default_entity_manager: default
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
    default:
        connection: default
        mappings:
           MycompanyCoreBundle: ~
    seconddb:
        connection: seconddb
        mappings:
           MycomanySecondBundle: ~

When running unit tests, the lines I have are:

php app/console doctrine:database:drop --force --env=test --connection=default 
php app/console doctrine:database:create --env=test --connection=default
php app/console doctrine:schema:drop --force --no-interaction --env=test --em=default
php app/console doctrine:schema:update --force --no-interaction --env=test --em=default

php app/console doctrine:database:drop --force --env=test --connection=seconddb
php app/console doctrine:database:create --env=test --connection=seconddb
php app/console doctrine:schema:drop --force --no-interaction --env=test --em=seconddb
php app/console doctrine:schema:update --force --no-interaction --env=test --em=seconddb

When running all this, the output is

Successfully deleted cache entries.
Dropping database schema...
Database schema dropped successfully!
Updating database schema...
Database schema updated successfully! "91" queries were executed

The problem is that those 91 queries are a combination of both Entity folders in the two bundles. I'm missing somewhere to point them separately so they go into their respective databases.

1
Probably won't help but replace the schema drop/update with a single schema:create. - Cerad
connection: streaming should be connection: seconddb? - Cerad
yeah it's seconddb. My example above was cut from real code and I was changing some of the names. schema:create does save the two steps but same result, all the tables mixed and put in both databases. - Kris White
I assume you have default and seconddb properly indented under entity_managers? And you are sure that seconddb has the correct database name? I use multiple em's all the time and what you posted should work. Check for silly mistakes. Maybe post your exact code except for passwords. - Cerad

1 Answers

1
votes

I found the answer to this eventually. There isn't a way to specify a database for a Migration, so you basically have to run Migration twice on EM and then test for the db connection.

Within each migration file, you put in a line to ignore it if it's not the correct one. Thus, some files have

$this->skipIf( $this->connection->getDatabase() != 'dbone', 'Skipping database.' );

and others have

$this->skipIf( $this->connection->getDatabase() != 'dbtwo', 'Skipping database.' );

Then when you run these commands:

doctrine:migrations:migrate --em="default"
doctrine:migrations:migrate --em="orm"

Both will cycle through all your migration files but the ones that don't apply to that situation will be ignored.