2013-10-08 18:14:52 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
/**
|
2017-07-21 00:14:52 +00:00
|
|
|
|
* Holds tests for DatabaseMysqlBase class.
|
2013-10-08 18:14:52 +00:00
|
|
|
|
*
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
|
*
|
|
|
|
|
|
* @file
|
|
|
|
|
|
* @author Antoine Musso
|
|
|
|
|
|
* @copyright © 2013 Antoine Musso
|
2017-06-13 16:51:53 +00:00
|
|
|
|
* @copyright © 2013 Wikimedia Foundation and contributors
|
2013-10-08 18:14:52 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
|
use Wikimedia\Rdbms\IMaintainableDatabase;
|
2017-02-08 06:48:30 +00:00
|
|
|
|
use Wikimedia\Rdbms\MySQLMasterPos;
|
2018-03-02 04:41:32 +00:00
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2017-01-26 17:42:38 +00:00
|
|
|
|
|
2018-02-17 12:29:13 +00:00
|
|
|
|
class DatabaseMysqlBaseTest extends PHPUnit\Framework\TestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
|
|
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
|
2013-10-08 18:14:52 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideDiapers
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
|
2013-10-08 18:14:52 +00:00
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
|
public function testAddIdentifierQuotes( $expected, $in ) {
|
2018-03-02 05:25:00 +00:00
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
|
->setMethods( null )
|
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
/** @var IDatabase $db */
|
2013-10-08 18:14:52 +00:00
|
|
|
|
$quoted = $db->addIdentifierQuotes( $in );
|
2013-11-19 18:03:54 +00:00
|
|
|
|
$this->assertEquals( $expected, $quoted );
|
2013-10-08 18:14:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Feeds testAddIdentifierQuotes
|
|
|
|
|
|
*
|
2017-02-20 23:45:58 +00:00
|
|
|
|
* Named per T22281 convention.
|
2013-10-08 18:14:52 +00:00
|
|
|
|
*/
|
2017-07-21 00:14:52 +00:00
|
|
|
|
public static function provideDiapers() {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
return [
|
2013-10-08 18:14:52 +00:00
|
|
|
|
// Format: expected, input
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ '``', '' ],
|
2013-10-08 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
// Yeah I really hate loosely typed PHP idiocies nowadays
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ '``', null ],
|
2013-10-08 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
// Dear codereviewer, guess what addIdentifierQuotes()
|
|
|
|
|
|
// will return with thoses:
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ '``', false ],
|
|
|
|
|
|
[ '`1`', true ],
|
2013-10-08 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
// We never know what could happen
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ '`0`', 0 ],
|
|
|
|
|
|
[ '`1`', 1 ],
|
2013-10-08 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
// Whatchout! Should probably use something more meaningful
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ "`'`", "'" ], # single quote
|
|
|
|
|
|
[ '`"`', '"' ], # double quote
|
|
|
|
|
|
[ '````', '`' ], # backtick
|
|
|
|
|
|
[ '`’`', '’' ], # apostrophe (look at your encyclopedia)
|
2013-10-08 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
// sneaky NUL bytes are lurking everywhere
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ '``', "\0" ],
|
|
|
|
|
|
[ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
|
2013-10-08 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
// unicode chars
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2018-01-08 22:18:00 +00:00
|
|
|
|
"`\u{0001}a\u{FFFF}b`",
|
|
|
|
|
|
"\u{0001}a\u{FFFF}b"
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-01-08 22:18:00 +00:00
|
|
|
|
"`\u{0001}\u{FFFF}`",
|
|
|
|
|
|
"\u{0001}\u{0000}\u{FFFF}\u{0000}"
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
|
|
|
|
|
[ '`☃`', '☃' ],
|
|
|
|
|
|
[ '`メインページ`', 'メインページ' ],
|
|
|
|
|
|
[ '`Басты_бет`', 'Басты_бет' ],
|
2013-10-08 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
// Real world:
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
|
|
|
|
|
|
[ '`Backtick: ```', 'Backtick: `' ],
|
|
|
|
|
|
[ '`This is a test`', 'This is a test' ],
|
|
|
|
|
|
];
|
2013-10-08 18:14:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
private function getMockForViews() : IMaintainableDatabase {
|
2018-01-13 00:02:09 +00:00
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
2013-05-24 15:14:40 +00:00
|
|
|
|
->disableOriginalConstructor()
|
2018-08-14 23:44:41 +00:00
|
|
|
|
->setMethods( [ 'fetchRow', 'query', 'getDBname' ] )
|
2013-05-24 15:14:40 +00:00
|
|
|
|
->getMock();
|
|
|
|
|
|
|
2016-09-14 22:51:05 +00:00
|
|
|
|
$db->method( 'query' )
|
2013-05-24 15:14:40 +00:00
|
|
|
|
->with( $this->anything() )
|
2016-09-23 03:11:18 +00:00
|
|
|
|
->willReturn( new FakeResultWrapper( [
|
|
|
|
|
|
(object)[ 'Tables_in_' => 'view1' ],
|
|
|
|
|
|
(object)[ 'Tables_in_' => 'view2' ],
|
|
|
|
|
|
(object)[ 'Tables_in_' => 'myview' ]
|
|
|
|
|
|
] ) );
|
2018-08-14 23:44:41 +00:00
|
|
|
|
$db->method( 'getDBname' )->willReturn( '' );
|
2013-05-24 15:14:40 +00:00
|
|
|
|
|
|
|
|
|
|
return $db;
|
|
|
|
|
|
}
|
2017-07-21 00:14:52 +00:00
|
|
|
|
|
2013-05-24 15:14:40 +00:00
|
|
|
|
/**
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::listViews
|
2013-05-24 15:14:40 +00:00
|
|
|
|
*/
|
2017-07-21 00:14:52 +00:00
|
|
|
|
public function testListviews() {
|
2013-05-24 15:14:40 +00:00
|
|
|
|
$db = $this->getMockForViews();
|
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->assertEquals( [ 'view1', 'view2', 'myview' ],
|
2013-05-24 15:14:40 +00:00
|
|
|
|
$db->listViews() );
|
|
|
|
|
|
|
|
|
|
|
|
// Prefix filtering
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->assertEquals( [ 'view1', 'view2' ],
|
2013-05-24 15:14:40 +00:00
|
|
|
|
$db->listViews( 'view' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->assertEquals( [ 'myview' ],
|
2013-05-24 15:14:40 +00:00
|
|
|
|
$db->listViews( 'my' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->assertEquals( [],
|
2013-05-24 15:14:40 +00:00
|
|
|
|
$db->listViews( 'UNUSED_PREFIX' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$this->assertEquals( [ 'view1', 'view2', 'myview' ],
|
2013-05-24 15:14:40 +00:00
|
|
|
|
$db->listViews( '' ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-03 00:36:22 +00:00
|
|
|
|
/**
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\MySQLMasterPos
|
2018-04-03 00:36:22 +00:00
|
|
|
|
*/
|
2018-02-07 09:39:24 +00:00
|
|
|
|
public function testBinLogName() {
|
|
|
|
|
|
$pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
|
|
|
|
|
|
|
2018-04-03 00:36:22 +00:00
|
|
|
|
$this->assertEquals( "db1052", $pos->getLogName() );
|
2018-02-07 09:39:24 +00:00
|
|
|
|
$this->assertEquals( "db1052.2424", $pos->getLogFile() );
|
2018-04-03 00:36:22 +00:00
|
|
|
|
$this->assertEquals( [ 2424, 4643 ], $pos->getLogPosition() );
|
2018-02-07 09:39:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-02-17 22:31:31 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideComparePositions
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\MySQLMasterPos
|
2016-02-17 22:31:31 +00:00
|
|
|
|
*/
|
2018-02-09 22:12:44 +00:00
|
|
|
|
public function testHasReached(
|
|
|
|
|
|
MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match, $hetero
|
|
|
|
|
|
) {
|
2016-05-21 00:26:08 +00:00
|
|
|
|
if ( $match ) {
|
|
|
|
|
|
$this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
|
|
|
|
|
|
|
2018-02-09 22:12:44 +00:00
|
|
|
|
if ( $hetero ) {
|
|
|
|
|
|
// Each position is has one channel higher than the other
|
|
|
|
|
|
$this->assertFalse( $higherPos->hasReached( $lowerPos ) );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->assertTrue( $higherPos->hasReached( $lowerPos ) );
|
|
|
|
|
|
}
|
2016-05-21 00:26:08 +00:00
|
|
|
|
$this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
|
2018-02-09 22:12:44 +00:00
|
|
|
|
$this->assertTrue( $higherPos->hasReached( $higherPos ) );
|
2016-05-21 00:26:08 +00:00
|
|
|
|
$this->assertFalse( $lowerPos->hasReached( $higherPos ) );
|
|
|
|
|
|
} else { // channels don't match
|
|
|
|
|
|
$this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $higherPos->hasReached( $lowerPos ) );
|
|
|
|
|
|
$this->assertFalse( $lowerPos->hasReached( $higherPos ) );
|
|
|
|
|
|
}
|
2016-02-17 22:31:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-21 00:14:52 +00:00
|
|
|
|
public static function provideComparePositions() {
|
2018-02-07 09:39:24 +00:00
|
|
|
|
$now = microtime( true );
|
|
|
|
|
|
|
2016-02-17 22:31:31 +00:00
|
|
|
|
return [
|
2016-05-21 00:26:08 +00:00
|
|
|
|
// Binlog style
|
2016-02-17 22:31:31 +00:00
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
|
|
|
|
|
|
new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
true,
|
|
|
|
|
|
false
|
2016-02-17 22:31:31 +00:00
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
|
|
|
|
|
|
new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
true,
|
|
|
|
|
|
false
|
2016-02-17 22:31:31 +00:00
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
|
|
|
|
|
|
new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
false,
|
2016-05-21 00:26:08 +00:00
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
// MySQL GTID style
|
|
|
|
|
|
[
|
2018-04-03 00:36:22 +00:00
|
|
|
|
new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-23', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-24', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
true,
|
|
|
|
|
|
false
|
2016-05-21 00:26:08 +00:00
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-04-03 00:36:22 +00:00
|
|
|
|
new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:5-99', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
true,
|
|
|
|
|
|
false
|
2016-05-21 00:26:08 +00:00
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-04-03 00:36:22 +00:00
|
|
|
|
new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:1-99', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:1-100', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
false,
|
2016-05-21 00:26:08 +00:00
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
// MariaDB GTID style
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( '255-11-23', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-24', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
true,
|
|
|
|
|
|
false
|
2016-05-21 00:26:08 +00:00
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( '255-11-99', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-100', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
true,
|
|
|
|
|
|
false
|
2016-05-21 00:26:08 +00:00
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( '255-11-999', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '254-11-1000', $now ),
|
2018-02-09 22:12:44 +00:00
|
|
|
|
false,
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos( '255-11-23,256-12-50', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-24', $now ),
|
|
|
|
|
|
true,
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-1000', $now ),
|
|
|
|
|
|
true,
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos( '255-11-23,256-12-50', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-24,155-52-63', $now ),
|
|
|
|
|
|
true,
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
|
|
|
|
|
|
true,
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos( '255-11-99,256-12-50', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
|
|
|
|
|
|
true,
|
|
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos( '253-11-999,255-11-999', $now ),
|
|
|
|
|
|
new MySQLMasterPos( '254-11-1000', $now ),
|
|
|
|
|
|
false,
|
2016-05-21 00:26:08 +00:00
|
|
|
|
false
|
2016-02-17 22:31:31 +00:00
|
|
|
|
],
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideChannelPositions
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\MySQLMasterPos
|
2016-02-17 22:31:31 +00:00
|
|
|
|
*/
|
2017-07-21 00:14:52 +00:00
|
|
|
|
public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
|
2016-02-17 22:31:31 +00:00
|
|
|
|
$this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
|
|
|
|
|
|
$this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
|
2018-02-07 09:39:24 +00:00
|
|
|
|
|
|
|
|
|
|
$roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
|
|
|
|
|
|
$this->assertEquals( (string)$pos1, (string)$roundtripPos );
|
2016-02-17 22:31:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-21 00:14:52 +00:00
|
|
|
|
public static function provideChannelPositions() {
|
2018-02-07 09:39:24 +00:00
|
|
|
|
$now = microtime( true );
|
|
|
|
|
|
|
2016-02-17 22:31:31 +00:00
|
|
|
|
return [
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
|
|
|
|
|
|
new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
|
2016-02-17 22:31:31 +00:00
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
|
|
|
|
|
|
new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
|
2016-02-17 22:31:31 +00:00
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
|
|
|
|
|
|
new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
|
2016-02-17 22:31:31 +00:00
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
2018-02-07 09:39:24 +00:00
|
|
|
|
new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
|
|
|
|
|
|
new MySQLMasterPos( 'trump2016.000976/10000', $now ),
|
2016-02-17 22:31:31 +00:00
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
];
|
2015-11-09 20:44:06 +00:00
|
|
|
|
}
|
2015-12-04 00:37:56 +00:00
|
|
|
|
|
2018-02-09 23:01:40 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideCommonDomainGTIDs
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\MySQLMasterPos
|
2018-02-09 23:01:40 +00:00
|
|
|
|
*/
|
2019-06-19 08:10:15 +00:00
|
|
|
|
public function testGetRelevantActiveGTIDs( MySQLMasterPos $pos, MySQLMasterPos $ref, $gtids ) {
|
|
|
|
|
|
$this->assertEquals( $gtids, MySQLMasterPos::getRelevantActiveGTIDs( $pos, $ref ) );
|
2018-02-09 23:01:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function provideCommonDomainGTIDs() {
|
|
|
|
|
|
return [
|
|
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-1000', 1 ),
|
|
|
|
|
|
[ '255-13-99' ]
|
|
|
|
|
|
],
|
2019-06-19 08:10:15 +00:00
|
|
|
|
[
|
|
|
|
|
|
( new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ) )
|
|
|
|
|
|
->setActiveDomain( 257 ),
|
|
|
|
|
|
new MySQLMasterPos( '255-11-1000,257-14-30', 1 ),
|
|
|
|
|
|
[ '257-14-50' ]
|
|
|
|
|
|
],
|
2018-02-09 23:01:40 +00:00
|
|
|
|
[
|
|
|
|
|
|
new MySQLMasterPos(
|
2018-04-03 00:36:22 +00:00
|
|
|
|
'2E11FA47-71CA-11E1-9E33-C80AA9429562:1-5,' .
|
|
|
|
|
|
'3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99,' .
|
|
|
|
|
|
'7E11FA47-71CA-11E1-9E33-C80AA9429562:1-30',
|
2018-02-09 23:01:40 +00:00
|
|
|
|
1
|
|
|
|
|
|
),
|
|
|
|
|
|
new MySQLMasterPos(
|
2018-04-03 00:36:22 +00:00
|
|
|
|
'1E11FA47-71CA-11E1-9E33-C80AA9429562:30-100,' .
|
|
|
|
|
|
'3E11FA47-71CA-11E1-9E33-C80AA9429562:30-66',
|
2018-02-09 23:01:40 +00:00
|
|
|
|
1
|
|
|
|
|
|
),
|
2018-04-03 00:36:22 +00:00
|
|
|
|
[ '3E11FA47-71CA-11E1-9E33-C80AA9429562:20-99' ]
|
2018-02-09 23:01:40 +00:00
|
|
|
|
]
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-04 00:37:56 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideLagAmounts
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::getLag
|
|
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
|
2015-12-04 00:37:56 +00:00
|
|
|
|
*/
|
2017-07-21 00:14:52 +00:00
|
|
|
|
public function testPtHeartbeat( $lag ) {
|
2018-01-13 00:02:09 +00:00
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
2015-12-04 00:37:56 +00:00
|
|
|
|
->disableOriginalConstructor()
|
2016-02-17 09:09:32 +00:00
|
|
|
|
->setMethods( [
|
|
|
|
|
|
'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
|
2015-12-04 00:37:56 +00:00
|
|
|
|
->getMock();
|
|
|
|
|
|
|
2016-09-14 22:51:05 +00:00
|
|
|
|
$db->method( 'getLagDetectionMethod' )
|
|
|
|
|
|
->willReturn( 'pt-heartbeat' );
|
2015-12-04 00:37:56 +00:00
|
|
|
|
|
2016-09-14 22:51:05 +00:00
|
|
|
|
$db->method( 'getMasterServerInfo' )
|
|
|
|
|
|
->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
|
2015-12-04 00:37:56 +00:00
|
|
|
|
|
|
|
|
|
|
// Fake the current time.
|
|
|
|
|
|
list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
|
|
|
|
|
|
$now = (float)$nowSec + (float)$nowSecFrac;
|
|
|
|
|
|
// Fake the heartbeat time.
|
|
|
|
|
|
// Work arounds for weak DataTime microseconds support.
|
|
|
|
|
|
$ptTime = $now - $lag;
|
|
|
|
|
|
$ptSec = (int)$ptTime;
|
|
|
|
|
|
$ptSecFrac = ( $ptTime - $ptSec );
|
|
|
|
|
|
$ptDateTime = new DateTime( "@$ptSec" );
|
|
|
|
|
|
$ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
|
|
|
|
|
|
$ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
|
|
|
|
|
|
|
2016-09-14 22:51:05 +00:00
|
|
|
|
$db->method( 'getHeartbeatData' )
|
2016-03-08 20:36:03 +00:00
|
|
|
|
->with( [ 'server_id' => 172 ] )
|
2016-09-14 22:51:05 +00:00
|
|
|
|
->willReturn( [ $ptTimeISO, $now ] );
|
2015-12-04 00:37:56 +00:00
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
/** @var IDatabase $db */
|
2015-12-04 00:37:56 +00:00
|
|
|
|
$db->setLBInfo( 'clusterMasterHost', 'db1052' );
|
|
|
|
|
|
$lagEst = $db->getLag();
|
|
|
|
|
|
|
2017-09-10 19:11:37 +00:00
|
|
|
|
$this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
|
|
|
|
|
|
$this->assertLessThan( $lag + 0.010, $lagEst, "Correct heatbeat lag" );
|
2015-12-04 00:37:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-21 00:14:52 +00:00
|
|
|
|
public static function provideLagAmounts() {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
return [
|
|
|
|
|
|
[ 0 ],
|
|
|
|
|
|
[ 0.3 ],
|
|
|
|
|
|
[ 6.5 ],
|
|
|
|
|
|
[ 10.1 ],
|
|
|
|
|
|
[ 200.2 ],
|
|
|
|
|
|
[ 400.7 ],
|
|
|
|
|
|
[ 600.22 ],
|
|
|
|
|
|
[ 1000.77 ],
|
|
|
|
|
|
];
|
2015-12-04 00:37:56 +00:00
|
|
|
|
}
|
2017-11-23 10:17:14 +00:00
|
|
|
|
|
2018-04-03 00:36:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideGtidData
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\MySQLMasterPos
|
|
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::getReplicaPos
|
|
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::getMasterPos
|
2018-04-03 00:36:22 +00:00
|
|
|
|
*/
|
|
|
|
|
|
public function testServerGtidTable( $gtable, $rBLtable, $mBLtable, $rGTIDs, $mGTIDs ) {
|
|
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
|
->setMethods( [
|
|
|
|
|
|
'useGTIDs',
|
|
|
|
|
|
'getServerGTIDs',
|
|
|
|
|
|
'getServerRoleStatus',
|
|
|
|
|
|
'getServerId',
|
|
|
|
|
|
'getServerUUID'
|
|
|
|
|
|
] )
|
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
|
|
$db->method( 'useGTIDs' )->willReturn( true );
|
|
|
|
|
|
$db->method( 'getServerGTIDs' )->willReturn( $gtable );
|
|
|
|
|
|
$db->method( 'getServerRoleStatus' )->willReturnCallback(
|
|
|
|
|
|
function ( $role ) use ( $rBLtable, $mBLtable ) {
|
|
|
|
|
|
if ( $role === 'SLAVE' ) {
|
|
|
|
|
|
return $rBLtable;
|
|
|
|
|
|
} elseif ( $role === 'MASTER' ) {
|
|
|
|
|
|
return $mBLtable;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
$db->method( 'getServerId' )->willReturn( 1 );
|
|
|
|
|
|
$db->method( 'getServerUUID' )->willReturn( '2E11FA47-71CA-11E1-9E33-C80AA9429562' );
|
|
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
/** @var DatabaseMysqlBase $db */
|
2018-04-03 00:36:22 +00:00
|
|
|
|
if ( is_array( $rGTIDs ) ) {
|
|
|
|
|
|
$this->assertEquals( $rGTIDs, $db->getReplicaPos()->getGTIDs() );
|
|
|
|
|
|
} else {
|
2019-09-17 14:19:26 +00:00
|
|
|
|
$this->assertFalse( $db->getReplicaPos() );
|
2018-04-03 00:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
if ( is_array( $mGTIDs ) ) {
|
|
|
|
|
|
$this->assertEquals( $mGTIDs, $db->getMasterPos()->getGTIDs() );
|
|
|
|
|
|
} else {
|
2019-09-17 14:19:26 +00:00
|
|
|
|
$this->assertFalse( $db->getMasterPos() );
|
2018-04-03 00:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function provideGtidData() {
|
|
|
|
|
|
return [
|
|
|
|
|
|
// MariaDB
|
|
|
|
|
|
[
|
|
|
|
|
|
[
|
|
|
|
|
|
'gtid_domain_id' => 100,
|
|
|
|
|
|
'gtid_current_pos' => '100-13-77',
|
|
|
|
|
|
'gtid_binlog_pos' => '100-13-77',
|
|
|
|
|
|
'gtid_slave_pos' => null // master
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'Relay_Master_Log_File' => 'host.1600',
|
|
|
|
|
|
'Exec_Master_Log_Pos' => '77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'File' => 'host.1600',
|
|
|
|
|
|
'Position' => '77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[ '100' => '100-13-77' ]
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[
|
|
|
|
|
|
'gtid_domain_id' => 100,
|
|
|
|
|
|
'gtid_current_pos' => '100-13-77',
|
|
|
|
|
|
'gtid_binlog_pos' => '100-13-77',
|
|
|
|
|
|
'gtid_slave_pos' => '100-13-77' // replica
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'Relay_Master_Log_File' => 'host.1600',
|
|
|
|
|
|
'Exec_Master_Log_Pos' => '77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[ '100' => '100-13-77' ],
|
|
|
|
|
|
[ '100' => '100-13-77' ]
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[
|
|
|
|
|
|
'gtid_current_pos' => '100-13-77',
|
|
|
|
|
|
'gtid_binlog_pos' => '100-13-77',
|
|
|
|
|
|
'gtid_slave_pos' => '100-13-77' // replica
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'Relay_Master_Log_File' => 'host.1600',
|
|
|
|
|
|
'Exec_Master_Log_Pos' => '77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[ '100' => '100-13-77' ],
|
|
|
|
|
|
[ '100' => '100-13-77' ]
|
|
|
|
|
|
],
|
|
|
|
|
|
// MySQL
|
|
|
|
|
|
[
|
|
|
|
|
|
[
|
|
|
|
|
|
'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'Relay_Master_Log_File' => 'host.1600',
|
|
|
|
|
|
'Exec_Master_Log_Pos' => '77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[], // only a replica
|
|
|
|
|
|
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
|
|
|
|
|
|
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
|
|
|
|
|
|
// replica/master use same var
|
|
|
|
|
|
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
|
|
|
|
|
|
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-77' ],
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[
|
|
|
|
|
|
'gtid_executed' => '2E11FA47-71CA-11E1-9E33-C80AA9429562:1-49,' .
|
|
|
|
|
|
'2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'Relay_Master_Log_File' => 'host.1600',
|
|
|
|
|
|
'Exec_Master_Log_Pos' => '77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[], // only a replica
|
|
|
|
|
|
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
|
|
|
|
|
|
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
|
|
|
|
|
|
// replica/master use same var
|
|
|
|
|
|
[ '2E11FA47-71CA-11E1-9E33-C80AA9429562'
|
|
|
|
|
|
=> '2E11FA47-71CA-11E1-9E33-C80AA9429562:51-77' ],
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[
|
|
|
|
|
|
'gtid_executed' => null, // not enabled?
|
|
|
|
|
|
'gtid_binlog_pos' => null
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
'Relay_Master_Log_File' => 'host.1600',
|
|
|
|
|
|
'Exec_Master_Log_Pos' => '77'
|
|
|
|
|
|
],
|
|
|
|
|
|
[], // only a replica
|
|
|
|
|
|
[], // binlog fallback
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[
|
|
|
|
|
|
'gtid_executed' => null, // not enabled?
|
|
|
|
|
|
'gtid_binlog_pos' => null
|
|
|
|
|
|
],
|
|
|
|
|
|
[], // no replication
|
|
|
|
|
|
[], // no replication
|
|
|
|
|
|
false,
|
|
|
|
|
|
false
|
|
|
|
|
|
]
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-23 03:23:19 +00:00
|
|
|
|
/**
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\MySQLMasterPos
|
2018-02-23 03:23:19 +00:00
|
|
|
|
*/
|
|
|
|
|
|
public function testSerialize() {
|
|
|
|
|
|
$pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
|
|
|
|
|
|
$roundtripPos = unserialize( serialize( $pos ) );
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $pos, $roundtripPos );
|
|
|
|
|
|
|
|
|
|
|
|
$pos = new MySQLMasterPos( '255-11-23', 53636363 );
|
|
|
|
|
|
$roundtripPos = unserialize( serialize( $pos ) );
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $pos, $roundtripPos );
|
|
|
|
|
|
}
|
2018-03-02 04:41:32 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
|
2018-03-02 04:41:32 +00:00
|
|
|
|
* @dataProvider provideInsertSelectCases
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
|
|
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
|
->setMethods( [ 'getReplicationSafetyInfo' ] )
|
|
|
|
|
|
->getMock();
|
|
|
|
|
|
$db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
|
|
|
|
|
|
$dbw = TestingAccessWrapper::newFromObject( $db );
|
|
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
/** @var Database $dbw */
|
2018-03-02 04:41:32 +00:00
|
|
|
|
$this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function provideInsertSelectCases() {
|
|
|
|
|
|
return [
|
|
|
|
|
|
[
|
|
|
|
|
|
[],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => '2',
|
|
|
|
|
|
'binlog_format' => 'ROW',
|
|
|
|
|
|
],
|
|
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[],
|
|
|
|
|
|
[ 'LIMIT' => 100 ],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => '2',
|
|
|
|
|
|
'binlog_format' => 'ROW',
|
|
|
|
|
|
],
|
|
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[],
|
|
|
|
|
|
[ 'LIMIT' => 100 ],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => '0',
|
|
|
|
|
|
'binlog_format' => 'STATEMENT',
|
|
|
|
|
|
],
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => '2',
|
|
|
|
|
|
'binlog_format' => 'STATEMENT',
|
|
|
|
|
|
],
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[ 'NO_AUTO_COLUMNS' ],
|
|
|
|
|
|
[ 'LIMIT' => 100 ],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => '0',
|
|
|
|
|
|
'binlog_format' => 'STATEMENT',
|
|
|
|
|
|
],
|
|
|
|
|
|
false
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => 0,
|
|
|
|
|
|
'binlog_format' => 'STATEMENT',
|
|
|
|
|
|
],
|
|
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[ 'NO_AUTO_COLUMNS' ],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => 2,
|
|
|
|
|
|
'binlog_format' => 'STATEMENT',
|
|
|
|
|
|
],
|
|
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
[ 'NO_AUTO_COLUMNS' ],
|
|
|
|
|
|
[],
|
|
|
|
|
|
[
|
|
|
|
|
|
'innodb_autoinc_lock_mode' => 0,
|
|
|
|
|
|
'binlog_format' => 'STATEMENT',
|
|
|
|
|
|
],
|
|
|
|
|
|
true
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
2018-03-04 13:50:28 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @covers \Wikimedia\Rdbms\DatabaseMysqlBase::buildIntegerCast
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testBuildIntegerCast() {
|
2018-03-02 05:25:00 +00:00
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
|
->setMethods( null )
|
|
|
|
|
|
->getMock();
|
2019-09-23 09:03:51 +00:00
|
|
|
|
|
|
|
|
|
|
/** @var IDatabase $db */
|
2018-03-04 13:50:28 +00:00
|
|
|
|
$output = $db->buildIntegerCast( 'fieldName' );
|
|
|
|
|
|
$this->assertSame( 'CAST( fieldName AS SIGNED )', $output );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-01 19:59:16 +00:00
|
|
|
|
/**
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\Database::setIndexAliases
|
2018-02-17 22:09:02 +00:00
|
|
|
|
*/
|
|
|
|
|
|
public function testIndexAliases() {
|
|
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
|
->disableOriginalConstructor()
|
2018-08-14 23:44:41 +00:00
|
|
|
|
->setMethods( [ 'mysqlRealEscapeString', 'dbSchema', 'tablePrefix' ] )
|
2018-02-17 22:09:02 +00:00
|
|
|
|
->getMock();
|
|
|
|
|
|
$db->method( 'mysqlRealEscapeString' )->willReturnCallback(
|
|
|
|
|
|
function ( $s ) {
|
|
|
|
|
|
return str_replace( "'", "\\'", $s );
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
/** @var IDatabase $db */
|
2018-02-17 22:09:02 +00:00
|
|
|
|
$db->setIndexAliases( [ 'a_b_idx' => 'a_c_idx' ] );
|
|
|
|
|
|
$sql = $db->selectSQLText(
|
|
|
|
|
|
'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
"SELECT field FROM `zend` FORCE INDEX (a_c_idx) WHERE a = 'x' ",
|
|
|
|
|
|
$sql
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$db->setIndexAliases( [] );
|
|
|
|
|
|
$sql = $db->selectSQLText(
|
|
|
|
|
|
'zend', 'field', [ 'a' => 'x' ], __METHOD__, [ 'USE INDEX' => 'a_b_idx' ] );
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
"SELECT field FROM `zend` FORCE INDEX (a_b_idx) WHERE a = 'x' ",
|
|
|
|
|
|
$sql
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-09-23 09:03:51 +00:00
|
|
|
|
* @covers \Wikimedia\Rdbms\Database::setTableAliases
|
2018-02-17 22:09:02 +00:00
|
|
|
|
*/
|
|
|
|
|
|
public function testTableAliases() {
|
|
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
|
->disableOriginalConstructor()
|
2018-08-14 23:44:41 +00:00
|
|
|
|
->setMethods( [ 'mysqlRealEscapeString', 'dbSchema', 'tablePrefix' ] )
|
2018-02-17 22:09:02 +00:00
|
|
|
|
->getMock();
|
|
|
|
|
|
$db->method( 'mysqlRealEscapeString' )->willReturnCallback(
|
|
|
|
|
|
function ( $s ) {
|
|
|
|
|
|
return str_replace( "'", "\\'", $s );
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2019-09-23 09:03:51 +00:00
|
|
|
|
/** @var IDatabase $db */
|
2018-02-17 22:09:02 +00:00
|
|
|
|
$db->setTableAliases( [
|
|
|
|
|
|
'meow' => [ 'dbname' => 'feline', 'schema' => null, 'prefix' => 'cat_' ]
|
|
|
|
|
|
] );
|
|
|
|
|
|
$sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
"SELECT field FROM `feline`.`cat_meow` WHERE a = 'x' ",
|
|
|
|
|
|
$sql
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$db->setTableAliases( [] );
|
|
|
|
|
|
$sql = $db->selectSQLText( 'meow', 'field', [ 'a' => 'x' ], __METHOD__ );
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
"SELECT field FROM `meow` WHERE a = 'x' ",
|
|
|
|
|
|
$sql
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2013-10-08 18:14:52 +00:00
|
|
|
|
}
|