Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using existing dbal connection for db-configuration #1324

Open
Fahani opened this issue Feb 17, 2023 · 4 comments
Open

Using existing dbal connection for db-configuration #1324

Fahani opened this issue Feb 17, 2023 · 4 comments

Comments

@Fahani
Copy link

Fahani commented Feb 17, 2023

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!

@stof
Copy link
Member

stof commented Feb 17, 2023

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.

@stof
Copy link
Member

stof commented Feb 17, 2023

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)

@Fahani
Copy link
Author

Fahani commented Feb 17, 2023

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!

@stof
Copy link
Member

stof commented Feb 17, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants