From f65bef86ed47d07f54c07bd3427bb1653e8677a3 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 13 Aug 2013 13:58:50 +0200 Subject: [PATCH] Set temp database on current request Was relying on cookie to set on NEXT request, which was too late since some of the following init logic relied on DB queries. This happened to work if your non-test DB was already set up, but failed on fresh checkouts. --- code/TestSessionController.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/code/TestSessionController.php b/code/TestSessionController.php index c370741..2a18ba0 100644 --- a/code/TestSessionController.php +++ b/code/TestSessionController.php @@ -35,9 +35,10 @@ class TestSessionController extends Controller { if(SapphireTest::using_temp_db()) return $this->renderWith('TestSession_inprogress'); // Database - if($request->getVar('database')) { + $dbName = $request->getVar('database'); + if($dbName) { $dbExists = (bool)DB::query( - sprintf("SHOW DATABASES LIKE '%s'", Convert::raw2sql($request->getVar('database'))) + sprintf("SHOW DATABASES LIKE '%s'", Convert::raw2sql($dbName)) )->value(); } else { $dbExists = false; @@ -45,13 +46,10 @@ class TestSessionController extends Controller { if(!$dbExists) { // Create a new one with a randomized name - $dbname = SapphireTest::create_temp_db(); - DB::set_alternative_database_name($dbname); - // Workaround for bug in Cookie::get(), fixed in 3.1-rc1 - self::$alternative_database_name = $dbname; + $dbName = SapphireTest::create_temp_db(); } - $this->setState($request->getVars()); + $this->setState(array_merge($request->getVars(), array('database' => $dbName))); return $this->renderWith('TestSession_start'); } @@ -153,6 +151,10 @@ class TestSessionController extends Controller { DB::set_alternative_database_name($dbname); // Workaround for bug in Cookie::get(), fixed in 3.1-rc1 self::$alternative_database_name = $dbname; + + // Database name is set in cookie (next request), ensure its available on this request already + global $databaseConfig; + DB::connect(array_merge($databaseConfig, array('database' => $dbname))); } // Fixtures