RefreshDatabase
trait RefreshDatabase
Traits
Provides per-worker database isolation for parallel testing.
Properties
| static protected string|null | $originalDatabaseName | The original database name before parallel suffixing. |
from InteractsWithParallelDatabase |
Methods
The parameters that should be used when running "migrate:fresh".
Determine if views should be dropped when refreshing the database.
Determine if types should be dropped when refreshing the database.
Determine if the seed task should be run when refreshing the database.
Determine the specific seeder class that should be used when refreshing the database.
Rewrite the default connection's database name for parallel testing.
Ensure the per-worker database exists, creating it if needed.
Determine if the database should be managed for parallel testing.
Get the per-worker test database name.
Define hooks to migrate the database before and after each test.
Refresh the model booted states.
Restore the in-memory database between tests.
Determine if any of the connections transacting is using in-memory databases.
Determine if a given database connection is an in-memory database.
Refresh a conventional test database.
Start database transaction in the test coroutine.
Rollback database transaction in the test coroutine.
Start transactions on all connections.
Rollback transactions on all connections.
The database connections that should have transactions.
Perform any work that should take place before the database has started refreshing.
Perform any work that should take place once the database has finished refreshing.
No description
Details
in
CanConfigureMigrationCommands at line 16
protected array
migrateFreshUsing()
The parameters that should be used when running "migrate:fresh".
in
CanConfigureMigrationCommands at line 32
protected bool
shouldDropViews()
Determine if views should be dropped when refreshing the database.
in
CanConfigureMigrationCommands at line 40
protected bool
shouldDropTypes()
Determine if types should be dropped when refreshing the database.
in
CanConfigureMigrationCommands at line 48
protected bool
shouldSeed()
Determine if the seed task should be run when refreshing the database.
in
CanConfigureMigrationCommands at line 64
protected mixed
seeder()
Determine the specific seeder class that should be used when refreshing the database.
in
InteractsWithParallelDatabase 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.
in
InteractsWithParallelDatabase 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.
in
InteractsWithParallelDatabase at line 132
protected bool
shouldManageParallelDatabase(mixed $driver, string $database)
Determine if the database should be managed for parallel testing.
in
InteractsWithParallelDatabase at line 159
protected string
parallelTestDatabase(string $database, string $token)
Get the per-worker test database name.
at line 26
void
refreshDatabase()
Define hooks to migrate the database before and after each test.
For tests using RunTestsInCoroutine, the transaction and post-refresh hooks are deferred to setUpRefreshDatabaseInCoroutine() to keep all transaction state in the same coroutine.
at line 52
protected void
refreshModelBootedStates()
Refresh the model booted states.
at line 60
protected void
restoreInMemoryDatabase()
Restore the in-memory database between tests.
at line 76
protected bool
usingInMemoryDatabases()
Determine if any of the connections transacting is using in-memory databases.
at line 90
protected bool
usingInMemoryDatabase(string|null $name = null)
Determine if a given database connection is an in-memory database.
at line 108
protected void
refreshTestDatabase()
Refresh a conventional test database.
Runs migrations if needed. For non-coroutine tests, also starts the wrapper transaction. For coroutine tests, transaction handling is deferred to setUpRefreshDatabaseInCoroutine().
at line 151
protected void
setUpRefreshDatabaseInCoroutine()
Start database transaction in the test coroutine.
Called by RunTestsInCoroutine before the test runs. Maintains correct ordering: transaction starts, then afterRefreshingDatabase runs, then test executes. This keeps all transaction state in the same coroutine.
Note: restoreInMemoryDatabase() runs earlier in refreshDatabase() before migrations, which is the correct ordering for in-memory SQLite.
at line 163
protected void
tearDownRefreshDatabaseInCoroutine()
Rollback database transaction in the test coroutine.
Called by RunTestsInCoroutine after the test runs.
at line 171
protected void
beginDatabaseTransactionWork()
Start transactions on all connections.
at line 215
protected void
rollbackDatabaseTransactionWork()
Rollback transactions on all connections.
at line 244
protected array
connectionsToTransact()
The database connections that should have transactions.
at line 253
protected void
beforeRefreshingDatabase()
Perform any work that should take place before the database has started refreshing.
at line 261
protected void
afterRefreshingDatabase()
Perform any work that should take place once the database has finished refreshing.
at line 266
protected string
getRefreshConnection()
No description