More work on getting SQLite to work with unit tests. DB Prefix changing is now static...
authorX! <soxred93@users.mediawiki.org>
Fri, 31 Dec 2010 20:42:39 +0000 (20:42 +0000)
committerX! <soxred93@users.mediawiki.org>
Fri, 31 Dec 2010 20:42:39 +0000 (20:42 +0000)
includes/db/CloneDatabase.php
includes/db/DatabaseSqlite.php
tests/phpunit/MediaWikiTestCase.php

index 9e1f05c..a553c9b 100644 (file)
@@ -91,14 +91,15 @@ class CloneDatabase {
                        # works correctly across DB engines, we need to change the pre-
                        # fix back and forth so tableName() works right.
                        
-                       $this->changePrefix( $this->oldTablePrefix );
+                       self::changePrefix( $this->oldTablePrefix );
                        $oldTableName = $this->db->tableName( $tbl );
                        
-                       $this->changePrefix( $this->newTablePrefix );
+                       self::changePrefix( $this->newTablePrefix );
                        $newTableName = $this->db->tableName( $tbl );
 
                        if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres' ) ) ) {
                                $this->db->dropTable( $tbl, __METHOD__ );
+                               wfDebug( "Dropping {$this->newTablePrefix}{$oldTableName}\n", __METHOD__ );
                                //Dropping the oldTable because the prefix was changed
                        }
 
@@ -116,12 +117,12 @@ class CloneDatabase {
         */
        public function destroy( $dropTables = false ) {
                if( $dropTables ) {
-                       $this->changePrefix( $this->newTablePrefix );
+                       self::changePrefix( $this->newTablePrefix );
                        foreach( $this->tablesToClone as $tbl ) {
                                $this->db->dropTable( $tbl );
                        }
                }
-               $this->changePrefix( $this->oldTablePrefix );
+               self::changePrefix( $this->oldTablePrefix );
        }
 
        /**
@@ -130,9 +131,9 @@ class CloneDatabase {
         * @param  $prefix
         * @return void
         */
-       protected function changePrefix( $prefix ) {
+       public static function changePrefix( $prefix ) {
                global $wgDBprefix;
-               wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
+               wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
                $wgDBprefix = $prefix;
        }
 
@@ -141,8 +142,8 @@ class CloneDatabase {
         * @param  $prefix
         * @return void
         */
-       public function changeLBPrefix( $lb, $prefix ) {
-               $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
+       public static function changeLBPrefix( $lb, $prefix ) {
+               $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
        }
 
        /**
@@ -150,7 +151,7 @@ class CloneDatabase {
         * @param  $prefix
         * @return void
         */
-       public function changeDBPrefix( $db, $prefix ) {
+       public static function changeDBPrefix( $db, $prefix ) {
                $db->tablePrefix( $prefix );
        }
 }
index 1058fd8..1842035 100644 (file)
@@ -595,6 +595,7 @@ class DatabaseSqlite extends DatabaseBase {
        }
 
        function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseSqlite::duplicateTableStructure' ) {
+       
                $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name='$oldName' AND type='table'", $fname );
                $obj = $this->fetchObject( $res );
                if ( !$obj ) {
index 8653353..25767be 100644 (file)
@@ -5,7 +5,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        public $regex = '';
        public $runDisabled = false;
        
-       protected static $databaseSetupDone = false;
        protected $db;
        protected $dbClone;
        protected $oldTablePrefix;
@@ -24,7 +23,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                
                if( $this->needsDB() ) {
                        
-                       $this->destroyDBCheck();
+                       $this->db = wfGetDB( DB_MASTER );
+                       
+                       $this->destroyDB();
                        
                        $this->initDB();
                        $this->addCoreDBData();
@@ -35,13 +36,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        }
        
        function __destruct() {
-               $this->destroyDBCheck();
-       }
-
-       function destroyDBCheck() {
-               if( is_object( $this->dbClone ) && $this->dbClone instanceof CloneDatabase ) {
-                       $this->destroyDB();
-               }
+               $this->destroyDB();
        }
        
        function needsDB() {
@@ -78,37 +73,19 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        private function initDB() {
                global $wgDBprefix;
 
-               if ( self::$databaseSetupDone ) {
-                       return;
-               }
-
-               $this->db = wfGetDB( DB_MASTER );
                $dbType = $this->db->getType();
                
                if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
                        throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
                }
 
-               self::$databaseSetupDone = true;
                $this->oldTablePrefix = $wgDBprefix;
 
-               # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
-               # It seems to have been fixed since (r55079?).
-               # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
-
-               # CREATE TEMPORARY TABLE breaks if there is more than one server
-               if ( wfGetLB()->getServerCount() != 1 ) {
-                       $this->useTemporaryTables = false;
-               }
-
-               $temporary = $this->useTemporaryTables || $dbType == 'postgres';
-               
                $tables = $this->listTables();
                
                $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
 
                $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
-               $this->dbClone->useTemporaryTables( $temporary );
                $this->dbClone->cloneTableStructure();
 
                if ( $dbType == 'oracle' )
@@ -126,13 +103,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        }
        
        protected function destroyDB() {
-               if ( !self::$databaseSetupDone ) {
-                       return;
-               }
+               global $wgDBprefix;
                
-               $this->dbClone->destroy();
-               self::$databaseSetupDone = false;
-
                if ( $this->useTemporaryTables ) {
                        # Don't need to do anything
                        //return;
@@ -147,14 +119,16 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
                
                foreach ( $tables as $table ) {
-                       $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
-                       $this->db->query( $sql );
+                       if( $this->db->tableExists( "`$table`" ) ) {
+                               $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
+                               $this->db->query( $sql, __METHOD__ );
+                       }
                }
-
-               if ( $this->db->getType() == 'oracle' )
-                       $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
                
+               if ( $this->db->getType() == 'oracle' )
+                       $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
                
+               CloneDatabase::changePrefix( $this->oldTablePrefix );
        }
 
        function __call( $func, $args ) {