Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how set up symfony 3 doctrine migrations with multiple db?

I am struggling to have symfony/doctrine exclude db views when validating and updating schema.

I first tried without doctrine migrations ( see this question) but that did not work.

I found out that doctrine migrations would filter out views from validation/update and it actually does, so that step seems to work with migrations.

So, if one has just one db doctrine migrations will work fine, but the set up with multiple db is not clean cut, to say the least.

This is a known issue as you can see on this link. Unfortunately when attempting to follow the solutions described in the link the results are messy.

Even though the command migrations:update --em=default will indicate the correct database is set up, when generating migrations:diff --em=default it will mingle with other db and likewise with migrations:migrate --em=default which ends up creating tables on the other db.

More specifically the errors are: - it will create the separate directories for the migrations files as indicated in the config files, but not to the corresponding em - it will generate mysql queries mixing up the two em - consequently it will update db

My set up is as follows:

config.yml

imports:
....
- { resource: doctrine_migrations_default.yml }
- { resource: doctrine_migrations_used.yml }

doctrine:
  dbal:
    default_connection: default
    connections:
        default:      
            .....
            #schema_filter: "~^(?!view).*$~"
            schema_filter: ~^(?!view_)~
        used:
             ......

orm:
    auto_generate_proxy_classes: '%kernel.debug%'
    default_entity_manager: default
    entity_managers:
        default:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: default
            auto_mapping: true
            mappings:
                AppBundle:  ~
        used:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: used
            mappings:
                UsedBundle: ~ 

Then, the specific configuration files for migrations are:

doctrine_migrations_default.yml

doctrine_migrations: 
        dir_name: "%kernel.root_dir%/DoctrineMigrationsDefault"
        namespace: App\DoctrineMigrationsDefault
        table_name: migration_versions
        name: Application_Migrations_Default 

doctrine_migrations_used.yml

doctrine_migrations:
    dir_name: "%kernel.root_dir%/DoctrineMigrationsUsed"
    namespace: Used\DoctrineMigrationsUsed
    table_name: migration_versions
    name: Application Migrations Used
    organize_migrations: false

This is one example of how it mixes up the configs. The database name is correct. It corresponds to the em=default. But other info are coming from the em=used

Thanks

php bin/console doctrine:migrations:status --em=default

== Configuration

>> Name:                                               Application Migrations Used
>> Database Driver:                                    pdo_mysql
>> Database Name:                                      symfony_cars
>> Configuration Source:                               manually configured
>> Version Table Name:                                 migration_versions
>> Version Column Name:                                version
>> Migrations Namespace:                               Used\DoctrineMigrationsUsed
>> Migrations Directory:                               /Users/BAMAC/Sites/Symfony1/app/DoctrineMigrationsUsed
>> Previous Version:                                   Already at first version
>> Current Version:                                    0
>> Next Version:                                       2017-10-19 08:03:52 (20171019080352)
>> Latest Version:                                     2017-10-19 08:03:52 (20171019080352)
>> Executed Migrations:                                0
>> Executed Unavailable Migrations:                    0
>> Available Migrations:                               1
>> New Migrations:                                     1

Also, if I try to specifically indicate the configuration file with:

php bin/console doctrine:migrations:status --em=default --configuration=./app/config/doctrine_migrations_default.yml

It will not recognize the file info, even though it does so when it gets the info directly from config.yml. The following error is thrown.

[Doctrine\DBAL\Migrations\MigrationException]
Migrations configuration key "doctrine_migrations" does not exists.

If I take the doctrine_migrations key out it will generate an error on the next info it encounters.

like image 779
BernardA Avatar asked Mar 07 '23 19:03

BernardA


1 Answers

Don't import the migrations settings in your config.yml file.

Your individual configuration files aren't actually configured correctly which is why you are receiving the error about the configuration keys not existing. The keys are different than they are in the normal migrations config. I had to search the code to find the right settings. (I found them around ln35 of the AbstractFileConfiguration.php file)

Try these -

doctrine_migrations_default.yml

migrations_directory: "app/DoctrineMigrationsDefault"
migrations_namespace: App\DoctrineMigrationsDefault
table_name: migration_versions
name: Application_Migrations_Default 

doctrine_migrations_used.yml

migrations_directory: "app/DoctrineMigrationsUsed"
migrations_namespace: Used\DoctrineMigrationsUsed
table_name: migration_versions
name: Application Migrations Used
organize_migrations: false #valid entries are false, 'year', and 'year_and_month'

doctrine_migrations, dir_name, and namespace are not valid entries for that configuration file.

Also, you can't use %kernel.root_dir% in your directory path but what worked for me was either changing it to 'app' or providing a full path.

like image 75
beta Avatar answered Mar 15 '23 20:03

beta