12use InvalidArgumentException;
20 private string $newTablePrefix;
23 private string $oldTablePrefix;
26 private array $tablesToClone;
29 private bool $dropCurrentTables;
32 private bool $useTemporaryTables =
true;
46 string $newTablePrefix,
47 ?
string $oldTablePrefix =
null,
48 bool $dropCurrentTables =
true
50 if ( !$tablesToClone ) {
51 throw new InvalidArgumentException(
'Empty list of tables to clone' );
54 $this->tablesToClone = $tablesToClone;
55 $this->newTablePrefix = $newTablePrefix;
56 $this->oldTablePrefix = $oldTablePrefix ?? $this->db->
tablePrefix();
57 $this->dropCurrentTables = $dropCurrentTables;
70 foreach ( $this->tablesToClone as $tbl ) {
74 throw new RuntimeException(
"Cannot clone shared table $tbl." );
76 # Clean up from previous aborted run. So that table escaping
77 # works correctly across DB engines, we need to change the pre-
78 # fix back and forth so tableName() works right.
80 $this->db->tablePrefix( $this->oldTablePrefix );
81 $oldTableName = $this->db->tableName( $tbl,
'raw' );
83 $this->db->tablePrefix( $this->newTablePrefix );
84 $newTableName = $this->db->tableName( $tbl,
'raw' );
88 if ( $this->dropCurrentTables ) {
89 if ( $oldTableName === $newTableName ) {
91 throw new LogicException(
"Not dropping new table, as '$newTableName'"
92 .
" is name of both the old and the new table." );
94 $this->db->dropTable( $tbl, __METHOD__ );
99 $this->db->duplicateTableStructure(
100 $oldTableName, $newTableName, $this->useTemporaryTables, __METHOD__ );
108 public function destroy(
bool $dropTables =
false ): void {
110 $this->db->tablePrefix( $this->newTablePrefix );
111 foreach ( $this->tablesToClone as $tbl ) {
112 $this->db->dropTable( $tbl, __METHOD__ );
115 $this->db->tablePrefix( $this->oldTablePrefix );
127 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
128 $lbFactory->setLocalDomainPrefix( $prefix );
131 $wgDBname => $lbFactory->getLocalDomainID()
133 $lbFactory->setDomainAliases( $aliases );
134 foreach ( $lbFactory->getAllLBs() as $lb ) {
135 $lb->setDomainAliases( $aliases );
143class_alias( CloneDatabase::class,
'CloneDatabase' );
if(!defined('MW_SETUP_CALLBACK'))
$wgDBprefix
Config variable stub for the DBprefix setting, for use by phpdoc and IDEs.
$wgSharedTables
Config variable stub for the SharedTables setting, for use by phpdoc and IDEs.
$wgDBname
Config variable stub for the DBname setting, for use by phpdoc and IDEs.
$wgSharedDB
Config variable stub for the SharedDB setting, for use by phpdoc and IDEs.
Advanced database interface for IDatabase handles that include maintenance methods.