2013-04-13 14:40:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-04-25 08:47:27 +00:00
|
|
|
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
|
2022-05-17 19:25:35 +00:00
|
|
|
use MediaWiki\Tests\Unit\Libs\Rdbms\SQLPlatformTestHelper;
|
2019-08-22 00:45:43 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2024-07-09 13:37:44 +00:00
|
|
|
use Wikimedia\ObjectCache\HashBagOStuff;
|
2018-03-04 13:23:39 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2022-08-19 12:18:07 +00:00
|
|
|
use Wikimedia\Rdbms\Database\DatabaseFlags;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\Rdbms\DatabaseDomain;
|
2022-05-27 20:38:32 +00:00
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
2020-02-19 08:34:41 +00:00
|
|
|
use Wikimedia\Rdbms\QueryStatus;
|
2022-10-13 14:03:27 +00:00
|
|
|
use Wikimedia\Rdbms\Replication\ReplicationReporter;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\Rdbms\TransactionProfiler;
|
2020-12-01 05:08:32 +00:00
|
|
|
use Wikimedia\RequestTimeout\RequestTimeout;
|
2017-01-26 17:42:38 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
/**
|
2016-09-26 22:40:07 +00:00
|
|
|
* Helper for testing the methods from the Database class
|
2013-04-13 14:40:04 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
2016-09-26 22:40:07 +00:00
|
|
|
class DatabaseTestHelper extends Database {
|
2013-04-13 14:40:04 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-06-03 15:25:27 +00:00
|
|
|
* @var string __CLASS__ of the test suite,
|
2013-04-13 14:40:04 +00:00
|
|
|
* used to determine, if the function name is passed every time to query()
|
|
|
|
|
*/
|
2023-06-03 15:25:27 +00:00
|
|
|
protected string $testName;
|
2013-04-13 14:40:04 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-01-08 22:04:05 +00:00
|
|
|
* @var string[] Array of lastSqls passed to query(),
|
2016-09-26 22:40:07 +00:00
|
|
|
* This is an array since some methods in Database can do more than one
|
2013-04-13 14:40:04 +00:00
|
|
|
* query. Cleared when calling getLastSqls().
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $lastSqls = [];
|
2013-04-13 14:40:04 +00:00
|
|
|
|
2020-02-19 08:34:41 +00:00
|
|
|
/** @var array Stack of result maps */
|
|
|
|
|
protected $nextResMapQueue = [];
|
2016-08-27 17:10:46 +00:00
|
|
|
|
2018-04-05 18:16:03 +00:00
|
|
|
/** @var array|null */
|
2020-02-19 08:34:41 +00:00
|
|
|
protected $lastResMap = null;
|
2018-04-05 18:16:03 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
/**
|
2021-01-08 22:04:05 +00:00
|
|
|
* @var string[] Array of tables to be considered as existing by tableExist()
|
2013-04-13 14:40:04 +00:00
|
|
|
* Use setExistingTables() to alter.
|
|
|
|
|
*/
|
|
|
|
|
protected $tablesExists;
|
|
|
|
|
|
2020-03-02 22:20:09 +00:00
|
|
|
/** @var int[] */
|
|
|
|
|
protected $forcedAffectedCountQueue = [];
|
|
|
|
|
|
2023-06-03 15:25:27 +00:00
|
|
|
public function __construct( string $testName, array $opts = [] ) {
|
2022-10-13 14:03:27 +00:00
|
|
|
$params = $opts + [
|
2019-08-22 00:45:43 +00:00
|
|
|
'host' => null,
|
|
|
|
|
'user' => null,
|
|
|
|
|
'password' => null,
|
|
|
|
|
'dbname' => null,
|
|
|
|
|
'schema' => null,
|
|
|
|
|
'tablePrefix' => '',
|
|
|
|
|
'flags' => 0,
|
2019-07-14 22:43:26 +00:00
|
|
|
'cliMode' => true,
|
2019-08-22 00:45:43 +00:00
|
|
|
'agent' => '',
|
2021-03-10 01:38:23 +00:00
|
|
|
'serverName' => null,
|
2019-07-14 22:43:26 +00:00
|
|
|
'topologyRole' => null,
|
2019-08-22 00:45:43 +00:00
|
|
|
'srvCache' => new HashBagOStuff(),
|
|
|
|
|
'profiler' => null,
|
|
|
|
|
'trxProfiler' => new TransactionProfiler(),
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
'logger' => new NullLogger(),
|
2021-02-07 13:10:36 +00:00
|
|
|
'errorLogger' => static function ( Exception $e ) {
|
2022-08-05 17:50:22 +00:00
|
|
|
wfWarn( get_class( $e ) . ': ' . $e->getMessage() );
|
2019-08-22 00:45:43 +00:00
|
|
|
},
|
2021-02-07 13:10:36 +00:00
|
|
|
'deprecationLogger' => static function ( $msg ) {
|
2019-08-22 00:45:43 +00:00
|
|
|
wfWarn( $msg );
|
2020-12-01 05:08:32 +00:00
|
|
|
},
|
|
|
|
|
'criticalSectionProvider' =>
|
|
|
|
|
RequestTimeout::singleton()->createCriticalSectionProvider( 120 )
|
2022-10-13 14:03:27 +00:00
|
|
|
];
|
|
|
|
|
parent::__construct( $params );
|
2019-08-22 00:45:43 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
$this->testName = $testName;
|
2022-05-17 19:25:35 +00:00
|
|
|
$this->platform = new SQLPlatformTestHelper( new AddQuoterMock() );
|
2022-08-19 12:18:07 +00:00
|
|
|
$this->flagsHolder = new DatabaseFlags( 0 );
|
2022-10-13 14:03:27 +00:00
|
|
|
$this->replicationReporter = new ReplicationReporter(
|
|
|
|
|
$params['topologyRole'],
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
$params['logger'],
|
2022-10-13 14:03:27 +00:00
|
|
|
$params['srvCache']
|
|
|
|
|
);
|
2016-08-29 17:41:59 +00:00
|
|
|
|
2016-09-21 10:00:48 +00:00
|
|
|
$this->currentDomain = DatabaseDomain::newUnspecified();
|
2018-08-14 23:44:41 +00:00
|
|
|
$this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' );
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns SQL queries grouped by '; '
|
|
|
|
|
* Clear the list of queries that have been done so far.
|
2017-09-09 20:47:04 +00:00
|
|
|
* @return string
|
2013-04-13 14:40:04 +00:00
|
|
|
*/
|
|
|
|
|
public function getLastSqls() {
|
|
|
|
|
$lastSqls = implode( '; ', $this->lastSqls );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->lastSqls = [];
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
return $lastSqls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setExistingTables( $tablesExists ) {
|
|
|
|
|
$this->tablesExists = (array)$tablesExists;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-27 17:10:46 +00:00
|
|
|
/**
|
|
|
|
|
* @param mixed $res Use an array of row arrays to set row result
|
2018-04-05 18:16:03 +00:00
|
|
|
* @param int $errno Error number
|
|
|
|
|
* @param string $error Error text
|
|
|
|
|
* @param array $options
|
2022-03-18 00:55:52 +00:00
|
|
|
* - isKnownStatementRollbackError: Return value for isKnownStatementRollbackError()
|
2018-04-05 18:16:03 +00:00
|
|
|
*/
|
2020-02-19 08:34:41 +00:00
|
|
|
public function forceNextResult( $res, $errno = 0, $error = '', $options = [] ) {
|
|
|
|
|
$this->nextResMapQueue[] = [
|
|
|
|
|
'res' => $res,
|
|
|
|
|
'errno' => $errno,
|
|
|
|
|
'error' => $error
|
|
|
|
|
] + $options;
|
2018-04-05 18:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
protected function addSql( $sql ) {
|
|
|
|
|
// clean up spaces before and after some words and the whole string
|
|
|
|
|
$this->lastSqls[] = trim( preg_replace(
|
|
|
|
|
'/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
|
|
|
|
|
' ', $sql
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function checkFunctionName( $fname ) {
|
2018-03-23 09:57:21 +00:00
|
|
|
if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
|
|
|
|
|
return; // no $fname parameter
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 18:16:03 +00:00
|
|
|
// Handle some internal calls from the Database class
|
|
|
|
|
$check = $fname;
|
2019-03-14 01:56:26 +00:00
|
|
|
if ( preg_match(
|
|
|
|
|
'/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/',
|
|
|
|
|
$fname,
|
|
|
|
|
$m
|
|
|
|
|
) ) {
|
2018-04-05 18:16:03 +00:00
|
|
|
$check = $m[1];
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 12:16:32 +00:00
|
|
|
if ( !str_starts_with( $check, $this->testName ) ) {
|
2023-06-03 15:25:27 +00:00
|
|
|
throw new LogicException( 'function name does not start with test class. ' .
|
2013-04-13 14:40:04 +00:00
|
|
|
$fname . ' vs. ' . $this->testName . '. ' .
|
|
|
|
|
'Please provide __METHOD__ to database methods.' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function strencode( $s ) {
|
2013-04-13 14:40:04 +00:00
|
|
|
// Choose apos to avoid handling of escaping double quotes in quoted text
|
|
|
|
|
return str_replace( "'", "\'", $s );
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 04:04:35 +00:00
|
|
|
public function query( $sql, $fname = '', $flags = 0 ) {
|
2013-04-13 14:40:04 +00:00
|
|
|
$this->checkFunctionName( $fname );
|
|
|
|
|
|
2019-03-20 04:04:35 +00:00
|
|
|
return parent::query( $sql, $fname, $flags );
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function tableExists( $table, $fname = __METHOD__ ) {
|
2024-04-11 22:26:23 +00:00
|
|
|
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table );
|
|
|
|
|
if ( isset( $this->sessionTempTables[$db][$pt] ) ) {
|
2016-09-21 17:44:15 +00:00
|
|
|
return true; // already known to exist
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
$this->checkFunctionName( $fname );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
return in_array( $table, (array)$this->tablesExists );
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function getType() {
|
2013-04-13 14:40:04 +00:00
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 01:38:23 +00:00
|
|
|
public function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
|
2018-03-23 09:57:21 +00:00
|
|
|
$this->conn = (object)[ 'test' ];
|
|
|
|
|
|
|
|
|
|
return true;
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-28 22:10:31 +00:00
|
|
|
protected function lastInsertId() {
|
2013-04-13 14:40:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function lastErrno() {
|
2020-02-19 08:34:41 +00:00
|
|
|
return $this->lastResMap ? $this->lastResMap['errno'] : -1;
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function lastError() {
|
2020-02-19 08:34:41 +00:00
|
|
|
return $this->lastResMap ? $this->lastResMap['error'] : 'test';
|
2018-04-05 18:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 00:55:52 +00:00
|
|
|
protected function isKnownStatementRollbackError( $errno ) {
|
2020-02-19 08:34:41 +00:00
|
|
|
return ( $this->lastResMap['errno'] ?? 0 ) === $errno
|
|
|
|
|
? ( $this->lastResMap['isKnownStatementRollbackError'] ?? false )
|
2022-03-18 00:55:52 +00:00
|
|
|
: false;
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function fieldInfo( $table, $field ) {
|
2013-04-13 14:40:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
|
2013-04-13 14:40:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function getSoftwareLink() {
|
2013-04-13 14:40:04 +00:00
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function getServerVersion() {
|
2013-04-13 14:40:04 +00:00
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function getServerInfo() {
|
2013-04-13 14:40:04 +00:00
|
|
|
return 'test';
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function ping( &$rtt = null ) {
|
2016-09-11 22:51:15 +00:00
|
|
|
$rtt = 0.0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 14:40:04 +00:00
|
|
|
protected function closeConnection() {
|
2018-03-23 09:57:21 +00:00
|
|
|
return true;
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 22:20:09 +00:00
|
|
|
public function setNextQueryAffectedRowCounts( array $counts ) {
|
|
|
|
|
$this->forcedAffectedCountQueue = $counts;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 08:34:41 +00:00
|
|
|
protected function doSingleStatementQuery( string $sql ): QueryStatus {
|
2018-04-05 18:16:03 +00:00
|
|
|
$sql = preg_replace( '< /\* .+? \*/>', '', $sql );
|
|
|
|
|
$this->addSql( $sql );
|
|
|
|
|
|
2020-02-19 08:34:41 +00:00
|
|
|
if ( $this->nextResMapQueue ) {
|
|
|
|
|
$this->lastResMap = array_shift( $this->nextResMapQueue );
|
|
|
|
|
if ( !$this->lastResMap['errno'] && $this->forcedAffectedCountQueue ) {
|
2022-07-28 22:10:31 +00:00
|
|
|
$count = array_shift( $this->forcedAffectedCountQueue );
|
|
|
|
|
$this->lastQueryAffectedRows = $count;
|
2020-02-19 08:34:41 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->lastResMap = [ 'res' => [], 'errno' => 0, 'error' => '' ];
|
2018-04-05 18:16:03 +00:00
|
|
|
}
|
2020-02-19 08:34:41 +00:00
|
|
|
$res = $this->lastResMap['res'];
|
|
|
|
|
|
|
|
|
|
return new QueryStatus(
|
|
|
|
|
is_bool( $res ) ? $res : new FakeResultWrapper( $res ),
|
|
|
|
|
$this->affectedRows(),
|
|
|
|
|
$this->lastError(),
|
|
|
|
|
$this->lastErrno()
|
|
|
|
|
);
|
2013-04-13 14:40:04 +00:00
|
|
|
}
|
|
|
|
|
}
|