0
|
1 #!/usr/bin/env php
|
|
2 <?php
|
|
3 /*
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | bin/updatedb.sh |
|
|
6 | |
|
|
7 | This file is part of the Roundcube Webmail client |
|
|
8 | Copyright (C) 2010-2012, The Roundcube Dev Team |
|
|
9 | Copyright (C) 2010-2012, Kolab Systems AG |
|
|
10 | |
|
|
11 | Licensed under the GNU General Public License version 3 or |
|
|
12 | any later version with exceptions for skins & plugins. |
|
|
13 | See the README file for a full license statement. |
|
|
14 | |
|
|
15 | PURPOSE: |
|
|
16 | Update database schema |
|
|
17 +-----------------------------------------------------------------------+
|
|
18 | Author: Aleksander Machniak <alec@alec.pl> |
|
|
19 +-----------------------------------------------------------------------+
|
|
20 */
|
|
21
|
|
22 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
|
|
23
|
|
24 require_once INSTALL_PATH . 'program/include/clisetup.php';
|
|
25
|
|
26 // get arguments
|
|
27 $opts = rcube_utils::get_opt(array(
|
|
28 'v' => 'version',
|
|
29 'd' => 'dir',
|
|
30 'p' => 'package',
|
|
31 ));
|
|
32
|
|
33 if (empty($opts['dir'])) {
|
|
34 rcube::raise_error("Database schema directory not specified (--dir).", false, true);
|
|
35 }
|
|
36 if (empty($opts['package'])) {
|
|
37 rcube::raise_error("Database schema package name not specified (--package).", false, true);
|
|
38 }
|
|
39
|
|
40 // Check if directory exists
|
|
41 if (!file_exists($opts['dir'])) {
|
|
42 rcube::raise_error("Specified database schema directory doesn't exist.", false, true);
|
|
43 }
|
|
44
|
|
45 $RC = rcube::get_instance();
|
|
46 $DB = rcube_db::factory($RC->config->get('db_dsnw'));
|
|
47
|
|
48 $DB->set_debug((bool)$RC->config->get('sql_debug'));
|
|
49
|
|
50 // Connect to database
|
|
51 $DB->db_connect('w');
|
|
52 if (!$DB->is_connected()) {
|
|
53 rcube::raise_error("Error connecting to database: " . $DB->is_error(), false, true);
|
|
54 }
|
|
55
|
|
56 // Read DB schema version from database (if 'system' table exists)
|
|
57 if (in_array($DB->table_name('system'), (array)$DB->list_tables())) {
|
|
58 $DB->query("SELECT `value`"
|
|
59 ." FROM " . $DB->table_name('system', true)
|
|
60 ." WHERE `name` = ?",
|
|
61 $opts['package'] . '-version');
|
|
62
|
|
63 $row = $DB->fetch_array();
|
|
64 $version = preg_replace('/[^0-9]/', '', $row[0]);
|
|
65 }
|
|
66
|
|
67 // DB version not found, but release version is specified
|
|
68 if (!$version && $opts['version']) {
|
|
69 // Map old release version string to DB schema version
|
|
70 // Note: This is for backward compat. only, do not need to be updated
|
|
71 $map = array(
|
|
72 '0.1-stable' => 1,
|
|
73 '0.1.1' => 2008030300,
|
|
74 '0.2-alpha' => 2008040500,
|
|
75 '0.2-beta' => 2008060900,
|
|
76 '0.2-stable' => 2008092100,
|
|
77 '0.2.1' => 2008092100,
|
|
78 '0.2.2' => 2008092100,
|
|
79 '0.3-stable' => 2008092100,
|
|
80 '0.3.1' => 2009090400,
|
|
81 '0.4-beta' => 2009103100,
|
|
82 '0.4' => 2010042300,
|
|
83 '0.4.1' => 2010042300,
|
|
84 '0.4.2' => 2010042300,
|
|
85 '0.5-beta' => 2010100600,
|
|
86 '0.5' => 2010100600,
|
|
87 '0.5.1' => 2010100600,
|
|
88 '0.5.2' => 2010100600,
|
|
89 '0.5.3' => 2010100600,
|
|
90 '0.5.4' => 2010100600,
|
|
91 '0.6-beta' => 2011011200,
|
|
92 '0.6' => 2011011200,
|
|
93 '0.7-beta' => 2011092800,
|
|
94 '0.7' => 2011111600,
|
|
95 '0.7.1' => 2011111600,
|
|
96 '0.7.2' => 2011111600,
|
|
97 '0.7.3' => 2011111600,
|
|
98 '0.7.4' => 2011111600,
|
|
99 '0.8-beta' => 2011121400,
|
|
100 '0.8-rc' => 2011121400,
|
|
101 '0.8.0' => 2011121400,
|
|
102 '0.8.1' => 2011121400,
|
|
103 '0.8.2' => 2011121400,
|
|
104 '0.8.3' => 2011121400,
|
|
105 '0.8.4' => 2011121400,
|
|
106 '0.8.5' => 2011121400,
|
|
107 '0.8.6' => 2011121400,
|
|
108 '0.9-beta' => 2012080700,
|
|
109 );
|
|
110
|
|
111 $version = $map[$opts['version']];
|
|
112 }
|
|
113
|
|
114 // Assume last version before the 'system' table was added
|
|
115 if (empty($version)) {
|
|
116 $version = 2012080700;
|
|
117 }
|
|
118
|
|
119 $dir = $opts['dir'] . '/' . $DB->db_provider;
|
|
120 if (!file_exists($dir)) {
|
|
121 rcube::raise_error("DDL Upgrade files for " . $DB->db_provider . " driver not found.", false, true);
|
|
122 }
|
|
123
|
|
124 $dh = opendir($dir);
|
|
125 $result = array();
|
|
126
|
|
127 while ($file = readdir($dh)) {
|
|
128 if (preg_match('/^([0-9]+)\.sql$/', $file, $m) && $m[1] > $version) {
|
|
129 $result[] = $m[1];
|
|
130 }
|
|
131 }
|
|
132 sort($result, SORT_NUMERIC);
|
|
133
|
|
134 foreach ($result as $v) {
|
|
135 echo "Updating database schema ($v)... ";
|
|
136 $error = update_db_schema($opts['package'], $v, "$dir/$v.sql");
|
|
137
|
|
138 if ($error) {
|
|
139 echo "[FAILED]\n";
|
|
140 rcube::raise_error("Error in DDL upgrade $v: $error", false, true);
|
|
141 }
|
|
142 echo "[OK]\n";
|
|
143 }
|
|
144
|
|
145
|
|
146 function update_db_schema($package, $version, $file)
|
|
147 {
|
|
148 global $DB;
|
|
149
|
|
150 // read DDL file
|
|
151 if ($sql = file_get_contents($file)) {
|
|
152 if (!$DB->exec_script($sql)) {
|
|
153 return $DB->is_error();
|
|
154 }
|
|
155 }
|
|
156
|
|
157 // escape if 'system' table does not exist
|
|
158 if ($version < 2013011000) {
|
|
159 return;
|
|
160 }
|
|
161
|
|
162 $system_table = $DB->table_name('system', true);
|
|
163
|
|
164 $DB->query("UPDATE " . $system_table
|
|
165 ." SET `value` = ?"
|
|
166 ." WHERE `name` = ?",
|
|
167 $version, $package . '-version');
|
|
168
|
|
169 if (!$DB->is_error() && !$DB->affected_rows()) {
|
|
170 $DB->query("INSERT INTO " . $system_table
|
|
171 ." (`name`, `value`) VALUES (?, ?)",
|
|
172 $package . '-version', $version);
|
|
173 }
|
|
174
|
|
175 return $DB->is_error();
|
|
176 }
|
|
177
|
|
178 ?>
|