r79093 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79092‎ | r79093 | r79094 >
Date:00:44, 28 December 2010
Author:demon
Status:resolved (Comments)
Tags:
Comment:
Refactor table cloning code into its own class so it can maybe be used by things other than the parser tests
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/db/CloneDatabase.php (added) (history)
  • /trunk/phase3/tests/parser/parserTest.inc (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/parser/parserTest.inc
@@ -751,36 +751,15 @@
752752
753753 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
754754 $tables = $this->listTables();
 755+ $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_';
755756
756 - foreach ( $tables as $tbl ) {
757 - # Clean up from previous aborted run. So that table escaping
758 - # works correctly across DB engines, we need to change the pre-
759 - # fix back and forth so tableName() works right.
760 - $this->changePrefix( $this->oldTablePrefix );
761 - $oldTableName = $this->db->tableName( $tbl );
762 - $this->changePrefix( $dbType != 'oracle' ? 'parsertest_' : 'pt_' );
763 - $newTableName = $this->db->tableName( $tbl );
 757+ $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix );
 758+ $this->dbClone->useTemporaryTables( $temporary );
 759+ $this->dbClone->cloneTableStructure();
764760
765 - if ( $dbType == 'mysql' ) {
766 - $this->db->query( "DROP TABLE IF EXISTS $newTableName" );
767 - } elseif ( in_array( $dbType, array( 'postgres', 'oracle' ) ) ) {
768 - /* DROPs wouldn't work due to Foreign Key Constraints (bug 14990, r58669)
769 - * Use "DROP TABLE IF EXISTS $newTableName CASCADE" for postgres? That
770 - * syntax would also work for mysql.
771 - */
772 - } elseif ( $this->db->tableExists( $tbl ) ) {
773 - $this->db->query( "DROP TABLE $newTableName" );
774 - }
775 -
776 - # Create new table
777 - $this->db->duplicateTableStructure( $oldTableName, $newTableName, $temporary );
778 - }
779 -
780761 if ( $dbType == 'oracle' )
781762 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
782763
783 - $this->changePrefix( $dbType != 'oracle' ? 'parsertest_' : 'pt_' );
784 -
785764 if ( $dbType == 'oracle' ) {
786765 # Insert 0 user to prevent FK violations
787766
@@ -866,23 +845,6 @@
867846 ), $this->db->timestamp( '20010115123500' ), $user );
868847 }
869848
870 - /**
871 - * Change the table prefix on all open DB connections/
872 - */
873 - protected function changePrefix( $prefix ) {
874 - global $wgDBprefix;
875 - wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
876 - $wgDBprefix = $prefix;
877 - }
878 -
879 - public function changeLBPrefix( $lb, $prefix ) {
880 - $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
881 - }
882 -
883 - public function changeDBPrefix( $db, $prefix ) {
884 - $db->tablePrefix( $prefix );
885 - }
886 -
887849 public function teardownDatabase() {
888850 if ( !$this->databaseSetupDone ) {
889851 $this->teardownGlobals();
@@ -890,7 +852,7 @@
891853 }
892854 $this->teardownUploadDir( $this->uploadDir );
893855
894 - $this->changePrefix( $this->oldTablePrefix );
 856+ $this->db->tablePrefix( $this->oldTablePrefix );
895857 $this->databaseSetupDone = false;
896858
897859 if ( $this->useTemporaryTables ) {
Index: trunk/phase3/includes/db/CloneDatabase.php
@@ -0,0 +1,131 @@
 2+<?php
 3+/**
 4+ * Helper class for making a copy of the database, mostly for unit testing.
 5+ *
 6+ * Copyright © 2010 Chad Horohoe <chad@anyonecanedit.org>
 7+ * http://www.mediawiki.org/
 8+ *
 9+ * This program is free software; you can redistribute it and/or modify
 10+ * it under the terms of the GNU General Public License as published by
 11+ * the Free Software Foundation; either version 2 of the License, or
 12+ * (at your option) any later version.
 13+ *
 14+ * This program is distributed in the hope that it will be useful,
 15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17+ * GNU General Public License for more details.
 18+ *
 19+ * You should have received a copy of the GNU General Public License along
 20+ * with this program; if not, write to the Free Software Foundation, Inc.,
 21+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 22+ * http://www.gnu.org/copyleft/gpl.html
 23+ *
 24+ * @ingroup Database
 25+ */
 26+
 27+class CloneDatabase {
 28+
 29+ /**
 30+ * Table prefix for cloning
 31+ * @var String
 32+ */
 33+ private $newTablePrefix = '';
 34+
 35+ /**
 36+ * Current table prefix
 37+ * @var String
 38+ */
 39+ private $oldTablePrefix = '';
 40+
 41+ /**
 42+ * List of tables to be cloned
 43+ * @var Array
 44+ */
 45+ private $tablesToClone = array();
 46+
 47+ /**
 48+ * Should we DROP tables containing the new names?
 49+ * @var Bool
 50+ */
 51+ private $dropCurrentTables = true;
 52+
 53+ /**
 54+ * Whether to use temporary tables or not
 55+ * @var Bool
 56+ */
 57+ private $useTemporaryTables = true;
 58+
 59+ /**
 60+ * Constructor
 61+ *
 62+ * @param $db DatabaseBase A database subclass
 63+ * @param $tablesToClone Array An array of tables to clone, unprefixed
 64+ * @param $newTablePrefix String Prefix to assign to the tables
 65+ * @param $oldTablePrefix String Prefix on current tables, if not $wgDBprefix
 66+ */
 67+ public function __construct( DatabaseBase $db, Array $tablesToClone,
 68+ $newTablePrefix = 'parsertest', $oldTablePrefix = '', $dropCurrentTables = true )
 69+ {
 70+ $this->db = $db;
 71+ $this->tablesToClone = $tablesToClone;
 72+ $this->newTablePrefix = $newTablePrefix;
 73+ $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
 74+ $this->dropCurrentTables = $dropCurrentTables;
 75+ }
 76+
 77+ /**
 78+ * Set whether to use temporary tables or not
 79+ * @param $u Bool Use temporary tables when cloning the structure
 80+ */
 81+ public function useTemporaryTables( $u = true ) {
 82+ $this->useTemporaryTables = false;
 83+ }
 84+
 85+ /**
 86+ * Clone the table structure
 87+ */
 88+ public function cloneTableStructure() {
 89+ foreach( $this->tablesToClone as $tbl ) {
 90+ # Clean up from previous aborted run. So that table escaping
 91+ # works correctly across DB engines, we need to change the pre-
 92+ # fix back and forth so tableName() works right.
 93+ $this->changePrefix( $this->oldTablePrefix );
 94+ $oldTableName = $this->db->tableName( $tbl );
 95+ $this->changePrefix( $this->newTablePrefix );
 96+ $newTableName = $this->db->tableName( $tbl );
 97+
 98+ if( $this->dropCurrentTables ) {
 99+ if ( $this->db->getType() == 'mysql' && $this->db->tableExists( $tbl ) ) {
 100+ $this->db->query( "DROP TABLE IF EXISTS $newTableName" );
 101+ } elseif ( in_array( $this->db->getType(), array( 'postgres', 'oracle' ) ) ) {
 102+ /* DROPs wouldn't work due to Foreign Key Constraints (bug 14990, r58669)
 103+ * Use "DROP TABLE IF EXISTS $newTableName CASCADE" for postgres? That
 104+ * syntax would also work for mysql.
 105+ */
 106+ } elseif ( $this->db->tableExists( $tbl ) ) {
 107+ $this->db->query( "DROP TABLE $newTableName" );
 108+ }
 109+ }
 110+
 111+ # Create new table
 112+ $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
 113+ }
 114+ }
 115+
 116+ /**
 117+ * Change the table prefix on all open DB connections/
 118+ */
 119+ protected function changePrefix( $prefix ) {
 120+ global $wgDBprefix;
 121+ wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
 122+ $wgDBprefix = $prefix;
 123+ }
 124+
 125+ public function changeLBPrefix( $lb, $prefix ) {
 126+ $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
 127+ }
 128+
 129+ public function changeDBPrefix( $db, $prefix ) {
 130+ $db->tablePrefix( $prefix );
 131+ }
 132+}
Property changes on: trunk/phase3/includes/db/CloneDatabase.php
___________________________________________________________________
Added: svn:eol-style
1133 + native
Index: trunk/phase3/includes/AutoLoader.php
@@ -367,6 +367,7 @@
368368 # includes/db
369369 'Blob' => 'includes/db/Database.php',
370370 'ChronologyProtector' => 'includes/db/LBFactory.php',
 371+ 'CloneDatabase' => 'includes/db/CloneDatabase.php',
371372 'Database' => 'includes/db/DatabaseMysql.php',
372373 'DatabaseBase' => 'includes/db/Database.php',
373374 'DatabaseMssql' => 'includes/db/DatabaseMssql.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r79094Followup r79093: Move DROP TABLE code to Database classes and properly rename...demon01:19, 28 December 2010
r79135Fix hardcoded debugging from r79093demon22:20, 28 December 2010

Comments

#Comment by OverlordQ (talk | contribs)   15:50, 28 December 2010

Breaks table duplication completely.

Query: CREATE  TABLE mwuser (LIKE mwuser INCLUDING DEFAULTS)
Function: DatabasePostgres::duplicateTableStructure
Error: 1 ERROR:  relation "mwuser" does not exist
LINE 1: ...teTableStructure 127.0.0.1 */  TABLE mwuser (LIKE mwuser INC...
#Comment by 😂 (talk | contribs)   18:49, 10 January 2011

Can you get me a stack trace of this?

#Comment by 😂 (talk | contribs)   22:24, 25 May 2011

I'm pretty sure this was fixed in r79337. I can confirm the behavior is correct in Mysql and Sqlite, so setting back to new.

#Comment by X! (talk | contribs)   18:33, 28 December 2010

Does not work with an empty $wgDBprefix.

#Comment by OverlordQ (talk | contribs)   22:18, 28 December 2010

And what's the point of?

+	public function useTemporaryTables( $u = true ) {
+		$this->useTemporaryTables = false;
+	}
#Comment by 😂 (talk | contribs)   22:18, 28 December 2010

Whoops, the false was from debugging.

Status & tagging log