3 * Helper class for making a copy of the database, mostly for unit testing.
5 * Copyright © 2010 Chad Horohoe <chad@anyonecanedit.org>
6 * https://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
26 use MediaWiki\MediaWikiServices
;
27 use Wikimedia\Rdbms\IMaintainableDatabase
;
30 /** @var string Table prefix for cloning */
31 private $newTablePrefix = '';
33 /** @var string Current table prefix */
34 private $oldTablePrefix = '';
36 /** @var array List of tables to be cloned */
37 private $tablesToClone = [];
39 /** @var bool Should we DROP tables containing the new names? */
40 private $dropCurrentTables = true;
42 /** @var bool Whether to use temporary tables or not */
43 private $useTemporaryTables = true;
45 /** @var IMaintainableDatabase */
51 * @param IMaintainableDatabase $db A database subclass
52 * @param array $tablesToClone An array of tables to clone, unprefixed
53 * @param string $newTablePrefix Prefix to assign to the tables
54 * @param string $oldTablePrefix Prefix on current tables, if not $wgDBprefix
55 * @param bool $dropCurrentTables
57 public function __construct( IMaintainableDatabase
$db, array $tablesToClone,
58 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true
61 $this->tablesToClone
= $tablesToClone;
62 $this->newTablePrefix
= $newTablePrefix;
63 $this->oldTablePrefix
= $oldTablePrefix ?
$oldTablePrefix : $this->db
->tablePrefix();
64 $this->dropCurrentTables
= $dropCurrentTables;
68 * Set whether to use temporary tables or not
69 * @param bool $u Use temporary tables when cloning the structure
71 public function useTemporaryTables( $u = true ) {
72 $this->useTemporaryTables
= $u;
76 * Clone the table structure
78 public function cloneTableStructure() {
79 global $wgSharedTables, $wgSharedDB;
80 foreach ( $this->tablesToClone
as $tbl ) {
81 if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
82 // Shared tables don't work properly when cloning due to
83 // how prefixes are handled (T67654)
84 throw new RuntimeException( "Cannot clone shared table $tbl." );
86 # Clean up from previous aborted run. So that table escaping
87 # works correctly across DB engines, we need to change the pre-
88 # fix back and forth so tableName() works right.
90 self
::changePrefix( $this->oldTablePrefix
);
91 $oldTableName = $this->db
->tableName( $tbl, 'raw' );
93 self
::changePrefix( $this->newTablePrefix
);
94 $newTableName = $this->db
->tableName( $tbl, 'raw' );
96 if ( $this->dropCurrentTables
97 && !in_array( $this->db
->getType(), [ 'postgres', 'oracle' ] )
99 if ( $oldTableName === $newTableName ) {
100 // Last ditch check to avoid data loss
101 throw new LogicException( "Not dropping new table, as '$newTableName'"
102 . " is name of both the old and the new table." );
104 $this->db
->dropTable( $tbl, __METHOD__
);
105 wfDebug( __METHOD__
. " dropping {$newTableName}\n" );
106 // Dropping the oldTable because the prefix was changed
110 wfDebug( __METHOD__
. " duplicating $oldTableName to $newTableName\n" );
111 $this->db
->duplicateTableStructure(
112 $oldTableName, $newTableName, $this->useTemporaryTables
);
117 * Change the prefix back to the original.
118 * @param bool $dropTables Optionally drop the tables we created
120 public function destroy( $dropTables = false ) {
122 self
::changePrefix( $this->newTablePrefix
);
123 foreach ( $this->tablesToClone
as $tbl ) {
124 $this->db
->dropTable( $tbl );
127 self
::changePrefix( $this->oldTablePrefix
);
131 * Change the table prefix on all open DB connections/
133 * @param string $prefix
136 public static function changePrefix( $prefix ) {
139 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
140 $lbFactory->setDomainPrefix( $prefix );
141 $wgDBprefix = $prefix;