Your Location is: Home > Php

How to execute a Doctrine migration with another connection in Symfony? (with DoctrineMigrationsBundle 3.0.x)

From: Kathmandu View: 4540 scandel 

Question

I develop a Symfony app, using DoctrineBundle and DoctrineMigrationsBundle. The versions of the doctrine and symfony dependencies are as follow:

composer.json

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": ">=7.2.5",
        ...
        "doctrine/annotations": "^1.0",
        "doctrine/doctrine-bundle": "^2.1",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "^2.7",
        ...
        "symfony/framework-bundle": "5.1.*",
        ...
    },
   ...
}

For security reasons, I want to use two different connections to the database, the default one with a database user which has SELECT,INSERT,UPDATE,DELETE rights on tables used by the application - basically, all rights on data, it is the "application user" - and another one, migrations, with a database user which has rights on structure: CREATE, ALTER, DROP... on tables.

Both connections are defined in configuration:

config/packages/doctrine.yaml:

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(resolve:DATABASE_URL)%'
                driver: 'pdo_pgsql'
                server_version: '11.1'
                charset: UTF8
            migrations:
                url: '%env(resolve:DATABASE_MIGRATIONS_URL)%'
                driver: 'pdo_pgsql'
                server_version: '11.1'
                charset: UTF8

With the previous versions of the bundles, in a Symfony 4.4 application, there were a --db option for the doctrine:migration:migrate command, which allowed to choose the connection to use to execute the migrations:

php bin/console doctrine:migrations:migrate --db=migrations --no-interaction

But, in the 3.0.x version of the bundle, this option has disappeared. There is only the --configuration and --db-configuration options which seem likely to allow to achieve the same, but I can't figure out how to make it work.

What I tried:

  • Added a second entity manager in doctrine.yaml, to use the migrations connection (no mappings - I want to use it just to execute migrations, wich are SQL commands):

doctrine.yaml:

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(resolve:DATABASE_URL)%'
                driver: 'pdo_pgsql'
                charset: UTF8
            # Used to execute database migrations - conection with the owner user
            migrations:
                url: '%env(resolve:DATABASE_MIGRATIONS_URL)%'
                driver: 'pdo_pgsql'
                charset: UTF8

    orm:
        auto_generate_proxy_classes: true

        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                auto_mapping: true
            # Used to create and execute database migrations
            migrations:
                connection: migrations
  • I kept the defaut config/packages/doctrine_migrations.yaml, which uses the default entity manager, and added a second file:

doctrine_migrations_db_owner.yaml

doctrine_migrations:
    migrations_paths:
        'DoctrineMigrations': '%kernel.project_dir%/migrations'
    em: migrations
  • Launched the migrations with this file:
php bin/console doctrine:migrations:migrate --configuration=./config/packages/doctrine_migrations_db_owner.yaml

But, I have an error about the doctrine_migrations key in the yaml file... I tried to remove it, but then another error on the migrations_paths...

Does anyone can provide hints on how to use this option, or another solution, to achieve what I want? Thanks.

Best answer