MessageCache invalidation improvements
[mediawiki.git] / includes / db / CloneDatabase.php
blobf1ccd2aebdb82bff7c5de5203fa49622623fccfa
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 * 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
23 * @file
24 * @ingroup Database
26 use MediaWiki\MediaWikiServices;
28 class CloneDatabase {
29 /** @var string Table prefix for cloning */
30 private $newTablePrefix = '';
32 /** @var string Current table prefix */
33 private $oldTablePrefix = '';
35 /** @var array List of tables to be cloned */
36 private $tablesToClone = [];
38 /** @var bool Should we DROP tables containing the new names? */
39 private $dropCurrentTables = true;
41 /** @var bool Whether to use temporary tables or not */
42 private $useTemporaryTables = true;
44 /** @var Database */
45 private $db;
47 /**
48 * Constructor
50 * @param Database $db A database subclass
51 * @param array $tablesToClone An array of tables to clone, unprefixed
52 * @param string $newTablePrefix Prefix to assign to the tables
53 * @param string $oldTablePrefix Prefix on current tables, if not $wgDBprefix
54 * @param bool $dropCurrentTables
56 public function __construct( Database $db, array $tablesToClone,
57 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true
58 ) {
59 $this->db = $db;
60 $this->tablesToClone = $tablesToClone;
61 $this->newTablePrefix = $newTablePrefix;
62 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
63 $this->dropCurrentTables = $dropCurrentTables;
66 /**
67 * Set whether to use temporary tables or not
68 * @param bool $u Use temporary tables when cloning the structure
70 public function useTemporaryTables( $u = true ) {
71 $this->useTemporaryTables = $u;
74 /**
75 * Clone the table structure
77 public function cloneTableStructure() {
78 global $wgSharedTables, $wgSharedDB;
79 foreach ( $this->tablesToClone as $tbl ) {
80 if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
81 // Shared tables don't work properly when cloning due to
82 // how prefixes are handled (bug 65654)
83 throw new RuntimeException( "Cannot clone shared table $tbl." );
85 # Clean up from previous aborted run. So that table escaping
86 # works correctly across DB engines, we need to change the pre-
87 # fix back and forth so tableName() works right.
89 self::changePrefix( $this->oldTablePrefix );
90 $oldTableName = $this->db->tableName( $tbl, 'raw' );
92 self::changePrefix( $this->newTablePrefix );
93 $newTableName = $this->db->tableName( $tbl, 'raw' );
95 if ( $this->dropCurrentTables
96 && !in_array( $this->db->getType(), [ 'postgres', 'oracle' ] )
97 ) {
98 if ( $oldTableName === $newTableName ) {
99 // Last ditch check to avoid data loss
100 throw new LogicException( "Not dropping new table, as '$newTableName'"
101 . " is name of both the old and the new table." );
103 $this->db->dropTable( $tbl, __METHOD__ );
104 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
105 // Dropping the oldTable because the prefix was changed
108 # Create new table
109 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
110 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
115 * Change the prefix back to the original.
116 * @param bool $dropTables Optionally drop the tables we created
118 public function destroy( $dropTables = false ) {
119 if ( $dropTables ) {
120 self::changePrefix( $this->newTablePrefix );
121 foreach ( $this->tablesToClone as $tbl ) {
122 $this->db->dropTable( $tbl );
125 self::changePrefix( $this->oldTablePrefix );
129 * Change the table prefix on all open DB connections/
131 * @param string $prefix
132 * @return void
134 public static function changePrefix( $prefix ) {
135 global $wgDBprefix;
137 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
138 $lbFactory->setDomainPrefix( $prefix );
139 $wgDBprefix = $prefix;