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 27, 2024
1 parent 8f47713 commit d7c4b0c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
if (class_exists('Resque_Event') && class_exists('SSResqueRun')) {
Resque_Event::listen('beforeFork', function ($data) {
$databaseConfig = DB::getConfig();
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);

// Reconnect to the database - this may connect to the old DB first, but is required because these processes
// are long-lived, and MySQL connections often get closed in between worker runs. We need to connect before
Expand Down
25 changes: 19 additions & 6 deletions src/TestSessionEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function applyState($state)
$this->extend('onBeforeApplyState', $state);

// back up source
$databaseConfig = DB::getConfig();
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);
$this->oldDatabaseName = $databaseConfig['database'];

// Load existing state from $this->state into $state, if there is any
Expand Down Expand Up @@ -523,9 +523,9 @@ public function loadFixtureIntoDb($fixtureFile)
public function resetDatabaseName()
{
if ($this->oldDatabaseName) {
$databaseConfig = DB::getConfig();
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);
$databaseConfig['database'] = $this->oldDatabaseName;
DB::setConfig($databaseConfig);
DB::setConfig($databaseConfig, DB::CONN_PRIMARY);

$conn = DB::get_conn();

Expand Down Expand Up @@ -568,7 +568,7 @@ public function connectToDatabase($state = null)
$state = $this->getState();
}

$databaseConfig = DB::getConfig();
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);

if (isset($state->database) && $state->database) {
if (!DB::get_conn()) {
Expand All @@ -579,14 +579,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 @@ -622,4 +622,17 @@ public function waitForPendingRequests($await = 700, $timeout = 10000)

return true;
}

private function connectToDB(array $databaseConfig): void
{
if (method_exists(DB::class, 'hasConfig')) {
// CMS 5.4+ - ensure we connect the primary connection and not a replica
// which can happen if we use the default value of DB::CONN_DYNAMIC
// and there is a replica database configured
DB::connect($databaseConfig, DB::CONN_PRIMARY);
} else {
// CMS 5.3 and below do not support replica connections, use 'default' connection
DB::connect($databaseConfig);
}
}
}

0 comments on commit d7c4b0c

Please sign in to comment.