jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/352992 )

Change subject: Fix some postgres test failures
......................................................................


Fix some postgres test failures

Bug: T75174
Change-Id: If9812ae301f0af84fa012e2e980b92036c1b29b2
---
M includes/libs/rdbms/database/DBConnRef.php
M includes/libs/rdbms/database/Database.php
M includes/libs/rdbms/database/DatabasePostgres.php
M includes/libs/rdbms/database/IDatabase.php
M tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
M tests/phpunit/includes/db/LBFactoryTest.php
6 files changed, 49 insertions(+), 5 deletions(-)

Approvals:
  Krinkle: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/libs/rdbms/database/DBConnRef.php 
b/includes/libs/rdbms/database/DBConnRef.php
index c15572c..b6167aa 100644
--- a/includes/libs/rdbms/database/DBConnRef.php
+++ b/includes/libs/rdbms/database/DBConnRef.php
@@ -349,6 +349,10 @@
                return $this->__call( __FUNCTION__, func_get_args() );
        }
 
+       public function databasesAreIndependent() {
+               return $this->__call( __FUNCTION__, func_get_args() );
+       }
+
        public function selectDB( $db ) {
                return $this->__call( __FUNCTION__, func_get_args() );
        }
diff --git a/includes/libs/rdbms/database/Database.php 
b/includes/libs/rdbms/database/Database.php
index 3bb7e6a..3de86ac 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -1691,6 +1691,10 @@
                return $field;
        }
 
+       public function databasesAreIndependent() {
+               return false;
+       }
+
        public function selectDB( $db ) {
                # Stub. Shouldn't cause serious problems if it's not 
overridden, but
                # if your database engine supports a concept similar to MySQL's
diff --git a/includes/libs/rdbms/database/DatabasePostgres.php 
b/includes/libs/rdbms/database/DatabasePostgres.php
index b92d072..6cf890d 100644
--- a/includes/libs/rdbms/database/DatabasePostgres.php
+++ b/includes/libs/rdbms/database/DatabasePostgres.php
@@ -105,7 +105,10 @@
                $this->mDBname = $dbName;
 
                $connectVars = [
-                       'dbname' => $dbName,
+                       // pg_connect() user $user as the default database. 
Since a database is *required*,
+                       // at least pick a "don't care" database that is more 
likely to exist. This case
+                       // arrises when LoadBalancer::getConnection( $i, [], '' 
) is used.
+                       'dbname' => strlen( $dbName ) ? $dbName : 'postgres',
                        'user' => $user,
                        'password' => $password
                ];
@@ -165,11 +168,16 @@
                return $this->mConn;
        }
 
+       public function databasesAreIndependent() {
+               return true;
+       }
+
        /**
         * Postgres doesn't support selectDB in the same way MySQL does. So if 
the
         * DB name doesn't match the open connection, open a new one
         * @param string $db
         * @return bool
+        * @throws DBUnexpectedError
         */
        public function selectDB( $db ) {
                if ( $this->mDBname !== $db ) {
diff --git a/includes/libs/rdbms/database/IDatabase.php 
b/includes/libs/rdbms/database/IDatabase.php
index ac9914b..bec26a6 100644
--- a/includes/libs/rdbms/database/IDatabase.php
+++ b/includes/libs/rdbms/database/IDatabase.php
@@ -1030,10 +1030,24 @@
        public function buildStringCast( $field );
 
        /**
+        * Returns true if DBs are assumed to be on potentially different 
servers
+        *
+        * In systems like mysql/mariadb, different databases can easily be 
referenced on a single
+        * connection merely by name, even in a single query via JOIN. On the 
other hand, Postgres
+        * treats databases as fully separate, only allowing mechanisms like 
postgres_fdw to
+        * effectively "mount" foreign DBs. This is true even among DBs on the 
same server.
+        *
+        * @return bool
+        * @since 1.29
+        */
+       public function databasesAreIndependent();
+
+       /**
         * Change the current database
         *
         * @param string $db
         * @return bool Success or failure
+        * @throws DBConnectionError If databasesAreIndependent() is true and 
an error occurs
         */
        public function selectDB( $db );
 
diff --git a/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php 
b/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
index 0a2cd83..b508928 100644
--- a/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
+++ b/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
@@ -1475,6 +1475,9 @@
 
                $this->watchPages( $otherUser, [ $target ] );
 
+               $reloadedUser = User::newFromName( $otherUser->getName() );
+               $this->assertEquals( '1234567890', $reloadedUser->getOption( 
'watchlisttoken' ) );
+
                $result = $this->doListWatchlistRequest( [
                        'wlowner' => $otherUser->getName(),
                        'wltoken' => '1234567890',
diff --git a/tests/phpunit/includes/db/LBFactoryTest.php 
b/tests/phpunit/includes/db/LBFactoryTest.php
index 8b285cb..049f81f 100644
--- a/tests/phpunit/includes/db/LBFactoryTest.php
+++ b/tests/phpunit/includes/db/LBFactoryTest.php
@@ -409,16 +409,27 @@
                        "Correct full table name"
                );
 
-               \MediaWiki\suppressWarnings();
-               $this->assertFalse( $db->selectDB( 'garbage-db' ) );
-               \MediaWiki\restoreWarnings();
-
                $this->assertEquals(
                        $this->quoteTable( $db, 'garbage-db' ) . '.' . 
$this->quoteTable( $db, 'page' ),
                        $db->tableName( 'garbage-db.page' ),
                        "Correct full table name"
                );
 
+               if ( $db->databasesAreIndependent() ) {
+                       try {
+                               $e = null;
+                               $db->selectDB( 'garbage-db' );
+                       } catch ( \Wikimedia\Rdbms\DBConnectionError $e ) {
+                               // expected
+                       }
+                       $this->assertInstanceOf( 
'\Wikimedia\Rdbms\DBConnectionError', $e );
+                       $this->assertFalse( $db->isOpen() );
+               } else {
+                       \MediaWiki\suppressWarnings();
+                       $this->assertFalse( $db->selectDB( 'garbage-db' ) );
+                       \MediaWiki\restoreWarnings();
+               }
+
                $factory->closeAll();
                $factory->destroy();
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/352992
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If9812ae301f0af84fa012e2e980b92036c1b29b2
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to