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