2669c3f2fe6e53055f8a255965114d1ee7753b2e
[iserv-mod-error-reporter.git] / maint / update-database.php
1 #!/usr/bin/php
2 <?php
3 /**
4 * @file update.php
5 * Perform the update operations like renaming SQL tables
6 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
7 * @date 23.10.2007
8 *
9 * Copyright © 2007 Roland Hieber
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30 /* this file is run by iservchk */
31 require_once("/usr/share/iserv/www/inc/quote.inc");
32 require_once("/usr/share/iserv/www/inc/db.inc");
33
34 function doSql($cmd, $quiet = false) {
35 $i = 0;
36 $a = array();
37 $cmd = "psql -c ".qs($cmd);
38 $cmd = ($quiet) ? $cmd . ">/dev/null 2>&1" : $cmd;
39 exec($cmd, $a, $i);
40 return ($i == 0);
41 }
42 function printSql($cmd) { echo $cmd."\n"; return doSql($cmd); }
43 function tableExists($table) {
44 return doSql("SELECT * FROM $table", tnue);
45 }
46 function columnExists($table, $col) {
47 return (doSql("SELECT $col FROM $table", true) and tableExists($table));
48 }
49
50 if(isset($_SERVER["REMOTE_ADDR"])) {
51 die("Run this script as root from the command line.");
52 }
53
54 /*** database schema updates **************************************************/
55 echo "updating database schema... \n";
56
57 /** table from pre-3.0 **/
58 if(tableExists("errors") and !columnExists("errors", "pclabel")) {
59 printSql("ALTER TABLE errors RENAME COLUMN pc_number TO pclabel;");
60 }
61
62 /** table from 3.0 **/
63 if(tableExists("errors") and !columnExists("errors", "pclabel")) {
64 printSql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,".
65 "er_comment,er_commentact,er_hidden) SELECT date,name,pclabel,text,".
66 "comment,commentby,hidden FROM errors;");
67 printSql("DROP TABLE errors;");
68 }
69
70 /** table from 4.0 **/
71 if(tableExists("errorreports")) {
72 printSql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,".
73 "er_comment,er_commentact,er_hidden) SELECT er_date,er_act,er_machine,".
74 "er_text,er_comment,er_commentact,er_hidden FROM errorreports;");
75 printSql("DROP TABLE errorreports;");
76 }
77
78 echo "done.\n";
79 ?>
This page took 0.046434 seconds and 3 git commands to generate.