Skip to content

Commit

Permalink
Merge "Make TermsDomainDb an interface"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Dec 17, 2024
2 parents 4a5258e + d499f8c commit 93746c8
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 44 deletions.
50 changes: 50 additions & 0 deletions lib/includes/Rdbms/RepoDomainTermsDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare( strict_types=1 );

namespace Wikibase\Lib\Rdbms;

use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\IReadableDatabase;

/**
* Accesses terms (labels, descriptions, aliases) database tables via {@link RepoDomainDb}.
*
* @license GPL-2.0-or-later
*/
class RepoDomainTermsDb implements TermsDomainDb {

private RepoDomainDb $repoDb;

public function __construct( RepoDomainDb $repoDb ) {
$this->repoDb = $repoDb;
}

public function getWriteConnection( int $flags = 0 ): IDatabase {
return $this->repoDb->connections()->getWriteConnection( $flags );
}

public function getReadConnection( ?array $groups = null, int $flags = 0 ): IReadableDatabase {
return $this->repoDb->connections()->getReadConnection( $groups, $flags );
}

public function waitForReplicationOfAllAffectedClusters( ?int $timeout = null ): void {
$this->repoDb->replication()->waitForAllAffectedClusters( $timeout );
}

/**
* @deprecated Don't use this unless it needs to be passed to a service we don't control
*/
public function loadBalancer(): ILoadBalancer {
return $this->repoDb->loadBalancer();
}

/**
* @deprecated Don't use this unless it needs to be passed to a service we don't control
*/
public function domain(): string {
return $this->repoDb->domain();
}

}
30 changes: 7 additions & 23 deletions lib/includes/Rdbms/TermsDomainDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,26 @@
* Database abstraction to access terms (labels, descriptions, aliases) database tables created by the WikibaseRepository extension.
* (This access may happen in repo, client, or lib.)
*
* The underlying database is either the same as {@link RepoDomainDb}, or a dedicated virtual domain database.
* The underlying database is either using a {@link RepoDomainDb}, or a dedicated virtual domain database.
*
* @license GPL-2.0-or-later
*/
class TermsDomainDb {
interface TermsDomainDb {

private RepoDomainDb $repoDb;
public function getWriteConnection( int $flags = 0 ): IDatabase;

public function __construct( RepoDomainDb $repoDb ) {
$this->repoDb = $repoDb;
}
public function getReadConnection( ?array $groups = null, int $flags = 0 ): IReadableDatabase;

public function getWriteConnection( int $flags = 0 ): IDatabase {
return $this->repoDb->connections()->getWriteConnection( $flags );
}

public function getReadConnection( ?array $groups = null, int $flags = 0 ): IReadableDatabase {
return $this->repoDb->connections()->getReadConnection( $groups, $flags );
}

public function waitForReplicationOfAllAffectedClusters( ?int $timeout = null ): void {
$this->repoDb->replication()->waitForAllAffectedClusters( $timeout );
}
public function waitForReplicationOfAllAffectedClusters( ?int $timeout = null ): void;

/**
* @deprecated Don't use this unless it needs to be passed to a service we don't control
*/
public function loadBalancer(): ILoadBalancer {
return $this->repoDb->loadBalancer();
}
public function loadBalancer(): ILoadBalancer;

/**
* @deprecated Don't use this unless it needs to be passed to a service we don't control
*/
public function domain(): string {
return $this->repoDb->domain();
}
public function domain(): string;

}
4 changes: 2 additions & 2 deletions lib/includes/Rdbms/TermsDomainDbFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function __construct( RepoDomainDbFactory $repoDomainDbFactory ) {
}

public function newTermsDb(): TermsDomainDb {
return new TermsDomainDb( $this->repoDomainDbFactory->newRepoDb() );
return new RepoDomainTermsDb( $this->repoDomainDbFactory->newRepoDb() );
}

public function newForEntitySource( DatabaseEntitySource $entitySource ): TermsDomainDb {
return new TermsDomainDb( $this->repoDomainDbFactory->newForEntitySource( $entitySource ) );
return new RepoDomainTermsDb( $this->repoDomainDbFactory->newForEntitySource( $entitySource ) );
}

}
6 changes: 3 additions & 3 deletions lib/tests/phpunit/Rdbms/LocalRepoDbTestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainTermsDb;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LBFactorySingle;
Expand All @@ -25,8 +25,8 @@ public function getRepoDomainDb( ?IDatabase $db = null ): RepoDomainDb {
);
}

public function getTermsDomainDb( ?IDatabase $db = null ): TermsDomainDb {
return new TermsDomainDb( $this->getRepoDomainDb( $db ) );
public function getTermsDomainDb( ?IDatabase $db = null ): RepoDomainTermsDb {
return new RepoDomainTermsDb( $this->getRepoDomainDb( $db ) );
}

public function getRepoDomainDbFactory( ?IDatabase $db = null ): RepoDomainDbFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

use Wikibase\Lib\Rdbms\ReplicationWaiter;
use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainTermsDb;
use Wikimedia\Rdbms\ConnectionManager;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;

/**
* @covers \Wikibase\Lib\Rdbms\TermsDomainDb
* @covers \Wikibase\Lib\Rdbms\RepoDomainTermsDb
*
* @group Wikibase
*
* @license GPL-2.0-or-later
*/
class TermsDomainDbTest extends \PHPUnit\Framework\TestCase {
class RepoDomainTermsDbTest extends \PHPUnit\Framework\TestCase {

public function testGetReadConnection(): void {
$loadGroups = [ 'some group' ];
Expand All @@ -36,7 +36,7 @@ public function testGetReadConnection(): void {

$this->assertSame(
$expected,
( new TermsDomainDb( $repoDomainDb ) )->getReadConnection( $loadGroups, $flags )
( new RepoDomainTermsDb( $repoDomainDb ) )->getReadConnection( $loadGroups, $flags )
);
}

Expand All @@ -55,7 +55,7 @@ public function testGetWriteConnection(): void {

$this->assertSame(
$expected,
( new TermsDomainDb( $repoDomainDb ) )->getWriteConnection( $flags )
( new RepoDomainTermsDb( $repoDomainDb ) )->getWriteConnection( $flags )
);
}

Expand All @@ -69,23 +69,23 @@ public function testWaitForReplication(): void {
$repoDomainDb = $this->createStub( RepoDomainDb::class );
$repoDomainDb->method( 'replication' )->willReturn( $replicationWaiter );

( new TermsDomainDb( $repoDomainDb ) )->waitForReplicationOfAllAffectedClusters( $timeout );
( new RepoDomainTermsDb( $repoDomainDb ) )->waitForReplicationOfAllAffectedClusters( $timeout );
}

public function testLoadBalancer(): void {
$expected = $this->createStub( ILoadBalancer::class );
$repoDomainDb = $this->createStub( RepoDomainDb::class );
$repoDomainDb->method( 'loadBalancer' )->willReturn( $expected );

$this->assertSame( $expected, ( new TermsDomainDb( $repoDomainDb ) )->loadBalancer() );
$this->assertSame( $expected, ( new RepoDomainTermsDb( $repoDomainDb ) )->loadBalancer() );
}

public function testDomain(): void {
$expected = 'wikidatawiki';
$repoDomainDb = $this->createStub( RepoDomainDb::class );
$repoDomainDb->method( 'domain' )->willReturn( $expected );

$this->assertSame( $expected, ( new TermsDomainDb( $repoDomainDb ) )->domain() );
$this->assertSame( $expected, ( new RepoDomainTermsDb( $repoDomainDb ) )->domain() );
}

}
6 changes: 3 additions & 3 deletions lib/tests/phpunit/Rdbms/TermsDomainDbFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Wikibase\DataAccess\DatabaseEntitySource;
use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainDbFactory;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainTermsDb;
use Wikibase\Lib\Rdbms\TermsDomainDbFactory;

/**
Expand All @@ -26,7 +26,7 @@ public function testNewTermsDb(): void {
->willReturn( $this->createStub( RepoDomainDb::class ) );

$this->assertInstanceOf(
TermsDomainDb::class,
RepoDomainTermsDb::class,
( new TermsDomainDbFactory( $repoDbFactory ) )->newTermsDb()
);
}
Expand All @@ -40,7 +40,7 @@ public function testNewForEntitySource(): void {
->willReturn( $this->createStub( RepoDomainDb::class ) );

$this->assertInstanceOf(
TermsDomainDb::class,
RepoDomainTermsDb::class,
( new TermsDomainDbFactory( $repoDbFactory ) )->newForEntitySource( $entitySource )
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\TestCase;
use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainTermsDb;
use Wikibase\Lib\Store\Sql\Terms\DatabaseTermInLangIdsAcquirer;
use Wikibase\Lib\Store\Sql\Terms\InMemoryTypeIdsStore;
use Wikibase\Lib\Tests\Rdbms\LocalRepoDbTestHelper;
Expand All @@ -30,7 +30,7 @@ class DatabaseTermInLangIdsAcquirerTest extends TestCase {
private $db;

/**
* @var TermsDomainDb
* @var RepoDomainTermsDb
*/
private $termsDb;

Expand Down Expand Up @@ -363,7 +363,7 @@ public function testIgnoresReplicaInRestoration() {
$lbFactory = new FakeLBFactory( [
'lb' => $loadBalancer,
] );
$termsDb = new TermsDomainDb( new RepoDomainDb( $lbFactory, $lbFactory->getLocalDomainID() ) );
$termsDb = new RepoDomainTermsDb( new RepoDomainDb( $lbFactory, $lbFactory->getLocalDomainID() ) );

$typeIdsAcquirer = new InMemoryTypeIdsStore();
$alreadyAcquiredTypeIds = $typeIdsAcquirer->acquireTypeIds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\TestCase;
use Wikibase\Lib\Rdbms\RepoDomainDb;
use Wikibase\Lib\Rdbms\TermsDomainDb;
use Wikibase\Lib\Rdbms\RepoDomainTermsDb;
use Wikibase\Lib\Store\Sql\Terms\Util\ReplicaPrimaryAwareRecordIdsAcquirer;
use Wikimedia\Rdbms\DatabaseSqlite;
use Wikimedia\Rdbms\IDatabase;
Expand Down Expand Up @@ -172,7 +172,7 @@ private function getTestSubjectInstance( $flags = 0x0 ) {
$lbFactory = new FakeLBFactory( [ 'lb' => $loadBalancer ] );

return new ReplicaPrimaryAwareRecordIdsAcquirer(
new TermsDomainDb( new RepoDomainDb( $lbFactory, $lbFactory->getLocalDomainID() ) ),
new RepoDomainTermsDb( new RepoDomainDb( $lbFactory, $lbFactory->getLocalDomainID() ) ),
self::TABLE_NAME,
self::ID_COLUMN,
$flags
Expand Down

0 comments on commit 93746c8

Please sign in to comment.