Mercurial > hg > rc2
comparison bin/updatedb.sh @ 8:bf99236cc5cd default tip
try to recover from upgrade fail
author | Charlie Root |
---|---|
date | Sat, 29 Dec 2018 07:07:34 -0500 |
parents | 4681f974d28b |
children |
comparison
equal
deleted
inserted
replaced
7:65fd7d441cf1 | 8:bf99236cc5cd |
---|---|
35 } | 35 } |
36 if (empty($opts['package'])) { | 36 if (empty($opts['package'])) { |
37 rcube::raise_error("Database schema package name not specified (--package).", false, true); | 37 rcube::raise_error("Database schema package name not specified (--package).", false, true); |
38 } | 38 } |
39 | 39 |
40 // Check if directory exists | 40 rcmail_utils::db_update($opts['dir'], $opts['package'], $opts['version'], array('errors' => true)); |
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 | 41 |
178 ?> | 42 ?> |