Skip to content

Commit

Permalink
PLATFORM-9062 | Replace use of PRIMARY_DB connection in CargoUtils, C…
Browse files Browse the repository at this point in the history
…argoAttach, CargoDeclare and CargoPageValues

Some of the actions are still using PRIMARY DB connection during the Read requests, and therefore we should replace them with use of the proper REPLICA DB. All of those cases are simple and shouldn't yield any errors when PRIMARY DB is not used to query the results.
  • Loading branch information
t-tomalak committed Feb 23, 2024
1 parent 0667936 commit 4c93579
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
3 changes: 1 addition & 2 deletions includes/CargoUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,7 @@ public static function tableFullyExists( $tableName ) {
return false;
}

$cdb = self::getDB();
return $cdb->tableExists( $tableName );
return CargoServices::getCargoConnectionProvider()->getConnection( DB_REPLICA )->tableExists( $tableName );
}

public static function fieldTypeToSQLType( $fieldType, $dbType, $size = null ) {
Expand Down
5 changes: 3 additions & 2 deletions includes/parserfunctions/CargoAttach.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public static function run( Parser $parser ) {
return CargoUtils::formatError( wfMessage( "cargo-notable" )->parse() );
}

$dbw = wfGetDB( DB_MASTER );
$res = $dbw->select( 'cargo_tables', 'COUNT(*) AS total', [ 'main_table' => $tableName ] );
$res = CargoServices::getCargoConnectionProvider()
->getConnection( DB_REPLICA )
->select( 'cargo_tables', 'COUNT(*) AS total', [ 'main_table' => $tableName ] );
$row = $res->fetchRow();
if ( !empty( $row ) && $row['total'] == 0 ) {
return CargoUtils::formatError( "Error: The specified table, \"$tableName\", does not exist." );
Expand Down
4 changes: 1 addition & 3 deletions includes/parserfunctions/CargoDeclare.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ public static function run( Parser $parser ) {
}

// Validate table name.

$cdb = CargoUtils::getDB();
$cdb = CargoServices::getCargoConnectionProvider()->getConnection( DB_REPLICA );

foreach ( $parentTables as $extraParams ) {
$parentTableName = $extraParams['Name'];
Expand Down Expand Up @@ -359,7 +358,6 @@ public static function run( Parser $parser ) {
// exists already - otherwise, explain that it needs to be
// created.
$text = wfMessage( 'cargo-definestable', $tableName )->text();
$cdb = CargoUtils::getDB();
if ( $cdb->tableExists( $tableName ) ) {
$ct = SpecialPage::getTitleFor( 'CargoTables' );
$pageName = $ct->getPrefixedText() . "/$tableName";
Expand Down
5 changes: 2 additions & 3 deletions includes/specials/CargoPageValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ private function getInfoForAllFields( $tableName ) {
}

public function getRowsForPageInTable( $tableName ) {
$cdb = CargoUtils::getDB();

$sqlQuery = new CargoSQLQuery();
$sqlQuery->mAliasedTableNames = [ $tableName => $tableName ];

Expand Down Expand Up @@ -197,7 +195,8 @@ public function getRowsForPageInTable( $tableName ) {
$sqlQuery->mOrigAliasedFieldNames = $aliasedFieldNames;
$sqlQuery->setDescriptionsAndTableNamesForFields();
$sqlQuery->handleDateFields();
$sqlQuery->mWhereStr = $cdb->addIdentifierQuotes( '_pageID' ) . " = " .
$sqlQuery->mWhereStr = CargoServices::getCargoConnectionProvider()->getConnection( DB_REPLICA )
->addIdentifierQuotes( '_pageID' ) . " = " .
$this->mTitle->getArticleID();

$queryResults = $sqlQuery->run();
Expand Down

0 comments on commit 4c93579

Please sign in to comment.