Skip to content

Commit

Permalink
FIX Ensure primary connection is updated with temporary database
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Sep 24, 2024
1 parent 097313b commit 5d14e87
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/TestSessionEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,14 @@ public function connectToDatabase($state = null)
}

// Connect to database
DB::connect($databaseConfig);
$this->connectToDB($databaseConfig);
} else {
// We've already connected to the database, do a fast check to see what database we're currently using
$db = DB::get_conn()->getSelectedDatabase();
if (isset($state->database) && $db != $state->database) {
$this->oldDatabaseName = $databaseConfig['database'];
$databaseConfig['database'] = $state->database;
DB::connect($databaseConfig);
$this->connectToDB($databaseConfig);
}
}
}
Expand Down Expand Up @@ -613,4 +613,15 @@ public function waitForPendingRequests($await = 700, $timeout = 10000)

return true;
}

private function connectToDB(array $databaseConfig): void
{
if (method_exists(DB::class, 'hasConfig') && DB::hasConfig('primary')) {
// CMS 5.4+ - ensure we connect the primary connection and not a replica
DB::connect($databaseConfig, 'primary');
} else {
// CMS 5.3 and below do not support replica connections
DB::connect($databaseConfig);
}
}
}

0 comments on commit 5d14e87

Please sign in to comment.