Followup r79093: Move DROP TABLE code to Database classes and properly rename back...
authorChad Horohoe <demon@users.mediawiki.org>
Tue, 28 Dec 2010 01:19:16 +0000 (01:19 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Tue, 28 Dec 2010 01:19:16 +0000 (01:19 +0000)
includes/db/CloneDatabase.php
includes/db/Database.php
includes/db/DatabaseMysql.php
tests/parser/parserTest.inc

index c0fcebc..11c95f9 100644 (file)
@@ -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/
         */
index 24596eb..e502796 100644 (file)
@@ -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.
index 2b2c969..24e522f 100644 (file)
@@ -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 );
+       }
+
 }
 
 /**
index a97f2c5..994dc4b 100644 (file)
@@ -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 ) {