r1649 + renderRackSpaceForObject(): fixed working copy building
[racktables] / upgrade.php
1 <?php
2
3 // At the moment we assume, that for any two releases we can
4 // sequentally execute all batches, that separate them, and
5 // nothing will break. If this changes one day, the function
6 // below will have to generate smarter upgrade paths, while
7 // the upper layer will remain the same.
8 // Returning an empty array means that no upgrade is necessary.
9 function getDBUpgradePath ($v1, $v2)
10 {
11 $versionhistory = array
12 (
13 '0.14.4',
14 '0.14.5',
15 '0.14.6',
16 '0.14.7',
17 '0.14.8',
18 '0.14.9',
19 '0.14.10'
20 );
21 if (!in_array ($v1, $versionhistory) || !in_array ($v2, $versionhistory))
22 {
23 showError ("An upgrade path has been requested for versions '${v1}' and '${v2}', " .
24 "and at least one of those isn't known to me.");
25 die;
26 }
27 $skip = TRUE;
28 $path = array();
29 // Now collect all versions > $v1 and <= $v2
30 foreach ($versionhistory as $v)
31 {
32 if ($v == $v1)
33 {
34 $skip = FALSE;
35 continue;
36 }
37 if ($skip)
38 continue;
39 $path[] = $v;
40 if ($v == $v2)
41 break;
42 }
43 return $path;
44 }
45
46 // Upgrade batches are name exactly as the release where they first appear.
47 // That simple, but seems sufficient for beginning.
48 function executeUpgradeBatch ($batchid)
49 {
50 $query = array();
51 global $dbxlink;
52 switch ($batchid)
53 {
54 case '0.14.5':
55 // We can't realiably distinguish between 0.14.4 and 0.14.5, but
56 // luckily the SQL statements below can be safely executed for both.
57
58
59 // This has to be checked once more to be sure IPAddress allocation
60 // conventions are correct.
61 $query[] = "delete from IPAddress where name = '' and reserved = 'no'";
62
63 // In the 0.14.4 release we had AUTO_INCREMENT low in the dictionary and auth
64 // data tables, thus causing new user's data to take primary keys equal to
65 // the values of shipped data in future releases. Let's shift user's data
66 // up and keep DB consistent.
67 $query[] = "alter table Attribute AUTO_INCREMENT = 10000";
68 $query[] = "alter table Chapter AUTO_INCREMENT = 10000";
69 $query[] = "alter table Dictionary AUTO_INCREMENT = 10000";
70 $query[] = "alter table UserAccount AUTO_INCREMENT = 10000";
71 $query[] = "update UserAccount set user_id = user_id + 10000 where user_id between 2 and 10000";
72 $query[] = "update UserPermission set user_id = user_id + 10000 where user_id between 2 and 10000";
73 $query[] = "update Attribute set attr_id = attr_id + 10000 where attr_id between 25 and 10000";
74 $query[] = "update AttributeMap set attr_id = attr_id + 10000 where attr_id between 25 and 10000";
75 $query[] = "update Chapter set chapter_no = chapter_no + 10000 where chapter_no between 21 and 10000";
76 $query[] = "update AttributeMap set chapter_no = chapter_no + 10000 where chapter_no between 21 and 10000";
77 break; // --------------------------------------------
78 case '0.14.6':
79 // This version features new dictionary entries, the correction above should allow us
80 // inject them w/o a problem.
81 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,25,'FreeBSD 1.x')";
82 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,26,'FreeBSD 2.x')";
83 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,27,'FreeBSD 3.x')";
84 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,28,'FreeBSD 4.x')";
85 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,29,'FreeBSD 5.x')";
86 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,30,'FreeBSD 6.x')";
87 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,31,'RHFC8')";
88 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (13,32,'ALTLinux Master 4.0')";
89 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (20,20)";
90 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (21,21)";
91 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (22,22)";
92 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (23,23)";
93 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (24,24)";
94 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (25,25)";
95 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (26,26)";
96 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (27,27)";
97 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (28,28)";
98 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,20,'KVM')";
99 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,21,'1000Base-ZX')";
100 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,22,'10GBase-ER')";
101 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,23,'10GBase-LR')";
102 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,24,'10GBase-LRM')";
103 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,25,'10GBase-ZR')";
104 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,26,'10GBase-LX4')";
105 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,27,'10GBase-CX4')";
106 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (2,28,'10GBase-Kx')";
107 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (12,114,'Cisco Catalyst 2970G-24T')";
108 $query[] = "INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) VALUES (12,115,'Cisco Catalyst 2970G-24TS')";
109 $query[] = "INSERT INTO `UserPermission` (`user_id`, `page`, `tab`, `access`) VALUES (0,'help','%','yes')";
110 // And 0.14.6 is the first release, which features Config table. Let's create
111 // and fill it with default values.
112 $query[] = "
113 CREATE TABLE `Config` (
114 `varname` char(32) NOT NULL,
115 `varvalue` char(64) NOT NULL,
116 `vartype` enum('string','uint') NOT NULL default 'string',
117 `emptyok` enum('yes','no') NOT NULL default 'no',
118 `is_hidden` enum('yes','no') NOT NULL default 'yes',
119 `description` text,
120 PRIMARY KEY (`varname`)
121 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
122 ";
123 $query[] = "INSERT INTO `Config` VALUES ('rtwidth_0','9','uint','no','yes','')";
124 $query[] = "INSERT INTO `Config` VALUES ('rtwidth_1','21','uint','no','yes','')";
125 $query[] = "INSERT INTO `Config` VALUES ('rtwidth_2','9','uint','no','yes','')";
126 $query[] = "INSERT INTO `Config` VALUES ('color_F','8fbfbf','string','no','no','HSV: 180-25-75. Free atoms, they are available for allocation to objects.')";
127 $query[] = "INSERT INTO `Config` VALUES ('color_A','bfbfbf','string','no','no','HSV: 0-0-75. Absent atoms.')";
128 $query[] = "INSERT INTO `Config` VALUES ('color_U','bf8f8f','string','no','no','HSV: 0-25-75. Unusable atoms. Some problems keep them from being free.')";
129 $query[] = "INSERT INTO `Config` VALUES ('color_T','408080','string','no','no','HSV: 180-50-50. Taken atoms, object_id should be set for such.')";
130 $query[] = "INSERT INTO `Config` VALUES ('color_Th','80ffff','string','no','no','HSV: 180-50-100. Taken atoms with highlight. They are not stored in the database and are only used for highlighting.')";
131 $query[] = "INSERT INTO `Config` VALUES ('color_Tw','804040','string','no','no','HSV: 0-50-50. Taken atoms with object problem. This is detected at runtime.')";
132 $query[] = "INSERT INTO `Config` VALUES ('color_Thw','ff8080','string','no','no','HSV: 0-50-100. An object can be both current and problematic. We run highlightObject() first and markupObjectProblems() second.')";
133 $query[] = "INSERT INTO `Config` VALUES ('default_port_type','11','uint','no','no','Default value for port type selects.')";
134 $query[] = "INSERT INTO `Config` VALUES ('MASSCOUNT','15','uint','no','no','Number of lines in object mass-adding form.')";
135 $query[] = "INSERT INTO `Config` VALUES ('MAXSELSIZE','30','uint','no','no','Maximum size of a SELECT HTML element.')";
136 $query[] = "INSERT INTO `Config` VALUES ('enterprise','MyCompanyName','string','no','no','Fit to your needs.')";
137 $query[] = "INSERT INTO `Config` VALUES ('NAMEFUL_OBJTYPES','4,7,8','string','yes','no','These are the object types, which assume a common name to be normally configured. If a name is absent for an object of one of such types, HTML output is corrected to accent this misconfiguration.')";
138 $query[] = "INSERT INTO `Config` VALUES ('ROW_SCALE','2','uint','no','no','Row-scope picture scale factor.')";
139 $query[] = "INSERT INTO `Config` VALUES ('PORTS_PER_ROW','12','uint','no','yes','Max switch port per one row on the switchvlans dynamic tab.')";
140 $query[] = "INSERT INTO `Config` VALUES ('DB_VERSION','0.14.6','string','no','yes','Database version.')";
141 break; // --------------------------------------------
142 case '0.14.7':
143 // IPAddress is hopefully fixed now finally.
144 $query[] = "delete from IPAddress where name = '' and reserved != 'yes'";
145
146 // Now rebuild the dictionary into a new table with the same data,
147 // but proper indexing. We are going to convert compound index
148 // into 1-field one to employ AUTO_INCREMENT properly. This means
149 // renumbering lots of records in Dictionary and adjusting records
150 // in related tables. After that we can safely swap the tables.
151 $query[] = "
152 CREATE TABLE `Dictionary_0_14_7_new` (
153 `chapter_no` int(10) unsigned NOT NULL,
154 `dict_key` int(10) unsigned NOT NULL auto_increment,
155 `dict_value` char(128) default NULL,
156 PRIMARY KEY (`dict_key`),
157 UNIQUE KEY `chap_to_key` (`chapter_no`,`dict_key`),
158 UNIQUE KEY `chap_to_val` (`chapter_no`,`dict_value`)
159 ) TYPE=MyISAM AUTO_INCREMENT=50000
160 ";
161
162 echo '<pre>';
163 // Find all chapter numbers, which will require AttributeValue adjustment.
164 $q2 = 'select distinct chapter_no from AttributeMap where chapter_no != 0';
165 $r2 = $dbxlink->query ($q2);
166 $chaplist = array();
167 while ($row = $r2->fetch (PDO::FETCH_NUM))
168 $chaplist[] = $row[0];
169 $r2->closeCursor();
170 unset ($r2);
171
172 $stock = array();
173 // Below I list the records, which are known to be the stock
174 // dictionary records of 0.14.6 release.
175 $stock[1] = array
176 (
177 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16
178 );
179 $stock[2] = array
180 (
181 3, 4, 5, 6, 7, 8, 9,
182 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
183 20, 21, 22, 23, 24, 25, 26, 27, 28
184 );
185 $stock[11] = array
186 (
187 1, 3, 4, 5, 6, 7, 8, 9,
188 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
189 21, 22, 24, 25, 26, 27, 28, 29,
190 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
191 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
192 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
193 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
194 70, 71, 72, 73, 74, 75, 76
195 );
196 $stock[12] = array
197 (
198 1, 11, 13, 14, 15, 16, 17, 18, 19, 20, 26, 29,
199 31, 32, 33, 34, 35, 36, 37, 38, 39,
200 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
201 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
202 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
203 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
204 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
205 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
206 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
207 110, 111, 112, 113, 114, 115
208 );
209 $stock[13] = array
210 (
211 1, 2, 3, 4, 5, 6, 7, 8, 9,
212 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
213 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
214 30, 31, 32
215 );
216 $stock[14] = array
217 (
218 1, 2, 9, 11, 13, 15, 19, 20, 21, 22
219 );
220 $stock[16] = array
221 (
222 1, 2, 3, 4, 5, 6, 7, 8
223 );
224 $stock[17] = array
225 (
226 1, 2, 3, 4, 5, 6, 7, 8, 9,
227 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
228 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
229 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
230 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
231 50
232 );
233 $stock[18] = array
234 (
235 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
236 );
237 $stock[19] = array
238 (
239 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
240 );
241 $stock[20] = array
242 (
243 1, 2
244 );
245
246 // Load dictionary and transform into two tree structures for
247 // stock and user record sets.
248 $dict = array();
249 $q3 = 'select chapter_no, dict_key, dict_value from Dictionary order by chapter_no, dict_key';
250 $r3 = $dbxlink->query ($q3);
251
252 while ($row = $r3->fetch (PDO::FETCH_ASSOC))
253 {
254 $tree = 'user';
255 $dict_key = $row['dict_key'];
256 $chapter_no = $row['chapter_no'];
257 switch ($chapter_no)
258 {
259 case 1: // RackObjectType
260 case 2: // PortType
261 case 11: // server models
262 case 12: // network switch models
263 case 13: // server OS type
264 case 14: // network switch OS type
265 case 16: // router OS type
266 case 17: // router models
267 case 18: // disk array models
268 case 19: // tape library models
269 case 20: // Protocols
270 if (in_array ($dict_key, $stock[$chapter_no]))
271 $tree = 'stock';
272 break;
273 }
274 $dict[$tree][$chapter_no][$dict_key] = array ('value' => $row['dict_value']);
275 }
276 $r3->closeCursor();
277 unset ($r3);
278
279
280 // Now we store stock dataset first, bump up key value and store
281 // user's data. After that we will know the new dict_key for all
282 // records.
283 // The result of both datasets processing is saved in $new_dict.
284 // Save on calling LAST_ISERT_ID() each time by keeping own key.
285 $newkey = 1;
286 $new_dict = array();
287 foreach ($dict['stock'] as $chapter_no => $words)
288 {
289 $new_dict[$chapter_no] = array();
290 foreach ($words as $dict_key => $entry)
291 {
292 $query[] = "insert into Dictionary_0_14_7_new (chapter_no, dict_key, dict_value) " .
293 "values (${chapter_no}, ${newkey}, '${entry['value']}')";
294 $new_dict[$chapter_no][$dict_key] = $entry;
295 $new_dict[$chapter_no][$dict_key]['newkey'] = $newkey;
296 $newkey++;
297 }
298 }
299 $newkey = 50000;
300 foreach ($dict['user'] as $chapter_no => $words)
301 {
302 // Some chapters may appear on the user dataset only.
303 if (!isset ($new_dict[$chapter_no]))
304 $new_dict[$chapter_no] = array();
305 foreach ($words as $dict_key => $entry)
306 {
307 $query[] = "insert into Dictionary_0_14_7_new " .
308 "values (${chapter_no}, ${newkey}, '${entry['value']}')";
309 $new_dict[$chapter_no][$dict_key] = $entry;
310 $new_dict[$chapter_no][$dict_key]['newkey'] = $newkey;
311 $newkey++;
312 }
313 }
314 // The new table should now have adequate AUTO_INCREMENT w/o our care.
315 // Install the new data.
316 $query[] = 'drop table Dictionary';
317 $query[] = 'alter table Dictionary_0_14_7_new rename to Dictionary';
318
319 // Now we iterate over the joint dataset, picking some chapters and
320 // performing additional processing:
321 // 1 (RackObjectType) --- adjust RackObject and regenerate AttributeMap
322 // 2 (PortType) --- adjust Port and regenerate PortCompat (at a latter point)
323 // 3 (RackRow) --- adjust Rack
324 // 20 (Protocols) --- adjust PortForwarding
325 // All other chapters listed in $chaplist --- adjust AttributeValue
326
327 $query[] = "delete from AttributeMap";
328 foreach ($new_dict as $chapter_no => $words)
329 {
330 foreach ($words as $oldkey => $data)
331 {
332 $value = $data['value'];
333 $newkey = $data['newkey'];
334 // Even if the key doesn't change, go on to have
335 // AttributeMap regenerated completely.
336 #echo "oldkey == ${oldkey} newkey == ${newkey} value == ${value}\n";
337 if ($chapter_no == 1)
338 {
339 $q4 = "select id from RackObject where objtype_id = ${oldkey}";
340 $r4 = $dbxlink->query ($q4);
341 while ($row = $r4->fetch (PDO::FETCH_ASSOC))
342 $query[] = "update RackObject set objtype_id = ${newkey} where id = ${row['id']} limit 1";
343 $r4->closeCursor();
344 unset ($r4);
345
346 $q5 = "select attr_id, chapter_no from AttributeMap where objtype_id = ${oldkey}";
347 $r5 = $dbxlink->query ($q5);
348 while ($row = $r5->fetch (PDO::FETCH_ASSOC))
349 $query[] = "insert into AttributeMap (objtype_id, attr_id, chapter_no) values (${newkey}, ${row['attr_id']}, ${row['chapter_no']})";
350 $r5->closeCursor();
351 unset ($r5);
352 }
353 elseif ($chapter_no == 2)
354 {
355 $q46 = "select id from Port where type = ${oldkey}";
356 $r46 = $dbxlink->query ($q46);
357 if ($r46 == NULL)
358 echo 'ERROR';
359 while ($row = $r46->fetch (PDO::FETCH_ASSOC))
360 $query[] = "update Port set type = ${newkey} where id = ${row['id']} limit 1";
361 $r46->closeCursor();
362 unset ($r46);
363 }
364 elseif ($chapter_no == 3)
365 {
366 $q7 = "select id from Rack where row_id = ${oldkey}";
367 $r7 = $dbxlink->query ($q7);
368 while ($row = $r7->fetch (PDO::FETCH_ASSOC))
369 $query[] = "update Rack set row_id = ${newkey} where id = ${row['id']} limit 1";
370 $r7->closeCursor();
371 unset ($r7);
372 }
373 elseif ($chapter_no == 20)
374 {
375 $q8 = "select object_id, localip, localport, remoteip, remoteport from PortForwarding where proto = ${oldkey}";
376 $r8 = $dbxlink->query ($q8);
377 while ($row = $r8->fetch (PDO::FETCH_ASSOC))
378 $query[] = "update PortForwarding set proto = ${newkey} where " .
379 "object_id = ${row['object_id']} and localip = ${row['localip']} and " .
380 "localport = ${row['localport']} and remoteip = ${row['remoteip']} and " .
381 "remoteport = ${row['remoteport']} and proto = ${oldkey} limit 1";
382 $r8->closeCursor();
383 unset ($r8);
384 }
385 elseif (in_array ($chapter_no, $chaplist))
386 {
387 $q81 = "select object_id, AttributeValue.attr_id from " .
388 "AttributeValue natural join Attribute natural join AttributeMap " .
389 "inner join RackObject on RackObject.id = object_id and RackObject.objtype_id = AttributeMap.objtype_id " .
390 "where attr_type = 'dict' and chapter_no = ${chapter_no} and uint_value = ${oldkey}";
391 $r81 = $dbxlink->query ($q81);
392 while ($row = $r81->fetch (PDO::FETCH_ASSOC))
393 $query[] = "update AttributeValue set uint_value = ${newkey} " .
394 "where object_id = ${row['object_id']} and attr_id = ${row['attr_id']}";
395 $r81->closeCursor();
396 unset ($r81);
397 }
398 }
399 }
400 // Now it's possible to schedule PortCompat regeneration.
401 // Convert the fields to unsigned on occasion.
402 $query[] = 'drop table PortCompat';
403 $query[] = 'create table PortCompat (type1 int(10) unsigned NOT NULL, type2 int(10) unsigned NOT NULL)';
404 $q9 = "select type1, type2 from PortCompat";
405 $r9 = $dbxlink->query ($q9);
406 while ($row = $r9->fetch (PDO::FETCH_ASSOC))
407 {
408 $new_type1 = $new_dict[2][$row['type1']]['newkey'];
409 $new_type2 = $new_dict[2][$row['type2']]['newkey'];
410 $query[] = "insert into PortCompat (type1, type2) values (${new_type1}, ${new_type2})";
411 }
412 $r9->closeCursor();
413 unset ($r9);
414 echo '</pre>';
415
416 // Give the configuration some finish
417 $query[] = "update Config set is_hidden = 'yes' where varname = 'color_F'";
418 $query[] = "update Config set is_hidden = 'yes' where varname = 'color_A'";
419 $query[] = "update Config set is_hidden = 'yes' where varname = 'color_U'";
420 $query[] = "update Config set is_hidden = 'yes' where varname = 'color_T'";
421 $query[] = "update Config set is_hidden = 'yes' where varname = 'color_Th'";
422 $query[] = "update Config set is_hidden = 'yes' where varname = 'color_Tw'";
423 $query[] = "update Config set is_hidden = 'yes' where varname = 'color_Thw'";
424 $query[] = "update Config set description = 'Default port type' where varname = 'default_port_type'";
425 $query[] = "update Config set description = 'Picture scale for rack row display' where varname = 'ROW_SCALE'";
426 $query[] = "update Config set description = 'Organization name' where varname = 'enterprise'";
427 $query[] = "update Config set description = 'Expect common name configured for the following object types' where varname = 'NAMEFUL_OBJTYPES'";
428 $query[] = "update Config set description = '&lt;SELECT&gt; lists height' where varname = 'MAXSELSIZE'";
429 $query[] = "update Config set description = '&quot;Fast&quot; form is this many records tall' where varname = 'MASSCOUNT'";
430 $query[] = "update Config set is_hidden = 'no', description = 'Ports per row in VLANs tab' where varname = 'PORTS_PER_ROW'";
431 $query[] = "INSERT INTO `Config` VALUES ('IPV4_ADDRS_PER_PAGE','256','uint','no','no','IPv4 addresses per page')";
432 $query[] = "INSERT INTO `Config` VALUES ('DEFAULT_RACK_HEIGHT','42','uint','yes','no','Default rack height')";
433 // After Dictionary transformation we ought to list 337 stock records in DB. Add more.
434 $new_words = array();
435 $new_words[338] = array (12 => 'Dell PowerConnect 2216');
436 $new_words[] = array (12 => 'Dell PowerConnect 2224');
437 $new_words[] = array (12 => 'Dell PowerConnect 2324');
438 $new_words[] = array (12 => 'Dell PowerConnect 2708');
439 $new_words[] = array (12 => 'Dell PowerConnect 2716');
440 $new_words[] = array (12 => 'Dell PowerConnect 2724');
441 $new_words[] = array (12 => 'Dell PowerConnect 2748');
442 $new_words[] = array (12 => 'Dell PowerConnect 3424');
443 $new_words[] = array (12 => 'Dell PowerConnect 3424P');
444 $new_words[] = array (12 => 'Dell PowerConnect 3448');
445 $new_words[] = array (12 => 'Dell PowerConnect 3448P');
446 $new_words[] = array (12 => 'Dell PowerConnect 5324');
447 $new_words[] = array (12 => 'Dell PowerConnect 6224');
448 $new_words[] = array (12 => 'Dell PowerConnect 6224P');
449 $new_words[] = array (12 => 'Dell PowerConnect 6224F');
450 $new_words[] = array (12 => 'Dell PowerConnect 6248');
451 $new_words[] = array (12 => 'Dell PowerConnect 6248P');
452 $new_words[] = array (11 => 'Dell PowerEdge 6850');
453 $new_words[] = array (11 => 'Dell PowerEdge 6950');
454 $new_words[] = array (11 => 'Dell PowerEdge R900');
455 $new_words[] = array (11 => 'Dell PowerEdge 4400');
456 $new_words[] = array (11 => 'Dell PowerEdge 2650');
457 $new_words[] = array (11 => 'Dell PowerEdge 2550');
458 $new_words[] = array (11 => 'Dell PowerEdge 750');
459 $new_words[] = array (11 => 'Dell PowerEdge 2450');
460 $new_words[] = array (11 => 'Dell PowerEdge 850');
461 $new_words[] = array (11 => 'Dell PowerEdge 1850');
462 $new_words[] = array (11 => 'Dell PowerEdge 860');
463 $new_words[] = array (11 => 'Dell PowerEdge 2900');
464 $new_words[] = array (11 => 'Dell PowerEdge 2970');
465 $new_words[] = array (11 => 'Dell PowerEdge SC1435');
466 $new_words[] = array (12 => 'Cisco Catalyst 6509');
467 $new_words[] = array (12 => 'Cisco ME 6524GS-8S');
468 $new_words[] = array (12 => 'Cisco ME 6524GT-8S');
469 $new_words[] = array (12 => 'Cisco Catalyst 4503-E');
470 $new_words[] = array (12 => 'Cisco Catalyst 4506-E');
471 $new_words[] = array (12 => 'Cisco Catalyst 4507R-E');
472 $new_words[] = array (12 => 'Cisco Catalyst 4510R-E');
473 $new_words[] = array (12 => 'Cisco Catalyst 3750-24TE-M');
474 $new_words[] = array (12 => 'Cisco Catalyst 4948-10GE');
475 $new_words[] = array (12 => 'Cisco ME 4924-10GE');
476 $new_words[] = array (12 => 'Cisco Catalyst 2960-24');
477 $new_words[] = array (12 => 'Cisco Catalyst 2950-24');
478 $new_words[] = array (12 => 'Cisco Catalyst 2950-12');
479 $new_words[] = array (12 => 'Cisco Catalyst 2950C-24');
480 $new_words[] = array (12 => 'Cisco Catalyst 2950G-24-DC');
481 $new_words[] = array (12 => 'Cisco Catalyst 2950SX-48');
482 $new_words[] = array (12 => 'Cisco Catalyst 2950SX-24');
483 $new_words[] = array (12 => 'Cisco Catalyst 2950T-24');
484 $new_words[] = array (12 => 'Cisco Catalyst 2950T-48');
485 $new_words[] = array (12 => 'Cisco Catalyst 2950G-12');
486 $new_words[] = array (12 => 'Cisco Catalyst 2950G-24');
487 $new_words[] = array (12 => 'Cisco Catalyst 2950G-48');
488 $new_words[] = array (12 => 'Cisco Catalyst 3508G XL');
489 $new_words[] = array (12 => 'Cisco Catalyst 3512 XL');
490 $new_words[] = array (12 => 'Cisco Catalyst 3524 XL');
491 $new_words[] = array (12 => 'Cisco Catalyst 3524 PWR XL');
492 $new_words[] = array (12 => 'Cisco Catalyst 3548 XL');
493 $new_words[] = array (12 => 'Cisco ME 2400-24TS-A');
494 $new_words[] = array (12 => 'Cisco ME 2400-24TS-D');
495 $new_words[] = array (12 => 'Cisco Catalyst 3550-12T');
496 $new_words[] = array (12 => 'Cisco Catalyst 3550-12G');
497 $new_words[] = array (12 => 'Cisco Catalyst 3550-24');
498 $new_words[] = array (12 => 'Cisco Catalyst 3550-24 FX');
499 $new_words[] = array (12 => 'Cisco Catalyst 3550-24 DC');
500 $new_words[] = array (12 => 'Cisco Catalyst 3550-24 PWR');
501 $new_words[] = array (12 => 'Cisco Catalyst 3550-48');
502 $new_words[] = array (12 => 'Cisco ME 3400G-12CS-A');
503 $new_words[] = array (12 => 'Cisco ME 3400G-12CS-D');
504 $new_words[] = array (12 => 'Cisco ME 3400G-2CS-A');
505 $new_words[] = array (12 => 'Cisco ME 3400-24TS-A');
506 $new_words[] = array (12 => 'Cisco ME 3400-24TS-D');
507 $new_words[] = array (12 => 'Cisco ME 3400-24FS-A');
508 $new_words[] = array (12 => 'Foundry FastIron GS 624XGP');
509 $new_words[] = array (12 => 'Foundry FastIron GS 624XGP-POE');
510 $new_words[] = array (12 => 'Foundry FastIron LS 624');
511 $new_words[] = array (12 => 'Foundry FastIron LS 648');
512 $new_words[] = array (12 => 'Foundry NetIron M2404F');
513 $new_words[] = array (12 => 'Foundry NetIron M2404C');
514 $new_words[] = array (17 => 'Foundry BigIron RX-32');
515 $new_words[] = array (13 => 'Debian 2.0 (hamm)');
516 $new_words[] = array (13 => 'Debian 2.1 (slink)');
517 $new_words[] = array (13 => 'Debian 2.2 (potato)');
518 $new_words[] = array (13 => 'Debian 4.0 (etch)');
519 $new_words[] = array (13 => 'ALTLinux Server 4.0');
520 $new_words[] = array (13 => 'ALTLinux Sisyphus');
521 $new_words[] = array (13 => 'openSUSE 10.0');
522 $new_words[] = array (13 => 'openSUSE 10.1');
523 $new_words[] = array (13 => 'openSUSE 10.2');
524 $new_words[] = array (13 => 'openSUSE 10.3');
525 $new_words[] = array (13 => 'Ubuntu 4.10');
526 $new_words[] = array (13 => 'Ubuntu 5.04');
527 $new_words[] = array (13 => 'Ubuntu 5.10');
528 $new_words[] = array (13 => 'Ubuntu 6.06 LTS');
529 $new_words[] = array (13 => 'Ubuntu 6.10');
530 $new_words[] = array (13 => 'Ubuntu 7.04');
531 $new_words[] = array (13 => 'Ubuntu 7.10');
532 $new_words[] = array (13 => 'Ubuntu 8.04 LTS');
533 $new_words[] = array (13 => 'RHEL5');
534 $new_words[] = array (18 => 'Dell PowerVault 210S');
535 $new_words[] = array (18 => 'Dell PowerVault 221S');
536 $new_words[] = array (2 => 'dry contact');
537 $new_words[] = array (2 => 'unknown');
538 // Two above records ought to take keys 439 and 440.
539 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES (439,439)";
540 $new_words[] = array (13 => 'CentOS-2');
541 $new_words[] = array (13 => 'CentOS-3');
542 $new_words[] = array (13 => 'CentOS-4');
543 $new_words[] = array (13 => 'CentOS-5');
544 foreach ($new_words as $dict_key => $tmp)
545 foreach ($tmp as $chapter_no => $dict_value)
546 $query[] = 'INSERT INTO `Dictionary` (`chapter_no`, `dict_key`, `dict_value`) ' .
547 "VALUES (${chapter_no}, ${dict_key}, '${dict_value}')";
548 // Resetting to defaults is worse, than remapping, but better than
549 // leaving messed values.
550 $query[] = "update Config set varvalue = '24' where varname = 'default_port_type' limit 1";
551 // We are done.
552 $query[] = "update Config set varvalue = '0.14.7' where varname = 'DB_VERSION'";
553 break; // --------------------------------------------
554 case '0.14.8':
555 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('REQUIRE_ASSET_TAG_FOR','4,7,8','string','yes','no','Require asset tag for the following object types')";
556 $query[] = "alter table Port modify column id int(10) unsigned NOT NULL auto_increment";
557 $query[] = "alter table Port modify column object_id int(10) unsigned NOT NULL";
558 $query[] = "alter table Port modify column type int(10) unsigned NOT NULL";
559 $query[] = "alter table Link modify column porta int(10) unsigned";
560 $query[] = "alter table Link modify column portb int(10) unsigned";
561 $query[] = "alter table PortForwarding modify column object_id int(10) unsigned not null";
562 $query[] = "alter table PortForwarding modify column localport smallint(5) unsigned not null";
563 $query[] = "alter table PortForwarding modify column remoteport smallint(5) unsigned not null";
564 $query[] = "alter table IPBonds modify column object_id int(10) unsigned not null";
565 $query[] = "alter table IPRanges modify column id int(10) unsigned not null";
566 $query[] = "alter table IPRanges modify column mask int(10) unsigned not null";
567 $query[] = "alter table Port add index `type` (type)";
568 $query[] = "alter table PortCompat add index `type1` (type1)";
569 $query[] = "alter table PortCompat add index `type2` (type2)";
570 $query[] = "update Dictionary set dict_value = 'Debian 3.0 (woody)' where dict_key = 234";
571 $query[] = "update Dictionary set dict_value = 'Debian 3.1 (sarge)' where dict_key = 235";
572 $query[] = "update Dictionary set dict_value = 'Foundry BigIron 15000' where dict_key = 311";
573 $query[] = "update Dictionary set dict_value = 'RHF7' where dict_key = 232";
574 $query[] = "update Dictionary set dict_value = 'RHF8' where dict_key = 242";
575 $query[] = "INSERT INTO `Attribute` (`attr_id`, `attr_type`, `attr_name`) VALUES (25,'string','UUID');";
576 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_no`) VALUES (4,25,0);";
577 $query[] = "update Dictionary set dict_value = '[[Cisco Catalyst 2970G-24T | http://www.cisco.com/en/US/products/hw/switches/ps5206/ps5313/index.html]]' where dict_key = 210";
578 $query[] = "update Dictionary set dict_value = '[[Cisco Catalyst 2970G-24TS | http://www.cisco.com/en/US/products/hw/switches/ps5206/ps5437/index.html]]' where dict_key = 211";
579 $query[] = "update Config set varvalue = '0.14.8' where varname = 'DB_VERSION'";
580 break; // --------------------------------------------
581 case '0.14.9':
582 $query[] = "alter table IPRanges modify column id int(10) unsigned not null auto_increment";
583 $query[] = "alter table Rack modify column height tinyint(3) unsigned not null default '42'";
584 $query[] = "alter table Rack add column thumb_data blob after comment";
585 $query[] = "
586 CREATE TABLE `IPLoadBalancer` (
587 `object_id` int(10) unsigned default NULL,
588 `rspool_id` int(10) unsigned default NULL,
589 `vs_id` int(10) unsigned default NULL,
590 `vsconfig` text,
591 `rsconfig` text,
592 UNIQUE KEY `LB-VS` (`object_id`,`vs_id`)
593 ) ENGINE=MyISAM";
594 $query[] = "
595 CREATE TABLE `IPRSPool` (
596 `id` int(10) unsigned NOT NULL auto_increment,
597 `name` char(255) default NULL,
598 `vsconfig` text,
599 `rsconfig` text,
600 PRIMARY KEY (`id`)
601 ) ENGINE=MyISAM";
602 $query[] = "
603 CREATE TABLE `IPRealServer` (
604 `id` int(10) unsigned NOT NULL auto_increment,
605 `inservice` enum('yes','no') NOT NULL default 'no',
606 `rsip` int(10) unsigned default NULL,
607 `rsport` smallint(5) unsigned default NULL,
608 `rspool_id` int(10) unsigned default NULL,
609 `rsconfig` text,
610 PRIMARY KEY (`id`),
611 UNIQUE KEY `pool-endpoint` (`rspool_id`,`rsip`,`rsport`)
612 ) ENGINE=MyISAM";
613 $query[] = "
614 CREATE TABLE `IPVirtualService` (
615 `id` int(10) unsigned NOT NULL auto_increment,
616 `vip` int(10) unsigned default NULL,
617 `vport` smallint(5) unsigned default NULL,
618 `proto` enum('TCP','UDP') NOT NULL default 'TCP',
619 `name` char(255) default NULL,
620 `vsconfig` text,
621 `rsconfig` text,
622 PRIMARY KEY (`id`)
623 ) ENGINE=MyISAM";
624 $query[] = "INSERT INTO `Config` VALUES ('DEFAULT_SLB_VS_PORT','','uint','yes','no','Default port of SLB virtual service')";
625 $query[] = "INSERT INTO `Config` VALUES ('DEFAULT_SLB_RS_PORT','','uint','yes','no','Default port of SLB real server')";
626 $query[] = "INSERT INTO `Config` VALUES ('IPV4_PERFORMERS','1,4,7,8,12,14','string','yes','no','IPv4-capable object types')";
627 $query[] = "INSERT INTO `Config` VALUES ('NATV4_PERFORMERS','4,7,8','string','yes','no','NATv4-capable object types')";
628 $query[] = "INSERT INTO `Config` VALUES ('USER_AUTH_SRC','database','string','no','no','User authentication source')";
629 $query[] = "alter table RackSpace drop column problem_id";
630 $query[] = "update Config set varvalue = '0.14.9' where varname = 'DB_VERSION'";
631 break; // --------------------------------------------
632 case '0.14.10':
633 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('DETECT_URLS','no','string','yes','no','Detect URLs in text fields')";
634 $query[] = "alter table RackHistory modify column height tinyint(3) unsigned";
635 $query[] = "alter table RackHistory add column thumb_data blob after comment";
636 $query[] = "INSERT INTO `Config` VALUES ('RACK_PRESELECT_THRESHOLD','1','uint','no','no','Rack pre-selection threshold')";
637 $query[] = "update Config set varvalue = '0.14.10' where varname = 'DB_VERSION'";
638 break; // --------------------------------------------
639 # case '0.14.11':
640 # $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('AUTO_PORTS_CONFIG','FIXME','string','yes','no','Autoports configuration')";
641 # $query[] = "alter table Rack add column left_is_front enum ('yes', 'no') not null default 'yes' after height";
642 # $query[] = "alter table Rack add column bottom_is_unit1 enum ('yes', 'no') not null default 'yes' after left_is_front";
643 # $query[] = "update Config set varvalue = '0.14.11' where varname = 'DB_VERSION'";
644 # break; // --------------------------------------------
645 default:
646 showError ("executeUpgradeBatch () failed, because batch '${batchid}' isn't defined");
647 die;
648 break;
649 }
650 $failures = array();
651 $ndots = 0;
652 echo "<pre>Executing database upgrade batch '${batchid}':\n";
653 foreach ($query as $q)
654 {
655 $result = $dbxlink->query ($q);
656 if ($result != NULL)
657 echo '.';
658 else
659 {
660 echo '!';
661 $errorInfo = $dbxlink->errorInfo();
662 $failures[] = array ($q, $errorInfo[2]);
663 }
664 if (++$ndots == 50)
665 {
666 echo "\n";
667 flush();
668 $ndots = 0;
669 }
670 }
671 echo '<br>';
672 if (!count ($failures))
673 echo "No errors!\n";
674 else
675 {
676 echo "The following queries failed:\n";
677 foreach ($failures as $f)
678 {
679 list ($q, $i) = $f;
680 echo "${q} // ${i}\n";
681 }
682 }
683 echo '</pre>';
684 }
685
686 // ******************************************************************
687 //
688 // Execution starts here
689 //
690 // ******************************************************************
691
692 $root = (empty($_SERVER['HTTPS'])?'http':'https').
693 '://'.
694 (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']))).
695 dirname($_SERVER['PHP_SELF']);
696 if (substr ($root, -1) != '/')
697 $root .= '/';
698
699 // The below will be necessary as long as we rely on showError()
700 require_once 'inc/interface.php';
701
702 require_once 'inc/config.php';
703 require_once 'inc/database.php';
704 if (file_exists ('inc/secret.php'))
705 require_once 'inc/secret.php';
706 else
707 die ("Database connection parameters are read from inc/secret.php file, " .
708 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
709 "inc/secret.php and modify to your setup.\n\nThen reload the page.");
710
711 try
712 {
713 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
714 }
715 catch (PDOException $e)
716 {
717 die ("Database connection failed:\n\n" . $e->getMessage());
718 }
719
720 if (isset ($_SERVER['PHP_AUTH_USER']))
721 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
722 if (isset ($_SERVER['PHP_AUTH_PW']))
723 $_SERVER['PHP_AUTH_PW'] = escapeString ($_SERVER['PHP_AUTH_PW']);
724
725 // Now we need to be sure that the current user is the administrator.
726 // The rest doesn't matter within this context.
727 // We still continue to use the current authenticator though, but this will
728 // last only till the UserAccounts remains the same. After that this file
729 // will have to dig into the DB for the user accounts.
730 require_once 'inc/auth.php';
731
732 // This will not fail sanely, because getUserAccounts() depends on showError()
733 $accounts = getUserAccounts();
734
735 // Auth prompt risk being a little broken here due to config cache absence.
736 $configCache = array();
737 // Only administrator is always authenticated locally, so reject others
738 // for authenticate() to succeed.
739 if ($accounts[$_SERVER['PHP_AUTH_USER']]['user_id'] != 1)
740 die ('You are not allowed to upgrade the database. Ask your RackTables administrator to do this.');
741 authenticate();
742
743 $dbver = getDatabaseVersion();
744 echo 'Code version == ' . CODE_VERSION;
745 echo '<br>Database version == ' . $dbver;
746 if ($dbver == CODE_VERSION)
747 {
748 die ("<p align=justify>Your database seems to be up-to-date. " .
749 "Now the best thing to do would be to follow to the <a href='${root}'>main page</a> " .
750 "and explore your data. Have a nice day.</p>");
751 }
752
753 foreach (getDBUpgradePath ($dbver, CODE_VERSION) as $batchid)
754 executeUpgradeBatch ($batchid);
755
756 echo '<br>Database version == ' . getDatabaseVersion();
757 echo "<p align=justify>Your database seems to be up-to-date. " .
758 "Now the best thing to do would be to follow to the <a href='${root}'>main page</a> " .
759 "and explore your data. Have a nice day.</p>";
760
761 ?>