Skip to content

Commit d79a83e

Browse files
committed
Tweaks the RefreshDatabase trait for better support of multi-dbs
When working with multiple databases and some of them are in-memory while others are regular disk-based databases, we usually have to override some of the traits methods to make it work. Also, the database restoration process doesn't handle multiple DBs that well. With these changes, when an app needs to support multiple databases, the user will have to do a few steps: 1. Set the `$connectionsToTransact` protected property on the TestCase listing all connections they use 1. Override the `migrateDatabase` method and call all the migrate:fresh commands for all their connections Restoring the databases between tests should now be completely handled by the trait, no matter which database connection uses in-memory databases or not.
1 parent 3e9ef5a commit d79a83e

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/Illuminate/Foundation/Testing/RefreshDatabase.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function refreshDatabase()
1818
{
1919
$this->beforeRefreshingDatabase();
2020

21-
if ($this->usingInMemoryDatabase()) {
21+
if ($this->usingInMemoryDatabases()) {
2222
$this->restoreInMemoryDatabase();
2323
}
2424

@@ -32,11 +32,23 @@ public function refreshDatabase()
3232
*
3333
* @return bool
3434
*/
35-
protected function usingInMemoryDatabase()
35+
protected function usingInMemoryDatabase(string $name)
3636
{
37-
$default = config('database.default');
37+
return config("database.connections.{$name}.database") === ':memory:';
38+
}
3839

39-
return config("database.connections.$default.database") === ':memory:';
40+
/**
41+
* Determines if any of the connections transacting is using in-memory databases.
42+
*/
43+
protected function usingInMemoryDatabases(): bool
44+
{
45+
foreach ($this->connectionsToTransact() as $name) {
46+
if (config("database.connections.{$name}.database") === ':memory:') {
47+
return true;
48+
}
49+
}
50+
51+
return false;
4052
}
4153

4254
/**
@@ -63,7 +75,7 @@ protected function restoreInMemoryDatabase()
6375
protected function refreshTestDatabase()
6476
{
6577
if (! RefreshDatabaseState::$migrated) {
66-
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
78+
$this->migrateDatabases();
6779

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

@@ -89,7 +101,7 @@ public function beginDatabaseTransaction()
89101

90102
$connection->setTransactionManager($transactionsManager);
91103

92-
if ($this->usingInMemoryDatabase()) {
104+
if ($this->usingInMemoryDatabase($name)) {
93105
RefreshDatabaseState::$inMemoryConnections[$name] ??= $connection->getPdo();
94106
}
95107

@@ -121,7 +133,15 @@ public function beginDatabaseTransaction()
121133
protected function connectionsToTransact()
122134
{
123135
return property_exists($this, 'connectionsToTransact')
124-
? $this->connectionsToTransact : [null];
136+
? $this->connectionsToTransact : [config('database.default')];
137+
}
138+
139+
/**
140+
* Perform the database migration command.
141+
*/
142+
protected function migrateDatabases(): void
143+
{
144+
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
125145
}
126146

127147
/**

0 commit comments

Comments
 (0)