-Destroy the DB automatically when initting the DB
[mediawiki.git] / includes / db / CloneDatabase.php
blob6ca06938babf29749c75de7b5fa3e5c5b0ac2786
1 <?php
2 /**
3 * Helper class for making a copy of the database, mostly for unit testing.
5 * Copyright © 2010 Chad Horohoe <chad@anyonecanedit.org>
6 * http://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup Database
26 class CloneDatabase {
28 /**
29 * Table prefix for cloning
30 * @var String
32 private $newTablePrefix = '';
34 /**
35 * Current table prefix
36 * @var String
38 private $oldTablePrefix = '';
40 /**
41 * List of tables to be cloned
42 * @var Array
44 private $tablesToClone = array();
46 /**
47 * Should we DROP tables containing the new names?
48 * @var Bool
50 private $dropCurrentTables = true;
52 /**
53 * Whether to use temporary tables or not
54 * @var Bool
56 private $useTemporaryTables = true;
58 /**
59 * Constructor
61 * @param $db DatabaseBase A database subclass
62 * @param $tablesToClone Array An array of tables to clone, unprefixed
63 * @param $newTablePrefix String Prefix to assign to the tables
64 * @param $oldTablePrefix String Prefix on current tables, if not $wgDBprefix
66 public function __construct( DatabaseBase $db, array $tablesToClone,
67 $newTablePrefix = 'parsertest', $oldTablePrefix = '', $dropCurrentTables = true )
69 $this->db = $db;
70 $this->tablesToClone = $tablesToClone;
71 $this->newTablePrefix = $newTablePrefix;
72 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
73 $this->dropCurrentTables = $dropCurrentTables;
76 /**
77 * Set whether to use temporary tables or not
78 * @param $u Bool Use temporary tables when cloning the structure
80 public function useTemporaryTables( $u = true ) {
81 $this->useTemporaryTables = $u;
84 /**
85 * Clone the table structure
87 public function cloneTableStructure() {
89 sort($this->tablesToClone);
91 foreach( $this->tablesToClone as $tbl ) {
92 # Clean up from previous aborted run. So that table escaping
93 # works correctly across DB engines, we need to change the pre-
94 # fix back and forth so tableName() works right.
95 $this->changePrefix( $this->oldTablePrefix );
96 $oldTableName = $this->db->tableName( $tbl );
98 $this->changePrefix( $this->newTablePrefix );
99 $newTableName = $this->db->tableName( $tbl );
101 if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres') ) ) {
102 $this->db->dropTable( $newTableName, __METHOD__ );
105 # Create new table
106 wfDebug( "Duplicating $oldTableName to $newTableName\n", __METHOD__ );
107 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
113 * Change the prefix back to the original.
114 * @param $dropTables bool Optionally drop the tables we created
116 public function destroy( $dropTables = false ) {
117 if( $dropTables ) {
118 $this->changePrefix( $this->newTablePrefix );
119 foreach( $this->tablesToClone as $tbl ) {
120 $this->db->dropTable( $tbl );
123 $this->changePrefix( $this->oldTablePrefix );
127 * Change the table prefix on all open DB connections/
129 * @param $prefix
130 * @return void
132 protected function changePrefix( $prefix ) {
133 global $wgDBprefix;
134 wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
135 $wgDBprefix = $prefix;
139 * @param $lb LoadBalancer
140 * @param $prefix
141 * @return void
143 public function changeLBPrefix( $lb, $prefix ) {
144 $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
148 * @param $db DatabaseBase
149 * @param $prefix
150 * @return void
152 public function changeDBPrefix( $db, $prefix ) {
153 $db->tablePrefix( $prefix );