Remove unused Database::isView()/clearViewsCache() methods
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 23 Sep 2016 03:11:18 +0000 (20:11 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 24 Sep 2016 03:32:36 +0000 (20:32 -0700)
Also make getLazyMasterHandle() protected.

Change-Id: Id6b48ff976a800052c22e90b572695ab3b8beb7e

includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabaseMysqlBase.php
includes/libs/rdbms/database/IMaintainableDatabase.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/db/DatabaseMysqlBaseTest.php

index 5cf7458..350194c 100644 (file)
@@ -211,12 +211,6 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
        private $lazyMasterHandle;
 
-       /**
-        * @since 1.22
-        * @var string[] Process cache of VIEWs names in the database
-        */
-       protected $allViews = null;
-
        /** @var float UNIX timestamp */
        protected $lastPing = 0.0;
 
@@ -500,7 +494,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
         * @see setLazyMasterHandle()
         * @since 1.27
         */
-       public function getLazyMasterHandle() {
+       protected function getLazyMasterHandle() {
                return $this->lazyMasterHandle;
        }
 
@@ -2867,30 +2861,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
        }
 
-       /**
-        * Reset the views process cache set by listViews()
-        * @since 1.22
-        */
-       final public function clearViewsCache() {
-               $this->allViews = null;
-       }
-
        public function listViews( $prefix = null, $fname = __METHOD__ ) {
                throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
        }
 
-       /**
-        * Differentiates between a TABLE and a VIEW
-        *
-        * @param string $name Name of the database-structure to test.
-        * @throws RuntimeException
-        * @return bool
-        * @since 1.22
-        */
-       public function isView( $name ) {
-               throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
-       }
-
        public function timestamp( $ts = 0 ) {
                $t = new ConvertibleTimestamp( $ts );
                // Let errors bubble up to avoid putting garbage in the DB
index 675bc87..1efc6c8 100644 (file)
@@ -1296,26 +1296,22 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * @since 1.22
         */
        public function listViews( $prefix = null, $fname = __METHOD__ ) {
+               // The name of the column containing the name of the VIEW
+               $propertyName = 'Tables_in_' . $this->mDBname;
 
-               if ( !isset( $this->allViews ) ) {
-
-                       // The name of the column containing the name of the VIEW
-                       $propertyName = 'Tables_in_' . $this->mDBname;
-
-                       // Query for the VIEWS
-                       $result = $this->query( 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"' );
-                       $this->allViews = [];
-                       while ( ( $row = $this->fetchRow( $result ) ) !== false ) {
-                               array_push( $this->allViews, $row[$propertyName] );
-                       }
+               // Query for the VIEWS
+               $res = $this->query( 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"' );
+               $allViews = [];
+               foreach ( $res as $row ) {
+                       array_push( $allViews, $row->$propertyName );
                }
 
                if ( is_null( $prefix ) || $prefix === '' ) {
-                       return $this->allViews;
+                       return $allViews;
                }
 
                $filteredViews = [];
-               foreach ( $this->allViews as $viewName ) {
+               foreach ( $allViews as $viewName ) {
                        // Does the name of this VIEW start with the table-prefix?
                        if ( strpos( $viewName, $prefix ) === 0 ) {
                                array_push( $filteredViews, $viewName );
index 7b03b5d..f65e0dd 100644 (file)
@@ -180,9 +180,6 @@ interface IMaintainableDatabase extends IDatabase {
        /**
         * Lists all the VIEWs in the database
         *
-        * For caching purposes the list of all views should be stored in
-        * $this->allViews. The process cache can be cleared with clearViewsCache()
-        *
         * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
         * @param string $fname Name of calling function
         * @throws RuntimeException
index 3cfd0b6..c952229 100644 (file)
@@ -1313,14 +1313,17 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         *
         * @return array
         */
-       public static function listTables( $db ) {
+       public static function listTables( DatabaseBase $db ) {
                $prefix = $db->tablePrefix();
                $tables = $db->listTables( $prefix, __METHOD__ );
 
                if ( $db->getType() === 'mysql' ) {
-                       # bug 43571: cannot clone VIEWs under MySQL
-                       $views = $db->listViews( $prefix, __METHOD__ );
-                       $tables = array_diff( $tables, $views );
+                       static $viewListCache = null;
+                       if ( $viewListCache === null ) {
+                               $viewListCache = $db->listViews( null, __METHOD__ );
+                       }
+                       // T45571: cannot clone VIEWs under MySQL
+                       $tables = array_diff( $tables, $viewListCache );
                }
                array_walk( $tables, [ __CLASS__, 'unprefixTable' ], $prefix );
 
index 9480c2d..c014e84 100644 (file)
@@ -178,16 +178,12 @@ class DatabaseMysqlBaseTest extends MediaWikiTestCase {
 
                $db->method( 'query' )
                        ->with( $this->anything() )
-                       ->willReturn( null );
+                       ->willReturn( new FakeResultWrapper( [
+                               (object)[ 'Tables_in_' => 'view1' ],
+                               (object)[ 'Tables_in_' => 'view2' ],
+                               (object)[ 'Tables_in_' => 'myview' ]
+                       ] ) );
 
-               $db->method( 'fetchRow' )
-                       ->with( $this->anything() )
-                       ->will( $this->onConsecutiveCalls(
-                               [ 'Tables_in_' => 'view1' ],
-                               [ 'Tables_in_' => 'view2' ],
-                               [ 'Tables_in_' => 'myview' ],
-                               false  # no more rows
-                       ) );
                return $db;
        }
        /**
@@ -196,9 +192,6 @@ class DatabaseMysqlBaseTest extends MediaWikiTestCase {
        function testListviews() {
                $db = $this->getMockForViews();
 
-               // The first call populate an internal cache of views
-               $this->assertEquals( [ 'view1', 'view2', 'myview' ],
-                       $db->listViews() );
                $this->assertEquals( [ 'view1', 'view2', 'myview' ],
                        $db->listViews() );
 
@@ -213,42 +206,6 @@ class DatabaseMysqlBaseTest extends MediaWikiTestCase {
                        $db->listViews( '' ) );
        }
 
-       /**
-        * @covers DatabaseMysqlBase::isView
-        * @dataProvider provideViewExistanceChecks
-        */
-       function testIsView( $isView, $viewName ) {
-               $db = $this->getMockForViews();
-
-               switch ( $isView ) {
-                       case true:
-                               $this->assertTrue( $db->isView( $viewName ),
-                                       "$viewName should be considered a view" );
-                       break;
-
-                       case false:
-                               $this->assertFalse( $db->isView( $viewName ),
-                                       "$viewName has not been defined as a view" );
-                       break;
-               }
-
-       }
-
-       function provideViewExistanceChecks() {
-               return [
-                       // format: whether it is a view, view name
-                       [ true, 'view1' ],
-                       [ true, 'view2' ],
-                       [ true, 'myview' ],
-
-                       [ false, 'user' ],
-
-                       [ false, 'view10' ],
-                       [ false, 'my' ],
-                       [ false, 'OH_MY_GOD' ],  # they killed kenny!
-               ];
-       }
-
        /**
         * @dataProvider provideComparePositions
         */