3 * Holds tests for DatabaseMysqlBase class.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
26 use Wikimedia\Rdbms\MySQLMasterPos
;
27 use Wikimedia\TestingAccessWrapper
;
29 class DatabaseMysqlBaseTest
extends PHPUnit\Framework\TestCase
{
31 use MediaWikiCoversValidator
;
32 use PHPUnit4And6Compat
;
35 * @dataProvider provideDiapers
36 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
38 public function testAddIdentifierQuotes( $expected, $in ) {
39 $db = $this->getMockBuilder( DatabaseMysqli
::class )
40 ->disableOriginalConstructor()
44 $quoted = $db->addIdentifierQuotes( $in );
45 $this->assertEquals( $expected, $quoted );
49 * Feeds testAddIdentifierQuotes
51 * Named per T22281 convention.
53 public static function provideDiapers() {
55 // Format: expected, input
58 // Yeah I really hate loosely typed PHP idiocies nowadays
61 // Dear codereviewer, guess what addIdentifierQuotes()
62 // will return with thoses:
66 // We never know what could happen
70 // Whatchout! Should probably use something more meaningful
71 [ "`'`", "'" ], # single quote
72 [ '`"`', '"' ], # double quote
73 [ '````', '`' ], # backtick
74 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
76 // sneaky NUL bytes are lurking everywhere
78 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
82 "`\u{0001}a\u{FFFF}b`",
87 "\u{0001}\u{0000}\u{FFFF}\u{0000}"
90 [ '`メインページ`', 'メインページ' ],
91 [ '`Басты_бет`', 'Басты_бет' ],
94 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
95 [ '`Backtick: ```', 'Backtick: `' ],
96 [ '`This is a test`', 'This is a test' ],
100 private function getMockForViews() {
101 $db = $this->getMockBuilder( DatabaseMysqli
::class )
102 ->disableOriginalConstructor()
103 ->setMethods( [ 'fetchRow', 'query', 'getDBname' ] )
106 $db->method( 'query' )
107 ->with( $this->anything() )
108 ->willReturn( new FakeResultWrapper( [
109 (object)[ 'Tables_in_' => 'view1' ],
110 (object)[ 'Tables_in_' => 'view2' ],
111 (object)[ 'Tables_in_' => 'myview' ]
113 $db->method( 'getDBname' )->willReturn( '' );
119 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
121 public function testListviews() {
122 $db = $this->getMockForViews();
124 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
128 $this->assertEquals( [ 'view1', 'view2' ],
129 $db->listViews( 'view' ) );
130 $this->assertEquals( [ 'myview' ],
131 $db->listViews( 'my' ) );
132 $this->assertEquals( [],
133 $db->listViews( 'UNUSED_PREFIX' ) );
134 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
135 $db->listViews( '' ) );
139 * @covers Wikimedia\Rdbms\MySQLMasterPos
141 public function testBinLogName() {
142 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
144 $this->assertEquals( "db1052", $pos->getLogName() );
145 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
146 $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
150 * @dataProvider provideComparePositions
151 * @covers Wikimedia\Rdbms\MySQLMasterPos
153 public function testHasReached(
154 MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match, $hetero
157 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
160 // Each position is has one channel higher than the other
161 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
163 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
165 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
166 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
167 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
168 } else { // channels don't match
169 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
171 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
172 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
176 public static function provideComparePositions() {
177 $now = microtime( true );
182 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
183 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
188 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
189 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
194 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
195 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
201 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
202 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
207 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
208 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
213 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
214 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
218 // MariaDB GTID style
220 new MySQLMasterPos( '255-11-23', $now ),
221 new MySQLMasterPos( '255-11-24', $now ),
226 new MySQLMasterPos( '255-11-99', $now ),
227 new MySQLMasterPos( '255-11-100', $now ),
232 new MySQLMasterPos( '255-11-999', $now ),
233 new MySQLMasterPos( '254-11-1000', $now ),
238 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
239 new MySQLMasterPos( '255-11-24', $now ),
244 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
245 new MySQLMasterPos( '255-11-1000', $now ),
250 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
251 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
256 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
257 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
262 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
263 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
268 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
269 new MySQLMasterPos( '254-11-1000', $now ),
277 * @dataProvider provideChannelPositions
278 * @covers Wikimedia\Rdbms\MySQLMasterPos
280 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
281 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
282 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
284 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
285 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
288 public static function provideChannelPositions() {
289 $now = microtime( true );
293 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
294 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
298 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
299 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
303 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
304 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
308 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
309 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
316 * @dataProvider provideCommonDomainGTIDs
317 * @covers Wikimedia\Rdbms\MySQLMasterPos
319 public function testCommonGtidDomains( MySQLMasterPos
$pos, MySQLMasterPos
$ref, $gtids ) {
320 $this->assertEquals( $gtids, MySQLMasterPos
::getCommonDomainGTIDs( $pos, $ref ) );
323 public static function provideCommonDomainGTIDs() {
326 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
327 new MySQLMasterPos( '255-11-1000', 1 ),
332 '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
333 '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
334 '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
338 '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
339 '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
342 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
348 * @dataProvider provideLagAmounts
349 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
350 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
352 public function testPtHeartbeat( $lag ) {
353 $db = $this->getMockBuilder( DatabaseMysqli
::class )
354 ->disableOriginalConstructor()
356 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
359 $db->method( 'getLagDetectionMethod' )
360 ->willReturn( 'pt-heartbeat' );
362 $db->method( 'getMasterServerInfo' )
363 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
365 $db->setLBInfo( 'replica', true );
367 // Fake the current time.
368 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
369 $now = (float)$nowSec +
(float)$nowSecFrac;
370 // Fake the heartbeat time.
371 // Work arounds for weak DataTime microseconds support.
372 $ptTime = $now - $lag;
373 $ptSec = (int)$ptTime;
374 $ptSecFrac = ( $ptTime - $ptSec );
375 $ptDateTime = new DateTime( "@$ptSec" );
376 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
377 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
379 $db->method( 'getHeartbeatData' )
380 ->with( [ 'server_id' => 172 ] )
381 ->willReturn( [ $ptTimeISO, $now ] );
383 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
384 $lagEst = $db->getLag();
386 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
387 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
390 public static function provideLagAmounts() {
404 * @dataProvider provideGtidData
405 * @covers Wikimedia\Rdbms\MySQLMasterPos
406 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
407 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
409 public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
410 $db = $this->getMockBuilder( DatabaseMysqli
::class )
411 ->disableOriginalConstructor()
415 'getServerRoleStatus',
421 $db->method( 'useGTIDs' )->willReturn( true );
422 $db->method( 'getServerGTIDs' )->willReturn( $gtable );
423 $db->method( 'getServerRoleStatus' )->willReturnCallback(
424 function ( $role ) use ( $rBLtable, $mBLtable ) {
425 if ( $role === 'SLAVE' ) {
427 } elseif ( $role === 'MASTER' ) {
434 $db->method( 'getServerId' )->willReturn( 1 );
435 $db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
437 if ( is_array( $rGTIDs ) ) {
438 $this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
440 $this->assertEquals( false, $db->getReplicaPos() );
442 if ( is_array( $mGTIDs ) ) {
443 $this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
445 $this->assertEquals( false, $db->getMasterPos() );
449 public static function provideGtidData() {
454 'gtid_domain_id' => 100,
455 'gtid_current_pos' => '100-13-77',
456 'gtid_binlog_pos' => '100-13-77',
457 'gtid_slave_pos' => null // master
460 'Relay_Master_Log_File' => 'host.1600',
461 'Exec_Master_Log_Pos' => '77'
464 'File' => 'host.1600',
468 [ '100' => '100-13-77' ]
472 'gtid_domain_id' => 100,
473 'gtid_current_pos' => '100-13-77',
474 'gtid_binlog_pos' => '100-13-77',
475 'gtid_slave_pos' => '100-13-77' // replica
478 'Relay_Master_Log_File' => 'host.1600',
479 'Exec_Master_Log_Pos' => '77'
482 [ '100' => '100-13-77' ],
483 [ '100' => '100-13-77' ]
487 'gtid_current_pos' => '100-13-77',
488 'gtid_binlog_pos' => '100-13-77',
489 'gtid_slave_pos' => '100-13-77' // replica
492 'Relay_Master_Log_File' => 'host.1600',
493 'Exec_Master_Log_Pos' => '77'
496 [ '100' => '100-13-77' ],
497 [ '100' => '100-13-77' ]
502 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77'
505 'Relay_Master_Log_File' => 'host.1600',
506 'Exec_Master_Log_Pos' => '77'
508 [], // only a replica
509 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
510 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
511 // replica/master use same var
512 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
513 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
517 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' .
518 '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77'
521 'Relay_Master_Log_File' => 'host.1600',
522 'Exec_Master_Log_Pos' => '77'
524 [], // only a replica
525 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
526 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
527 // replica/master use same var
528 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
529 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
533 'gtid_executed' => null, // not enabled?
534 'gtid_binlog_pos' => null
537 'Relay_Master_Log_File' => 'host.1600',
538 'Exec_Master_Log_Pos' => '77'
540 [], // only a replica
541 [], // binlog fallback
546 'gtid_executed' => null, // not enabled?
547 'gtid_binlog_pos' => null
549 [], // no replication
550 [], // no replication
558 * @covers Wikimedia\Rdbms\MySQLMasterPos
560 public function testSerialize() {
561 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
562 $roundtripPos = unserialize( serialize( $pos ) );
564 $this->assertEquals( $pos, $roundtripPos );
566 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
567 $roundtripPos = unserialize( serialize( $pos ) );
569 $this->assertEquals( $pos, $roundtripPos );
573 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
574 * @dataProvider provideInsertSelectCases
576 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
577 $db = $this->getMockBuilder( DatabaseMysqli
::class )
578 ->disableOriginalConstructor()
579 ->setMethods( [ 'getReplicationSafetyInfo' ] )
581 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
582 $dbw = TestingAccessWrapper
::newFromObject( $db );
584 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
587 public function provideInsertSelectCases() {
593 'innodb_autoinc_lock_mode' => '2',
594 'binlog_format' => 'ROW',
602 'innodb_autoinc_lock_mode' => '2',
603 'binlog_format' => 'ROW',
611 'innodb_autoinc_lock_mode' => '0',
612 'binlog_format' => 'STATEMENT',
620 'innodb_autoinc_lock_mode' => '2',
621 'binlog_format' => 'STATEMENT',
626 [ 'NO_AUTO_COLUMNS' ],
629 'innodb_autoinc_lock_mode' => '0',
630 'binlog_format' => 'STATEMENT',
638 'innodb_autoinc_lock_mode' => 0,
639 'binlog_format' => 'STATEMENT',
644 [ 'NO_AUTO_COLUMNS' ],
647 'innodb_autoinc_lock_mode' => 2,
648 'binlog_format' => 'STATEMENT',
653 [ 'NO_AUTO_COLUMNS' ],
656 'innodb_autoinc_lock_mode' => 0,
657 'binlog_format' => 'STATEMENT',
666 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
668 public function testBuildIntegerCast() {
669 $db = $this->getMockBuilder( DatabaseMysqli
::class )
670 ->disableOriginalConstructor()
673 $output = $db->buildIntegerCast( 'fieldName' );
674 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
678 * @covers Wikimedia\Rdbms\Database::setIndexAliases
680 public function testIndexAliases() {
681 $db = $this->getMockBuilder( DatabaseMysqli
::class )
682 ->disableOriginalConstructor()
683 ->setMethods( [ 'mysqlRealEscapeString', 'dbSchema', 'tablePrefix' ] )
685 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
687 return str_replace( "'", "\\'", $s );
691 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
692 $sql = $db->selectSQLText(
693 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
696 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
700 $db->setIndexAliases( [] );
701 $sql = $db->selectSQLText(
702 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
705 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
711 * @covers Wikimedia\Rdbms\Database::setTableAliases
713 public function testTableAliases() {
714 $db = $this->getMockBuilder( DatabaseMysqli
::class )
715 ->disableOriginalConstructor()
716 ->setMethods( [ 'mysqlRealEscapeString', 'dbSchema', 'tablePrefix' ] )
718 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
720 return str_replace( "'", "\\'", $s );
724 $db->setTableAliases( [
725 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
727 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
730 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
734 $db->setTableAliases( [] );
735 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
738 "SELECT field FROM `meow` WHERE a = 'x' ",