This is intended for use with scripts like addWiki.php to avoid mismatched domain errors.
Bug: T209483
Change-Id: Ie24f83f4e0fad7b01690d2055e1529a4cb0275b5
/**
* Change the current database
*
+ * This should not be called outside LoadBalancer for connections managed by a LoadBalancer
+ *
* @param string $db
* @return bool True unless an exception was thrown
* @throws DBConnectionError If databasesAreIndependent() is true and an error occurs
* @throws DBError
- * @deprecated Since 1.32
+ * @deprecated Since 1.32 Use selectDomain() instead
*/
public function selectDB( $db );
*
* This will throw an error for some database types if the database unspecified
*
+ * This should not be called outside LoadBalancer for connections managed by a LoadBalancer
+ *
* @param string|DatabaseDomain $domain
* @since 1.32
* @throws DBConnectionError
*/
public function resolveDomainID( $domain );
+ /**
+ * Close all connection and redefine the local domain for testing or schema creation
+ *
+ * @param DatabaseDomain|string $domain
+ * @since 1.33
+ */
+ public function redefineLocalDomain( $domain );
+
/**
* Create a new load balancer object. The resulting object will be untracked,
* not chronology-protected, and the caller is responsible for cleaning it up.
} );
}
+ public function redefineLocalDomain( $domain ) {
+ $this->closeAll();
+
+ $this->localDomain = DatabaseDomain::newFromId( $domain );
+
+ $this->forEachLB( function ( ILoadBalancer $lb ) {
+ $lb->redefineLocalDomain( $this->localDomain );
+ } );
+ }
+
public function closeAll() {
$this->forEachLBCallMethod( 'closeAll', [] );
}
*/
public function resolveDomainID( $domain );
+ /**
+ * Close all connection and redefine the local domain for testing or schema creation
+ *
+ * @param DatabaseDomain|string $domain
+ * @since 1.33
+ */
+ public function redefineLocalDomain( $domain );
+
/**
* Get the index of the reader connection, which may be a replica DB
*
} );
}
+ public function redefineLocalDomain( $domain ) {
+ $this->closeAll();
+
+ $this->setLocalDomain( DatabaseDomain::newFromId( $domain ) );
+ }
+
/**
* @param DatabaseDomain $domain
*/
}
}
+ public function testRedefineLocalDomain() {
+ global $wgDBname;
+
+ if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
+ self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
+ return;
+ }
+
+ $factory = $this->newLBFactoryMulti(
+ [],
+ []
+ );
+ $lb = $factory->getMainLB();
+
+ $conn1 = $lb->getConnectionRef( DB_MASTER );
+ $this->assertEquals(
+ wfWikiID(),
+ $conn1->getDomainID()
+ );
+ unset( $conn1 );
+
+ $factory->redefineLocalDomain( 'somedb-prefix' );
+ $this->assertEquals( 'somedb-prefix', $factory->getLocalDomainID() );
+
+ $domain = new DatabaseDomain( $wgDBname, null, 'pref' );
+ $factory->redefineLocalDomain( $domain );
+
+ $n = 0;
+ $lb->forEachOpenConnection( function () use ( &$n ) {
+ ++$n;
+ } );
+ $this->assertEquals( 0, $n, "Connections closed" );
+
+ $conn2 = $lb->getConnectionRef( DB_MASTER );
+ $this->assertEquals(
+ $domain->getId(),
+ $conn2->getDomainID()
+ );
+ unset( $conn2 );
+
+ $factory->closeAll();
+ $factory->destroy();
+ }
+
private function quoteTable( Database $db, $table ) {
if ( $db->getType() === 'sqlite' ) {
return $table;