]> git.wh0rd.org Git - tt-rss.git/commitdiff
actually check for failures properly in the dbupdater
authorAndrew Dolgov <noreply@fakecake.org>
Tue, 26 Apr 2016 17:04:24 +0000 (20:04 +0300)
committerAndrew Dolgov <noreply@fakecake.org>
Tue, 26 Apr 2016 17:04:24 +0000 (20:04 +0300)
classes/db.php
classes/db/mysql.php
classes/db/mysqli.php
classes/db/pgsql.php
classes/dbupdater.php
classes/handler/public.php
classes/idb.php
include/db.php
update.php

index 695ca6ea2e47bcd2e523474bf5acafd769a05049..8d2f895160e8fc89329a1da63a4740b8499e0852 100644 (file)
@@ -94,5 +94,8 @@ class Db implements IDb {
                return $this->adapter->last_error();
        }
 
+       function last_query_error() {
+               return $this->adapter->last_query_error();
+       }
 }
 ?>
index d4b45b98c6034516eefb27526d8c3dc71cb55109..e8701f7bb5c45802010498d01829cd94cb478f0a 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 class Db_Mysql implements IDb {
        private $link;
+       private $last_error;
 
        function connect($host, $user, $pass, $db, $port) {
                $this->link = mysql_connect($host, $user, $pass);
@@ -28,10 +29,10 @@ class Db_Mysql implements IDb {
        function query($query, $die_on_error = true) {
                $result = @mysql_query($query, $this->link);
                if (!$result) {
-                       $error = @mysql_error($this->link);
+                       $this->last_error = @mysql_error($this->link);
 
                        @mysql_query("ROLLBACK", $this->link);
-                       user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
+                       user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"),
                                $die_on_error ? E_USER_ERROR : E_USER_WARNING);
                }
                return $result;
@@ -62,6 +63,10 @@ class Db_Mysql implements IDb {
                return mysql_error();
        }
 
+       function last_query_error() {
+               return $this->last_error;
+       }
+
        function init() {
                $this->query("SET time_zone = '+0:0'");
 
index c685b75a02c8e30db2f8d9bdd016b426861e9633..a9689df70cde9254c6c1015352857803aac49603 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 class Db_Mysqli implements IDb {
        private $link;
+       private $last_error;
 
        function connect($host, $user, $pass, $db, $port) {
                if ($port)
@@ -26,10 +27,10 @@ class Db_Mysqli implements IDb {
        function query($query, $die_on_error = true) {
                $result = @mysqli_query($this->link, $query);
                if (!$result) {
-                       $error = @mysqli_error($this->link);
+                       $this->last_error = @mysqli_error($this->link);
 
                        @mysqli_query($this->link, "ROLLBACK");
-                       user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
+                       user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"),
                                $die_on_error ? E_USER_ERROR : E_USER_WARNING);
                }
 
@@ -66,6 +67,10 @@ class Db_Mysqli implements IDb {
                return mysqli_error();
        }
 
+       function last_query_error() {
+               return $this->last_error;
+       }
+
        function init() {
                $this->query("SET time_zone = '+0:0'");
 
index 6b772d9e1de47e1d444f2dc8d20b7eec1f0ee7f3..7bacfef60df28046e617d5ba85be46e7dcfd30dd 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 class Db_Pgsql implements IDb {
        private $link;
+       private $last_error;
 
        function connect($host, $user, $pass, $db, $port) {
                $string = "dbname=$db user=$user";
@@ -38,11 +39,11 @@ class Db_Pgsql implements IDb {
                $result = @pg_query($this->link, $query);
 
                if (!$result) {
-                       $error = @pg_last_error($this->link);
+                       $this->last_error = @pg_last_error($this->link);
 
                        @pg_query($this->link, "ROLLBACK");
                        $query = htmlspecialchars($query); // just in case
-                       user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
+                       user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"),
                                $die_on_error ? E_USER_ERROR : E_USER_WARNING);
                }
                return $result;
@@ -73,6 +74,10 @@ class Db_Pgsql implements IDb {
                return pg_last_error($this->link);
        }
 
+       function last_query_error() {
+               return $this->last_error;
+       }
+
        function init() {
                $this->query("set client_encoding = 'UTF-8'");
                pg_set_client_encoding("UNICODE");
index a319da03d207424360a73b050b531ef2df6f10f0..f9ef4f3bb09ee4e070ff4e674fb52853669cabd2 100644 (file)
@@ -30,7 +30,7 @@ class DbUpdater {
                }
        }
 
-       function performUpdateTo($version) {
+       function performUpdateTo($version, $html_output = true) {
                if ($this->getSchemaVersion() == $version - 1) {
 
                        $lines = $this->getSchemaLines($version);
@@ -41,7 +41,17 @@ class DbUpdater {
 
                                foreach ($lines as $line) {
                                        if (strpos($line, "--") !== 0 && $line) {
-                                               db_query($line);
+                                               if (!db_query($line, false)) {
+                                                       if ($html_output) {
+                                                               print_notice("Query: $line");
+                                                               print_error("Error: " . db_last_query_error());
+                                                       } else {
+                                                               _debug("Query: $line");
+                                                               _debug("Error: " . db_last_query_error());
+                                                       }
+
+                                                       return false;
+                                               }
                                        }
                                }
 
index 46fc90fe114ff6cfde12bdc61ba534cc86956abc..d166e315e99a874cde2eb9a404c273fe0e9d17e5 100644 (file)
@@ -980,7 +980,7 @@ class Handler_Public extends Handler {
                                                for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
                                                        print "<li>Performing update up to version $i...";
 
-                                                       $result = $updater->performUpdateTo($i);
+                                                       $result = $updater->performUpdateTo($i, true);
 
                                                        if (!$result) {
                                                                print "<span class='err'>FAILED!</span></li></ul>";
@@ -990,7 +990,7 @@ class Handler_Public extends Handler {
                                                                <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
                                                                </form>";
 
-                                                               break;
+                                                               return;
                                                        } else {
                                                                print "<span class='ok'>OK!</span></li>";
                                                        }
index 16f760bf6ade60d0d4bc41e4c4b67eb7111a8b1c..dd1eba142745bb77aa03c5d3f2e640b4e3f48f6d 100644 (file)
@@ -9,5 +9,6 @@ interface IDb {
        function close();
        function affected_rows($result);
        function last_error();
+       function last_query_error();
 }
 ?>
index 55d1d654eb1c5ede3593efccb6b30f05e59fdcf9..f45f0727644e3644f083c7a96d6775fd9315351a 100644 (file)
@@ -29,6 +29,10 @@ function db_last_error() {
        return Db::get()->last_error();
 }
 
+function db_last_query_error() {
+       return Db::get()->last_query_error();
+}
+
 function db_quote($str){
        return Db::get()->quote($str);
 }
index 54401f99acfbffd2476bb4594954b25771fa64c2..f174361853a9106269cdb01264ad28b339d224d9 100755 (executable)
                        for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
                                _debug("performing update up to version $i...");
 
-                               $result = $updater->performUpdateTo($i);
+                               $result = $updater->performUpdateTo($i, false);
 
                                _debug($result ? "OK!" : "FAILED!");