From 5d14e87748af4d3bddd9a20846baed407e58d400 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 25 Sep 2024 11:03:19 +1200 Subject: [PATCH] FIX Ensure primary connection is updated with temporary database --- src/TestSessionEnvironment.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/TestSessionEnvironment.php b/src/TestSessionEnvironment.php index dcd1405..9418086 100644 --- a/src/TestSessionEnvironment.php +++ b/src/TestSessionEnvironment.php @@ -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); } } } @@ -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); + } + } }