dmaicher / doctrine-test-bundle

Symfony bundle to isolate your app's doctrine database tests and improve the test performance
MIT License
1.08k stars 61 forks source link

How to work with dynamic Connections created at runtime? #278

Closed Lehren closed 9 months ago

Lehren commented 9 months ago

Hi, in my app I create connections pointing to different databases based on which account is currently logged in. So for instance account with ID 1 would get a connection built to point to database_1 for instance, and account 2 to database_2 and so on. I'm doing this with the DBAL Connection Factory service, but in your bundle you decorate this service to allow for rollbacks, which is great, but because my connection params do not contain dama.keep_static it will not return a connection that supports the rollback in my tests due to this line in StaticConnectionFactory

if (!StaticDriver::isKeepStaticConnections() || !isset($params['dama.keep_static']) || !$params['dama.keep_static']) {
            return $connectionOriginalDriver;
}

How should I approach this? Do you have a recommended way? Or should I just hardcode the dama.keep_static parameter into the connection if I'm running PHPUnit?

dmaicher commented 9 months ago

Hm interesting use-case. Indeed the only way would be to manually add the dama.keep_static parameter during runtime :confused:

I need to think about if this implementation detail is something that should be part of the public API though. Currently I would not consider this to be covered by any kind of backwards compatibility promises.

Also make sure to use the latest version of the bundle though. The decorated connection factory service was removed for v8.

dmaicher commented 9 months ago

Ah actually I think the parameter dama.keep_static is not enough, or? There is also a DBAL middleware registered for the connections. You have to make sure to also pass the correct configuration (with the middlware) to ConnectionFactory::createConnection.

Lehren commented 9 months ago

Hm interesting use-case. Indeed the only way would be to manually add the dama.keep_static parameter during runtime 😕

I need to think about if this implementation detail is something that should be part of the public API though. Currently I would not consider this to be covered by any kind of backwards compatibility promises.

Also make sure to use the latest version of the bundle though. The decorated connection factory service was removed for v8.

Ah yes, well I'm still on Symfony 4.4 for the time being so I'm using a bit outdated version. Adding the one parameters does the job, I can get the right configuration from the doctrine config since that will remain the same for all connections I make.

dmaicher commented 9 months ago

Closing then for the time being as we have a workaround