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 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
83 self
::createUnicodeString( '\u0001a\uFFFFb' )
86 self
::createUnicodeString( '`\u0001\uFFFF`' ),
87 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
90 [ '`メインページ`', 'メインページ' ],
91 [ '`Басты_бет`', 'Басты_бет' ],
94 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
95 [ '`Backtick: ```', 'Backtick: `' ],
96 [ '`This is a test`', 'This is a test' ],
100 private static function createUnicodeString( $str ) {
101 return json_decode( '"' . $str . '"' );
104 private function getMockForViews() {
105 $db = $this->getMockBuilder( DatabaseMysqli
::class )
106 ->disableOriginalConstructor()
107 ->setMethods( [ 'fetchRow', 'query' ] )
110 $db->method( 'query' )
111 ->with( $this->anything() )
112 ->willReturn( new FakeResultWrapper( [
113 (object)[ 'Tables_in_' => 'view1' ],
114 (object)[ 'Tables_in_' => 'view2' ],
115 (object)[ 'Tables_in_' => 'myview' ]
122 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
124 public function testListviews() {
125 $db = $this->getMockForViews();
127 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
131 $this->assertEquals( [ 'view1', 'view2' ],
132 $db->listViews( 'view' ) );
133 $this->assertEquals( [ 'myview' ],
134 $db->listViews( 'my' ) );
135 $this->assertEquals( [],
136 $db->listViews( 'UNUSED_PREFIX' ) );
137 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
138 $db->listViews( '' ) );
142 * @covers Wikimedia\Rdbms\MySQLMasterPos
144 public function testBinLogName() {
145 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
147 $this->assertEquals( "db1052", $pos->getLogName() );
148 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
149 $this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
153 * @dataProvider provideComparePositions
154 * @covers Wikimedia\Rdbms\MySQLMasterPos
156 public function testHasReached(
157 MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match, $hetero
160 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
163 // Each position is has one channel higher than the other
164 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
166 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
168 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
169 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
170 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
171 } else { // channels don't match
172 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
174 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
175 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
179 public static function provideComparePositions() {
180 $now = microtime( true );
185 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
186 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
191 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
192 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
197 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
198 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
204 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
205 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
210 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
211 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
216 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
217 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
221 // MariaDB GTID style
223 new MySQLMasterPos( '255-11-23', $now ),
224 new MySQLMasterPos( '255-11-24', $now ),
229 new MySQLMasterPos( '255-11-99', $now ),
230 new MySQLMasterPos( '255-11-100', $now ),
235 new MySQLMasterPos( '255-11-999', $now ),
236 new MySQLMasterPos( '254-11-1000', $now ),
241 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
242 new MySQLMasterPos( '255-11-24', $now ),
247 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
248 new MySQLMasterPos( '255-11-1000', $now ),
253 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
254 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
259 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
260 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
265 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
266 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
271 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
272 new MySQLMasterPos( '254-11-1000', $now ),
280 * @dataProvider provideChannelPositions
281 * @covers Wikimedia\Rdbms\MySQLMasterPos
283 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
284 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
285 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
287 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
288 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
291 public static function provideChannelPositions() {
292 $now = microtime( true );
296 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
297 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
301 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
302 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
306 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
307 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
311 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
312 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
319 * @dataProvider provideCommonDomainGTIDs
320 * @covers Wikimedia\Rdbms\MySQLMasterPos
322 public function testCommonGtidDomains( MySQLMasterPos
$pos, MySQLMasterPos
$ref, $gtids ) {
323 $this->assertEquals( $gtids, MySQLMasterPos
::getCommonDomainGTIDs( $pos, $ref ) );
326 public static function provideCommonDomainGTIDs() {
329 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
330 new MySQLMasterPos( '255-11-1000', 1 ),
335 '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
336 '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
337 '7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
341 '1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
342 '3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
345 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
351 * @dataProvider provideLagAmounts
352 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
353 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
355 public function testPtHeartbeat( $lag ) {
356 $db = $this->getMockBuilder( DatabaseMysqli
::class )
357 ->disableOriginalConstructor()
359 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
362 $db->method( 'getLagDetectionMethod' )
363 ->willReturn( 'pt-heartbeat' );
365 $db->method( 'getMasterServerInfo' )
366 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
368 // Fake the current time.
369 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
370 $now = (float)$nowSec +
(float)$nowSecFrac;
371 // Fake the heartbeat time.
372 // Work arounds for weak DataTime microseconds support.
373 $ptTime = $now - $lag;
374 $ptSec = (int)$ptTime;
375 $ptSecFrac = ( $ptTime - $ptSec );
376 $ptDateTime = new DateTime( "@$ptSec" );
377 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
378 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
380 $db->method( 'getHeartbeatData' )
381 ->with( [ 'server_id' => 172 ] )
382 ->willReturn( [ $ptTimeISO, $now ] );
384 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
385 $lagEst = $db->getLag();
387 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
388 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
391 public static function provideLagAmounts() {
405 * @dataProvider provideGtidData
406 * @covers Wikimedia\Rdbms\MySQLMasterPos
407 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
408 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
410 public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
411 $db = $this->getMockBuilder( DatabaseMysqli
::class )
412 ->disableOriginalConstructor()
416 'getServerRoleStatus',
422 $db->method( 'useGTIDs' )->willReturn( true );
423 $db->method( 'getServerGTIDs' )->willReturn( $gtable );
424 $db->method( 'getServerRoleStatus' )->willReturnCallback(
425 function ( $role ) use ( $rBLtable, $mBLtable ) {
426 if ( $role === 'SLAVE' ) {
428 } elseif ( $role === 'MASTER' ) {
435 $db->method( 'getServerId' )->willReturn( 1 );
436 $db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
438 if ( is_array( $rGTIDs ) ) {
439 $this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
441 $this->assertEquals( false, $db->getReplicaPos() );
443 if ( is_array( $mGTIDs ) ) {
444 $this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
446 $this->assertEquals( false, $db->getMasterPos() );
450 public static function provideGtidData() {
455 'gtid_domain_id' => 100,
456 'gtid_current_pos' => '100-13-77',
457 'gtid_binlog_pos' => '100-13-77',
458 'gtid_slave_pos' => null // master
461 'Relay_Master_Log_File' => 'host.1600',
462 'Exec_Master_Log_Pos' => '77'
465 'File' => 'host.1600',
469 [ '100' => '100-13-77' ]
473 'gtid_domain_id' => 100,
474 'gtid_current_pos' => '100-13-77',
475 'gtid_binlog_pos' => '100-13-77',
476 'gtid_slave_pos' => '100-13-77' // replica
479 'Relay_Master_Log_File' => 'host.1600',
480 'Exec_Master_Log_Pos' => '77'
483 [ '100' => '100-13-77' ],
484 [ '100' => '100-13-77' ]
488 'gtid_current_pos' => '100-13-77',
489 'gtid_binlog_pos' => '100-13-77',
490 'gtid_slave_pos' => '100-13-77' // replica
493 'Relay_Master_Log_File' => 'host.1600',
494 'Exec_Master_Log_Pos' => '77'
497 [ '100' => '100-13-77' ],
498 [ '100' => '100-13-77' ]
503 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77'
506 'Relay_Master_Log_File' => 'host.1600',
507 'Exec_Master_Log_Pos' => '77'
509 [], // only a replica
510 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
511 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
512 // replica/master use same var
513 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
514 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
518 'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' .
519 '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77'
522 'Relay_Master_Log_File' => 'host.1600',
523 'Exec_Master_Log_Pos' => '77'
525 [], // only a replica
526 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
527 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
528 // replica/master use same var
529 [ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
530 => '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
534 'gtid_executed' => null, // not enabled?
535 'gtid_binlog_pos' => null
538 'Relay_Master_Log_File' => 'host.1600',
539 'Exec_Master_Log_Pos' => '77'
541 [], // only a replica
542 [], // binlog fallback
547 'gtid_executed' => null, // not enabled?
548 'gtid_binlog_pos' => null
550 [], // no replication
551 [], // no replication
559 * @covers Wikimedia\Rdbms\MySQLMasterPos
561 public function testSerialize() {
562 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
563 $roundtripPos = unserialize( serialize( $pos ) );
565 $this->assertEquals( $pos, $roundtripPos );
567 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
568 $roundtripPos = unserialize( serialize( $pos ) );
570 $this->assertEquals( $pos, $roundtripPos );
574 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
575 * @dataProvider provideInsertSelectCases
577 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
578 $db = $this->getMockBuilder( DatabaseMysqli
::class )
579 ->disableOriginalConstructor()
580 ->setMethods( [ 'getReplicationSafetyInfo' ] )
582 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
583 $dbw = TestingAccessWrapper
::newFromObject( $db );
585 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
588 public function provideInsertSelectCases() {
594 'innodb_autoinc_lock_mode' => '2',
595 'binlog_format' => 'ROW',
603 'innodb_autoinc_lock_mode' => '2',
604 'binlog_format' => 'ROW',
612 'innodb_autoinc_lock_mode' => '0',
613 'binlog_format' => 'STATEMENT',
621 'innodb_autoinc_lock_mode' => '2',
622 'binlog_format' => 'STATEMENT',
627 [ 'NO_AUTO_COLUMNS' ],
630 'innodb_autoinc_lock_mode' => '0',
631 'binlog_format' => 'STATEMENT',
639 'innodb_autoinc_lock_mode' => 0,
640 'binlog_format' => 'STATEMENT',
645 [ 'NO_AUTO_COLUMNS' ],
648 'innodb_autoinc_lock_mode' => 2,
649 'binlog_format' => 'STATEMENT',
654 [ 'NO_AUTO_COLUMNS' ],
657 'innodb_autoinc_lock_mode' => 0,
658 'binlog_format' => 'STATEMENT',
667 * @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
669 public function testBuildIntegerCast() {
670 $db = $this->getMockBuilder( DatabaseMysqli
::class )
671 ->disableOriginalConstructor()
674 $output = $db->buildIntegerCast( 'fieldName' );
675 $this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
679 * @covers Wikimedia\Rdbms\Database::setIndexAliases
681 public function testIndexAliases() {
682 $db = $this->getMockBuilder( DatabaseMysqli
::class )
683 ->disableOriginalConstructor()
684 ->setMethods( [ 'mysqlRealEscapeString' ] )
686 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
688 return str_replace( "'", "\\'", $s );
692 $db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
693 $sql = $db->selectSQLText(
694 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
697 "SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
701 $db->setIndexAliases( [] );
702 $sql = $db->selectSQLText(
703 'zend', 'field', [ 'a' => 'x' ], __METHOD__
, [ 'USE INDEX' => 'a_b_idx' ] );
706 "SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
712 * @covers Wikimedia\Rdbms\Database::setTableAliases
714 public function testTableAliases() {
715 $db = $this->getMockBuilder( DatabaseMysqli
::class )
716 ->disableOriginalConstructor()
717 ->setMethods( [ 'mysqlRealEscapeString' ] )
719 $db->method( 'mysqlRealEscapeString' )->willReturnCallback(
721 return str_replace( "'", "\\'", $s );
725 $db->setTableAliases( [
726 'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
728 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
731 "SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
735 $db->setTableAliases( [] );
736 $sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__
);
739 "SELECT field FROM `meow` WHERE a = 'x' ",