doctrine / DoctrineMigrationsBundle

Symfony integration for the doctrine/migrations library
MIT License
4.21k stars 215 forks source link

Specify entity manager for migrations on 3.0, since --em has been dropped #341

Closed davrodrila closed 4 years ago

davrodrila commented 4 years ago

Hello,

First of all I hope this is the right place to put this kind of questions

I've been trying for a little bit to make a project work with 3.0 but couldn't.

So, the situation here is, I have two databases in the same project, and the way is set up there are two different entity managers that connect to the databases, so the app has default and stats.

In order to have migrations working, I had a workaround that consisted of two configuration files: migrations_default.yaml

migrations_directory: 'src/Migrations/Default/'
migrations_namespace: App\Migrations\Default
table_name: migration_versions
name: Application_Migrations_Default

and migrations_stats.yaml

migrations_directory: 'src/Migrations/Stats/'
migrations_namespace: App\Migrations\Stats
table_name: migration_versions
name: Application_Migrations_Stats

And what I did was launch the following command to migrate,,for the default EM:

php bin/console doctrine:migrations:migrate --em=default --configuration=./migrations_default.yaml

And for the Stats EM:

php bin/console doctrine:migrations:migrate --em=stats --configuration=./migrations_stats.yaml This made it so the proper entity manager was used, and the migrations will each go into its own folder.

However this no longer works in 3.0 as the --em parameter is no longer available, and I'm having a little bit of trouble making it work. I tried updating the config files to match the new version format, so the previous migrations_default.yaml will look something like this:

doctrine_migrations:
  migrations_paths:
    'App\Migrations\Default': 'src/Migrations/Default'
  em: default

And changed the command to:

php bin/console doctrine:migrations:migrate --configuration=./migrations_default.yaml

If I execute that, I get a "Migrations configuration key "doctrine_migrations" does not exist.
" error, so it looks like I'm doing something wrong here, but I'm just not sure what is it. I followed this manual to build the new config file format.

goetas commented 4 years ago

Related to https://github.com/doctrine/migrations/issues/1021

Most probably requires something similar as for the --db option.

Custom --configuration has been implemented in https://github.com/doctrine/migrations/pull/959 (that uses the library config format, not the bundle config format)

goetas commented 4 years ago

@davrodrila Can you please explain a bit more in detail how are you using this feature? We had a team discussion on how to re-implement this in 3.0 and we wanted to see if there are ways to improve it (as example allowing to run migrations across all defined connections...?) Are you using the --em parameter to generate a per-entity-manger diff?

mdeherder commented 4 years ago

For my part, I use the --em parameter to generate a difference by manager-entity. I used doctrine:migrations:diff and doctrine:migrations:migrate with --em for entity-manager and --configuration for the migrations-paths option

davrodrila commented 4 years ago

Thanks, I'll do my best to explain, but It's pretty much what @goetas and @mdeherder said, It's so I can get diffs per entity manager. Each entity manager has its own connection to a different database.

This is because the app has two sets of entities, (Default and Stats) each one with its own database. The entities are placed on different folders. (src/Default/Entity and src/Stats/Entity), so in my doctrine.yaml looks like this:

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(resolve:DATABASE_DEFAULT_URL)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
            stats:
                url: '%env(resolve:DATABASE_STATS_URL)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        default_entity_manager: default
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: default
                mappings:
                    Default:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Default/Entity'
                        prefix: 'App\Default\Entity'
                        alias: Default
                    FOSOAuthServerBundle: ~
                    SonataUserBundle: ~
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true
            stats:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: false
                connection: stats
                mappings:
                    analytics:
                        type: annotation
                        dir: '%kernel.project_dir%/src/Stats/Entity'
                        prefix: 'App\Stats\Entity'
                        alias: Stats

So with this setup I hope you can see the problem I was trying to solve with the --em and --configuration workaround. Since the doctrine migrations config doesnt really have a way to discriminate between the two entity managers, I just loaded different config files depending which entity manager I wanted to use.

For example, what @dvc suggested here would fix my particular need for --em and different config files.

GasparMDQ commented 4 years ago

@davrodrila Can you please explain a bit more in detail how are you using this feature? We had a team discussion on how to re-implement this in 3.0 and we wanted to see if there are ways to improve it (as example allowing to run migrations across all defined connections...?) Are you using the --em parameter to generate a per-entity-manger diff?

We have a similar issue, and on version 2.x we created an extended AbstractMigration class that uses the ContainerWareTrait from Symfony. In there we have a method to get the current connection name, and then we 'skip' the migration if the name doesn't match the expected one. This way, all migrations are run everytime, but only the one for a specific connection is executed.

On our system, we have 3 different DBs (2 Postrgres and 1 MySQL) and are being handle by different entity managers.

For the time being, we restricted our DoctrineMigrationBundle to version 2.1.2 to prevent losing this feature.

deluxetom commented 4 years ago

I have the same issue, I have 2 different connections and I was using the em option to manage the migrations for each DB. Is there a way to add that option back? Or it would be great if in the migration_paths option we could define the em for each namespace

mdeherder commented 4 years ago

Here you will find a method that is not trivial but works perfectly : https://jmsche.fr/en/blog/upgrade-doctrine-migrations-from-2-x-to-3-0

deluxetom commented 4 years ago

Here you will find a method that is not trivial but works perfectly : https://jmsche.fr/en/blog/upgrade-doctrine-migrations-from-2-x-to-3-0

Thank you!

goetas commented 4 years ago

Implemented in https://github.com/doctrine/migrations/pull/1023

kilobyte2007 commented 4 years ago

Sorry for opening this back up. When I run: bin/console doctrine:migrations:migrate --configuration=config/migrations/default.yaml --em=default with the last (dev-master) version, it throws an error:

Only one entity manager is supported by Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager

Is there a way around this cause it's not really clear what does the message mean.

goetas commented 4 years ago

This is not yet released, the bundle 3.1 is needed for this to work as expected

goetas commented 4 years ago

@kilobyte2007 https://github.com/doctrine/DoctrineMigrationsBundle/pull/364 will introduce that feature in the symfony bundle

guruparthi19 commented 9 months ago

I''m using below versions of symfony, doctrine bundle & doctrine-migrations-bundle: "doctrine/doctrine-bundle": "^2.11", "doctrine/doctrine-migrations-bundle": "^3.0", "doctrine/orm": "^3.0"

My doctrine.yaml file would be:

    doctrine:
dbal:
    connections:
        default:
            driver: 'pdo_mysql'
            server_version: '8.0.36'
            charset: 'UTF8'
            url: '%env(resolve:DATABASE_URL)%'
            logging: true
        postgres:
            driver: 'pdo_pgsql'
            server_version: '10'
            charset: 'UTF8'
            url: '%env(resolve:POSTGRES_DATABASE_URL)%'
            logging: true
    default_connection: default
orm:
    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
            mappings:
                Main:
                    is_bundle: false
                    type: attribute
                    dir: '%kernel.project_dir%/src/Entity/Main'
                    prefix: 'App\Entity\Main'
                    alias: Main
        postgres:
            connection: postgres
            naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
            mappings:
                PostgresSQL:
                    is_bundle: false
                    type: attribute
                    dir: '%kernel.project_dir%/src/Entity/PostgresSQL'
                    prefix: 'App\Entity\PostgresSQL'
                    alias: PostgresSQL`

my doctrine_migrations.yaml file would be:

          doctrine_migrations:
              migrations_paths:
                  'DoctrineMigrations': '%kernel.project_dir%/migrations/Main'
                  'DoctrineMigrations\PostgreSQL': '%kernel.project_dir%/migrations/PostgresSQL'
              storage:
                  table_storage:
                      table_name: 'doctrine_migration_versions'
                      version_column_name: 'version'
                      executed_at_column_name: 'executed_at'
                      execution_time_column_name: 'execution_time'

              services: ~`

I wanted to have both MySQL(default) & postgres entity manager. I configured the entity files to be stored in "src/Entity/Main" & "src/Entity/PostgresSQL" respectively.

The migrations files will be stored in "migrations/Main" & "migrations/PostgresSQL".

When I run the command "doctrine:migrations:diff" with --em of "default" & "postgres", it generated the migration file inside the "migrations/Main" folder. I expected that when i use "postgres", it should generate the migration file at "migrations/PostgresSQL".

Even I tried to cut & past my migration files to "migrations/PostgresSQL" & I tried to run the command "php bin/console doctrine:migrations:migrate --em=postgres". But it tried to generate the migration files from the folder "migrations/Main". What should I do to make it work?