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

Added the ability to extend StaticDriver for greater flexibility #291

Closed prohalexey closed 5 months ago

prohalexey commented 5 months ago

Hi! I use this package to run tests, but have some troubles with it.

I have 2 connections to different databases in the production. Master server had one database and local servers that have their own databases and database replicated from master server. Doctrine have 2 connections. For the tests I want to have 1 connection to two local databases without replication. But they have different SHA hash string, and I cannot see transactions that I made with first database on the second connenction.

With this changes I will be able to extend StaticDriver and get hash from only a few parameters, for example:

json_encode([
       'driver'        => $params['driver'],
       'host'          => $params['host'],
       'port'          => $params['port'],
       'user'          => $params['user'],
       'password'      => $params['password'],
       'dbname'        => $params['dbname'],
       'driverOptions' => $params['driverOptions'],
])
dmaicher commented 5 months ago

How exactly does your doctrine dbal config look like for the test environment?

prohalexey commented 5 months ago
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(DATABASE_URL)%'
                server_version: "8.0.31"
                use_savepoints: true
                options:
                    # some TLS settings
            master:
                url: '%env(MASTER_DATABASE_URL)%'
                server_version: "8.0.31"
                use_savepoints: true
                options:
                    # some TLS settings
                replicas:
                    replica1:
                        url: '%env(DATABASE_URL)%'
                mapping_types:
                    enum: string
dmaicher commented 5 months ago

You mentioned

For the tests I want to have 1 connection to two local databases without replication

But that is not what you have configured? You have

So the default and read replica connection are using the exact same database? Interesting setup :thinking:

prohalexey commented 5 months ago

Production: Code reads and writes to the LOCAL DB and MASTER GLOBAL SERVER and read from REPLICA DB FROM MASTER

       ---                    |----MASTER GLOBAL SERVER---|         ---
       |                      |       A data center       |           |

 |--------LOCAL SERVER--------|                          |--------LOCAL SERVER--------|
 | B data center              |                          | C data center              |
 | 1. LOCAL DB                |                          | 1. LOCAL DB                |
 | 2. REPLICA DB FROM MASTER  |                          | 2. REPLICA DB FROM MASTER  |

Test: 2 connections with the same connection params to the 1 database

dmaicher commented 5 months ago

In the end this is related to https://github.com/dmaicher/doctrine-test-bundle/issues/289

I'm not sure how to fix this properly yet. I don't see StaticDriver as an extension point really :confused:

prohalexey commented 5 months ago

Now I wrote a copy of your StaticDriver with changes in param hash and did

class ConnectionIsolationBreakerMiddleware implements Middleware
{
    public function wrap(Driver $driver): Driver
    {
        return new ExtendedStaticDriver($driver);
    }
}

And add into services_test.yaml

    # Removing isolation between default and master connections
    doctrine.logging.middleware.connection_isolation_breaker:
        class: Tests\ConnectionIsolationBreakerMiddleware

And set keep static connection to the true in the bootstrap.php(phpunit)

ExtendedStaticDriver::setKeepStaticConnections(true);
prohalexey commented 5 months ago

But this is not the best way, because I have to rewrite this every time you release :))

dmaicher commented 5 months ago

See https://github.com/dmaicher/doctrine-test-bundle/issues/289#issuecomment-2079076089

I think this should solve such problems

prohalexey commented 5 months ago

@dmaicher Yes, closing this PR.