Added tests for new DatabaseBase::upsert
authorumherirrender <umherirrender_de.wp@web.de>
Tue, 4 Jun 2013 18:34:49 +0000 (20:34 +0200)
committerumherirrender <umherirrender_de.wp@web.de>
Tue, 4 Jun 2013 18:34:49 +0000 (20:34 +0200)
Follow up Id7fc6652
Pass function name to begin/rollback/commit
Use __METHOD__ as default param (see I86cbdeab)

Change-Id: I9eb326c035d4a604db5b3492f090d8dd9d21c920

includes/db/Database.php
includes/db/DatabaseMysql.php
tests/phpunit/includes/db/DatabaseSQLTest.php

index 2f82e65..4b2eae7 100644 (file)
@@ -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;
index 8bc975f..3eb8c44 100644 (file)
@@ -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
index 965a5f3..46ccfe0 100644 (file)
@@ -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
         */