Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Better support for multi-dbs in the RefreshDatabase trait #53231

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/Illuminate/Foundation/Testing/RefreshDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function refreshDatabase()
{
$this->beforeRefreshingDatabase();

if ($this->usingInMemoryDatabase()) {
if ($this->usingInMemoryDatabases()) {
$this->restoreInMemoryDatabase();
}

Expand All @@ -28,15 +28,29 @@ public function refreshDatabase()
}

/**
* Determine if an in-memory database is being used.
* Determine if any of the connections transacting is using in-memory databases.
*
* @return bool
*/
protected function usingInMemoryDatabase()
protected function usingInMemoryDatabases()
{
$default = config('database.default');
foreach ($this->connectionsToTransact() as $name) {
if ($this->usingInMemoryDatabase($name)) {
return true;
}
}

return config("database.connections.$default.database") === ':memory:';
return false;
}

/**
* Determine if a given database connection is an in-memory database.
*
* @return bool
*/
protected function usingInMemoryDatabase(string $name)
{
return config("database.connections.{$name}.database") === ':memory:';
}

/**
Expand All @@ -63,7 +77,7 @@ protected function restoreInMemoryDatabase()
protected function refreshTestDatabase()
{
if (! RefreshDatabaseState::$migrated) {
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
$this->migrateDatabases();

$this->app[Kernel::class]->setArtisan(null);

Expand All @@ -73,6 +87,16 @@ protected function refreshTestDatabase()
$this->beginDatabaseTransaction();
}

/**
* Migrate the database.
*
* @return void
*/
protected function migrateDatabases()
{
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
}

/**
* Begin a database transaction on the testing database.
*
Expand All @@ -89,7 +113,7 @@ public function beginDatabaseTransaction()

$connection->setTransactionManager($transactionsManager);

if ($this->usingInMemoryDatabase()) {
if ($this->usingInMemoryDatabase($name)) {
RefreshDatabaseState::$inMemoryConnections[$name] ??= $connection->getPdo();
}

Expand Down Expand Up @@ -121,7 +145,7 @@ public function beginDatabaseTransaction()
protected function connectionsToTransact()
{
return property_exists($this, 'connectionsToTransact')
? $this->connectionsToTransact : [null];
? $this->connectionsToTransact : [config('database.default')];
}

/**
Expand Down