2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-03-08 04:10:13 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2018-02-28 20:56:34 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2018-03-08 04:10:13 +00:00
|
|
|
use Wikimedia\Rdbms\DatabaseMysqli;
|
2017-07-26 09:04:34 +00:00
|
|
|
use Wikimedia\Rdbms\LBFactorySingle;
|
2017-07-20 20:17:11 +00:00
|
|
|
use Wikimedia\Rdbms\TransactionProfiler;
|
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2018-02-28 20:56:34 +00:00
|
|
|
use Wikimedia\Rdbms\DatabaseSqlite;
|
|
|
|
|
use Wikimedia\Rdbms\DatabasePostgres;
|
|
|
|
|
use Wikimedia\Rdbms\DatabaseMssql;
|
2017-02-10 18:09:05 +00:00
|
|
|
|
2018-02-17 12:29:13 +00:00
|
|
|
class DatabaseTest extends PHPUnit\Framework\TestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2017-12-29 23:22:37 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2017-07-20 20:17:11 +00:00
|
|
|
$this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() );
|
2012-01-11 20:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-28 20:56:34 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideAddQuotes
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::factory
|
|
|
|
|
*/
|
|
|
|
|
public function testFactory() {
|
|
|
|
|
$m = Database::NEW_UNCONNECTED; // no-connect mode
|
|
|
|
|
$p = [ 'host' => 'localhost', 'user' => 'me', 'password' => 'myself', 'dbname' => 'i' ];
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf( DatabaseMysqli::class, Database::factory( 'mysqli', $p, $m ) );
|
|
|
|
|
$this->assertInstanceOf( DatabaseMysqli::class, Database::factory( 'MySqli', $p, $m ) );
|
|
|
|
|
$this->assertInstanceOf( DatabaseMysqli::class, Database::factory( 'MySQLi', $p, $m ) );
|
|
|
|
|
$this->assertInstanceOf( DatabasePostgres::class, Database::factory( 'postgres', $p, $m ) );
|
|
|
|
|
$this->assertInstanceOf( DatabasePostgres::class, Database::factory( 'Postgres', $p, $m ) );
|
|
|
|
|
|
|
|
|
|
$x = $p + [ 'port' => 10000, 'UseWindowsAuth' => false ];
|
|
|
|
|
$this->assertInstanceOf( DatabaseMssql::class, Database::factory( 'mssql', $x, $m ) );
|
|
|
|
|
|
|
|
|
|
$x = $p + [ 'dbFilePath' => 'some/file.sqlite' ];
|
|
|
|
|
$this->assertInstanceOf( DatabaseSqlite::class, Database::factory( 'sqlite', $x, $m ) );
|
|
|
|
|
$x = $p + [ 'dbDirectory' => 'some/file' ];
|
|
|
|
|
$this->assertInstanceOf( DatabaseSqlite::class, Database::factory( 'sqlite', $x, $m ) );
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 20:17:11 +00:00
|
|
|
public static function provideAddQuotes() {
|
|
|
|
|
return [
|
|
|
|
|
[ null, 'NULL' ],
|
|
|
|
|
[ 1234, "'1234'" ],
|
|
|
|
|
[ 1234.5678, "'1234.5678'" ],
|
|
|
|
|
[ 'string', "'string'" ],
|
|
|
|
|
[ 'string\'s cause trouble', "'string\'s cause trouble'" ],
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2016-09-15 21:40:00 +00:00
|
|
|
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
2017-07-20 20:17:11 +00:00
|
|
|
* @dataProvider provideAddQuotes
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::addQuotes
|
2013-10-18 10:36:09 +00:00
|
|
|
*/
|
2017-07-20 20:17:11 +00:00
|
|
|
public function testAddQuotes( $input, $expected ) {
|
|
|
|
|
$this->assertEquals( $expected, $this->db->addQuotes( $input ) );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-20 20:17:11 +00:00
|
|
|
public static function provideTableName() {
|
|
|
|
|
// Formatting is mostly ignored since addIdentifierQuotes is abstract.
|
|
|
|
|
// For testing of addIdentifierQuotes, see actual Database subclas tests.
|
|
|
|
|
return [
|
|
|
|
|
'local' => [
|
|
|
|
|
'tablename',
|
|
|
|
|
'tablename',
|
|
|
|
|
'quoted',
|
|
|
|
|
],
|
|
|
|
|
'local-raw' => [
|
|
|
|
|
'tablename',
|
|
|
|
|
'tablename',
|
|
|
|
|
'raw',
|
|
|
|
|
],
|
|
|
|
|
'shared' => [
|
|
|
|
|
'sharedb.tablename',
|
|
|
|
|
'tablename',
|
|
|
|
|
'quoted',
|
|
|
|
|
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
|
|
|
|
|
],
|
|
|
|
|
'shared-raw' => [
|
|
|
|
|
'sharedb.tablename',
|
|
|
|
|
'tablename',
|
|
|
|
|
'raw',
|
|
|
|
|
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => '' ],
|
|
|
|
|
],
|
|
|
|
|
'shared-prefix' => [
|
|
|
|
|
'sharedb.sh_tablename',
|
|
|
|
|
'tablename',
|
|
|
|
|
'quoted',
|
|
|
|
|
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
|
|
|
|
|
],
|
|
|
|
|
'shared-prefix-raw' => [
|
|
|
|
|
'sharedb.sh_tablename',
|
|
|
|
|
'tablename',
|
|
|
|
|
'raw',
|
|
|
|
|
[ 'dbname' => 'sharedb', 'schema' => null, 'prefix' => 'sh_' ],
|
|
|
|
|
],
|
|
|
|
|
'foreign' => [
|
|
|
|
|
'databasename.tablename',
|
|
|
|
|
'databasename.tablename',
|
|
|
|
|
'quoted',
|
|
|
|
|
],
|
|
|
|
|
'foreign-raw' => [
|
|
|
|
|
'databasename.tablename',
|
|
|
|
|
'databasename.tablename',
|
|
|
|
|
'raw',
|
|
|
|
|
],
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-20 20:17:11 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTableName
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::tableName
|
|
|
|
|
*/
|
|
|
|
|
public function testTableName( $expected, $table, $format, array $alias = null ) {
|
|
|
|
|
if ( $alias ) {
|
|
|
|
|
$this->db->setTableAliases( [ $table => $alias ] );
|
2013-05-26 14:26:41 +00:00
|
|
|
}
|
2012-07-18 08:18:49 +00:00
|
|
|
$this->assertEquals(
|
2017-07-20 20:17:11 +00:00
|
|
|
$expected,
|
|
|
|
|
$this->db->tableName( $table, $format ?: 'quoted' )
|
2012-07-18 08:18:49 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
Database: Support parenthesized JOINs
SQL supports parentheses for grouping in the FROM clause.[1] This is
useful when you want to left-join against a join of other tables.
For example, say you have tables 'a', 'b', and 'c'. You want all rows
from 'a', along with rows from 'b' + 'c' only where both of those
exist.
SELECT * FROM a LEFT JOIN b ON (a_b = b_id) JOIN c ON (b_c = c_id)
doesn't work, it'll only give you the rows where 'c' exists.
SELECT * FROM a LEFT JOIN b ON (a_b = b_id) LEFT JOIN c ON (b_c = c_id)
doesn't work either, it'll give you rows from 'b' without a
corresponding row in 'c'. What you need to do is
SELECT * FROM a LEFT JOIN (b JOIN c ON (b_c = c_id)) ON (a_b = b_id)
This patch implements this by extending the syntax for the $table
parameter to IDatabase::select(). When passing an array of tables, if a
value in the array is itself an array that is interpreted as a request
for a parenthesized join. To produce the example above, you'd do
something like
$db->select(
[ 'a', 'nest' => [ 'b', 'c' ] ],
'*',
[],
__METHOD__,
[],
[
'c' => [ 'JOIN', 'b_c = c_id ],
'nest' => [ 'LEFT JOIN', 'a_b = b_id' ],
]
);
[1]: In standards as far back as SQL-1992 (I couldn't find an earlier
version), and it seems to be supported by at least MySQL 5.6, MariaDB
10.1.28, PostgreSQL 9.3, PostgreSQL 10.0, Oracle 11g R2, SQLite 3.20.1,
and MSSQL 2014 (from local testing and sqlfiddle.com).
Change-Id: I1e0a77381e06d885650a94f53847fb82f01c2694
2017-10-13 17:51:43 +00:00
|
|
|
public function provideTableNamesWithIndexClauseOrJOIN() {
|
|
|
|
|
return [
|
|
|
|
|
'one-element array' => [
|
|
|
|
|
[ 'table' ], [], 'table '
|
|
|
|
|
],
|
|
|
|
|
'comma join' => [
|
|
|
|
|
[ 'table1', 'table2' ], [], 'table1,table2 '
|
|
|
|
|
],
|
|
|
|
|
'real join' => [
|
|
|
|
|
[ 'table1', 'table2' ],
|
|
|
|
|
[ 'table2' => [ 'LEFT JOIN', 't1_id = t2_id' ] ],
|
|
|
|
|
'table1 LEFT JOIN table2 ON ((t1_id = t2_id))'
|
|
|
|
|
],
|
|
|
|
|
'real join with multiple conditionals' => [
|
|
|
|
|
[ 'table1', 'table2' ],
|
|
|
|
|
[ 'table2' => [ 'LEFT JOIN', [ 't1_id = t2_id', 't2_x = \'X\'' ] ] ],
|
|
|
|
|
'table1 LEFT JOIN table2 ON ((t1_id = t2_id) AND (t2_x = \'X\'))'
|
|
|
|
|
],
|
|
|
|
|
'join with parenthesized group' => [
|
|
|
|
|
[ 'table1', 'n' => [ 'table2', 'table3' ] ],
|
|
|
|
|
[
|
|
|
|
|
'table3' => [ 'JOIN', 't2_id = t3_id' ],
|
|
|
|
|
'n' => [ 'LEFT JOIN', 't1_id = t2_id' ],
|
|
|
|
|
],
|
|
|
|
|
'table1 LEFT JOIN (table2 JOIN table3 ON ((t2_id = t3_id))) ON ((t1_id = t2_id))'
|
|
|
|
|
],
|
2017-11-29 20:42:27 +00:00
|
|
|
'join with degenerate parenthesized group' => [
|
|
|
|
|
[ 'table1', 'n' => [ 't2' => 'table2' ] ],
|
|
|
|
|
[
|
|
|
|
|
'n' => [ 'LEFT JOIN', 't1_id = t2_id' ],
|
|
|
|
|
],
|
|
|
|
|
'table1 LEFT JOIN table2 t2 ON ((t1_id = t2_id))'
|
|
|
|
|
],
|
Database: Support parenthesized JOINs
SQL supports parentheses for grouping in the FROM clause.[1] This is
useful when you want to left-join against a join of other tables.
For example, say you have tables 'a', 'b', and 'c'. You want all rows
from 'a', along with rows from 'b' + 'c' only where both of those
exist.
SELECT * FROM a LEFT JOIN b ON (a_b = b_id) JOIN c ON (b_c = c_id)
doesn't work, it'll only give you the rows where 'c' exists.
SELECT * FROM a LEFT JOIN b ON (a_b = b_id) LEFT JOIN c ON (b_c = c_id)
doesn't work either, it'll give you rows from 'b' without a
corresponding row in 'c'. What you need to do is
SELECT * FROM a LEFT JOIN (b JOIN c ON (b_c = c_id)) ON (a_b = b_id)
This patch implements this by extending the syntax for the $table
parameter to IDatabase::select(). When passing an array of tables, if a
value in the array is itself an array that is interpreted as a request
for a parenthesized join. To produce the example above, you'd do
something like
$db->select(
[ 'a', 'nest' => [ 'b', 'c' ] ],
'*',
[],
__METHOD__,
[],
[
'c' => [ 'JOIN', 'b_c = c_id ],
'nest' => [ 'LEFT JOIN', 'a_b = b_id' ],
]
);
[1]: In standards as far back as SQL-1992 (I couldn't find an earlier
version), and it seems to be supported by at least MySQL 5.6, MariaDB
10.1.28, PostgreSQL 9.3, PostgreSQL 10.0, Oracle 11g R2, SQLite 3.20.1,
and MSSQL 2014 (from local testing and sqlfiddle.com).
Change-Id: I1e0a77381e06d885650a94f53847fb82f01c2694
2017-10-13 17:51:43 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTableNamesWithIndexClauseOrJOIN
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::tableNamesWithIndexClauseOrJOIN
|
|
|
|
|
*/
|
|
|
|
|
public function testTableNamesWithIndexClauseOrJOIN( $tables, $join_conds, $expect ) {
|
|
|
|
|
$clause = TestingAccessWrapper::newFromObject( $this->db )
|
|
|
|
|
->tableNamesWithIndexClauseOrJOIN( $tables, [], [], $join_conds );
|
|
|
|
|
$this->assertSame( $expect, $clause );
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 20:17:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::onTransactionIdle
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
|
|
|
|
|
*/
|
2015-12-08 01:26:15 +00:00
|
|
|
public function testTransactionIdle() {
|
|
|
|
|
$db = $this->db;
|
|
|
|
|
|
2018-03-19 05:26:11 +00:00
|
|
|
$db->clearFlag( DBO_TRX );
|
2016-07-04 18:02:42 +00:00
|
|
|
$called = false;
|
2015-12-08 01:26:15 +00:00
|
|
|
$flagSet = null;
|
2018-03-19 05:26:11 +00:00
|
|
|
$callback = function () use ( $db, &$flagSet, &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
$flagSet = $db->getFlag( DBO_TRX );
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$db->onTransactionIdle( $callback, __METHOD__ );
|
2016-07-04 18:02:42 +00:00
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
2018-03-19 05:26:11 +00:00
|
|
|
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX still default' );
|
2015-12-08 01:26:15 +00:00
|
|
|
|
|
|
|
|
$flagSet = null;
|
2018-03-19 05:26:11 +00:00
|
|
|
$called = false;
|
|
|
|
|
$db->startAtomic( __METHOD__ );
|
|
|
|
|
$db->onTransactionIdle( $callback, __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Callback not reached during TRX' );
|
|
|
|
|
$db->endAtomic( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $called, 'Callback reached after COMMIT' );
|
2015-12-08 01:26:15 +00:00
|
|
|
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
2016-09-15 21:40:00 +00:00
|
|
|
$db->onTransactionIdle(
|
|
|
|
|
function () use ( $db ) {
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
},
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2015-12-08 01:26:15 +00:00
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
}
|
2016-07-04 18:02:42 +00:00
|
|
|
|
2018-03-19 05:26:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::onTransactionIdle
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
|
|
|
|
|
*/
|
|
|
|
|
public function testTransactionIdle_TRX() {
|
|
|
|
|
$db = $this->getMockDB( [ 'isOpen', 'ping' ] );
|
|
|
|
|
$db->method( 'isOpen' )->willReturn( true );
|
|
|
|
|
$db->method( 'ping' )->willReturn( true );
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
|
|
|
|
|
$lbFactory = LBFactorySingle::newFromConnection( $db );
|
|
|
|
|
// Ask for the connection so that LB sets internal state
|
|
|
|
|
// about this connection being the master connection
|
|
|
|
|
$lb = $lbFactory->getMainLB();
|
|
|
|
|
$conn = $lb->openConnection( $lb->getWriterIndex() );
|
|
|
|
|
$this->assertSame( $db, $conn, 'Same DB instance' );
|
|
|
|
|
$this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX is set' );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$flagSet = null;
|
|
|
|
|
$callback = function () use ( $db, &$flagSet, &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
$flagSet = $db->getFlag( DBO_TRX );
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$db->onTransactionIdle( $callback, __METHOD__ );
|
|
|
|
|
$this->assertTrue( $called, 'Called when idle if DBO_TRX is set' );
|
|
|
|
|
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
|
|
|
|
|
$this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX still default' );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$lbFactory->beginMasterChanges( __METHOD__ );
|
|
|
|
|
$db->onTransactionIdle( $callback, __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Not called when lb-transaction is active' );
|
|
|
|
|
|
|
|
|
|
$lbFactory->commitMasterChanges( __METHOD__ );
|
|
|
|
|
$this->assertTrue( $called, 'Called when lb-transaction is committed' );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$lbFactory->beginMasterChanges( __METHOD__ );
|
|
|
|
|
$db->onTransactionIdle( $callback, __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Not called when lb-transaction is active' );
|
|
|
|
|
|
|
|
|
|
$lbFactory->rollbackMasterChanges( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Not called when lb-transaction is rolled back' );
|
|
|
|
|
|
|
|
|
|
$lbFactory->commitMasterChanges( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Not called in next round commit' );
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 09:04:34 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::runOnTransactionPreCommitCallbacks
|
|
|
|
|
*/
|
|
|
|
|
public function testTransactionPreCommitOrIdle() {
|
|
|
|
|
$db = $this->getMockDB( [ 'isOpen' ] );
|
|
|
|
|
$db->method( 'isOpen' )->willReturn( true );
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX is not set' );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->onTransactionPreCommitOrIdle(
|
|
|
|
|
function () use ( &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
},
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->assertTrue( $called, 'Called when idle' );
|
|
|
|
|
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->onTransactionPreCommitOrIdle(
|
|
|
|
|
function () use ( &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
},
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->assertFalse( $called, 'Not called when transaction is active' );
|
|
|
|
|
$db->commit( __METHOD__ );
|
|
|
|
|
$this->assertTrue( $called, 'Called when transaction is committed' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::onTransactionPreCommitOrIdle
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::runOnTransactionPreCommitCallbacks
|
|
|
|
|
*/
|
|
|
|
|
public function testTransactionPreCommitOrIdle_TRX() {
|
2018-03-19 05:26:11 +00:00
|
|
|
$db = $this->getMockDB( [ 'isOpen', 'ping' ] );
|
2017-07-26 09:04:34 +00:00
|
|
|
$db->method( 'isOpen' )->willReturn( true );
|
2018-03-19 05:26:11 +00:00
|
|
|
$db->method( 'ping' )->willReturn( true );
|
2017-07-26 09:04:34 +00:00
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
|
|
|
|
|
$lbFactory = LBFactorySingle::newFromConnection( $db );
|
2018-03-19 05:26:11 +00:00
|
|
|
// Ask for the connection so that LB sets internal state
|
2017-07-26 09:04:34 +00:00
|
|
|
// about this connection being the master connection
|
|
|
|
|
$lb = $lbFactory->getMainLB();
|
|
|
|
|
$conn = $lb->openConnection( $lb->getWriterIndex() );
|
|
|
|
|
$this->assertSame( $db, $conn, 'Same DB instance' );
|
|
|
|
|
$this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX is set' );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
2018-03-19 23:20:15 +00:00
|
|
|
$callback = function () use ( &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
};
|
|
|
|
|
$db->onTransactionPreCommitOrIdle( $callback, __METHOD__ );
|
2018-03-19 05:26:11 +00:00
|
|
|
$this->assertTrue( $called, 'Called when idle if DBO_TRX is set' );
|
2017-07-26 09:04:34 +00:00
|
|
|
|
2018-03-19 05:26:11 +00:00
|
|
|
$called = false;
|
2017-07-26 09:04:34 +00:00
|
|
|
$lbFactory->beginMasterChanges( __METHOD__ );
|
2018-03-19 05:26:11 +00:00
|
|
|
$db->onTransactionPreCommitOrIdle( $callback, __METHOD__ );
|
2017-07-26 09:04:34 +00:00
|
|
|
$this->assertFalse( $called, 'Not called when lb-transaction is active' );
|
|
|
|
|
$lbFactory->commitMasterChanges( __METHOD__ );
|
|
|
|
|
$this->assertTrue( $called, 'Called when lb-transaction is committed' );
|
2018-03-19 23:20:15 +00:00
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$lbFactory->beginMasterChanges( __METHOD__ );
|
|
|
|
|
$db->onTransactionPreCommitOrIdle( $callback, __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Not called when lb-transaction is active' );
|
|
|
|
|
|
|
|
|
|
$lbFactory->rollbackMasterChanges( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Not called when lb-transaction is rolled back' );
|
|
|
|
|
|
|
|
|
|
$lbFactory->commitMasterChanges( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Not called in next round commit' );
|
2017-07-26 09:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-20 20:17:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::onTransactionResolution
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::runOnTransactionIdleCallbacks
|
|
|
|
|
*/
|
2016-07-04 18:02:42 +00:00
|
|
|
public function testTransactionResolution() {
|
|
|
|
|
$db = $this->db;
|
|
|
|
|
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$called = false;
|
2016-09-15 21:40:00 +00:00
|
|
|
$db->onTransactionResolution( function () use ( $db, &$called ) {
|
2016-07-04 18:02:42 +00:00
|
|
|
$called = true;
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
} );
|
|
|
|
|
$db->commit( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
|
|
|
|
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$called = false;
|
2016-09-15 21:40:00 +00:00
|
|
|
$db->onTransactionResolution( function () use ( $db, &$called ) {
|
2016-07-04 18:02:42 +00:00
|
|
|
$called = true;
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
} );
|
2016-08-17 21:05:41 +00:00
|
|
|
$db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
|
2016-07-04 18:02:42 +00:00
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
|
|
|
|
}
|
2016-08-17 21:05:41 +00:00
|
|
|
|
2016-08-26 07:19:34 +00:00
|
|
|
/**
|
2017-07-20 20:17:11 +00:00
|
|
|
* @covers Wikimedia\Rdbms\Database::setTransactionListener
|
2016-08-26 07:19:34 +00:00
|
|
|
*/
|
|
|
|
|
public function testTransactionListener() {
|
|
|
|
|
$db = $this->db;
|
|
|
|
|
|
2016-09-15 21:40:00 +00:00
|
|
|
$db->setTransactionListener( 'ping', function () use ( $db, &$called ) {
|
2016-08-26 07:19:34 +00:00
|
|
|
$called = true;
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$db->commit( __METHOD__ );
|
|
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$db->commit( __METHOD__ );
|
|
|
|
|
$this->assertTrue( $called, 'Callback still reached' );
|
|
|
|
|
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$db->rollback( __METHOD__ );
|
|
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
|
|
|
|
|
|
|
|
|
$db->setTransactionListener( 'ping', null );
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$db->commit( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $called, 'Callback not reached' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-07-20 20:17:11 +00:00
|
|
|
* Use this mock instead of DatabaseTestHelper for cases where
|
|
|
|
|
* DatabaseTestHelper is too inflexibile due to mocking too much
|
|
|
|
|
* or being too restrictive about fname matching (e.g. for tests
|
|
|
|
|
* that assert behaviour when the name is a mismatch, we need to
|
|
|
|
|
* catch the error here instead of there).
|
|
|
|
|
*
|
|
|
|
|
* @return Database
|
|
|
|
|
*/
|
|
|
|
|
private function getMockDB( $methods = [] ) {
|
|
|
|
|
static $abstractMethods = [
|
2018-01-28 14:10:39 +00:00
|
|
|
'fetchAffectedRowCount',
|
2017-07-20 20:17:11 +00:00
|
|
|
'closeConnection',
|
|
|
|
|
'dataSeek',
|
|
|
|
|
'doQuery',
|
|
|
|
|
'fetchObject', 'fetchRow',
|
|
|
|
|
'fieldInfo', 'fieldName',
|
|
|
|
|
'getSoftwareLink', 'getServerVersion',
|
|
|
|
|
'getType',
|
|
|
|
|
'indexInfo',
|
|
|
|
|
'insertId',
|
|
|
|
|
'lastError', 'lastErrno',
|
|
|
|
|
'numFields', 'numRows',
|
|
|
|
|
'open',
|
|
|
|
|
'strencode',
|
|
|
|
|
];
|
|
|
|
|
$db = $this->getMockBuilder( Database::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->setMethods( array_values( array_unique( array_merge(
|
|
|
|
|
$abstractMethods,
|
|
|
|
|
$methods
|
|
|
|
|
) ) ) )
|
|
|
|
|
->getMock();
|
|
|
|
|
$wdb = TestingAccessWrapper::newFromObject( $db );
|
|
|
|
|
$wdb->trxProfiler = new TransactionProfiler();
|
|
|
|
|
$wdb->connLogger = new \Psr\Log\NullLogger();
|
|
|
|
|
$wdb->queryLogger = new \Psr\Log\NullLogger();
|
|
|
|
|
return $db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::flushSnapshot
|
2016-08-26 07:19:34 +00:00
|
|
|
*/
|
2016-09-08 11:28:52 +00:00
|
|
|
public function testFlushSnapshot() {
|
2017-07-20 20:17:11 +00:00
|
|
|
$db = $this->getMockDB( [ 'isOpen' ] );
|
|
|
|
|
$db->method( 'isOpen' )->willReturn( true );
|
2016-08-26 07:19:34 +00:00
|
|
|
|
2016-09-08 11:28:52 +00:00
|
|
|
$db->flushSnapshot( __METHOD__ ); // ok
|
|
|
|
|
$db->flushSnapshot( __METHOD__ ); // ok
|
2016-08-26 07:19:34 +00:00
|
|
|
|
|
|
|
|
$db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
|
|
|
|
|
$db->query( 'SELECT 1', __METHOD__ );
|
|
|
|
|
$this->assertTrue( (bool)$db->trxLevel(), "Transaction started." );
|
2016-09-08 11:28:52 +00:00
|
|
|
$db->flushSnapshot( __METHOD__ ); // ok
|
2016-08-26 07:19:34 +00:00
|
|
|
$db->restoreFlags( $db::RESTORE_PRIOR );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( (bool)$db->trxLevel(), "Transaction cleared." );
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 08:27:14 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::getScopedLockAndFlush
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::lock
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::unlock
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::lockIsFree
|
|
|
|
|
*/
|
2016-08-17 21:05:41 +00:00
|
|
|
public function testGetScopedLock() {
|
2017-07-20 20:17:11 +00:00
|
|
|
$db = $this->getMockDB( [ 'isOpen' ] );
|
|
|
|
|
$db->method( 'isOpen' )->willReturn( true );
|
2016-08-17 21:05:41 +00:00
|
|
|
|
2018-02-14 08:27:14 +00:00
|
|
|
$this->assertEquals( 0, $db->trxLevel() );
|
|
|
|
|
$this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( true, $db->lock( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( false, $db->lockIsFree( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( true, $db->unlock( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( 0, $db->trxLevel() );
|
|
|
|
|
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
$this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( true, $db->lock( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( false, $db->lockIsFree( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( true, $db->unlock( 'x', __METHOD__ ) );
|
|
|
|
|
$this->assertEquals( true, $db->lockIsFree( 'x', __METHOD__ ) );
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 0, $db->trxLevel() );
|
|
|
|
|
|
2016-08-17 21:05:41 +00:00
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
try {
|
|
|
|
|
$this->badLockingMethodImplicit( $db );
|
|
|
|
|
} catch ( RunTimeException $e ) {
|
|
|
|
|
$this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
|
|
|
|
|
}
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
$db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
|
|
|
|
|
$this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->badLockingMethodExplicit( $db );
|
|
|
|
|
} catch ( RunTimeException $e ) {
|
|
|
|
|
$this->assertTrue( $db->trxLevel() > 0, "Transaction not committed." );
|
|
|
|
|
}
|
|
|
|
|
$db->rollback( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
|
|
|
|
|
$this->assertTrue( $db->lockIsFree( 'meow', __METHOD__ ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function badLockingMethodImplicit( IDatabase $db ) {
|
|
|
|
|
$lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
|
|
|
|
|
$db->query( "SELECT 1" ); // trigger DBO_TRX
|
|
|
|
|
throw new RunTimeException( "Uh oh!" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function badLockingMethodExplicit( IDatabase $db ) {
|
|
|
|
|
$lock = $db->getScopedLockAndFlush( 'meow', __METHOD__, 1 );
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
throw new RunTimeException( "Uh oh!" );
|
|
|
|
|
}
|
2016-08-22 05:35:12 +00:00
|
|
|
|
|
|
|
|
/**
|
2017-07-20 20:17:11 +00:00
|
|
|
* @covers Wikimedia\Rdbms\Database::getFlag
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::setFlag
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::restoreFlags
|
2016-08-22 05:35:12 +00:00
|
|
|
*/
|
|
|
|
|
public function testFlagSetting() {
|
|
|
|
|
$db = $this->db;
|
|
|
|
|
$origTrx = $db->getFlag( DBO_TRX );
|
|
|
|
|
$origSsl = $db->getFlag( DBO_SSL );
|
|
|
|
|
|
2016-09-20 23:24:16 +00:00
|
|
|
$origTrx
|
|
|
|
|
? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR )
|
|
|
|
|
: $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
|
2016-08-22 05:35:12 +00:00
|
|
|
$this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
|
|
|
|
|
|
2016-09-20 23:24:16 +00:00
|
|
|
$origSsl
|
|
|
|
|
? $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR )
|
|
|
|
|
: $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
|
2016-08-22 05:35:12 +00:00
|
|
|
$this->assertEquals( !$origSsl, $db->getFlag( DBO_SSL ) );
|
|
|
|
|
|
2016-09-20 23:24:16 +00:00
|
|
|
$db->restoreFlags( $db::RESTORE_INITIAL );
|
|
|
|
|
$this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
|
|
|
|
|
$this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
|
|
|
|
|
|
|
|
|
|
$origTrx
|
|
|
|
|
? $db->clearFlag( DBO_TRX, $db::REMEMBER_PRIOR )
|
|
|
|
|
: $db->setFlag( DBO_TRX, $db::REMEMBER_PRIOR );
|
|
|
|
|
$origSsl
|
|
|
|
|
? $db->clearFlag( DBO_SSL, $db::REMEMBER_PRIOR )
|
|
|
|
|
: $db->setFlag( DBO_SSL, $db::REMEMBER_PRIOR );
|
2016-08-22 05:35:12 +00:00
|
|
|
|
|
|
|
|
$db->restoreFlags();
|
|
|
|
|
$this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
|
|
|
|
|
$this->assertEquals( !$origTrx, $db->getFlag( DBO_TRX ) );
|
|
|
|
|
|
|
|
|
|
$db->restoreFlags();
|
|
|
|
|
$this->assertEquals( $origSsl, $db->getFlag( DBO_SSL ) );
|
|
|
|
|
$this->assertEquals( $origTrx, $db->getFlag( DBO_TRX ) );
|
|
|
|
|
}
|
2016-09-16 18:32:23 +00:00
|
|
|
|
2018-03-08 04:10:13 +00:00
|
|
|
/**
|
|
|
|
|
* @expectedException UnexpectedValueException
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::setFlag
|
|
|
|
|
*/
|
|
|
|
|
public function testDBOIgnoreSet() {
|
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->setMethods( null )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$db->setFlag( Database::DBO_IGNORE );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @expectedException UnexpectedValueException
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::clearFlag
|
|
|
|
|
*/
|
|
|
|
|
public function testDBOIgnoreClear() {
|
|
|
|
|
$db = $this->getMockBuilder( DatabaseMysqli::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->setMethods( null )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$db->clearFlag( Database::DBO_IGNORE );
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:32:23 +00:00
|
|
|
/**
|
2017-07-20 20:17:11 +00:00
|
|
|
* @covers Wikimedia\Rdbms\Database::tablePrefix
|
|
|
|
|
* @covers Wikimedia\Rdbms\Database::dbSchema
|
2016-09-16 18:32:23 +00:00
|
|
|
*/
|
|
|
|
|
public function testMutators() {
|
|
|
|
|
$old = $this->db->tablePrefix();
|
2017-07-20 20:17:11 +00:00
|
|
|
$this->assertInternalType( 'string', $old, 'Prefix is string' );
|
2016-09-16 18:32:23 +00:00
|
|
|
$this->assertEquals( $old, $this->db->tablePrefix(), "Prefix unchanged" );
|
|
|
|
|
$this->assertEquals( $old, $this->db->tablePrefix( 'xxx' ) );
|
|
|
|
|
$this->assertEquals( 'xxx', $this->db->tablePrefix(), "Prefix set" );
|
|
|
|
|
$this->db->tablePrefix( $old );
|
|
|
|
|
$this->assertNotEquals( 'xxx', $this->db->tablePrefix() );
|
|
|
|
|
|
|
|
|
|
$old = $this->db->dbSchema();
|
2017-07-20 20:17:11 +00:00
|
|
|
$this->assertInternalType( 'string', $old, 'Schema is string' );
|
2016-09-16 18:32:23 +00:00
|
|
|
$this->assertEquals( $old, $this->db->dbSchema(), "Schema unchanged" );
|
|
|
|
|
$this->assertEquals( $old, $this->db->dbSchema( 'xxx' ) );
|
|
|
|
|
$this->assertEquals( 'xxx', $this->db->dbSchema(), "Schema set" );
|
|
|
|
|
$this->db->dbSchema( $old );
|
|
|
|
|
$this->assertNotEquals( 'xxx', $this->db->dbSchema() );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|