doctrine-test-bundle icon indicating copy to clipboard operation
doctrine-test-bundle copied to clipboard

Added the ability to extend StaticDriver for greater flexibility

Open prohalexey opened this issue 1 year ago • 8 comments

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'],
])

prohalexey avatar Apr 25 '24 10:04 prohalexey

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

dmaicher avatar Apr 25 '24 11:04 dmaicher

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

prohalexey avatar Apr 25 '24 11:04 prohalexey

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

  • default with env(DATABASE_URL)
  • master with primay env(MASTER_DATABASE_URL) and replica env(DATABASE_URL)

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

dmaicher avatar Apr 25 '24 11:04 dmaicher

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

prohalexey avatar Apr 25 '24 11:04 prohalexey

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:

dmaicher avatar Apr 25 '24 11:04 dmaicher

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 avatar Apr 25 '24 11:04 prohalexey

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

prohalexey avatar Apr 25 '24 11:04 prohalexey

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

I think this should solve such problems

dmaicher avatar Apr 30 '24 07:04 dmaicher

@dmaicher Yes, closing this PR.

prohalexey avatar May 07 '24 08:05 prohalexey