From 508d3b21eae7ac1d5c1fe93721d84c762c599360 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 22 Sep 2016 20:11:18 -0700 Subject: [PATCH] Remove unused Database::isView()/clearViewsCache() methods Also make getLazyMasterHandle() protected. Change-Id: Id6b48ff976a800052c22e90b572695ab3b8beb7e --- includes/libs/rdbms/database/Database.php | 28 +--------- .../libs/rdbms/database/DatabaseMysqlBase.php | 22 ++++---- .../rdbms/database/IMaintainableDatabase.php | 3 -- tests/phpunit/MediaWikiTestCase.php | 11 ++-- .../includes/db/DatabaseMysqlBaseTest.php | 53 ++----------------- 5 files changed, 22 insertions(+), 95 deletions(-) diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index 5cf7458349..350194c82c 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -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 diff --git a/includes/libs/rdbms/database/DatabaseMysqlBase.php b/includes/libs/rdbms/database/DatabaseMysqlBase.php index 675bc87059..1efc6c8fd8 100644 --- a/includes/libs/rdbms/database/DatabaseMysqlBase.php +++ b/includes/libs/rdbms/database/DatabaseMysqlBase.php @@ -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 ); diff --git a/includes/libs/rdbms/database/IMaintainableDatabase.php b/includes/libs/rdbms/database/IMaintainableDatabase.php index 7b03b5d11a..f65e0dd31f 100644 --- a/includes/libs/rdbms/database/IMaintainableDatabase.php +++ b/includes/libs/rdbms/database/IMaintainableDatabase.php @@ -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 diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 3cfd0b6da9..c95222976e 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -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 ); diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php index 9480c2df2d..c014e84b0a 100644 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php @@ -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 */ -- 2.20.1