* Cleanup setDomainPrefix() methods to handle existing LBs/DBs.
* This also makes it set just the prefix as the prefix rather
than as the whole domain.
* Fix regression from
789a54a5f1 where "read" mode of
tablePrefix()/dbSchema() still set the field.
* Make getConnectionRef() always have the domain set.
* Add a check in openConnection() for explicit local
connections, treating $domain as false and avoiding
openForeignConnection().
Bug: T145840
Change-Id: Idf392bd9992a215c4fa3bddf275562f3916596aa
$lbFactory = wfGetLBFactory();
$lbFactory->setDomainPrefix( $prefix );
- $lbFactory->forEachLB( function( LoadBalancer $lb ) use ( $prefix ) {
- $lb->setDomainPrefix( $prefix );
- $lb->forEachOpenConnection( function ( IDatabase $db ) use ( $prefix ) {
- $db->tablePrefix( $prefix );
- } );
- } );
$wgDBprefix = $prefix;
}
}
protected $mTrxEndCallbacksSuppressed = false;
/** @var string */
- protected $mTablePrefix;
+ protected $mTablePrefix = '';
/** @var string */
- protected $mSchema;
+ protected $mSchema = '';
/** @var integer */
protected $mFlags;
/** @var array */
$p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
$p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : '';
if ( !isset( $p['schema'] ) ) {
- $p['schema'] = isset( $defaultSchemas[$dbType] ) ? $defaultSchemas[$dbType] : null;
+ $p['schema'] = isset( $defaultSchemas[$dbType] ) ? $defaultSchemas[$dbType] : '';
}
$p['foreign'] = isset( $p['foreign'] ) ? $p['foreign'] : false;
public function tablePrefix( $prefix = null ) {
$old = $this->mTablePrefix;
- $this->mTablePrefix = $prefix;
+ if ( $prefix !== null ) {
+ $this->mTablePrefix = $prefix;
+ }
return $old;
}
public function dbSchema( $schema = null ) {
$old = $this->mSchema;
- $this->mSchema = $schema;
+ if ( $schema !== null ) {
+ $this->mSchema = $schema;
+ }
return $old;
}
}
public function getWikiID() {
- if ( $this->mTablePrefix ) {
+ if ( $this->mTablePrefix != '' ) {
return "{$this->mDBname}-{$this->mTablePrefix}";
} else {
return $this->mDBname;
# In dbs that support it, $database may actually be the schema
# but that doesn't affect any of the functionality here
$prefix = '';
- $schema = null;
+ $schema = '';
} else {
list( $table ) = $dbDetails;
if ( isset( $this->tableAliases[$table] ) ) {
}
/**
- * Define a new local domain (for testing)
+ * Set a new table prefix for the existing local domain ID for testing
*
- * Caller should make sure no local connection are open to the old local domain
- *
- * @param string $domain
+ * @param string $prefix
* @since 1.28
*/
- public function setDomainPrefix( $domain ) {
- $this->localDomain = $domain;
+ public function setDomainPrefix( $prefix ) {
+ list( $dbName, ) = explode( '-', $this->localDomain, 2 );
+ $this->localDomain = "{$dbName}-{$prefix}";
+
+ $this->forEachLB( function( LoadBalancer $lb ) use ( $prefix ) {
+ $lb->setDomainPrefix( $prefix );
+ } );
}
/**
}
if ( $domain === $this->localDomain ) {
- $domain = false;
+ $domain = false; // local connection requested
}
$groups = ( $groups === false || $groups === [] )
* @since 1.22
*/
public function getConnectionRef( $db, $groups = [], $domain = false ) {
+ $domain = ( $domain !== false ) ? $domain : $this->localDomain;
+
return new DBConnRef( $this, $this->getConnection( $db, $groups, $domain ) );
}
}
public function openConnection( $i, $domain = false ) {
+ if ( $domain === $this->localDomain ) {
+ $domain = false; // local connection requested
+ }
+
if ( $domain !== false ) {
$conn = $this->openForeignConnection( $i, $domain );
} elseif ( isset( $this->mConns['local'][$i][0] ) ) {
*/
public function setDomainPrefix( $prefix ) {
list( $dbName, ) = explode( '-', $this->localDomain, 2 );
-
$this->localDomain = "{$dbName}-{$prefix}";
+
+ $this->forEachOpenConnection( function ( IDatabase $db ) use ( $prefix ) {
+ $db->tablePrefix( $prefix );
+ } );
}
}
$this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
$this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
}
+
+ /**
+ * @covers DatabaseBase::tablePrefix()
+ * @covers DatabaseBase::dbSchema()
+ */
+ public function testMutators() {
+ $old = $this->db->tablePrefix();
+ $this->assertType( 'string', $old, 'Prefix is string' );
+ $this->assertEquals( $old, $this->db->tablePrefix(), "Prefix unchanged" );
+ $this->assertEquals( $old, $this->db->tablePrefix( 'xxx' ) );
+ $this->assertEquals( 'xxx', $this->db->tablePrefix(), "Prefix set" );
+ $this->db->tablePrefix( $old );
+ $this->assertNotEquals( 'xxx', $this->db->tablePrefix() );
+
+ $old = $this->db->dbSchema();
+ $this->assertType( 'string', $old, 'Schema is string' );
+ $this->assertEquals( $old, $this->db->dbSchema(), "Schema unchanged" );
+ $this->assertEquals( $old, $this->db->dbSchema( 'xxx' ) );
+ $this->assertEquals( 'xxx', $this->db->dbSchema(), "Schema set" );
+ $this->db->dbSchema( $old );
+ $this->assertNotEquals( 'xxx', $this->db->dbSchema() );
+ }
}