From 070e11da65dcbca61365b91b41f2be7d52252cc5 Mon Sep 17 00:00:00 2001 From: umherirrender Date: Tue, 4 Jun 2013 20:34:49 +0200 Subject: [PATCH] Added tests for new DatabaseBase::upsert Follow up Id7fc6652 Pass function name to begin/rollback/commit Use __METHOD__ as default param (see I86cbdeab) Change-Id: I9eb326c035d4a604db5b3492f090d8dd9d21c920 --- includes/db/Database.php | 8 ++--- includes/db/DatabaseMysql.php | 2 +- tests/phpunit/includes/db/DatabaseSQLTest.php | 35 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index 2f82e65c42..4b2eae7174 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -2565,7 +2565,7 @@ abstract class DatabaseBase implements DatabaseType { * @since 1.22 */ public function upsert( - $table, array $rows, array $uniqueIndexes, array $set, $fname = 'DatabaseBase::upsert' + $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__ ) { if ( !count( $rows ) ) { return true; // nothing to do @@ -2591,7 +2591,7 @@ abstract class DatabaseBase implements DatabaseType { $useTrx = !$this->mTrxLevel; if ( $useTrx ) { - $this->begin(); + $this->begin( $fname ); } try { # Update any existing conflicting row(s) @@ -2604,12 +2604,12 @@ abstract class DatabaseBase implements DatabaseType { $ok = $this->insert( $table, $rows, $fname, array( 'IGNORE' ) ) && $ok; } catch ( Exception $e ) { if ( $useTrx ) { - $this->rollback(); + $this->rollback( $fname ); } throw $e; } if ( $useTrx ) { - $this->commit(); + $this->commit( $fname ); } return $ok; diff --git a/includes/db/DatabaseMysql.php b/includes/db/DatabaseMysql.php index 8bc975fd9e..3eb8c44e90 100644 --- a/includes/db/DatabaseMysql.php +++ b/includes/db/DatabaseMysql.php @@ -362,7 +362,7 @@ class DatabaseMysql extends DatabaseBase { * @return bool */ public function upsert( - $table, array $rows, array $uniqueIndexes, array $set, $fname = 'DatabaseMysql::upsert' + $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__ ) { if ( !count( $rows ) ) { return true; // nothing to do diff --git a/tests/phpunit/includes/db/DatabaseSQLTest.php b/tests/phpunit/includes/db/DatabaseSQLTest.php index 965a5f388f..46ccfe00a5 100644 --- a/tests/phpunit/includes/db/DatabaseSQLTest.php +++ b/tests/phpunit/includes/db/DatabaseSQLTest.php @@ -204,6 +204,41 @@ class DatabaseSQLTest extends MediaWikiTestCase { ); } + /** + * @dataProvider provideUpsert + */ + function testUpsert( $sql, $sqlText ) { + $this->database->upsert( + $sql['table'], + $sql['rows'], + $sql['uniqueIndexes'], + $sql['set'], + __METHOD__ + ); + $this->assertLastSql( $sqlText ); + } + + public static function provideUpsert() { + return array( + array( + array( + 'table' => 'upsert_table', + 'rows' => array( 'field' => 'text', 'field2' => 'text2' ), + 'uniqueIndexes' => array( 'field' ), + 'set' => array( 'field' => 'set' ), + ), + "BEGIN; " . + "UPDATE upsert_table " . + "SET field = 'set' " . + "WHERE ((field = 'text')); " . + "INSERT IGNORE INTO upsert_table " . + "(field,field2) " . + "VALUES ('text','text2'); " . + "COMMIT" + ), + ); + } + /** * @dataProvider provideDeleteJoin */ -- 2.20.1