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.
This commit is contained in:
Ingo Schommer 2013-08-13 13:58:50 +02:00
parent 9e2bcbcde7
commit f65bef86ed

View File

@ -35,9 +35,10 @@ class TestSessionController extends Controller {
if(SapphireTest::using_temp_db()) return $this->renderWith('TestSession_inprogress'); if(SapphireTest::using_temp_db()) return $this->renderWith('TestSession_inprogress');
// Database // Database
if($request->getVar('database')) { $dbName = $request->getVar('database');
if($dbName) {
$dbExists = (bool)DB::query( $dbExists = (bool)DB::query(
sprintf("SHOW DATABASES LIKE '%s'", Convert::raw2sql($request->getVar('database'))) sprintf("SHOW DATABASES LIKE '%s'", Convert::raw2sql($dbName))
)->value(); )->value();
} else { } else {
$dbExists = false; $dbExists = false;
@ -45,13 +46,10 @@ class TestSessionController extends Controller {
if(!$dbExists) { if(!$dbExists) {
// Create a new one with a randomized name // Create a new one with a randomized name
$dbname = SapphireTest::create_temp_db(); $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;
} }
$this->setState($request->getVars()); $this->setState(array_merge($request->getVars(), array('database' => $dbName)));
return $this->renderWith('TestSession_start'); return $this->renderWith('TestSession_start');
} }
@ -153,6 +151,10 @@ class TestSessionController extends Controller {
DB::set_alternative_database_name($dbname); DB::set_alternative_database_name($dbname);
// Workaround for bug in Cookie::get(), fixed in 3.1-rc1 // Workaround for bug in Cookie::get(), fixed in 3.1-rc1
self::$alternative_database_name = $dbname; 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 // Fixtures