Skip to content

Commit a48c542

Browse files
tonysmtaylorotwell
andauthored
[12.x] Better support for multi-dbs in the RefreshDatabase trait (#53231)
* 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. * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0269401 commit a48c542

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/Illuminate/Foundation/Testing/RefreshDatabase.php

Lines changed: 32 additions & 8 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

@@ -28,15 +28,29 @@ public function refreshDatabase()
2828
}
2929

3030
/**
31-
* Determine if an in-memory database is being used.
31+
* Determine if any of the connections transacting is using in-memory databases.
3232
*
3333
* @return bool
3434
*/
35-
protected function usingInMemoryDatabase()
35+
protected function usingInMemoryDatabases()
3636
{
37-
$default = config('database.default');
37+
foreach ($this->connectionsToTransact() as $name) {
38+
if ($this->usingInMemoryDatabase($name)) {
39+
return true;
40+
}
41+
}
3842

39-
return config("database.connections.$default.database") === ':memory:';
43+
return false;
44+
}
45+
46+
/**
47+
* Determine if a given database connection is an in-memory database.
48+
*
49+
* @return bool
50+
*/
51+
protected function usingInMemoryDatabase(string $name)
52+
{
53+
return config("database.connections.{$name}.database") === ':memory:';
4054
}
4155

4256
/**
@@ -63,7 +77,7 @@ protected function restoreInMemoryDatabase()
6377
protected function refreshTestDatabase()
6478
{
6579
if (! RefreshDatabaseState::$migrated) {
66-
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
80+
$this->migrateDatabases();
6781

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

@@ -73,6 +87,16 @@ protected function refreshTestDatabase()
7387
$this->beginDatabaseTransaction();
7488
}
7589

90+
/**
91+
* Migrate the database.
92+
*
93+
* @return void
94+
*/
95+
protected function migrateDatabases()
96+
{
97+
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
98+
}
99+
76100
/**
77101
* Begin a database transaction on the testing database.
78102
*
@@ -89,7 +113,7 @@ public function beginDatabaseTransaction()
89113

90114
$connection->setTransactionManager($transactionsManager);
91115

92-
if ($this->usingInMemoryDatabase()) {
116+
if ($this->usingInMemoryDatabase($name)) {
93117
RefreshDatabaseState::$inMemoryConnections[$name] ??= $connection->getPdo();
94118
}
95119

@@ -121,7 +145,7 @@ public function beginDatabaseTransaction()
121145
protected function connectionsToTransact()
122146
{
123147
return property_exists($this, 'connectionsToTransact')
124-
? $this->connectionsToTransact : [null];
148+
? $this->connectionsToTransact : [config('database.default')];
125149
}
126150

127151
/**

0 commit comments

Comments
 (0)