database update: db.inc works here now, no crappy psql -c needed anymore
[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 table_exists($table) {
35 return pg_num_rows(db_query("SELECT * FROM information_schema.tables WHERE ".
36 "table_catalog = 'iserv' AND table_name = ".qdb($table).";")) > 0;
37 }
38 function column_exists($table, $column) {
39 return table_exists($table) && pg_num_rows(db_query("SELECT * FROM ".
40 "information_schema.columns WHERE table_name = ".qdb($table)." AND ".
41 "column_name = ".qdb($column).";")) > 0;
42 }
43 function print_sql($cmd) {
44 echo $cmd."\n";
45 db_query($cmd);
46 }
47
48 if(isset($_SERVER["REMOTE_ADDR"])) {
49 die("Run this script as root from the command line.");
50 }
51
52 /*** database schema updates **************************************************/
53 echo "updating database schema... \n";
54
55 /** table from pre-3.0 **/
56 if(table_exists("errors") and !column_exists("errors", "pclabel")) {
57 print_sql("ALTER TABLE errors RENAME COLUMN pc_number TO pclabel;");
58 }
59
60 /** table from 3.0 **/
61 if(table_exists("errors") and !column_exists("errors", "pclabel")) {
62 print_sql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,".
63 "er_comment,er_commentact,er_hidden) SELECT date,name,pclabel,text,".
64 "comment,commentby,hidden FROM errors;");
65 print_sql("DROP TABLE errors;");
66 }
67
68 /** table from 4.0 **/
69 if(table_exists("errorreports")) {
70 print_sql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,".
71 "er_comment,er_commentact,er_hidden) SELECT er_date,er_act,er_machine,".
72 "er_text,er_comment,er_commentact,er_hidden FROM errorreports;");
73 print_sql("DROP TABLE errorreports;");
74 }
75
76 echo "done.\n";
77 ?>
This page took 0.044904 seconds and 5 git commands to generate.