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
;
34 * @dataProvider provideDiapers
35 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
37 public function testAddIdentifierQuotes( $expected, $in ) {
38 $db = $this->getMockBuilder( DatabaseMysqli
::class )
39 ->disableOriginalConstructor()
43 $quoted = $db->addIdentifierQuotes( $in );
44 $this->assertEquals( $expected, $quoted );
48 * Feeds testAddIdentifierQuotes
50 * Named per T22281 convention.
52 public static function provideDiapers() {
54 // Format: expected, input
57 // Yeah I really hate loosely typed PHP idiocies nowadays
60 // Dear codereviewer, guess what addIdentifierQuotes()
61 // will return with thoses:
65 // We never know what could happen
69 // Whatchout! Should probably use something more meaningful
70 [ "`'`", "'" ], # single quote
71 [ '`"`', '"' ], # double quote
72 [ '````', '`' ], # backtick
73 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
75 // sneaky NUL bytes are lurking everywhere
77 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
81 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
82 self
::createUnicodeString( '\u0001a\uFFFFb' )
85 self
::createUnicodeString( '`\u0001\uFFFF`' ),
86 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
89 [ '`メインページ`', 'メインページ' ],
90 [ '`Басты_бет`', 'Басты_бет' ],
93 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
94 [ '`Backtick: ```', 'Backtick: `' ],
95 [ '`This is a test`', 'This is a test' ],
99 private static function createUnicodeString( $str ) {
100 return json_decode( '"' . $str . '"' );
103 private function getMockForViews() {
104 $db = $this->getMockBuilder( DatabaseMysqli
::class )
105 ->disableOriginalConstructor()
106 ->setMethods( [ 'fetchRow', 'query' ] )
109 $db->method( 'query' )
110 ->with( $this->anything() )
111 ->willReturn( new FakeResultWrapper( [
112 (object)[ 'Tables_in_' => 'view1' ],
113 (object)[ 'Tables_in_' => 'view2' ],
114 (object)[ 'Tables_in_' => 'myview' ]
121 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
123 public function testListviews() {
124 $db = $this->getMockForViews();
126 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
130 $this->assertEquals( [ 'view1', 'view2' ],
131 $db->listViews( 'view' ) );
132 $this->assertEquals( [ 'myview' ],
133 $db->listViews( 'my' ) );
134 $this->assertEquals( [],
135 $db->listViews( 'UNUSED_PREFIX' ) );
136 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
137 $db->listViews( '' ) );
141 * @covers Wikimedia\Rdbms\MySQLMasterPos
143 public function testBinLogName() {
144 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
146 $this->assertEquals( "db1052", $pos->getLogName() );
147 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
148 $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
152 * @dataProvider provideComparePositions
153 * @covers Wikimedia\Rdbms\MySQLMasterPos
155 public function testHasReached(
156 MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match, $hetero
159 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
162 // Each position is has one channel higher than the other
163 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
165 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
167 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
168 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
169 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
170 } else { // channels don't match
171 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
173 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
174 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
178 public static function provideComparePositions() {
179 $now = microtime( true );
184 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
185 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
190 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
191 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
196 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
197 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
203 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
204 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
209 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
210 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
215 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
216 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
220 // MariaDB GTID style
222 new MySQLMasterPos( '255-11-23', $now ),
223 new MySQLMasterPos( '255-11-24', $now ),
228 new MySQLMasterPos( '255-11-99', $now ),
229 new MySQLMasterPos( '255-11-100', $now ),
234 new MySQLMasterPos( '255-11-999', $now ),
235 new MySQLMasterPos( '254-11-1000', $now ),
240 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
241 new MySQLMasterPos( '255-11-24', $now ),
246 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
247 new MySQLMasterPos( '255-11-1000', $now ),
252 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
253 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
258 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
259 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
264 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
265 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
270 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
271 new MySQLMasterPos( '254-11-1000', $now ),
279 * @dataProvider provideChannelPositions
280 * @covers Wikimedia\Rdbms\MySQLMasterPos
282 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
283 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
284 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
286 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
287 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
290 public static function provideChannelPositions() {
291 $now = microtime( true );
295 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
296 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
300 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
301 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
305 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
306 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
310 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
311 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
318 * @dataProvider provideCommonDomainGTIDs
319 * @covers Wikimedia\Rdbms\MySQLMasterPos
321 public function testCommonGtidDomains( MySQLMasterPos
$pos, MySQLMasterPos
$ref, $gtids ) {
322 $this->assertEquals( $gtids, MySQLMasterPos
::getCommonDomainGTIDs( $pos, $ref ) );
325 public static function provideCommonDomainGTIDs() {
328 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
329 new MySQLMasterPos( '255-11-1000', 1 ),
334 '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
335 '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
336 '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
340 '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
341 '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
344 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
350 * @dataProvider provideLagAmounts
351 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
352 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
354 public function testPtHeartbeat( $lag ) {
355 $db = $this->getMockBuilder( DatabaseMysqli
::class )
356 ->disableOriginalConstructor()
358 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
361 $db->method( 'getLagDetectionMethod' )
362 ->willReturn( 'pt-heartbeat' );
364 $db->method( 'getMasterServerInfo' )
365 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
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
461 'File' => 'host.1600',
464 [ '100' => '100-13-77' ],
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?
533 'Relay_Master_Log_File' => 'host.1600',
534 'Exec_Master_Log_Pos' => '77'
536 [], // only a replica
537 [], // binlog fallback
542 'gtid_executed' => null // not enabled?
544 [], // no replication
545 [], // no replication
553 * @covers Wikimedia\Rdbms\MySQLMasterPos
555 public function testSerialize() {
556 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
557 $roundtripPos = unserialize( serialize( $pos ) );
559 $this->assertEquals( $pos, $roundtripPos );
561 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
562 $roundtripPos = unserialize( serialize( $pos ) );
564 $this->assertEquals( $pos, $roundtripPos );
568 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
569 * @dataProvider provideInsertSelectCases
571 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
572 $db = $this->getMockBuilder( DatabaseMysqli
::class )
573 ->disableOriginalConstructor()
574 ->setMethods( [ 'getReplicationSafetyInfo' ] )
576 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
577 $dbw = TestingAccessWrapper
::newFromObject( $db );
579 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
582 public function provideInsertSelectCases() {
588 'innodb_autoinc_lock_mode' => '2',
589 'binlog_format' => 'ROW',
597 'innodb_autoinc_lock_mode' => '2',
598 'binlog_format' => 'ROW',
606 'innodb_autoinc_lock_mode' => '0',
607 'binlog_format' => 'STATEMENT',
615 'innodb_autoinc_lock_mode' => '2',
616 'binlog_format' => 'STATEMENT',
621 [ 'NO_AUTO_COLUMNS' ],
624 'innodb_autoinc_lock_mode' => '0',
625 'binlog_format' => 'STATEMENT',
633 'innodb_autoinc_lock_mode' => 0,
634 'binlog_format' => 'STATEMENT',
639 [ 'NO_AUTO_COLUMNS' ],
642 'innodb_autoinc_lock_mode' => 2,
643 'binlog_format' => 'STATEMENT',
648 [ 'NO_AUTO_COLUMNS' ],
651 'innodb_autoinc_lock_mode' => 0,
652 'binlog_format' => 'STATEMENT',
661 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
663 public function testBuildIntegerCast() {
664 $db = $this->getMockBuilder( DatabaseMysqli
::class )
665 ->disableOriginalConstructor()
668 $output = $db->buildIntegerCast( 'fieldName' );
669 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
673 * @covers Wikimedia\Rdbms\Database::setIndexAliases
675 public function testIndexAliases() {
676 $db = $this->getMockBuilder( DatabaseMysqli
::class )
677 ->disableOriginalConstructor()
678 ->setMethods( [ 'mysqlRealEscapeString' ] )
680 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
682 return str_replace( "'", "\\'", $s );
686 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
687 $sql = $db->selectSQLText(
688 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
691 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
695 $db->setIndexAliases( [] );
696 $sql = $db->selectSQLText(
697 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
700 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
706 * @covers Wikimedia\Rdbms\Database::setTableAliases
708 public function testTableAliases() {
709 $db = $this->getMockBuilder( DatabaseMysqli
::class )
710 ->disableOriginalConstructor()
711 ->setMethods( [ 'mysqlRealEscapeString' ] )
713 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
715 return str_replace( "'", "\\'", $s );
719 $db->setTableAliases( [
720 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
722 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
725 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
729 $db->setTableAliases( [] );
730 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
733 "SELECT field FROM `meow` WHERE a = 'x' ",