3

I am trying to setup a second database for a symfony 4.2 project. Everything seems to working fine up until the point where I run migrations where all migrations execute on the given connection instead of only the migrations for the connection it was created.

Following symfony's own documentation about this, my doctrine.yaml looks like this:

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(DATABASE_URL)%'
            logging:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(DATABASE_URL_LOG)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: true
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App
            logging:
                connection: logging
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: false
                mappings:
                    Log:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Log'
                        prefix: 'App\Log'
                        alias: Log

Now with this configuration I am able to run the migration commands with the --em=log/default parameter like this:

php bin/console doctrine:migrations:diff --em=log
php bin/console doctrine:migrations:migrate --em=log

And you get the expected results: It only creates a new migration for a new entity I created at src/Log when I add --em=log.

this is the new entity:

<?php

namespace App\Log;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class LogItem
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;
}

However, the migration is created in the default src/Migrations folder and as a result:

1) this migration is also executed when I run doctrine:migrations:migrate --em=default (creating the table in the default database)

2) the entire default schema is loaded into the log database when I run doctrine:migrations:migrate --em=log

So I think the solution would be to split the migration files between the to entity managers into different directories. But I have been spending hours and can't find the way to do this for symfony4. Also, since the symfony documentation mentions absolutely nothing about this I feel like maybe something is wrong in the way it is setup now.

So can anyone tell me what I am doing wrong here? Or can (and if yes: how) do I split the migration files so it will only execute the migrations created for the given entity manager?

Piels
  • 31
  • 2

2 Answers2

0

doctrine/doctrine-migrations-bundle doesn't support migrations for multiple entity managers out of the box. There are various workarounds to this problem, including:

  • Container aware migrations (only feasible on Symfony <4)
  • Storing migration configuration for each entity manager separately and passing it directly via the --configuration option of the doctrine:migrations:migrate command (only feasible if you don't need to inject services/dependencies into your migrations)
  • Using avaibooksports/doctrine-migrations-multiple-database-bundle (or rolling your own bundle), which implements support for separate configuration per-entity-manager (in which you can specify a separate directory for each entity's migrations, and inject services via factories as needed).

There's an open GitHub issue (opened in 2012) that describes these approaches in more detail. Also see this answer for a summary.

Matt Bennett
  • 120
  • 9
0

I think you have a typo in your samples.

In your doctrine.yaml file, the names of your entity managers are "default" and "logging".

But in your command line samples you use the entity manager name "log". It should work when you change the flag from --em=log to --em=logging.

René Pöpperl
  • 567
  • 5
  • 18