trait InteractsWithParallelDatabase

Provides per-worker database isolation for parallel testing.

Two-phase approach:

  1. Config rewrite (early, in CreatesApplication): rewrites the default connection's database name to {database}test{token} before defineEnvironment() runs, so custom connections derived from the default connection inherit the correct database name.
  2. Database creation (later, in database traits): ensures the per-worker database exists, creating it on demand if needed.

In-memory SQLite databases are skipped — each worker process gets its own memory space naturally.

Properties

static protected string|null $originalDatabaseName

The original database name before parallel suffixing.

Methods

void
configureParallelDatabaseName(mixed $app)

Rewrite the default connection's database name for parallel testing.

void
ensureParallelDatabaseExists()

Ensure the per-worker database exists, creating it if needed.

bool
shouldManageParallelDatabase(mixed $driver, string $database)

Determine if the database should be managed for parallel testing.

string
parallelTestDatabase(string $database, string $token)

Get the per-worker test database name.

Details

at line 45
protected void configureParallelDatabaseName(mixed $app)

Rewrite the default connection's database name for parallel testing.

Config-only — does not create connections or purge pools. Called early in CreatesApplication (after config is loaded, before defineEnvironment) so that custom connections derived from the default connection inherit the per-worker database name.

No-op when not running in parallel or when using in-memory SQLite.

Parameters

mixed $app

Return Value

void

at line 86
protected void ensureParallelDatabaseExists()

Ensure the per-worker database exists, creating it if needed.

Called from database testing traits (RefreshDatabase, DatabaseMigrations, DatabaseTransactions) after the app is booted and connections are available. The config has already been rewritten by configureParallelDatabaseName().

No-op when not running in parallel or when using in-memory SQLite.

Return Value

void

at line 132
protected bool shouldManageParallelDatabase(mixed $driver, string $database)

Determine if the database should be managed for parallel testing.

Parameters

mixed $driver
string $database

Return Value

bool

at line 159
protected string parallelTestDatabase(string $database, string $token)

Get the per-worker test database name.

Parameters

string $database
string $token

Return Value

string