From 977cea1438dafcdc98756b070a1383e5887d64c1 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 26 Apr 2016 20:04:24 +0300 Subject: [PATCH] actually check for failures properly in the dbupdater --- classes/db.php | 3 +++ classes/db/mysql.php | 9 +++++++-- classes/db/mysqli.php | 9 +++++++-- classes/db/pgsql.php | 9 +++++++-- classes/dbupdater.php | 14 ++++++++++++-- classes/handler/public.php | 4 ++-- classes/idb.php | 1 + include/db.php | 4 ++++ update.php | 2 +- 9 files changed, 44 insertions(+), 11 deletions(-) diff --git a/classes/db.php b/classes/db.php index 695ca6ea..8d2f8951 100644 --- a/classes/db.php +++ b/classes/db.php @@ -94,5 +94,8 @@ class Db implements IDb { return $this->adapter->last_error(); } + function last_query_error() { + return $this->adapter->last_query_error(); + } } ?> diff --git a/classes/db/mysql.php b/classes/db/mysql.php index d4b45b98..e8701f7b 100644 --- a/classes/db/mysql.php +++ b/classes/db/mysql.php @@ -1,6 +1,7 @@ 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'"); diff --git a/classes/db/mysqli.php b/classes/db/mysqli.php index c685b75a..a9689df7 100644 --- a/classes/db/mysqli.php +++ b/classes/db/mysqli.php @@ -1,6 +1,7 @@ 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'"); diff --git a/classes/db/pgsql.php b/classes/db/pgsql.php index 6b772d9e..7bacfef6 100644 --- a/classes/db/pgsql.php +++ b/classes/db/pgsql.php @@ -1,6 +1,7 @@ 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"); diff --git a/classes/dbupdater.php b/classes/dbupdater.php index a319da03..f9ef4f3b 100644 --- a/classes/dbupdater.php +++ b/classes/dbupdater.php @@ -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; + } } } diff --git a/classes/handler/public.php b/classes/handler/public.php index 46fc90fe..d166e315 100644 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -980,7 +980,7 @@ class Handler_Public extends Handler { for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) { print "
  • Performing update up to version $i..."; - $result = $updater->performUpdateTo($i); + $result = $updater->performUpdateTo($i, true); if (!$result) { print "FAILED!
  • "; @@ -990,7 +990,7 @@ class Handler_Public extends Handler { "; - break; + return; } else { print "OK!"; } diff --git a/classes/idb.php b/classes/idb.php index 16f760bf..dd1eba14 100644 --- a/classes/idb.php +++ b/classes/idb.php @@ -9,5 +9,6 @@ interface IDb { function close(); function affected_rows($result); function last_error(); + function last_query_error(); } ?> diff --git a/include/db.php b/include/db.php index 55d1d654..f45f0727 100644 --- a/include/db.php +++ b/include/db.php @@ -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); } diff --git a/update.php b/update.php index 54401f99..f1743618 100755 --- a/update.php +++ b/update.php @@ -325,7 +325,7 @@ 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!");