r79368 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79367‎ | r79368 | r79369 >
Date:20:42, 31 December 2010
Author:soxred93
Status:ok
Tags:
Comment:
More work on getting SQLite to work with unit tests. DB Prefix changing is now static to allow for external classes to change it.
Modified paths:
  • /trunk/phase3/includes/db/CloneDatabase.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseSqlite.php (modified) (history)
  • /trunk/phase3/tests/phpunit/MediaWikiTestCase.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/MediaWikiTestCase.php
@@ -5,7 +5,6 @@
66 public $regex = '';
77 public $runDisabled = false;
88
9 - protected static $databaseSetupDone = false;
109 protected $db;
1110 protected $dbClone;
1211 protected $oldTablePrefix;
@@ -24,8 +23,10 @@
2524
2625 if( $this->needsDB() ) {
2726
28 - $this->destroyDBCheck();
 27+ $this->db = wfGetDB( DB_MASTER );
2928
 29+ $this->destroyDB();
 30+
3031 $this->initDB();
3132 $this->addCoreDBData();
3233 $this->addDBData();
@@ -35,14 +36,8 @@
3637 }
3738
3839 function __destruct() {
39 - $this->destroyDBCheck();
 40+ $this->destroyDB();
4041 }
41 -
42 - function destroyDBCheck() {
43 - if( is_object( $this->dbClone ) && $this->dbClone instanceof CloneDatabase ) {
44 - $this->destroyDB();
45 - }
46 - }
4742
4843 function needsDB() {
4944 $rc = new ReflectionClass( $this );
@@ -78,37 +73,19 @@
7974 private function initDB() {
8075 global $wgDBprefix;
8176
82 - if ( self::$databaseSetupDone ) {
83 - return;
84 - }
85 -
86 - $this->db = wfGetDB( DB_MASTER );
8777 $dbType = $this->db->getType();
8878
8979 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
9080 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
9181 }
9282
93 - self::$databaseSetupDone = true;
9483 $this->oldTablePrefix = $wgDBprefix;
9584
96 - # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
97 - # It seems to have been fixed since (r55079?).
98 - # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
99 -
100 - # CREATE TEMPORARY TABLE breaks if there is more than one server
101 - if ( wfGetLB()->getServerCount() != 1 ) {
102 - $this->useTemporaryTables = false;
103 - }
104 -
105 - $temporary = $this->useTemporaryTables || $dbType == 'postgres';
106 -
10785 $tables = $this->listTables();
10886
10987 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
11088
11189 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
112 - $this->dbClone->useTemporaryTables( $temporary );
11390 $this->dbClone->cloneTableStructure();
11491
11592 if ( $dbType == 'oracle' )
@@ -126,13 +103,8 @@
127104 }
128105
129106 protected function destroyDB() {
130 - if ( !self::$databaseSetupDone ) {
131 - return;
132 - }
 107+ global $wgDBprefix;
133108
134 - $this->dbClone->destroy();
135 - self::$databaseSetupDone = false;
136 -
137109 if ( $this->useTemporaryTables ) {
138110 # Don't need to do anything
139111 //return;
@@ -147,14 +119,16 @@
148120 }
149121
150122 foreach ( $tables as $table ) {
151 - $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
152 - $this->db->query( $sql );
 123+ if( $this->db->tableExists( "`$table`" ) ) {
 124+ $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
 125+ $this->db->query( $sql, __METHOD__ );
 126+ }
153127 }
154 -
 128+
155129 if ( $this->db->getType() == 'oracle' )
156 - $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
 130+ $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
157131
158 -
 132+ CloneDatabase::changePrefix( $this->oldTablePrefix );
159133 }
160134
161135 function __call( $func, $args ) {
Index: trunk/phase3/includes/db/CloneDatabase.php
@@ -91,14 +91,15 @@
9292 # works correctly across DB engines, we need to change the pre-
9393 # fix back and forth so tableName() works right.
9494
95 - $this->changePrefix( $this->oldTablePrefix );
 95+ self::changePrefix( $this->oldTablePrefix );
9696 $oldTableName = $this->db->tableName( $tbl );
9797
98 - $this->changePrefix( $this->newTablePrefix );
 98+ self::changePrefix( $this->newTablePrefix );
9999 $newTableName = $this->db->tableName( $tbl );
100100
101101 if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres' ) ) ) {
102102 $this->db->dropTable( $tbl, __METHOD__ );
 103+ wfDebug( "Dropping {$this->newTablePrefix}{$oldTableName}\n", __METHOD__ );
103104 //Dropping the oldTable because the prefix was changed
104105 }
105106
@@ -116,12 +117,12 @@
117118 */
118119 public function destroy( $dropTables = false ) {
119120 if( $dropTables ) {
120 - $this->changePrefix( $this->newTablePrefix );
 121+ self::changePrefix( $this->newTablePrefix );
121122 foreach( $this->tablesToClone as $tbl ) {
122123 $this->db->dropTable( $tbl );
123124 }
124125 }
125 - $this->changePrefix( $this->oldTablePrefix );
 126+ self::changePrefix( $this->oldTablePrefix );
126127 }
127128
128129 /**
@@ -130,9 +131,9 @@
131132 * @param $prefix
132133 * @return void
133134 */
134 - protected function changePrefix( $prefix ) {
 135+ public static function changePrefix( $prefix ) {
135136 global $wgDBprefix;
136 - wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
 137+ wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
137138 $wgDBprefix = $prefix;
138139 }
139140
@@ -141,8 +142,8 @@
142143 * @param $prefix
143144 * @return void
144145 */
145 - public function changeLBPrefix( $lb, $prefix ) {
146 - $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
 146+ public static function changeLBPrefix( $lb, $prefix ) {
 147+ $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
147148 }
148149
149150 /**
@@ -150,7 +151,7 @@
151152 * @param $prefix
152153 * @return void
153154 */
154 - public function changeDBPrefix( $db, $prefix ) {
 155+ public static function changeDBPrefix( $db, $prefix ) {
155156 $db->tablePrefix( $prefix );
156157 }
157158 }
Index: trunk/phase3/includes/db/DatabaseSqlite.php
@@ -595,6 +595,7 @@
596596 }
597597
598598 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseSqlite::duplicateTableStructure' ) {
 599+
599600 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name='$oldName' AND type='table'", $fname );
600601 $obj = $this->fetchObject( $res );
601602 if ( !$obj ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r84371correct wfDebug() calls added in r79272 & r79368hashar11:39, 20 March 2011

Status & tagging log