From 5933586266e5530d491cf0e518abfe0ed56e1c0a Mon Sep 17 00:00:00 2001 From: Jure Kajzer Date: Thu, 10 Nov 2011 13:29:32 +0000 Subject: [PATCH] some more oracle-phpunit-fu (should not affect non-oracle) * CloneDatabase - already droping tables in internal function, removed duplicate action * DatabaseOracle - stopped ignoring "temporary" parameter * added two parameters to phpunit (use-normal-tables & reuse-db), default actions stay the same * with reuse-db oracle phpunit test run on oracle down to 1m 20s ;) --- includes/db/CloneDatabase.php | 2 +- includes/db/DatabaseOracle.php | 2 +- tests/phpunit/MediaWikiPHPUnitCommand.php | 7 ++ tests/phpunit/MediaWikiTestCase.php | 102 ++++++++++++---------- 4 files changed, 63 insertions(+), 50 deletions(-) diff --git a/includes/db/CloneDatabase.php b/includes/db/CloneDatabase.php index f29e0b2e96..bd0895cfb2 100644 --- a/includes/db/CloneDatabase.php +++ b/includes/db/CloneDatabase.php @@ -98,7 +98,7 @@ class CloneDatabase { self::changePrefix( $this->newTablePrefix ); $newTableName = $this->db->tableName( $tbl, 'raw' ); - if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres' ) ) ) { + if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres', 'oracle' ) ) ) { $this->db->dropTable( $tbl, __METHOD__ ); wfDebug( __METHOD__." dropping {$newTableName}\n", true); //Dropping the oldTable because the prefix was changed diff --git a/includes/db/DatabaseOracle.php b/includes/db/DatabaseOracle.php index a5ceceb94e..cafd1e94c2 100644 --- a/includes/db/DatabaseOracle.php +++ b/includes/db/DatabaseOracle.php @@ -746,7 +746,7 @@ class DatabaseOracle extends DatabaseBase { } function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseOracle::duplicateTableStructure' ) { - $temporary = 'FALSE'; //$temporary ? 'TRUE' : 'FALSE'; + $temporary = $temporary ? 'TRUE' : 'FALSE'; $newName = strtoupper( $newName ); $oldName = strtoupper( $oldName ); diff --git a/tests/phpunit/MediaWikiPHPUnitCommand.php b/tests/phpunit/MediaWikiPHPUnitCommand.php index c6f042020e..7d544ea891 100644 --- a/tests/phpunit/MediaWikiPHPUnitCommand.php +++ b/tests/phpunit/MediaWikiPHPUnitCommand.php @@ -6,6 +6,8 @@ class MediaWikiPHPUnitCommand extends PHPUnit_TextUI_Command { 'regex=' => false, 'file=' => false, 'keep-uploads' => false, + 'use-normal-tables' => false, + 'reuse-db' => false, ); public function __construct() { @@ -62,6 +64,11 @@ ParserTest-specific options: --keep-uploads Re-use the same upload directory for each test, don't delete it +Database options: + --use-normal-tables Use normal DB tables. + --reuse-db Init DB only if tables are missing and keep after finish. + + EOT; } diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 0dc32f3212..82b2a47e2b 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -11,6 +11,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { protected $db; protected $oldTablePrefix; protected $useTemporaryTables = true; + protected $reuseDB = false; private static $dbSetup = false; /** @@ -41,8 +42,10 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { ObjectCache::$instances[CACHE_DB] = new HashBagOStuff; if( $this->needsDB() ) { - global $wgDBprefix; + + $this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' ); + $this->reuseDB = $this->getCliArg('reuse-db'); $this->db = wfGetDB( DB_MASTER ); @@ -82,6 +85,30 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { function addDBData() {} private function addCoreDBData() { + if ( $this->db->getType() == 'oracle' ) { + + # Insert 0 user to prevent FK violations + # Anonymous user + $this->db->insert( 'user', array( + 'user_id' => 0, + 'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) ); + + # Insert 0 page to prevent FK violations + # Blank page + $this->db->insert( 'page', array( + 'page_id' => 0, + 'page_namespace' => 0, + 'page_title' => ' ', + 'page_restrictions' => NULL, + 'page_counter' => 0, + 'page_is_redirect' => 0, + 'page_is_new' => 0, + 'page_random' => 0, + 'page_touched' => $this->db->timestamp(), + 'page_latest' => 0, + 'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) ); + + } User::resetIdByNameCache(); @@ -115,32 +142,17 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() ); $dbClone->useTemporaryTables( $this->useTemporaryTables ); - $dbClone->cloneTableStructure(); + + if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) { + CloneDatabase::changePrefix( $this->dbPrefix() ); + $this->resetDB(); + return; + } else { + $dbClone->cloneTableStructure(); + } if ( $this->db->getType() == 'oracle' ) { $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' ); - - # Insert 0 user to prevent FK violations - # Anonymous user - $this->db->insert( 'user', array( - 'user_id' => 0, - 'user_name' => 'Anonymous' ) ); - - # Insert 0 page to prevent FK violations - # Blank page - $this->db->insert( 'page', array( - 'page_id' => 0, - 'page_namespace' => 0, - 'page_title' => ' ', - 'page_restrictions' => NULL, - 'page_counter' => 0, - 'page_is_redirect' => 0, - 'page_is_new' => 0, - 'page_random' => 0, - 'page_touched' => $this->db->timestamp(), - 'page_latest' => 0, - 'page_len' => 0 ) ); - } } @@ -149,35 +161,29 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { */ private function resetDB() { if( $this->db ) { - foreach( $this->listTables() as $tbl ) { - if( $tbl == 'interwiki' || $tbl == 'user' || $tbl == 'MWUSER' ) continue; - if ( $this->db->getType() == 'oracle' ) - $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ ); - else - $this->db->delete( $tbl, '*', __METHOD__ ); - } - - if ( $this->db->getType() == 'oracle' ) { - # Insert 0 page to prevent FK violations - # Blank page - $this->db->insert( 'page', array( - 'page_id' => 0, - 'page_namespace' => 0, - 'page_title' => ' ', - 'page_restrictions' => NULL, - 'page_counter' => 0, - 'page_is_redirect' => 0, - 'page_is_new' => 0, - 'page_random' => 0, - 'page_touched' => $this->db->timestamp(), - 'page_latest' => 0, - 'page_len' => 0 ) ); + if ( $this->db->getType() == 'oracle' ) { + if ( $this->useTemporaryTables ) { + wfGetLB()->closeAll(); + $this->db = wfGetDB( DB_MASTER ); + } else { + foreach( $this->listTables() as $tbl ) { + if( $tbl == 'interwiki') continue; + $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ ); + } + } + } else { + foreach( $this->listTables() as $tbl ) { + if( $tbl == 'interwiki' || $tbl == 'user' ) continue; + $this->db->delete( $tbl, '*', __METHOD__ ); + } } } } protected function destroyDB() { - if ( $this->useTemporaryTables || is_null( $this->db ) ) { + if ( is_null( $this->db ) || + ( $this->useTemporaryTables && $this->db->getType() != 'oracle' ) || + ( $this->reuseDB ) ) { # Don't need to do anything return; } -- 2.20.1