actually check for failures properly in the dbupdater

This commit is contained in:
Andrew Dolgov 2016-04-26 20:04:24 +03:00
parent 9e84bab449
commit 977cea1438
9 changed files with 44 additions and 11 deletions

View File

@ -94,5 +94,8 @@ class Db implements IDb {
return $this->adapter->last_error(); return $this->adapter->last_error();
} }
function last_query_error() {
return $this->adapter->last_query_error();
}
} }
?> ?>

View File

@ -1,6 +1,7 @@
<?php <?php
class Db_Mysql implements IDb { class Db_Mysql implements IDb {
private $link; private $link;
private $last_error;
function connect($host, $user, $pass, $db, $port) { function connect($host, $user, $pass, $db, $port) {
$this->link = mysql_connect($host, $user, $pass); $this->link = mysql_connect($host, $user, $pass);
@ -28,10 +29,10 @@ class Db_Mysql implements IDb {
function query($query, $die_on_error = true) { function query($query, $die_on_error = true) {
$result = @mysql_query($query, $this->link); $result = @mysql_query($query, $this->link);
if (!$result) { if (!$result) {
$error = @mysql_error($this->link); $this->last_error = @mysql_error($this->link);
@mysql_query("ROLLBACK", $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); $die_on_error ? E_USER_ERROR : E_USER_WARNING);
} }
return $result; return $result;
@ -62,6 +63,10 @@ class Db_Mysql implements IDb {
return mysql_error(); return mysql_error();
} }
function last_query_error() {
return $this->last_error;
}
function init() { function init() {
$this->query("SET time_zone = '+0:0'"); $this->query("SET time_zone = '+0:0'");

View File

@ -1,6 +1,7 @@
<?php <?php
class Db_Mysqli implements IDb { class Db_Mysqli implements IDb {
private $link; private $link;
private $last_error;
function connect($host, $user, $pass, $db, $port) { function connect($host, $user, $pass, $db, $port) {
if ($port) if ($port)
@ -26,10 +27,10 @@ class Db_Mysqli implements IDb {
function query($query, $die_on_error = true) { function query($query, $die_on_error = true) {
$result = @mysqli_query($this->link, $query); $result = @mysqli_query($this->link, $query);
if (!$result) { if (!$result) {
$error = @mysqli_error($this->link); $this->last_error = @mysqli_error($this->link);
@mysqli_query($this->link, "ROLLBACK"); @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); $die_on_error ? E_USER_ERROR : E_USER_WARNING);
} }
@ -66,6 +67,10 @@ class Db_Mysqli implements IDb {
return mysqli_error(); return mysqli_error();
} }
function last_query_error() {
return $this->last_error;
}
function init() { function init() {
$this->query("SET time_zone = '+0:0'"); $this->query("SET time_zone = '+0:0'");

View File

@ -1,6 +1,7 @@
<?php <?php
class Db_Pgsql implements IDb { class Db_Pgsql implements IDb {
private $link; private $link;
private $last_error;
function connect($host, $user, $pass, $db, $port) { function connect($host, $user, $pass, $db, $port) {
$string = "dbname=$db user=$user"; $string = "dbname=$db user=$user";
@ -38,11 +39,11 @@ class Db_Pgsql implements IDb {
$result = @pg_query($this->link, $query); $result = @pg_query($this->link, $query);
if (!$result) { if (!$result) {
$error = @pg_last_error($this->link); $this->last_error = @pg_last_error($this->link);
@pg_query($this->link, "ROLLBACK"); @pg_query($this->link, "ROLLBACK");
$query = htmlspecialchars($query); // just in case $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); $die_on_error ? E_USER_ERROR : E_USER_WARNING);
} }
return $result; return $result;
@ -73,6 +74,10 @@ class Db_Pgsql implements IDb {
return pg_last_error($this->link); return pg_last_error($this->link);
} }
function last_query_error() {
return $this->last_error;
}
function init() { function init() {
$this->query("set client_encoding = 'UTF-8'"); $this->query("set client_encoding = 'UTF-8'");
pg_set_client_encoding("UNICODE"); pg_set_client_encoding("UNICODE");

View File

@ -30,7 +30,7 @@ class DbUpdater {
} }
} }
function performUpdateTo($version) { function performUpdateTo($version, $html_output = true) {
if ($this->getSchemaVersion() == $version - 1) { if ($this->getSchemaVersion() == $version - 1) {
$lines = $this->getSchemaLines($version); $lines = $this->getSchemaLines($version);
@ -41,7 +41,17 @@ class DbUpdater {
foreach ($lines as $line) { foreach ($lines as $line) {
if (strpos($line, "--") !== 0 && $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;
}
} }
} }

View File

@ -980,7 +980,7 @@ class Handler_Public extends Handler {
for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) { for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
print "<li>Performing update up to version $i..."; print "<li>Performing update up to version $i...";
$result = $updater->performUpdateTo($i); $result = $updater->performUpdateTo($i, true);
if (!$result) { if (!$result) {
print "<span class='err'>FAILED!</span></li></ul>"; 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")."\"> <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
</form>"; </form>";
break; return;
} else { } else {
print "<span class='ok'>OK!</span></li>"; print "<span class='ok'>OK!</span></li>";
} }

View File

@ -9,5 +9,6 @@ interface IDb {
function close(); function close();
function affected_rows($result); function affected_rows($result);
function last_error(); function last_error();
function last_query_error();
} }
?> ?>

View File

@ -29,6 +29,10 @@ function db_last_error() {
return Db::get()->last_error(); return Db::get()->last_error();
} }
function db_last_query_error() {
return Db::get()->last_query_error();
}
function db_quote($str){ function db_quote($str){
return Db::get()->quote($str); return Db::get()->quote($str);
} }

View File

@ -325,7 +325,7 @@
for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) { for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
_debug("performing update up to version $i..."); _debug("performing update up to version $i...");
$result = $updater->performUpdateTo($i); $result = $updater->performUpdateTo($i, false);
_debug($result ? "OK!" : "FAILED!"); _debug($result ? "OK!" : "FAILED!");