From a581e748d9f985aa5f5263652de7135c28894029 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 4 Mar 2009 07:31:23 +0000 Subject: [PATCH] ENHANCEMENT: Added dev/tests/cleanupdb action to remove test databases git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@72463 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/MySQLDatabase.php | 17 ++++++++++++++++- dev/SapphireTest.php | 10 ++++++++++ dev/TestRunner.php | 5 +++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/core/model/MySQLDatabase.php b/core/model/MySQLDatabase.php index c9f1c07f9..d06957b55 100644 --- a/core/model/MySQLDatabase.php +++ b/core/model/MySQLDatabase.php @@ -142,7 +142,15 @@ class MySQLDatabase extends Database { * Use with caution. */ public function dropDatabase() { - $this->query("DROP DATABASE $this->database"); + $this->dropDatabaseByName($this->database); + } + + /** + * Drop the database that this object is currently connected to. + * Use with caution. + */ + public function dropDatabaseByName($dbName) { + $this->query("DROP DATABASE \"$dbName\""); } /** @@ -171,6 +179,13 @@ class MySQLDatabase extends Database { $SQL_name = Convert::raw2sql($name); return $this->query("SHOW DATABASES LIKE '$SQL_name'")->value() ? true : false; } + + /** + * Returns a column + */ + public function allDatabaseNames() { + return $this->query("SHOW DATABASES")->column(); + } public function createTable($tableName, $fields = null, $indexes = null) { $fieldSchemas = $indexSchemas = ""; diff --git a/dev/SapphireTest.php b/dev/SapphireTest.php index 1dc66de96..e533648ee 100644 --- a/dev/SapphireTest.php +++ b/dev/SapphireTest.php @@ -240,6 +240,16 @@ class SapphireTest extends PHPUnit_Framework_TestCase { return $dbname; } + + static function delete_all_temp_dbs() { + foreach(DB::getConn()->allDatabaseNames() as $dbName) { + if(preg_match('/^tmpdb[0-9]+$/', $dbName)) { + DB::getConn()->dropDatabaseByName($dbName); + echo "
  • Dropped databse \"$dbName\"\n"; + flush(); + } + } + } } ?> diff --git a/dev/TestRunner.php b/dev/TestRunner.php index 8ae7d6082..656d062db 100644 --- a/dev/TestRunner.php +++ b/dev/TestRunner.php @@ -36,6 +36,7 @@ class TestRunner extends Controller { 'coverage' => 'coverage', 'startsession' => 'startsession', 'endsession' => 'endsession', + 'cleanupdb' => 'cleanupdb', '$TestCase' => 'only', ); @@ -116,6 +117,10 @@ class TestRunner extends Controller { echo "Please install PHPUnit using pear"; } } + + function cleanupdb() { + SapphireTest::delete_all_temp_dbs(); + } /** * Run only a single test class