Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
CloneDatabase | |
0.00% |
0 / 38 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
useTemporaryTables | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
cloneTableStructure | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
destroy | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
changePrefix | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * Helper class for making a copy of the database, mostly for unit testing. |
4 | * |
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. |
9 | * |
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. |
14 | * |
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 |
19 | * |
20 | * @file |
21 | * @ingroup Database |
22 | */ |
23 | use MediaWiki\MediaWikiServices; |
24 | use Wikimedia\Rdbms\IMaintainableDatabase; |
25 | |
26 | class CloneDatabase { |
27 | /** @var string Table prefix for cloning */ |
28 | private $newTablePrefix; |
29 | |
30 | /** @var string Current table prefix */ |
31 | private $oldTablePrefix; |
32 | |
33 | /** @var array List of tables to be cloned */ |
34 | private $tablesToClone; |
35 | |
36 | /** @var bool Should we DROP tables containing the new names? */ |
37 | private $dropCurrentTables; |
38 | |
39 | /** @var bool Whether to use temporary tables or not */ |
40 | private $useTemporaryTables = true; |
41 | |
42 | /** @var IMaintainableDatabase */ |
43 | private $db; |
44 | |
45 | /** |
46 | * @param IMaintainableDatabase $db A database subclass |
47 | * @param array $tablesToClone An array of tables to clone, unprefixed |
48 | * @param string $newTablePrefix Prefix to assign to the tables |
49 | * @param string|null $oldTablePrefix Prefix on current tables, if not $wgDBprefix |
50 | * @param bool $dropCurrentTables |
51 | */ |
52 | public function __construct( |
53 | IMaintainableDatabase $db, |
54 | array $tablesToClone, |
55 | string $newTablePrefix, |
56 | string $oldTablePrefix = null, |
57 | bool $dropCurrentTables = true |
58 | ) { |
59 | if ( !$tablesToClone ) { |
60 | throw new InvalidArgumentException( 'Empty list of tables to clone' ); |
61 | } |
62 | $this->db = $db; |
63 | $this->tablesToClone = $tablesToClone; |
64 | $this->newTablePrefix = $newTablePrefix; |
65 | $this->oldTablePrefix = $oldTablePrefix ?? $this->db->tablePrefix(); |
66 | $this->dropCurrentTables = $dropCurrentTables; |
67 | } |
68 | |
69 | /** |
70 | * Set whether to use temporary tables or not |
71 | * @param bool $u Use temporary tables when cloning the structure |
72 | */ |
73 | public function useTemporaryTables( $u = true ) { |
74 | $this->useTemporaryTables = $u; |
75 | } |
76 | |
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 (T67654) |
83 | throw new RuntimeException( "Cannot clone shared table $tbl." ); |
84 | } |
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. |
88 | |
89 | $this->db->tablePrefix( $this->oldTablePrefix ); |
90 | $oldTableName = $this->db->tableName( $tbl, 'raw' ); |
91 | |
92 | $this->db->tablePrefix( $this->newTablePrefix ); |
93 | $newTableName = $this->db->tableName( $tbl, 'raw' ); |
94 | |
95 | // Postgres: Temp tables are automatically deleted upon end of session |
96 | // Same Temp table name hides existing table for current session |
97 | if ( $this->dropCurrentTables ) { |
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." ); |
102 | } |
103 | $this->db->dropTable( $tbl, __METHOD__ ); |
104 | wfDebug( __METHOD__ . " dropping {$newTableName}" ); |
105 | // Dropping the oldTable because the prefix was changed |
106 | } |
107 | |
108 | # Create new table |
109 | wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName" ); |
110 | $this->db->duplicateTableStructure( |
111 | $oldTableName, $newTableName, $this->useTemporaryTables, __METHOD__ ); |
112 | } |
113 | } |
114 | |
115 | /** |
116 | * Change the prefix back to the original. |
117 | * @param bool $dropTables Optionally drop the tables we created |
118 | */ |
119 | public function destroy( $dropTables = false ) { |
120 | if ( $dropTables ) { |
121 | $this->db->tablePrefix( $this->newTablePrefix ); |
122 | foreach ( $this->tablesToClone as $tbl ) { |
123 | $this->db->dropTable( $tbl, __METHOD__ ); |
124 | } |
125 | } |
126 | $this->db->tablePrefix( $this->oldTablePrefix ); |
127 | } |
128 | |
129 | /** |
130 | * Change the table prefix on all open DB connections |
131 | * |
132 | * @param string $prefix |
133 | * @return void |
134 | */ |
135 | public static function changePrefix( $prefix ) { |
136 | global $wgDBprefix, $wgDBname; |
137 | |
138 | $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
139 | $lbFactory->setLocalDomainPrefix( $prefix ); |
140 | |
141 | $aliases = [ |
142 | $wgDBname => $lbFactory->getLocalDomainID() |
143 | ]; |
144 | $lbFactory->setDomainAliases( $aliases ); |
145 | foreach ( $lbFactory->getAllLBs() as $lb ) { |
146 | $lb->setDomainAliases( $aliases ); |
147 | } |
148 | |
149 | $wgDBprefix = $prefix; |
150 | } |
151 | } |