r3064 - InnoDB: employ foreign keys in TagTree, TagStorage
[racktables] / upgrade.php
1 <?php
2
3 $relnotes = array
4 (
5 '0.17.0' => "LDAP options have been moved to LDAP_options array. This means, that if you were<br>" .
6 "using LDAP authentication for users in version 0.16.x, it will break right after<br>" .
7 "upgrade to 0.17.0. To get things working again, adjust existing secret.php file<br>" .
8 "according to secret-sample.php file provided with 0.17.0 release.<br><br>" .
9 "Another change is the addition of support for file uploads. Files are stored<br>" .
10 "in the database. There are several settings in php.ini which you may need to modify:<br>" .
11 "<ul><li>file_uploads - needs to be On</li>" .
12 "<li>upload_max_filesize - max size for uploaded files</li>" .
13 "<li>post_max_size - max size of all form data submitted via POST (including files)</li></ul><br>" .
14 "Local user accounts used to have 'enabled' flag, which allowed individual blocking and<br>" .
15 "unblocking of each. This flag was dropped in favor of existing mean of access<br>" .
16 "setup (RackCode). An unconditional denying rule is automatically added into RackCode<br>" .
17 "for such blocked account, so the effective security policy remains the same.<br>",
18 );
19
20 // At the moment we assume, that for any two releases we can
21 // sequentally execute all batches, that separate them, and
22 // nothing will break. If this changes one day, the function
23 // below will have to generate smarter upgrade paths, while
24 // the upper layer will remain the same.
25 // Returning an empty array means that no upgrade is necessary.
26 // Returning NULL indicates an error.
27 function getDBUpgradePath ($v1, $v2)
28 {
29 $versionhistory = array
30 (
31 '0.16.4',
32 '0.16.5',
33 '0.16.6',
34 '0.17.0',
35 '0.17.1',
36 '0.17.2',
37 '0.17.3',
38 '0.17.4',
39 '0.17.5',
40 );
41 if (!in_array ($v1, $versionhistory) or !in_array ($v2, $versionhistory))
42 return NULL;
43 $skip = TRUE;
44 $path = NULL;
45 // Now collect all versions > $v1 and <= $v2
46 foreach ($versionhistory as $v)
47 {
48 if ($skip and $v == $v1)
49 {
50 $skip = FALSE;
51 $path = array();
52 continue;
53 }
54 if ($skip)
55 continue;
56 $path[] = $v;
57 if ($v == $v2)
58 break;
59 }
60 return $path;
61 }
62
63 // Upgrade batches are named exactly as the release where they first appear.
64 // That is simple, but seems sufficient for beginning.
65 function executeUpgradeBatch ($batchid)
66 {
67 $query = array();
68 global $dbxlink;
69 switch ($batchid)
70 {
71 case '0.16.5':
72 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4_TREE_SHOW_USAGE','yes','string','no','no','Show address usage in IPv4 tree')";
73 $query[] = "update Config set varvalue = '0.16.5' where varname = 'DB_VERSION'";
74 break;
75 case '0.16.6':
76 $query[] = "update Config set varvalue = '0.16.6' where varname = 'DB_VERSION'";
77 break;
78 case '0.17.0':
79 // create tables for storing files (requires InnoDB support)
80 if (!isInnoDBSupported ())
81 {
82 showFailure ("Cannot upgrade because InnoDB tables are not supported by your MySQL server. See the README for details.", __FILE__);
83 die;
84 }
85
86 $query[] = "alter table Chapter change chapter_no id int(10) unsigned NOT NULL auto_increment";
87 $query[] = "alter table Chapter change chapter_name name char(128) NOT NULL";
88 $query[] = "alter table Chapter drop key chapter_name";
89 $query[] = "alter table Chapter add UNIQUE KEY name (name)";
90 $query[] = "alter table Attribute change attr_id id int(10) unsigned NOT NULL auto_increment";
91 $query[] = "alter table Attribute change attr_type type enum('string','uint','float','dict') default NULL";
92 $query[] = "alter table Attribute change attr_name name char(64) default NULL";
93 $query[] = "alter table Attribute drop key attr_name";
94 $query[] = "alter table Attribute add UNIQUE KEY name (name)";
95 $query[] = "alter table AttributeMap change chapter_no chapter_id int(10) unsigned NOT NULL";
96 $query[] = "alter table Dictionary change chapter_no chapter_id int(10) unsigned NOT NULL";
97 // Only after the above call it is Ok to use reloadDictionary()
98 $query = array_merge ($query, reloadDictionary ($batchid));
99 // schema changes for file management
100 $query[] = "
101 CREATE TABLE `File` (
102 `id` int(10) unsigned NOT NULL auto_increment,
103 `name` char(255) NOT NULL,
104 `type` char(255) NOT NULL,
105 `size` int(10) unsigned NOT NULL,
106 `ctime` datetime NOT NULL,
107 `mtime` datetime NOT NULL,
108 `atime` datetime NOT NULL,
109 `contents` longblob NOT NULL,
110 `comment` text,
111 PRIMARY KEY (`id`),
112 UNIQUE KEY `name` (`name`)
113 ) ENGINE=InnoDB";
114 $query[] = "
115 CREATE TABLE `FileLink` (
116 `id` int(10) unsigned NOT NULL auto_increment,
117 `file_id` int(10) unsigned NOT NULL,
118 `entity_type` enum('ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object',
119 `entity_id` int(10) NOT NULL,
120 PRIMARY KEY (`id`),
121 UNIQUE KEY `FileLink-unique` (`file_id`,`entity_type`,`entity_id`),
122 KEY `FileLink-file_id` (`file_id`),
123 CONSTRAINT `FileLink-File_fkey` FOREIGN KEY (`file_id`) REFERENCES `File` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
124 ) ENGINE=InnoDB";
125 $query[] = "ALTER TABLE TagStorage MODIFY COLUMN target_realm enum('file','ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object'";
126
127 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (24,'no','network security models')";
128 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (25,'no','wireless models')";
129 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,1,0)";
130 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,2,24)";
131 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,3,0)";
132 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,5,0)";
133 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,14,0)";
134 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,16,0)";
135 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,17,0)";
136 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,18,0)";
137 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,20,0)";
138 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,21,0)";
139 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,22,0)";
140 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,24,0)";
141 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,1,0)";
142 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,3,0)";
143 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,2,25)";
144 $query[] = 'alter table IPBonds rename to IPv4Allocation';
145 $query[] = 'alter table PortForwarding rename to IPv4NAT';
146 $query[] = 'alter table IPRanges rename to IPv4Network';
147 $query[] = 'alter table IPAddress rename to IPv4Address';
148 $query[] = 'alter table IPLoadBalancer rename to IPv4LB';
149 $query[] = 'alter table IPRSPool rename to IPv4RSPool';
150 $query[] = 'alter table IPRealServer rename to IPv4RS';
151 $query[] = 'alter table IPVirtualService rename to IPv4VS';
152 $query[] = "alter table TagStorage change column target_realm entity_realm enum('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') NOT NULL default 'object'";
153 $query[] = 'alter table TagStorage change column target_id entity_id int(10) unsigned NOT NULL';
154 $query[] = 'alter table TagStorage drop key entity_tag';
155 $query[] = 'alter table TagStorage drop key target_id';
156 $query[] = 'alter table TagStorage add UNIQUE KEY `entity_tag` (`entity_realm`,`entity_id`,`tag_id`)';
157 $query[] = 'alter table TagStorage add KEY `entity_id` (`entity_id`)';
158 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_MAXCHARS','10240','uint','yes','no','Max chars for text file preview')";
159 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_ROWS','25','uint','yes','no','Rows for text file preview')";
160 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_COLS','80','uint','yes','no','Columns for text file preview')";
161 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_IMAGE_MAXPXS','320','uint','yes','no','Max pixels per axis for image file preview')";
162 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('VENDOR_SIEVE','','string','yes','no','Vendor sieve configuration')";
163 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4LB_LISTSRC','{\$typeid_4}','string','yes','no','List source: IPv4 load balancers')";
164 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4OBJ_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8} or {\$typeid_12} or {\$typeid_445} or {\$typeid_447}','string','yes','no','List source: IPv4-enabled objects')";
165 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4NAT_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: IPv4 NAT performers')";
166 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('ASSETWARN_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: object, for which asset tag should be set')";
167 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('NAMEWARN_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: object, for which common name should be set')";
168 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('RACKS_PER_ROW','12','unit','yes','no','Racks per row')";
169 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_PREDICATE_SIEVE','','string','yes','no','Predicate sieve regex(7)')";
170 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_DEFAULT_ANDOR','or','string','no','no','Default list filter boolean operation (or/and)')";
171 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_ANDOR','yes','string','no','no','Suggest and/or selector in list filter')";
172 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_TAGS','yes','string','no','no','Suggest tags in list filter')";
173 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_PREDICATES','yes','string','no','no','Suggest predicates in list filter')";
174 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_EXTRA','no','string','no','no','Suggest extra expression in list filter')";
175 $query[] = "delete from Config where varname = 'USER_AUTH_SRC'";
176 $query[] = "delete from Config where varname = 'COOKIE_TTL'";
177 $query[] = "delete from Config where varname = 'rtwidth_0'";
178 $query[] = "delete from Config where varname = 'rtwidth_1'";
179 $query[] = "delete from Config where varname = 'rtwidth_2'";
180 $query[] = "delete from Config where varname = 'NAMEFUL_OBJTYPES'";
181 $query[] = "delete from Config where varname = 'REQUIRE_ASSET_TAG_FOR'";
182 $query[] = "delete from Config where varname = 'IPV4_PERFORMERS'";
183 $query[] = "delete from Config where varname = 'NATV4_PERFORMERS'";
184 $query[] = "alter table TagTree add column valid_realm set('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') not null default 'file,ipv4net,ipv4vs,ipv4rspool,object,rack,user' after parent_id";
185 $result = $dbxlink->query ("select user_id, user_name, user_realname from UserAccount where user_enabled = 'no'");
186 while ($row = $result->fetch (PDO::FETCH_ASSOC))
187 $query[] = "update Script set script_text = concat('deny {\$userid_${row['user_id']}} # ${row['user_name']} (${row['user_realname']})\n', script_text) where script_name = 'RackCode'";
188 $query[] = "update Script set script_text = NULL where script_name = 'RackCodeCache'";
189 unset ($result);
190 $query[] = "alter table UserAccount drop column user_enabled";
191
192 $query[] = "CREATE TABLE RackRow ( id int(10) unsigned NOT NULL auto_increment, name char(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM";
193
194 $result = $dbxlink->query ("select dict_key, dict_value from Dictionary where chapter_no = 3");
195 while($row = $result->fetch(PDO::FETCH_NUM))
196 $query[] = "insert into RackRow set id=${row[0]}, name='${row[1]}'";
197 unset ($result);
198 $query[] = "delete from Dictionary where chapter_id = 3";
199 $query[] = "delete from Chapter where id = 3";
200 $query[] = "
201 CREATE TABLE `LDAPCache` (
202 `presented_username` char(64) NOT NULL,
203 `successful_hash` char(40) NOT NULL,
204 `first_success` timestamp NOT NULL default CURRENT_TIMESTAMP,
205 `last_retry` timestamp NOT NULL default '0000-00-00 00:00:00',
206 `displayed_name` char(128) default NULL,
207 `memberof` text,
208 UNIQUE KEY `presented_username` (`presented_username`),
209 KEY `scanidx` (`presented_username`,`successful_hash`)
210 ) ENGINE=InnoDB;";
211 $query[] = "alter table UserAccount modify column user_password_hash char(40) NULL";
212 $query[] = 'ALTER TABLE Rack DROP COLUMN deleted';
213 $query[] = 'ALTER TABLE RackHistory DROP COLUMN deleted';
214 $query[] = 'ALTER TABLE RackObject DROP COLUMN deleted';
215 $query[] = 'ALTER TABLE RackObjectHistory DROP COLUMN deleted';
216 // Can't be added straight due to many duplicates, even in "dictbase" data.
217 $result = $dbxlink->query ('SELECT type1, type2, count(*) - 1 as excess FROM PortCompat GROUP BY type1, type2 HAVING excess > 0');
218 while ($row = $result->fetch (PDO::FETCH_ASSOC))
219 $query[] = "DELETE FROM PortCompat WHERE type1 = ${row['type1']} AND type2 = ${row['type2']} limit ${row['excess']}";
220 unset ($result);
221 $query[] = 'ALTER TABLE PortCompat DROP KEY type1';
222 $query[] = 'ALTER TABLE PortCompat ADD UNIQUE `type1_2` (type1, type2)';
223 $query[] = "UPDATE Config SET varvalue = '0.17.0' WHERE varname = 'DB_VERSION'";
224
225 break;
226 case '0.17.1':
227 $query[] = "ALTER TABLE Dictionary DROP KEY `chap_to_key`";
228 $query = array_merge ($query, reloadDictionary ($batchid));
229 // Token set has changed, so the cache isn't valid any more.
230 $query[] = "UPDATE Script SET script_text = NULL WHERE script_name = 'RackCodeCache'";
231 $query[] = "UPDATE Config SET varvalue = '0.17.1' WHERE varname = 'DB_VERSION'";
232 break;
233 case '0.17.2':
234 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (26,'no','fibre channel switch models')";
235 $query = array_merge ($query, reloadDictionary ($batchid));
236 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1055,2,26)";
237 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('DEFAULT_SNMP_COMMUNITY','public','string','no','no','Default SNMP Community string')";
238 // wipe irrelevant records (ticket:250)
239 $query[] = "DELETE FROM TagStorage WHERE entity_realm = 'file' AND entity_id NOT IN (SELECT id FROM File)";
240 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4_ENABLE_KNIGHT','yes','string','no','no','Enable IPv4 knight feature')";
241 $query[] = "ALTER TABLE IPv4Network ADD COLUMN comment text AFTER name";
242 $query[] = "ALTER TABLE Port ADD INDEX comment (reservation_comment)";
243 $query[] = "ALTER TABLE Port DROP KEY l2address"; // UNIQUE
244 $query[] = "ALTER TABLE Port ADD KEY (l2address)"; // not UNIQUE
245 $query[] = "ALTER TABLE Port DROP KEY object_id";
246 $query[] = "ALTER TABLE Port ADD UNIQUE KEY per_object (object_id, name, type)";
247 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (20,1083)";
248 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (21,1083)";
249 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1077,1077)";
250 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,20)";
251 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,21)";
252 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,1083)";
253 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1087,1087)";
254 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (27,'no','PDU models')";
255 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (2,2,27)";
256 $query[] = "UPDATE Config SET varvalue = '0.17.2' WHERE varname = 'DB_VERSION'";
257 break;
258 case '0.17.3':
259 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_TOPLIST_SIZE','50','uint','yes','no','Tags top list size')";
260 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_SIZE','20','uint','no','no','Tags quick list size')";
261 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_THRESHOLD','50','uint','yes','no','Tags quick list threshold')";
262 $query[] = "ALTER TABLE AttributeMap MODIFY COLUMN chapter_id int(10) unsigned NULL";
263 $query[] = "UPDATE AttributeMap SET chapter_id = NULL WHERE attr_id IN (SELECT id FROM Attribute WHERE type != 'dict')";
264 // ticket:239
265 $query[] = 'UPDATE AttributeValue SET uint_value = 1018 WHERE uint_value = 731 AND attr_id IN (SELECT attr_id FROM AttributeMap WHERE chapter_id = 12)';
266 $query[] = 'DELETE FROM Dictionary WHERE dict_key = 731';
267 $query = array_merge ($query, reloadDictionary ($batchid));
268 $query[] = "UPDATE Config SET vartype='uint' WHERE varname='RACKS_PER_ROW'";
269 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('ENABLE_MULTIPORT_FORM','no','string','no','no','Enable \"Add/update multiple ports\" form')";
270 $query[] = "UPDATE Config SET varvalue = '0.17.3' WHERE varname = 'DB_VERSION'";
271 break;
272 case '0.17.4':
273 $query[] = "ALTER TABLE Link ENGINE=InnoDB";
274 $query[] = "ALTER TABLE Port ENGINE=InnoDB";
275 $query[] = "ALTER TABLE IPv4RS ENGINE=InnoDB";
276 $query[] = "ALTER TABLE IPv4RSPool ENGINE=InnoDB";
277 $query[] = "ALTER TABLE AttributeValue ENGINE=InnoDB";
278 $query[] = "ALTER TABLE RackObject ENGINE=InnoDB";
279 $query[] = "ALTER TABLE IPv4NAT ENGINE=InnoDB";
280 $query[] = "ALTER TABLE IPv4LB ENGINE=InnoDB";
281 $query[] = "ALTER TABLE IPv4VS ENGINE=InnoDB";
282 $query[] = "DELETE FROM IPv4RS WHERE rspool_id NOT IN (SELECT id FROM IPv4RSPool)";
283 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-b` FOREIGN KEY (portb) REFERENCES Port (id)";
284 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-a` FOREIGN KEY (porta) REFERENCES Port (id)";
285 $query[] = "ALTER TABLE IPv4RS ADD CONSTRAINT `IPv4RS-FK` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id) ON DELETE CASCADE";
286 $query[] = "ALTER TABLE AttributeValue ADD CONSTRAINT `AttributeValue-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
287 $query[] = "ALTER TABLE IPv4NAT ADD CONSTRAINT `IPv4NAT-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
288 $query[] = "ALTER TABLE Port ADD CONSTRAINT `Port-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
289 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-rspool_id` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id)";
290 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
291 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-vs_id` FOREIGN KEY (vs_id) REFERENCES IPv4VS (id)";
292 $query = array_merge ($query, reloadDictionary ($batchid));
293 $query[] = "UPDATE Config SET varvalue = '0.17.4' WHERE varname = 'DB_VERSION'";
294 break;
295 case '0.17.5':
296 $query[] = "ALTER TABLE TagTree ENGINE=InnoDB";
297 $query[] = "ALTER TABLE TagStorage ENGINE=InnoDB";
298 $query[] = "ALTER TABLE TagStorage ADD CONSTRAINT `TagStorage-FK-tag_id` FOREIGN KEY (tag_id) REFERENCES TagTree (id)";
299 $query[] = "ALTER TABLE TagTree ADD CONSTRAINT `TagTree-K-parent_id` FOREIGN KEY (parent_id) REFERENCES TagTree (id)";
300 $query[] = "UPDATE Config SET varvalue = '0.17.5' WHERE varname = 'DB_VERSION'";
301 break;
302 default:
303 showFailure ("executeUpgradeBatch () failed, because batch '${batchid}' isn't defined", __FILE__);
304 die;
305 break;
306 }
307 $failures = array();
308 echo "<tr><th>Executing batch '${batchid}'</th><td>";
309 foreach ($query as $q)
310 {
311 $result = $dbxlink->query ($q);
312 if ($result == NULL)
313 {
314 $errorInfo = $dbxlink->errorInfo();
315 $failures[] = array ($q, $errorInfo[2]);
316 }
317 }
318 if (!count ($failures))
319 echo "<strong><font color=green>done</font></strong>";
320 else
321 {
322 echo "<strong><font color=red>The following queries failed:</font></strong><br><pre>";
323 foreach ($failures as $f)
324 {
325 list ($q, $i) = $f;
326 echo "${q} -- ${i}\n";
327 }
328 echo "</pre>";
329 }
330 echo '</td></tr>';
331 }
332
333 // ******************************************************************
334 //
335 // Execution starts here
336 //
337 // ******************************************************************
338
339 $root = (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') ? 'http://' : 'https://';
340 $root .= isset ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']));
341 $root .= strtr (dirname ($_SERVER['PHP_SELF']), '\\', '/');
342 if (substr ($root, -1) != '/')
343 $root .= '/';
344
345 // a clone of showError() to drop dependency on interface.php
346 function showFailure ($info = '', $location = 'N/A')
347 {
348 global $root;
349 if (preg_match ('/\.php$/', $location))
350 $location = basename ($location);
351 elseif ($location != 'N/A')
352 $location = $location . '()';
353 echo "<div class=msg_error>An error has occured in [${location}]. ";
354 if (empty ($info))
355 echo 'No additional information is available.';
356 else
357 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
358 echo "This failure is most probably fatal.<br></div>\n";
359 }
360
361 require_once 'inc/config.php'; // for CODE_VERSION
362 require_once 'inc/database.php'; // for getDatabaseVersion()
363 require_once 'inc/dictionary.php';
364 // Enforce default value for now, releases prior to 0.17.0 didn't support 'httpd' auth source.
365 $user_auth_src = 'database';
366
367 if (file_exists ('inc/secret.php'))
368 require_once 'inc/secret.php';
369 else
370 die ("Database connection parameters are read from inc/secret.php file, " .
371 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
372 "inc/secret.php and modify to your setup.\n\nThen reload the page.");
373
374 try
375 {
376 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
377 }
378 catch (PDOException $e)
379 {
380 die ("Database connection failed:\n\n" . $e->getMessage());
381 }
382
383 // Now we need to be sure that the current user is the administrator.
384 // The rest doesn't matter within this context.
385
386 function authenticate_admin ($username, $password)
387 {
388 global $dbxlink;
389 $hash = sha1 ($password);
390 $query = "select count(*) from UserAccount where user_id = 1 and user_name = '${username}' and user_password_hash = '${hash}'";
391 if (($result = $dbxlink->query ($query)) == NULL)
392 die ('SQL query failed in ' . __FUNCTION__);
393 $rows = $result->fetchAll (PDO::FETCH_NUM);
394 return $rows[0][0] == 1;
395 }
396
397 switch ($user_auth_src)
398 {
399 case 'database':
400 case 'ldap': // authenticate against DB as well
401 if
402 (
403 !isset ($_SERVER['PHP_AUTH_USER']) or
404 !strlen ($_SERVER['PHP_AUTH_USER']) or
405 !isset ($_SERVER['PHP_AUTH_PW']) or
406 !strlen ($_SERVER['PHP_AUTH_PW']) or
407 !authenticate_admin (escapeString ($_SERVER['PHP_AUTH_USER']), escapeString ($_SERVER['PHP_AUTH_PW']))
408 )
409 {
410 header ('WWW-Authenticate: Basic realm="RackTables upgrade"');
411 header ('HTTP/1.0 401 Unauthorized');
412 showFailure ('You must be authenticated as an administrator to complete the upgrade.', __FILE__);
413 die;
414 }
415 break; // cleared
416 case 'httpd':
417 if
418 (
419 !isset ($_SERVER['REMOTE_USER']) or
420 !strlen ($_SERVER['REMOTE_USER'])
421 )
422 {
423 showFailure ('System misconfiguration. The web-server didn\'t authenticate the user, although ought to do.');
424 die;
425 }
426 break; // cleared
427 default:
428 showFailure ('authentication source misconfiguration', __FILE__);
429 die;
430 }
431
432 $dbver = getDatabaseVersion();
433 echo '<table border=1>';
434 echo "<tr><th>Current status</th><td>Data version: ${dbver}<br>Code version: " . CODE_VERSION . "</td></tr>\n";
435
436 $path = getDBUpgradePath ($dbver, CODE_VERSION);
437 if ($path === NULL)
438 {
439 echo "<tr><th>Upgrade path</th><td><font color=red>not found</font></td></tr>\n";
440 echo "<tr><th>Summary</th><td>Check README for more information. RackTables releases prior to 0.16.4 ";
441 echo "must be upgraded to 0.16.4 first.</td></tr>\n";
442 }
443 else
444 {
445 if (!count ($path))
446 echo "<tr><th>Summary</th><td>Come back later.</td></tr>\n";
447 else
448 {
449 echo "<tr><th>Upgrade path</th><td>${dbver} &rarr; " . implode (' &rarr; ', $path) . "</td></tr>\n";
450 foreach ($path as $batchid)
451 {
452 executeUpgradeBatch ($batchid);
453 if (isset ($relnotes[$batchid]))
454 echo "<tr><th>Release notes for ${batchid}</th><td>" . $relnotes[$batchid] . "</td></tr>\n";
455 }
456 echo "<tr><th>Summary</th><td>Upgrade complete, it is Ok to <a href='${root}'>enter</a> the system.</td></tr>\n";
457 }
458 }
459 echo '</table>';
460
461 ?>