migrations
migrations copied to clipboard
Using existing dbal connection for db-configuration
Q | A |
---|---|
Version | 3.5.2 |
Support Question
In a Symfony project, I have these two dependencies installed:
"doctrine/dbal": "^3.3",
"doctrine/migrations": "^3.5",
I have several db connections defined in Symfony, one of them is:
db.connection:
class: Doctrine\DBAL\Connection
public: true
factory: Doctrine\DBAL\DriverManager::getConnection
arguments:
$params:
driver: pdo_pgsql
url: '%env(DB_URL)%'
charset: UTF8
To run the migrations in the application I execute: vendor/bin/doctrine-migrations migrate --configuration=doctrine-config.php --db-configuration=doctrine-db.php
The content of doctrine-config.php is:
<?php
declare(strict_types=1);
return [
'table_storage' => [
'table_name' => 'doctrine_migration_versions',
'version_column_name' => 'version',
'version_column_length' => 1024,
'executed_at_column_name' => 'executed_at',
'execution_time_column_name' => 'execution_time'
],
'migrations_paths' => [
'Infrastructure\Doctrine\Migrations'
=> 'src/Infrastructure/Doctrine/Migrations'
],
'all_or_nothing' => true,
'transactional' => true,
'check_database_platform' => true,
'organize_migrations' => 'none',
'connection' => null,
'em' => null,
];
The content of doctrine-db.php is:
<?php
declare(strict_types=1);
use Doctrine\DBAL\DriverManager;
return DriverManager::getConnection([
'diver' => 'pdo_pgsql',
'url' => getenv('DB_URL'),
'charset' => 'UTF8'
]);
I wonder if there is a way to reuse the defined connection db.connection
when running the migration to avoid writing the file doctrine-db.php.
Thanks!
Use DoctrineMigrationsBundle, which integrates the doctrine/migrations library with the Symfony framework, exposing the commands in the Symfony bin/console
CLI with the connection config defined for DoctrineBundle.
This also assumes that you use doctrine/doctrine-bundle
to manage your DBAL connections instead of defining the services yourselves (which will give you additional benefits like the integration with the Symfony profiler)
Hello @stof,
Adding that dependency to the project is not an option at this moment. Is that the only way to reuse an existing connection?
Thank you!
Well, if you want to reuse the connection configured through Symfony, you would have to run the commands through the Symfony console, not through vendor/bin/doctrine-migrations
(which knows nothing about Symfony).
so the alternative is to re-do manually most of the configuration done in DoctrineMigrationsBundle (but then, I'm wondering why you would not use the official integration instead)