Index: trunk/phase3/tests/parser/parserTest.inc |
— | — | @@ -751,36 +751,15 @@ |
752 | 752 | |
753 | 753 | $temporary = $this->useTemporaryTables || $dbType == 'postgres'; |
754 | 754 | $tables = $this->listTables(); |
| 755 | + $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_'; |
755 | 756 | |
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(); |
764 | 760 | |
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 | | - |
780 | 761 | if ( $dbType == 'oracle' ) |
781 | 762 | $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' ); |
782 | 763 | |
783 | | - $this->changePrefix( $dbType != 'oracle' ? 'parsertest_' : 'pt_' ); |
784 | | - |
785 | 764 | if ( $dbType == 'oracle' ) { |
786 | 765 | # Insert 0 user to prevent FK violations |
787 | 766 | |
— | — | @@ -866,23 +845,6 @@ |
867 | 846 | ), $this->db->timestamp( '20010115123500' ), $user ); |
868 | 847 | } |
869 | 848 | |
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 | | - |
887 | 849 | public function teardownDatabase() { |
888 | 850 | if ( !$this->databaseSetupDone ) { |
889 | 851 | $this->teardownGlobals(); |
— | — | @@ -890,7 +852,7 @@ |
891 | 853 | } |
892 | 854 | $this->teardownUploadDir( $this->uploadDir ); |
893 | 855 | |
894 | | - $this->changePrefix( $this->oldTablePrefix ); |
| 856 | + $this->db->tablePrefix( $this->oldTablePrefix ); |
895 | 857 | $this->databaseSetupDone = false; |
896 | 858 | |
897 | 859 | 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 |
1 | 133 | + native |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -367,6 +367,7 @@ |
368 | 368 | # includes/db |
369 | 369 | 'Blob' => 'includes/db/Database.php', |
370 | 370 | 'ChronologyProtector' => 'includes/db/LBFactory.php', |
| 371 | + 'CloneDatabase' => 'includes/db/CloneDatabase.php', |
371 | 372 | 'Database' => 'includes/db/DatabaseMysql.php', |
372 | 373 | 'DatabaseBase' => 'includes/db/Database.php', |
373 | 374 | 'DatabaseMssql' => 'includes/db/DatabaseMssql.php', |