Skip to content

Commit

Permalink
PLATFORM-9062 | Fix wgCargoDBIndex support
Browse files Browse the repository at this point in the history
  • Loading branch information
mszabo-wikia committed Feb 29, 2024
1 parent 081d5a9 commit e86f124
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@
"CargoDBprefix": null,
"CargoDBRowFormat": null,
"CargoDBCluster": null,
"CargoDBIndex": null,
"CargoDefaultStringBytes": 300,
"CargoDefaultQueryLimit": 100,
"CargoMaxQueryLimit": 5000,
Expand Down
7 changes: 6 additions & 1 deletion includes/CargoConnectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class CargoConnectionProvider {
'CargoDBprefix',
'CargoDBtype',

// Fandom change: Optional DB index override to use for Cargo in a single-connection setup (PLATFORM-7466)
'CargoDBIndex',

// Optional external cluster name to use for Cargo.
// Supersedes all above configuration if present.
'CargoDBCluster'
Expand Down Expand Up @@ -114,7 +117,9 @@ public function getDBType(): string {
*/
private function initConnection(): IDatabase {
$lb = $this->lbFactory->getMainLB();
$dbr = $lb->getConnection( DB_REPLICA );
// Fandom change: Use the DB index specified in the CargoDBIndex option (PLATFORM-7466).
$index = $this->serviceOptions->get( 'CargoDBIndex' ) ?? $lb::DB_PRIMARY;
$dbr = $lb->getConnection( $index );

$dbServers = $this->serviceOptions->get( 'DBservers' );
$dbUser = $this->serviceOptions->get( 'DBuser' );
Expand Down
46 changes: 45 additions & 1 deletion tests/phpunit/unit/CargoConnectionProviderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testShouldCreateAndManageConnectionBasedOnConfigVars(

$this->dbLoadBalancer->expects( $this->any() )
->method( 'getConnection' )
->with( DB_REPLICA )
->with( $serviceOptions->get( 'CargoDBIndex' ) ?? DB_PRIMARY )
->willReturn( $mainConn );

$mainConn->expects( $this->any() )
Expand Down Expand Up @@ -90,6 +90,8 @@ public static function provideConnectionConfigs(): iterable {
'CargoDBprefix' => null,
'CargoDBtype' => null,

'CargoDBIndex' => null,

'CargoDBCluster' => null,
],
'mysql',
Expand Down Expand Up @@ -120,6 +122,8 @@ public static function provideConnectionConfigs(): iterable {
'CargoDBprefix' => null,
'CargoDBtype' => null,

'CargoDBIndex' => null,

'CargoDBCluster' => null,
],
'mysql',
Expand All @@ -132,6 +136,38 @@ public static function provideConnectionConfigs(): iterable {
]
];

yield 'inferred from DBservers with CargoDBIndex override' => [
[
'DBuser' => 'db_user',
'DBpassword' => 'db_password',
'DBport' => 0,
'DBprefix' => '',
'DBservers' => [
[ 'user' => 'primary_db_user', 'password' => 'primary_db_password' ],
[ 'user' => 'replica_db_user', 'password' => 'replica_db_password' ],
],

'CargoDBserver' => null,
'CargoDBname' => null,
'CargoDBuser' => null,
'CargoDBpassword' => null,
'CargoDBprefix' => null,
'CargoDBtype' => null,

'CargoDBIndex' => DB_REPLICA,

'CargoDBCluster' => null,
],
'mysql',
[
'host' => 'localhost',
'user' => 'replica_db_user',
'password' => 'replica_db_password',
'dbname' => 'test_wiki_db',
'tablePrefix' => 'cargo__',
]
];

yield 'inferred from CargoDB overrides' => [
[
'DBuser' => 'db_user',
Expand All @@ -150,6 +186,8 @@ public static function provideConnectionConfigs(): iterable {
'CargoDBprefix' => 'cargoprefix',
'CargoDBtype' => 'postgres',

'CargoDBIndex' => null,

'CargoDBCluster' => null,
],
'postgres',
Expand Down Expand Up @@ -181,6 +219,8 @@ public static function provideConnectionConfigs(): iterable {
'CargoDBprefix' => null,
'CargoDBtype' => null,

'CargoDBIndex' => null,

'CargoDBCluster' => null,
],
'mysql',
Expand Down Expand Up @@ -212,6 +252,8 @@ public function testShouldObtainConnectionFromMediaWikiLoadBalancerIfClusterOpti
'CargoDBprefix' => null,
'CargoDBtype' => null,

'CargoDBIndex' => null,

'CargoDBCluster' => 'testCargoCluster',
] );

Expand Down Expand Up @@ -258,6 +300,8 @@ public function testShouldObtainPrimaryConnectionFromMediaWikiLoadBalancerIfClus
'CargoDBprefix' => null,
'CargoDBtype' => null,

'CargoDBIndex' => null,

'CargoDBCluster' => 'testCargoCluster',
] );

Expand Down

0 comments on commit e86f124

Please sign in to comment.