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

I ended with a solution, wich is, in my opinion, even cleaner that what I was doing with the previous version of the Doctrine migrations Bundle.

In the application, I use only the default database url (defined as an environment variable, or in .env):

# config/packages/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        driver: 'pdo_pgsql'
        charset: UTF8
# config/packages/doctrine_migrations.yaml
doctrine_migrations:
    migrations_paths:
        # namespace is arbitrary but should be different from App\Migrations
        # as migrations classes should NOT be autoloaded
        'DoctrineMigrations': '%kernel.project_dir%/migrations'

And, when I want to execute a migration with a privileged database user, defined in a DATABASE_MIGRATIONS_URL variable, I pass it as env var directly in the command:

DATABASE_URL="$DATABASE_MIGRATIONS_URL" php bin/console doctrine:migrations:migrate

The variable is defined as follow:

DATABASE_MIGRATIONS_URL="postgresql://$DB_OWNER:[email protected]$DB_HOST:5432/$DB_NAME?serverVersion=$PG_VERSION

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top