commit | author | age
|
b7e7c8
|
1 |
#!/usr/bin/env php |
AM |
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(dirname(__FILE__) . '/..') . '/' ); |
|
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', |
9e329c
|
30 |
'p' => 'package', |
b7e7c8
|
31 |
)); |
AM |
32 |
|
|
33 |
if (empty($opts['dir'])) { |
f23ef1
|
34 |
rcube::raise_error("Database schema directory not specified (--dir).", false, true); |
b7e7c8
|
35 |
} |
9e329c
|
36 |
if (empty($opts['package'])) { |
f23ef1
|
37 |
rcube::raise_error("Database schema package name not specified (--package).", false, true); |
b7e7c8
|
38 |
} |
AM |
39 |
|
|
40 |
// Check if directory exists |
|
41 |
if (!file_exists($opts['dir'])) { |
f23ef1
|
42 |
rcube::raise_error("Specified database schema directory doesn't exist.", false, true); |
b7e7c8
|
43 |
} |
AM |
44 |
|
7e7431
|
45 |
$RC = rcube::get_instance(); |
AM |
46 |
$DB = rcube_db::factory($RC->config->get('db_dsnw')); |
|
47 |
|
|
48 |
// Connect to database |
|
49 |
$DB->db_connect('w'); |
|
50 |
if (!$DB->is_connected()) { |
f23ef1
|
51 |
rcube::raise_error("Error connecting to database: " . $DB->is_error(), false, true); |
7e7431
|
52 |
} |
AM |
53 |
|
c10187
|
54 |
// Read DB schema version from database (if 'system' table exists) |
15e4c8
|
55 |
if (in_array($DB->table_name('system'), (array)$DB->list_tables())) { |
9be085
|
56 |
$DB->query("SELECT " . $DB->quote_identifier('value') |
c10187
|
57 |
." FROM " . $DB->quote_identifier($DB->table_name('system')) |
9be085
|
58 |
." WHERE " . $DB->quote_identifier('name') ." = ?", |
9e329c
|
59 |
$opts['package'] . '-version'); |
7e7431
|
60 |
|
9be085
|
61 |
$row = $DB->fetch_array(); |
9e46fb
|
62 |
$version = preg_replace('/[^0-9]/', '', $row[0]); |
9be085
|
63 |
} |
7e7431
|
64 |
|
9be085
|
65 |
// DB version not found, but release version is specified |
7e7431
|
66 |
if (!$version && $opts['version']) { |
b7e7c8
|
67 |
// Map old release version string to DB schema version |
AM |
68 |
// Note: This is for backward compat. only, do not need to be updated |
|
69 |
$map = array( |
|
70 |
'0.1-stable' => 1, |
|
71 |
'0.1.1' => 2008030300, |
|
72 |
'0.2-alpha' => 2008040500, |
|
73 |
'0.2-beta' => 2008060900, |
|
74 |
'0.2-stable' => 2008092100, |
20334a
|
75 |
'0.2.1' => 2008092100, |
AM |
76 |
'0.2.2' => 2008092100, |
b7e7c8
|
77 |
'0.3-stable' => 2008092100, |
AM |
78 |
'0.3.1' => 2009090400, |
|
79 |
'0.4-beta' => 2009103100, |
20334a
|
80 |
'0.4' => 2010042300, |
AM |
81 |
'0.4.1' => 2010042300, |
b7e7c8
|
82 |
'0.4.2' => 2010042300, |
AM |
83 |
'0.5-beta' => 2010100600, |
|
84 |
'0.5' => 2010100600, |
|
85 |
'0.5.1' => 2010100600, |
20334a
|
86 |
'0.5.2' => 2010100600, |
AM |
87 |
'0.5.3' => 2010100600, |
|
88 |
'0.5.4' => 2010100600, |
b7e7c8
|
89 |
'0.6-beta' => 2011011200, |
AM |
90 |
'0.6' => 2011011200, |
|
91 |
'0.7-beta' => 2011092800, |
|
92 |
'0.7' => 2011111600, |
|
93 |
'0.7.1' => 2011111600, |
|
94 |
'0.7.2' => 2011111600, |
|
95 |
'0.7.3' => 2011111600, |
c2c1bb
|
96 |
'0.7.4' => 2011111600, |
b7e7c8
|
97 |
'0.8-beta' => 2011121400, |
AM |
98 |
'0.8-rc' => 2011121400, |
|
99 |
'0.8.0' => 2011121400, |
|
100 |
'0.8.1' => 2011121400, |
|
101 |
'0.8.2' => 2011121400, |
|
102 |
'0.8.3' => 2011121400, |
|
103 |
'0.8.4' => 2011121400, |
c2c1bb
|
104 |
'0.8.5' => 2011121400, |
AM |
105 |
'0.8.6' => 2011121400, |
b7e7c8
|
106 |
'0.9-beta' => 2012080700, |
AM |
107 |
); |
|
108 |
|
|
109 |
$version = $map[$opts['version']]; |
|
110 |
} |
|
111 |
|
c10187
|
112 |
// Assume last version before the 'system' table was added |
b7e7c8
|
113 |
if (empty($version)) { |
AM |
114 |
$version = 2012080700; |
|
115 |
} |
|
116 |
|
|
117 |
$dir = $opts['dir'] . DIRECTORY_SEPARATOR . $DB->db_provider; |
|
118 |
if (!file_exists($dir)) { |
f23ef1
|
119 |
rcube::raise_error("DDL Upgrade files for " . $DB->db_provider . " driver not found.", false, true); |
b7e7c8
|
120 |
} |
AM |
121 |
|
|
122 |
$dh = opendir($dir); |
|
123 |
$result = array(); |
|
124 |
|
|
125 |
while ($file = readdir($dh)) { |
|
126 |
if (preg_match('/^([0-9]+)\.sql$/', $file, $m) && $m[1] > $version) { |
|
127 |
$result[] = $m[1]; |
|
128 |
} |
|
129 |
} |
|
130 |
sort($result, SORT_NUMERIC); |
|
131 |
|
|
132 |
foreach ($result as $v) { |
|
133 |
echo "Updating database schema ($v)... "; |
9e329c
|
134 |
$error = update_db_schema($opts['package'], $v, $dir . DIRECTORY_SEPARATOR . "$v.sql"); |
b7e7c8
|
135 |
|
AM |
136 |
if ($error) { |
f23ef1
|
137 |
echo "[FAILED]\n"; |
AM |
138 |
rcube::raise_error("Error in DDL upgrade $v: $error", false, true); |
b7e7c8
|
139 |
} |
AM |
140 |
echo "[OK]\n"; |
|
141 |
} |
|
142 |
|
|
143 |
|
9e329c
|
144 |
function update_db_schema($package, $version, $file) |
b7e7c8
|
145 |
{ |
AM |
146 |
global $DB; |
|
147 |
|
|
148 |
// read DDL file |
720e7d
|
149 |
if ($sql = file_get_contents($file)) { |
AM |
150 |
if (!$DB->exec_script($sql)) { |
|
151 |
return $DB->is_error(); |
b7e7c8
|
152 |
} |
AM |
153 |
} |
|
154 |
|
c3d061
|
155 |
// escape if 'system' table does not exist |
AM |
156 |
if ($version < 2013011000) { |
|
157 |
return; |
|
158 |
} |
|
159 |
|
c10187
|
160 |
$system_table = $DB->quote_identifier($DB->table_name('system')); |
AM |
161 |
|
|
162 |
$DB->query("UPDATE " . $system_table |
b7e7c8
|
163 |
." SET " . $DB->quote_identifier('value') . " = ?" |
AM |
164 |
." WHERE " . $DB->quote_identifier('name') . " = ?", |
9e329c
|
165 |
$version, $package . '-version'); |
b7e7c8
|
166 |
|
AM |
167 |
if (!$DB->is_error() && !$DB->affected_rows()) { |
c10187
|
168 |
$DB->query("INSERT INTO " . $system_table |
b7e7c8
|
169 |
." (" . $DB->quote_identifier('name') . ", " . $DB->quote_identifier('value') . ")" |
AM |
170 |
." VALUES (?, ?)", |
9e329c
|
171 |
$package . '-version', $version); |
b7e7c8
|
172 |
} |
AM |
173 |
|
|
174 |
return $DB->is_error(); |
15e4c8
|
175 |
} |
TB |
176 |
|
b7e7c8
|
177 |
?> |