From: Roland Hieber Date: Mon, 8 Mar 2010 02:36:43 +0000 (+0100) Subject: database update: db.inc works here now, no crappy psql -c needed anymore X-Git-Tag: REL_10.03.08~7 X-Git-Url: http://git.rohieb.name/iserv-mod-error-reporter.git/commitdiff_plain/36a47ab0347c6ec92d8cdec443895342f6153288 database update: db.inc works here now, no crappy psql -c needed anymore --- diff --git a/maint/update-database.php b/maint/update-database.php index 2669c3f..b392ee5 100755 --- a/maint/update-database.php +++ b/maint/update-database.php @@ -31,20 +31,18 @@ require_once("/usr/share/iserv/www/inc/quote.inc"); require_once("/usr/share/iserv/www/inc/db.inc"); -function doSql($cmd, $quiet = false) { - $i = 0; - $a = array(); - $cmd = "psql -c ".qs($cmd); - $cmd = ($quiet) ? $cmd . ">/dev/null 2>&1" : $cmd; - exec($cmd, $a, $i); - return ($i == 0); +function table_exists($table) { + return pg_num_rows(db_query("SELECT * FROM information_schema.tables WHERE ". + "table_catalog = 'iserv' AND table_name = ".qdb($table).";")) > 0; } -function printSql($cmd) { echo $cmd."\n"; return doSql($cmd); } -function tableExists($table) { - return doSql("SELECT * FROM $table", tnue); +function column_exists($table, $column) { + return table_exists($table) && pg_num_rows(db_query("SELECT * FROM ". + "information_schema.columns WHERE table_name = ".qdb($table)." AND ". + "column_name = ".qdb($column).";")) > 0; } -function columnExists($table, $col) { - return (doSql("SELECT $col FROM $table", true) and tableExists($table)); +function print_sql($cmd) { + echo $cmd."\n"; + db_query($cmd); } if(isset($_SERVER["REMOTE_ADDR"])) { @@ -55,24 +53,24 @@ if(isset($_SERVER["REMOTE_ADDR"])) { echo "updating database schema... \n"; /** table from pre-3.0 **/ -if(tableExists("errors") and !columnExists("errors", "pclabel")) { - printSql("ALTER TABLE errors RENAME COLUMN pc_number TO pclabel;"); +if(table_exists("errors") and !column_exists("errors", "pclabel")) { + print_sql("ALTER TABLE errors RENAME COLUMN pc_number TO pclabel;"); } /** table from 3.0 **/ -if(tableExists("errors") and !columnExists("errors", "pclabel")) { - printSql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,". +if(table_exists("errors") and !column_exists("errors", "pclabel")) { + print_sql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,". "er_comment,er_commentact,er_hidden) SELECT date,name,pclabel,text,". "comment,commentby,hidden FROM errors;"); - printSql("DROP TABLE errors;"); + print_sql("DROP TABLE errors;"); } /** table from 4.0 **/ -if(tableExists("errorreports")) { - printSql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,". +if(table_exists("errorreports")) { + print_sql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,". "er_comment,er_commentact,er_hidden) SELECT er_date,er_act,er_machine,". "er_text,er_comment,er_commentact,er_hidden FROM errorreports;"); - printSql("DROP TABLE errorreports;"); + print_sql("DROP TABLE errorreports;"); } echo "done.\n";