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' ] )
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' ]
118 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
120 public function testListviews() {
121 $db = $this->getMockForViews();
123 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
127 $this->assertEquals( [ 'view1', 'view2' ],
128 $db->listViews( 'view' ) );
129 $this->assertEquals( [ 'myview' ],
130 $db->listViews( 'my' ) );
131 $this->assertEquals( [],
132 $db->listViews( 'UNUSED_PREFIX' ) );
133 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
134 $db->listViews( '' ) );
138 * @covers Wikimedia\Rdbms\MySQLMasterPos
140 public function testBinLogName() {
141 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
143 $this->assertEquals( "db1052", $pos->getLogName() );
144 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
145 $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
149 * @dataProvider provideComparePositions
150 * @covers Wikimedia\Rdbms\MySQLMasterPos
152 public function testHasReached(
153 MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match, $hetero
156 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
159 // Each position is has one channel higher than the other
160 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
162 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
164 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
165 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
166 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
167 } else { // channels don't match
168 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
170 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
171 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
175 public static function provideComparePositions() {
176 $now = microtime( true );
181 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
182 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
187 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
188 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
193 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
194 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
200 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
201 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
206 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
207 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
212 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
213 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
217 // MariaDB GTID style
219 new MySQLMasterPos( '255-11-23', $now ),
220 new MySQLMasterPos( '255-11-24', $now ),
225 new MySQLMasterPos( '255-11-99', $now ),
226 new MySQLMasterPos( '255-11-100', $now ),
231 new MySQLMasterPos( '255-11-999', $now ),
232 new MySQLMasterPos( '254-11-1000', $now ),
237 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
238 new MySQLMasterPos( '255-11-24', $now ),
243 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
244 new MySQLMasterPos( '255-11-1000', $now ),
249 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
250 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
255 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
256 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
261 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
262 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
267 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
268 new MySQLMasterPos( '254-11-1000', $now ),
276 * @dataProvider provideChannelPositions
277 * @covers Wikimedia\Rdbms\MySQLMasterPos
279 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
280 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
281 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
283 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
284 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
287 public static function provideChannelPositions() {
288 $now = microtime( true );
292 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
293 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
297 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
298 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
302 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
303 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
307 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
308 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
315 * @dataProvider provideCommonDomainGTIDs
316 * @covers Wikimedia\Rdbms\MySQLMasterPos
318 public function testCommonGtidDomains( MySQLMasterPos
$pos, MySQLMasterPos
$ref, $gtids ) {
319 $this->assertEquals( $gtids, MySQLMasterPos
::getCommonDomainGTIDs( $pos, $ref ) );
322 public static function provideCommonDomainGTIDs() {
325 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
326 new MySQLMasterPos( '255-11-1000', 1 ),
331 '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
332 '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
333 '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
337 '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
338 '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
341 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
347 * @dataProvider provideLagAmounts
348 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
349 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
351 public function testPtHeartbeat( $lag ) {
352 $db = $this->getMockBuilder( DatabaseMysqli
::class )
353 ->disableOriginalConstructor()
355 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
358 $db->method( 'getLagDetectionMethod' )
359 ->willReturn( 'pt-heartbeat' );
361 $db->method( 'getMasterServerInfo' )
362 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
364 // Fake the current time.
365 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
366 $now = (float)$nowSec +
(float)$nowSecFrac;
367 // Fake the heartbeat time.
368 // Work arounds for weak DataTime microseconds support.
369 $ptTime = $now - $lag;
370 $ptSec = (int)$ptTime;
371 $ptSecFrac = ( $ptTime - $ptSec );
372 $ptDateTime = new DateTime( "@$ptSec" );
373 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
374 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
376 $db->method( 'getHeartbeatData' )
377 ->with( [ 'server_id' => 172 ] )
378 ->willReturn( [ $ptTimeISO, $now ] );
380 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
381 $lagEst = $db->getLag();
383 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
384 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
387 public static function provideLagAmounts() {
401 * @dataProvider provideGtidData
402 * @covers Wikimedia\Rdbms\MySQLMasterPos
403 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
404 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
406 public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
407 $db = $this->getMockBuilder( DatabaseMysqli
::class )
408 ->disableOriginalConstructor()
412 'getServerRoleStatus',
418 $db->method( 'useGTIDs' )->willReturn( true );
419 $db->method( 'getServerGTIDs' )->willReturn( $gtable );
420 $db->method( 'getServerRoleStatus' )->willReturnCallback(
421 function ( $role ) use ( $rBLtable, $mBLtable ) {
422 if ( $role === 'SLAVE' ) {
424 } elseif ( $role === 'MASTER' ) {
431 $db->method( 'getServerId' )->willReturn( 1 );
432 $db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
434 if ( is_array( $rGTIDs ) ) {
435 $this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
437 $this->assertEquals( false, $db->getReplicaPos() );
439 if ( is_array( $mGTIDs ) ) {
440 $this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
442 $this->assertEquals( false, $db->getMasterPos() );
446 public static function provideGtidData() {
451 'gtid_domain_id' => 100,
452 'gtid_current_pos' => '100-13-77',
453 'gtid_binlog_pos' => '100-13-77',
454 'gtid_slave_pos' => null // master
457 'Relay_Master_Log_File' => 'host.1600',
458 'Exec_Master_Log_Pos' => '77'
461 'File' => 'host.1600',
465 [ '100' => '100-13-77' ]
469 'gtid_domain_id' => 100,
470 'gtid_current_pos' => '100-13-77',
471 'gtid_binlog_pos' => '100-13-77',
472 'gtid_slave_pos' => '100-13-77' // replica
475 'Relay_Master_Log_File' => 'host.1600',
476 'Exec_Master_Log_Pos' => '77'
479 [ '100' => '100-13-77' ],
480 [ '100' => '100-13-77' ]
484 'gtid_current_pos' => '100-13-77',
485 'gtid_binlog_pos' => '100-13-77',
486 'gtid_slave_pos' => '100-13-77' // replica
489 'Relay_Master_Log_File' => 'host.1600',
490 'Exec_Master_Log_Pos' => '77'
493 [ '100' => '100-13-77' ],
494 [ '100' => '100-13-77' ]
499 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77'
502 'Relay_Master_Log_File' => 'host.1600',
503 'Exec_Master_Log_Pos' => '77'
505 [], // only a replica
506 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
507 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
508 // replica/master use same var
509 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
510 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
514 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' .
515 '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77'
518 'Relay_Master_Log_File' => 'host.1600',
519 'Exec_Master_Log_Pos' => '77'
521 [], // only a replica
522 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
523 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
524 // replica/master use same var
525 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
526 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
530 'gtid_executed' => null, // not enabled?
531 'gtid_binlog_pos' => null
534 'Relay_Master_Log_File' => 'host.1600',
535 'Exec_Master_Log_Pos' => '77'
537 [], // only a replica
538 [], // binlog fallback
543 'gtid_executed' => null, // not enabled?
544 'gtid_binlog_pos' => null
546 [], // no replication
547 [], // no replication
555 * @covers Wikimedia\Rdbms\MySQLMasterPos
557 public function testSerialize() {
558 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
559 $roundtripPos = unserialize( serialize( $pos ) );
561 $this->assertEquals( $pos, $roundtripPos );
563 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
564 $roundtripPos = unserialize( serialize( $pos ) );
566 $this->assertEquals( $pos, $roundtripPos );
570 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
571 * @dataProvider provideInsertSelectCases
573 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
574 $db = $this->getMockBuilder( DatabaseMysqli
::class )
575 ->disableOriginalConstructor()
576 ->setMethods( [ 'getReplicationSafetyInfo' ] )
578 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
579 $dbw = TestingAccessWrapper
::newFromObject( $db );
581 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
584 public function provideInsertSelectCases() {
590 'innodb_autoinc_lock_mode' => '2',
591 'binlog_format' => 'ROW',
599 'innodb_autoinc_lock_mode' => '2',
600 'binlog_format' => 'ROW',
608 'innodb_autoinc_lock_mode' => '0',
609 'binlog_format' => 'STATEMENT',
617 'innodb_autoinc_lock_mode' => '2',
618 'binlog_format' => 'STATEMENT',
623 [ 'NO_AUTO_COLUMNS' ],
626 'innodb_autoinc_lock_mode' => '0',
627 'binlog_format' => 'STATEMENT',
635 'innodb_autoinc_lock_mode' => 0,
636 'binlog_format' => 'STATEMENT',
641 [ 'NO_AUTO_COLUMNS' ],
644 'innodb_autoinc_lock_mode' => 2,
645 'binlog_format' => 'STATEMENT',
650 [ 'NO_AUTO_COLUMNS' ],
653 'innodb_autoinc_lock_mode' => 0,
654 'binlog_format' => 'STATEMENT',
663 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
665 public function testBuildIntegerCast() {
666 $db = $this->getMockBuilder( DatabaseMysqli
::class )
667 ->disableOriginalConstructor()
670 $output = $db->buildIntegerCast( 'fieldName' );
671 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
675 * @covers Wikimedia\Rdbms\Database::setIndexAliases
677 public function testIndexAliases() {
678 $db = $this->getMockBuilder( DatabaseMysqli
::class )
679 ->disableOriginalConstructor()
680 ->setMethods( [ 'mysqlRealEscapeString' ] )
682 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
684 return str_replace( "'", "\\'", $s );
688 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
689 $sql = $db->selectSQLText(
690 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
693 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
697 $db->setIndexAliases( [] );
698 $sql = $db->selectSQLText(
699 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
702 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
708 * @covers Wikimedia\Rdbms\Database::setTableAliases
710 public function testTableAliases() {
711 $db = $this->getMockBuilder( DatabaseMysqli
::class )
712 ->disableOriginalConstructor()
713 ->setMethods( [ 'mysqlRealEscapeString' ] )
715 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
717 return str_replace( "'", "\\'", $s );
721 $db->setTableAliases( [
722 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
724 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
727 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
731 $db->setTableAliases( [] );
732 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
735 "SELECT field FROM `meow` WHERE a = 'x' ",