Symfony2 - Change Migration Directory

2020-03-04 08:08发布

问题:

How can I change the default migration dirctory in the config.yml? Right now I am using 2 bundles with different db-connections and I would like to create migrations files and store them in different directories to use the doctrine:migrations:migrate --em=whatever function in depency of the bundle.

For example:

doctrine:migrate:diff --em=whatever #creating a version file in the DoctrineMigrationsWhatever directory

php app/console doctrine:migrations:status --em=whatever # shows only the version files, that belong to the bundle

回答1:

If you'll create separate entity manager for this second connection/bundle you will get another directory in your DoctrineMigrations dir. For example:

app/
    DoctrineMigrations/
        entityManager1/
        entityManager2/

If you want put all migrations to another directory, you can set it inside your config.yml:

doctrine_migrations:
    dir_name: '%kernel.root_dir%/../Acme/CommonBundle/DoctrineMigrations'
    namespace: 'Acme\CommonBundle\DoctrineMigrations'

If you want some more complex thing like put migrations from em1 to dir1 inside bundle1 and put migrations from em2 to dir2 inside bundle2 you will need an additional two configuration files where you'll specify dirs for particular bundles:

http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/reference/introduction.html#configuration

And then you run your migrations like this:

doctrine:migrations:status --em=em1 --configuration=./path/to/bundle1/Resources/config/migrations.yml
doctrine:migrations:status --em=em2 --configuration=./path/to/bundle2/Resources/config/migrations.yml

By https://github.com/doctrine/DoctrineMigrationsBundle/pull/46
the migrations.yml file should look like:

name: Doctrine Postgres Migrations  
migrations_namespace: Application\Migrations  
table_name: migration_versions  
migrations_directory: PostgreSqlMigrations  


回答2:

For other people who found this page and spent hours trying to implement Cyprian's solution, it doesn't work.

First, --configuration gets clobbered, and second, the doctrine migrations bundle doesn't support multiple entity managers.

See https://github.com/doctrine/DoctrineMigrationsBundle/issues/18 for information about --configuration and see https://github.com/doctrine/DoctrineMigrationsBundle/pull/46 for an open pull request to support multiple entity managers.

If and when PR #46 goes through, this will be a trivial configuration:

doctrine_migrations:
  default:
    dir_name: ...
    namespace: ...
  em2:
    dir_name: ...
    namespace: ...

The only tweaks available right now, are:

doctrine_migrations:
    dir_name: '%kernel.root_dir%/../Acme/CommonBundle/DoctrineMigrations'
    namespace: 'Acme\CommonBundle\DoctrineMigrations'

But that will update the configuration for all migrations across all entity managers.



回答3:

For Symfony 4, the recommended approach is to use %kernel.project_dir% instead, and put it in the src/ folder namespaced by App\:

doctrine_migrations:
    dir_name: '%kernel.project_dir%/src/DoctrineMigrations'
    namespace: 'App\DoctrineMigrations'