From 3a9a451e8c61dfeb460213cff15a50687acffd63 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Tue, 28 Dec 2010 01:19:16 +0000 Subject: [PATCH] Followup r79093: Move DROP TABLE code to Database classes and properly rename back to original table prefix in destroyDatabase() --- includes/db/CloneDatabase.php | 25 +++++++++++++++---------- includes/db/Database.php | 14 ++++++++++++++ includes/db/DatabaseMysql.php | 7 +++++++ tests/parser/parserTest.inc | 8 +++++++- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/includes/db/CloneDatabase.php b/includes/db/CloneDatabase.php index c0fcebc635..11c95f9f6a 100644 --- a/includes/db/CloneDatabase.php +++ b/includes/db/CloneDatabase.php @@ -95,16 +95,7 @@ class CloneDatabase { $newTableName = $this->db->tableName( $tbl ); if( $this->dropCurrentTables ) { - if ( $this->db->getType() == 'mysql' && $this->db->tableExists( $tbl ) ) { - $this->db->query( "DROP TABLE IF EXISTS $newTableName" ); - } elseif ( in_array( $this->db->getType(), array( 'postgres', 'oracle' ) ) ) { - /* DROPs wouldn't work due to Foreign Key Constraints (bug 14990, r58669) - * Use "DROP TABLE IF EXISTS $newTableName CASCADE" for postgres? That - * syntax would also work for mysql. - */ - } elseif ( $this->db->tableExists( $tbl ) ) { - $this->db->query( "DROP TABLE $newTableName" ); - } + $this->db->dropTable( $newTableName, __METHOD__ ); } # Create new table @@ -112,6 +103,20 @@ class CloneDatabase { } } + /** + * Change the prefix back to the original. + * @param $dropTables bool Optionally drop the tables we created + */ + public function destroy( $dropTables = false ) { + if( $dropTables ) { + $this->changePrefix( $this->newTablePrefix ); + foreach( $this->tablesToClone as $tbl ) { + $this->db->dropTable( $tbl ); + } + } + $this->changePrefix( $this->oldTablePrefix ); + } + /** * Change the table prefix on all open DB connections/ */ diff --git a/includes/db/Database.php b/includes/db/Database.php index 24596eb4ee..e502796530 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -2639,6 +2639,20 @@ abstract class DatabaseBase implements DatabaseType { return true; } + /** + * Delete a table + */ + public function dropTable( $tableName, $method = 'DatabaseBase::dropTable' ) { + if( !$this->tableExists( $tableName, $method ) ) { + return false; + } + $sql = "DROP TABLE " . $this->tableName( $tableName ); + if( $this->cascadingDeletes() ) { + $sql .= " CASCADE"; + } + return $this->query( $sql ); + } + /** * Get search engine class. All subclasses of this need to implement this * if they wish to use searching. diff --git a/includes/db/DatabaseMysql.php b/includes/db/DatabaseMysql.php index 2b2c96938d..24e522f1a4 100644 --- a/includes/db/DatabaseMysql.php +++ b/includes/db/DatabaseMysql.php @@ -531,6 +531,13 @@ class DatabaseMysql extends DatabaseBase { $this->query( $query, $fname ); } + public function dropTable( $tableName, $fName = 'DatabaseMysql::dropTable' ) { + if( !$this->tableExists( $tableName ) ) { + return false; + } + return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName ); + } + } /** diff --git a/tests/parser/parserTest.inc b/tests/parser/parserTest.inc index a97f2c5578..994dc4b5b1 100644 --- a/tests/parser/parserTest.inc +++ b/tests/parser/parserTest.inc @@ -53,6 +53,12 @@ class ParserTest { */ private $db; + /** + * Database clone helper + * @var CloneDatabase + */ + private $dbClone; + /** * string $oldTablePrefix Original table prefix */ @@ -852,7 +858,7 @@ class ParserTest { } $this->teardownUploadDir( $this->uploadDir ); - $this->db->tablePrefix( $this->oldTablePrefix ); + $this->dbClone->destroy(); $this->databaseSetupDone = false; if ( $this->useTemporaryTables ) { -- 2.20.1