Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shawnveltman's avatar

parallel testing with multiple databases

Hey all!

Hoping somebody here can point out a simple solution to a problem I've been working with lately.

I have a project that uses multiple database connections, and am having a hard time figuring out how to make it work smoothly while doing parallel testing while using the RefreshDatabase trait.

In the old days with the DatabaseTransactions trait, I know there was a property that allowed us to specify which databases to run transactions on. Does something similar exist for RefreshDatabase? If so, that'd probably solve all my problems, haha.

What I'm doing now is not calling any refresh/transactions trait in the tests that touch the secondary DB, and manually deleting the records in the 2nd DB before starting the test. Obviously this doesn't work too well for me in parallel, because it hasn't created the multiple DBs for the 2nd database.

So I end up with a bunch of failing tests in parallel, which I then run in isolation as they pass.

Certainly not ideal, and would love to hear workflow suggestions from you smarter folks to help me out with this!

Thanks so much

0 likes
2 replies
tisuchi's avatar

@shawnveltman Have you tried the 1using1 method on the RefreshDatabase trait? This method takes an array of database connections as its argument.

For example-


/**
 * @test
 */
public function test_something()
{
    $this->using([
        'primary_db',
        'secondary_db'
    ])->refreshDatabase();
    // ...
}

This will refresh the primary_db and secondary_db connections.

You can also use the connectionsToTransact method to specify which connections should be rolled back at the end of the test. This method takes an array of database connections as its argument.

For example-

use RefreshDatabase;

/**
 * @test
 */
public function test_something()
{
    $this->using([
        'primary_db',
        'secondary_db'
    ])->connectionsToTransact([
        'secondary_db'
    ])->refreshDatabase();
    // ...
}

This will refresh the primary_db and secondary_db connections, and will only roll back the secondary_db connection at the end of the test.

shawnveltman's avatar

Ah, I made a mistake in my initial message... I'm using the LazilyRefreshDatabase trait, not the RefreshDatabase trait.

Sorry for the confusion!

Please or to participate in this conversation.