3 * Helper class for making a copy of the database, mostly for unit testing.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use Wikimedia\Rdbms\IMaintainableDatabase
;
27 /** Table prefix for cloning */
28 private string $newTablePrefix;
30 /** Current table prefix */
31 private string $oldTablePrefix;
33 /** List of tables to be cloned */
34 private array $tablesToClone;
36 /** Should we DROP tables containing the new names? */
37 private bool $dropCurrentTables;
39 /** Whether to use temporary tables or not */
40 private bool $useTemporaryTables = true;
42 private IMaintainableDatabase
$db;
45 * @param IMaintainableDatabase $db A database subclass
46 * @param array $tablesToClone An array of tables to clone, unprefixed
47 * @param string $newTablePrefix Prefix to assign to the tables
48 * @param string|null $oldTablePrefix Prefix on current tables, if not $wgDBprefix
49 * @param bool $dropCurrentTables
51 public function __construct(
52 IMaintainableDatabase
$db,
54 string $newTablePrefix,
55 ?
string $oldTablePrefix = null,
56 bool $dropCurrentTables = true
58 if ( !$tablesToClone ) {
59 throw new InvalidArgumentException( 'Empty list of tables to clone' );
62 $this->tablesToClone
= $tablesToClone;
63 $this->newTablePrefix
= $newTablePrefix;
64 $this->oldTablePrefix
= $oldTablePrefix ??
$this->db
->tablePrefix();
65 $this->dropCurrentTables
= $dropCurrentTables;
69 * Set whether to use temporary tables or not
70 * @param bool $u Use temporary tables when cloning the structure
72 public function useTemporaryTables( bool $u = true ): void
{
73 $this->useTemporaryTables
= $u;
76 public function cloneTableStructure(): void
{
77 global $wgSharedTables, $wgSharedDB;
78 foreach ( $this->tablesToClone
as $tbl ) {
79 if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
80 // Shared tables don't work properly when cloning due to
81 // how prefixes are handled (T67654)
82 throw new RuntimeException( "Cannot clone shared table $tbl." );
84 # Clean up from previous aborted run. So that table escaping
85 # works correctly across DB engines, we need to change the pre-
86 # fix back and forth so tableName() works right.
88 $this->db
->tablePrefix( $this->oldTablePrefix
);
89 $oldTableName = $this->db
->tableName( $tbl, 'raw' );
91 $this->db
->tablePrefix( $this->newTablePrefix
);
92 $newTableName = $this->db
->tableName( $tbl, 'raw' );
94 // Postgres: Temp tables are automatically deleted upon end of session
95 // Same Temp table name hides existing table for current session
96 if ( $this->dropCurrentTables
) {
97 if ( $oldTableName === $newTableName ) {
98 // Last ditch check to avoid data loss
99 throw new LogicException( "Not dropping new table, as '$newTableName'"
100 . " is name of both the old and the new table." );
102 $this->db
->dropTable( $tbl, __METHOD__
);
103 // Dropping the oldTable because the prefix was changed
107 $this->db
->duplicateTableStructure(
108 $oldTableName, $newTableName, $this->useTemporaryTables
, __METHOD__
);
113 * Change the prefix back to the original.
114 * @param bool $dropTables Optionally drop the tables we created
116 public function destroy( bool $dropTables = false ): void
{
118 $this->db
->tablePrefix( $this->newTablePrefix
);
119 foreach ( $this->tablesToClone
as $tbl ) {
120 $this->db
->dropTable( $tbl, __METHOD__
);
123 $this->db
->tablePrefix( $this->oldTablePrefix
);
127 * Change the table prefix on all open DB connections
129 * @param string $prefix
132 public static function changePrefix( string $prefix ): void
{
133 global $wgDBprefix, $wgDBname;
135 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
136 $lbFactory->setLocalDomainPrefix( $prefix );
139 $wgDBname => $lbFactory->getLocalDomainID()
141 $lbFactory->setDomainAliases( $aliases );
142 foreach ( $lbFactory->getAllLBs() as $lb ) {
143 $lb->setDomainAliases( $aliases );
146 $wgDBprefix = $prefix;