90d474f7ccf7239e888004d2c22724e12522c08e
[racktables] / wwwroot / inc / upgrade.php
1 <?php
2
3 # This file is a part of RackTables, a datacenter and server room management
4 # framework. See accompanying file "COPYING" for the full copyright and
5 # licensing information.
6
7 $relnotes = array
8 (
9 '0.20.0' => <<<ENDOFTEXT
10 WARNING: This release have too many internal changes, some of them were waiting more than a year
11 to be released. So this release is considered "BETA" and is recommended only to curiuos users,
12 who agree to sacrifice the stability to the progress.
13
14 Racks and Rows are now stored in the database as Objects. The RackObject table
15 was renamed to Object. SQL views were created to ease the migration of custom
16 reports and scripts.
17
18 New plugins engine instead of local.php file. To make your own code stored in local.php work,
19 you must move the local.php file into the plugins/ directory. The name of this file does not
20 matter any more. You also can store multiple files in that dir, separate your plugins by features,
21 share them and try the plugins from other people just placing them into plugins/ dir, no more merging.
22 \$path_to_local_php variable has no special meaning any more.
23 \$racktables_confdir variable is now used only to search for secret.php file.
24 \$racktables_plugins_dir is a new overridable special variable pointing to plugins/ directory.
25
26 Beginning with this version it is possible to delete IP prefixes, VLANs, Virtual services
27 and RS pools from within theirs properties tab. So please inspect your permissions rules
28 to assure there are no undesired allows for deletion of these objects. To ensure this, you could
29 try this code in the beginning of permissions script:
30
31 allow {userid_1} and {\$op_del}
32 deny {\$op_del} and ({\$tab_edit} or {\$tab_properties})
33
34 Hardware gateways engine was rewritten in this version of RackTables. This means that
35 the file gateways/deviceconfig/switch.secrets.php is not used any more. To get information
36 about configuring connection properties and credentials in a new way please visit
37 http://wiki.racktables.org/index.php/Gateways
38
39 This also means that recently added features based on old API (D-Link switches and Linux
40 gateway support contributed by Ilya Evseev) are not working any more and waiting to be
41 forward-ported to new gateways API. Sorry for that.
42
43 Two new config variables appeared in this version:
44 - SEARCH_DOMAINS. Comma-separated list of DNS domains which are considered "base" for your
45 network. If RackTables search engine finds multiple objects based on your search input, but
46 there is only one which FQDN consists of your input and one of these search domains, you will
47 be redirected to this object and other results will be discarded. Such behavior was unconditional
48 since 0.19.3, which caused many objections from users. So welcome this config var.
49 - QUICK_LINK_PAGES. Comma-separated list of RackTables pages to display links to them on top.
50 Each user could have his own list.
51
52 Also some of config variables have changed their default values in this version.
53 This means that upgrade script will change their values if you have them in previous default state.
54 This could be inconvenient, but it is the most effective way to encourage users to use new features.
55 If this behavior is not what you want, simply revert these variables' values:
56 - SHOW_LAST_TAB no => yes
57 - IPV4_TREE_SHOW_USAGE yes =>no (networks' usage is still available by click)
58 - IPV4LB_LISTSRC {\$typeid_4} => false
59 - FILTER_DEFAULT_ANDOR or => and (this implicitly enables the feature of dynamic tree shrinking)
60 - FILTER_SUGGEST_EXTRA no => yes (yes, we have extra logical filters!)
61 - IPV4_TREE_RTR_AS_CELL yes => no (display routers as simple text, not cell)
62
63 Also please note that variable IPV4_TREE_RTR_AS_CELL now has third special value
64 besides 'yes' and 'no': 'none'. Use 'none' value if you are experiencing low performance
65 on IP tree page. It will completely disable IP ranges scan for used/spare IPs and the
66 speed of IP tree will increase radically. The price is you will not see the routers in
67 IP tree at all.
68
69 ENDOFTEXT
70 ,
71
72 '0.20.1' => <<<ENDOFTEXT
73 The 0.20.0 release includes bug which breaks IP networks' capacity displaying on 32-bit architecture machines. To fix this, this release makes use of PHP's BC Math module. It is a new reqiurement. Most PHP distributions have this module already enabled, but if yours does not - you need yo recompile PHP.
74
75 Security context of 'ipaddress' page now includes tags from the network containing an IP address. This means that you should audit your permission rules to check there is no unintended allows of changing IPs based on network's tagset. Example:
76 allow {client network} and {New York}
77 This rule now not only allows any operation on NY client networks, but also any operation with IP addresses included in those networks. To fix this, you should change the rule this way:
78 allow {client network} and {New York} and not {\$page_ipaddress}
79
80 ENDOFTEXT
81 ,
82
83 '0.20.4' => <<<ENDOFTEXT
84 Please note that some dictionary items of Cisco Catalyst 2960 series switches
85 were renamed to meet official Cisco classification:
86 2960-48TT => 2960-48TT-L
87 2960-24TC => 2960-24TC-L
88 2960-24TT => 2960-24TT-L
89 2960-8TC => 2960-8TC-L
90 2960G-48TC => 2960G-48TC-L
91 2960G-24TC => 2960G-24TC-L
92 2960G-8TC => 2960G-8TC-L
93 C2960-24 => C2960-24-S
94 C2960G-24PC => C2960-24PC-L
95
96 The DATETIME_FORMAT configuration option used in setting date and time output
97 format now uses a different [1] syntax. During upgrade the option is reset to
98 the default value, which is now %Y-%m-%d (YYYY-MM-DD) per ISO 8601.
99
100 This release intoduces two new configuration options:
101 REVERSED_RACKS_LISTSRC and NEAREST_RACKS_CHECKBOX.
102
103 [1] http://php.net/manual/en/function.strftime.php
104 ENDOFTEXT
105 ,
106
107 '0.20.5' => <<<ENDOFTEXT
108 This release introduces the VS groups feature. VS groups is a new way to store
109 and display virtual services configuration. There is a new "ipvs" (VS group)
110 realm. All previously existing VS configuration remains functional and user
111 is free to convert it to the new format, which displays it in a more natural way
112 and allows to generate virtual_server_group keepalived configs. To convert a
113 virtual service to the new format, it is necessary to manually create a VS group
114 object and assign IP addresses to it. The VS group will display a "Migrate" tab
115 to convert the old-style VS objects, which can be removed after a successful
116 conversion.
117
118 The old-style VS configuration becomes DEPRECATED. Its support will be removed
119 in a future major release. So it is strongly recommended to convert it to the
120 new format.
121 ENDOFTEXT
122 ,
123
124 '0.20.6' => <<<ENDOFTEXT
125 New MGMT_PROTOS configuration option replaces the TELNET_OBJS_LISTSRC,
126 SSH_OBJS_LISTSRC and RDP_OBJS_LISTSRC options (converting existing settings as
127 necessary). MGMT_PROTOS allows to specify any management protocol for a
128 particular device list using a RackCode filter. The default value
129 ("ssh: {\$typeid_4}, telnet: {\$typeid_8}") produces "ssh://server.fqdn" for
130 servers and "telnet://switch.fqdn" for network switches.
131 ENDOFTEXT
132 ,
133
134 '0.20.7' => <<<ENDOFTEXT
135 From now on the minimum (oldest) release of PHP that can run RackTables is
136 5.2.10. In particular, to continue running RackTables on CentOS 5 it is
137 necessary to replace its php* RPM packages with respective php53* packages
138 before the upgrade (except the JSON package, which PHP 5.3 provides internally).
139
140 Database triggers are used for some data consistency measures. The database
141 user account must have the 'TRIGGER' privilege, which was introduced in
142 MySQL 5.1.7.
143
144 The IPV4OBJ_LISTSRC configuration option is reset to an expression which enables
145 the IP addressing feature for all object types except those listed.
146
147 Tags could now be assigned on the Edit/Properties tab using a text input with
148 auto-completion. Type a star '*' to view full tag tree in auto-complete menu.
149 It is worth to add the following line to the permissions script if the
150 old-fashioned 'Tags' tab is not needed any more:
151 deny {\$tab_tags} # this hides 'Tags' tab
152
153 This release converts collation of all DB fields to the utf8_unicode_ci. This
154 procedure may take some time, and could fail if there are rows that differ only
155 by letter case. If this happen, you'll see the failed SQL query in upgrade report
156 with the "Duplicate entry" error message. Feel free to continue using your
157 installation. If desired so, you could eliminate the case-duplicating rows
158 and re-apply the failed query.
159 ENDOFTEXT
160 ,
161 '0.20.11' => <<<ENDOFTEXT
162 New IPV4_TREE_SHOW_UNALLOCATED configuration option introduced to disable
163 dsplaying unallocated networks in IPv4 space tree. Setting it also disables
164 the "knight" feature.
165 ENDOFTEXT
166 ,
167 );
168
169 // At the moment we assume, that for any two releases we can
170 // sequentally execute all batches, that separate them, and
171 // nothing will break. If this changes one day, the function
172 // below will have to generate smarter upgrade paths, while
173 // the upper layer will remain the same.
174 // Returning an empty array means that no upgrade is necessary.
175 // Returning NULL indicates an error.
176 function getDBUpgradePath ($v1, $v2)
177 {
178 $versionhistory = array
179 (
180 '0.20.0',
181 '0.20.1',
182 '0.20.2',
183 '0.20.3',
184 '0.20.4',
185 '0.20.5',
186 '0.20.6',
187 '0.20.7',
188 '0.20.8',
189 '0.20.9',
190 '0.20.10',
191 '0.20.11',
192 '0.20.12',
193 '0.20.13',
194 );
195 if (! in_array ($v1, $versionhistory) || ! in_array ($v2, $versionhistory))
196 return NULL;
197 $skip = TRUE;
198 $path = NULL;
199 // foreach() below cannot handle this specific case
200 if ($v1 == $v2)
201 return array();
202 // Now collect all versions > $v1 and <= $v2
203 foreach ($versionhistory as $v)
204 {
205 if ($skip && $v == $v1)
206 {
207 $skip = FALSE;
208 $path = array();
209 continue;
210 }
211 if ($skip)
212 continue;
213 $path[] = $v;
214 if ($v == $v2)
215 break;
216 }
217 return $path;
218 }
219
220 // Upgrade batches are named exactly as the release where they first appear.
221 // That is simple, but seems sufficient for beginning.
222 function getUpgradeBatch ($batchid)
223 {
224 $query = array();
225 global $dbver, $dbxlink;
226 switch ($batchid)
227 {
228 case '0.20.0':
229 $query[] = "
230 CREATE TABLE `PortLog` (
231 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
232 `port_id` int(10) unsigned NOT NULL,
233 `date` datetime NOT NULL,
234 `user` varchar(64) NOT NULL,
235 `message` text NOT NULL,
236 PRIMARY KEY (`id`),
237 KEY `port_id-date` (`port_id`,`date`),
238 CONSTRAINT `PortLog_ibfk_1` FOREIGN KEY (`port_id`) REFERENCES `Port` (`id`) ON DELETE CASCADE
239 ) ENGINE=InnoDB;
240 ";
241 $query[] = "
242 CREATE TABLE `IPv4Log` (
243 `id` int(10) NOT NULL AUTO_INCREMENT,
244 `ip` int(10) unsigned NOT NULL,
245 `date` datetime NOT NULL,
246 `user` varchar(64) NOT NULL,
247 `message` text NOT NULL,
248 PRIMARY KEY (`id`),
249 KEY `ip-date` (`ip`,`date`)
250 ) ENGINE=InnoDB;
251 ";
252 $query[] = "
253 CREATE TABLE `IPv6Log` (
254 `id` int(10) NOT NULL AUTO_INCREMENT,
255 `ip` binary(16) NOT NULL,
256 `date` datetime NOT NULL,
257 `user` varchar(64) NOT NULL,
258 `message` text NOT NULL,
259 PRIMARY KEY (`id`),
260 KEY `ip-date` (`ip`,`date`)
261 ) ENGINE=InnoDB;
262 ";
263 $query[] = "ALTER TABLE `FileLink` MODIFY COLUMN `entity_type` ENUM('ipv4net','ipv4rspool','ipv4vs','ipv6net','location','object','rack','user') NOT NULL DEFAULT 'object'";
264 $query[] = "ALTER TABLE `TagStorage` MODIFY COLUMN `entity_realm` ENUM('file','ipv4net','ipv4rspool','ipv4vs','ipv6net','location','object','rack','user','vst') NOT NULL default 'object'";
265 $query[] = "ALTER TABLE `TagStorage` ADD COLUMN `user` char(64) DEFAULT NULL, ADD COLUMN `date` datetime DEFAULT NULL";
266
267 // Rename object tables and keys, 'name' no longer needs to be unique
268 $dbxlink->query ('ALTER TABLE `RackObject` RENAME TO `Object`');
269 $dbxlink->query ('ALTER TABLE `RackObjectHistory` RENAME TO `ObjectHistory`');
270 $dbxlink->query ('ALTER TABLE `Object` DROP KEY `name`');
271 $query[] = 'ALTER TABLE `Object` DROP KEY `RackObject_asset_no`';
272 $query[] = 'ALTER TABLE `Object` ADD UNIQUE KEY `asset_no` (`asset_no`)';
273 $query[] = 'ALTER TABLE `Object` ADD KEY `type_id` (`objtype_id`,`id`)';
274 $query[] = 'ALTER TABLE `ObjectHistory` DROP FOREIGN KEY `RackObjectHistory-FK-object_id`';
275 $query[] = 'ALTER TABLE `ObjectHistory` ADD CONSTRAINT `ObjectHistory-FK-object_id` FOREIGN KEY (`id`) REFERENCES `Object` (`id`) ON DELETE CASCADE';
276 $query[] = 'ALTER TABLE `RackSpace` DROP FOREIGN KEY `RackSpace-FK-rack_id`';
277
278 // Rack height is now an attribute
279 $query[] = "INSERT INTO `Attribute` (`id`,`type`,`name`) VALUES (27,'uint','Height, units')";
280 $query[] = 'INSERT INTO `AttributeMap` (`objtype_id`,`attr_id`,`chapter_id`) VALUES (1560,27,NULL)';
281
282 // Racks are now sorted using an attribute
283 $query[] = "INSERT INTO `Attribute` (`id`,`type`,`name`) VALUES (29,'uint','Sort order')";
284 $query[] = 'INSERT INTO `AttributeMap` (`objtype_id`,`attr_id`,`chapter_id`) VALUES (1560,29,NULL)';
285
286 // Relate 'contact person' with locations
287 $query[] = 'INSERT INTO `AttributeMap` (`objtype_id`,`attr_id`,`chapter_id`) VALUES (1562,14,NULL)';
288
289 // Allow relationships between racks/rows/locations
290 $query[] = "ALTER TABLE `EntityLink` MODIFY COLUMN `parent_entity_type` ENUM('ipv4net','ipv4rspool','ipv4vs','ipv6net','location','object','rack','row','user') NOT NULL";
291 $query[] = "ALTER TABLE `EntityLink` MODIFY COLUMN `child_entity_type` ENUM('file','location','object','rack','row') NOT NULL";
292
293 // Turn rows into objects
294 $result = $dbxlink->query ('SELECT id, name FROM RackRow');
295 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
296 unset ($result);
297 foreach ($rows as $row)
298 {
299 $prepared = $dbxlink->prepare ('INSERT INTO `Object` (`name`,`objtype_id`) VALUES (?,?)');
300 $prepared->execute (array($row['name'], 1561));
301 $row_id = $dbxlink->lastInsertID();
302 // Turn all racks in this row into objects
303 $result = $dbxlink->query ("SELECT id, name, height, comment FROM Rack WHERE row_id=${row['id']} ORDER BY name");
304 $racks = $result->fetchAll (PDO::FETCH_ASSOC);
305 unset ($result);
306 $sort_order = 1;
307 foreach ($racks as $rack)
308 {
309 // Add the rack as an object, set the height and sort order as attributes, link the rack to the row,
310 // update rackspace, tags and files to reflect new rack_id, move history
311 $prepared = $dbxlink->prepare ('INSERT INTO `Object` (`name`,`objtype_id`,`comment`) VALUES (?,?,?)');
312 $prepared->execute (array($rack['name'], 1560, $rack['comment']));
313 $rack_id = $dbxlink->lastInsertID();
314 $query[] = "INSERT INTO `AttributeValue` (`object_id`,`object_tid`,`attr_id`,`uint_value`) VALUES (${rack_id},1560,27,${rack['height']})";
315 $query[] = "INSERT INTO `AttributeValue` (`object_id`,`object_tid`,`attr_id`,`uint_value`) VALUES (${rack_id},1560,29,${sort_order})";
316 $query[] = "INSERT INTO `EntityLink` (`parent_entity_type`,`parent_entity_id`,`child_entity_type`,`child_entity_id`) VALUES ('row',${row_id},'rack',${rack_id})";
317 $query[] = "UPDATE `RackSpace` SET `rack_id`=${rack_id} WHERE `rack_id`=${rack['id']}";
318 $query[] = "UPDATE `Atom` SET `rack_id`=${rack_id} WHERE `rack_id`=${rack['id']}";
319 $query[] = "UPDATE `TagStorage` SET `entity_id`=${rack_id} WHERE `entity_realm`='rack' AND `entity_id`=${rack['id']}";
320 $query[] = "UPDATE `FileLink` SET `entity_id`=${rack_id} WHERE `entity_type`='rack' AND `entity_id`=${rack['id']}";
321 $query[] = "INSERT INTO `ObjectHistory` (`id`,`name`,`objtype_id`,`comment`,`ctime`,`user_name`) SELECT ${rack_id},`name`,1560,`comment`,`ctime`,`user_name` FROM `RackHistory` WHERE `id`=${rack['id']}";
322 $sort_order++;
323 }
324 }
325 $query[] = 'ALTER TABLE `RackSpace` ADD CONSTRAINT `RackSpace-FK-rack_id` FOREIGN KEY (`rack_id`) REFERENCES `Object` (`id`)';
326 $query[] = 'DROP TABLE `Rack`';
327 $query[] = 'DROP TABLE `RackRow`';
328 $query[] = 'DROP TABLE `RackHistory`';
329 $query[] = "
330 CREATE TABLE `RackThumbnail` (
331 `rack_id` int(10) unsigned NOT NULL,
332 `thumb_data` blob,
333 UNIQUE KEY `rack_id` (`rack_id`),
334 CONSTRAINT `RackThumbnail-FK-rack_id` FOREIGN KEY (`rack_id`) REFERENCES `Object` (`id`) ON DELETE CASCADE
335 ) ENGINE=InnoDB
336 ";
337 $query[] = "
338 CREATE VIEW `Location` AS SELECT O.id, O.name, O.has_problems, O.comment, P.id AS parent_id, P.name AS parent_name
339 FROM `Object` O
340 LEFT JOIN (
341 `Object` P INNER JOIN `EntityLink` EL
342 ON EL.parent_entity_id = P.id AND P.objtype_id = 1562 AND EL.parent_entity_type = 'location' AND EL.child_entity_type = 'location'
343 ) ON EL.child_entity_id = O.id
344 WHERE O.objtype_id = 1562
345 ";
346 $query[] = "
347 CREATE VIEW `Row` AS SELECT O.id, O.name, L.id AS location_id, L.name AS location_name
348 FROM `Object` O
349 LEFT JOIN `EntityLink` EL ON O.id = EL.child_entity_id AND EL.parent_entity_type = 'location' AND EL.child_entity_type = 'row'
350 LEFT JOIN `Object` L ON EL.parent_entity_id = L.id AND L.objtype_id = 1562
351 WHERE O.objtype_id = 1561
352 ";
353 $query[] = "
354 CREATE VIEW `Rack` AS SELECT O.id, O.name AS name, O.asset_no, O.has_problems, O.comment,
355 AV_H.uint_value AS height,
356 AV_S.uint_value AS sort_order,
357 RT.thumb_data,
358 R.id AS row_id,
359 R.name AS row_name
360 FROM `Object` O
361 LEFT JOIN `AttributeValue` AV_H ON O.id = AV_H.object_id AND AV_H.attr_id = 27
362 LEFT JOIN `AttributeValue` AV_S ON O.id = AV_S.object_id AND AV_S.attr_id = 29
363 LEFT JOIN `RackThumbnail` RT ON O.id = RT.rack_id
364 LEFT JOIN `EntityLink` EL ON O.id = EL.child_entity_id AND EL.parent_entity_type = 'row' AND EL.child_entity_type = 'rack'
365 INNER JOIN `Object` R ON R.id = EL.parent_entity_id
366 WHERE O.objtype_id = 1560
367 ";
368 $query[] = "
369 CREATE VIEW `RackObject` AS SELECT id, name, label, objtype_id, asset_no, has_problems, comment FROM `Object`
370 WHERE `objtype_id` NOT IN (1560, 1561, 1562)
371 ";
372 $query[] = "UPDATE `Chapter` SET `name` = 'ObjectType' WHERE `id` = 1";
373 $query[] = "DELETE FROM RackSpace WHERE object_id IS NULL AND state = 'T'";
374
375 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('SYNC_802Q_LISTSRC','','string','yes','no','no','List of VLAN switches sync is enabled on')";
376 $query[] = "UPDATE `Config` SET is_userdefined='yes' WHERE varname='PROXIMITY_RANGE'";
377 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('QUICK_LINK_PAGES','depot,ipv4space,rackspace','string','yes','no','yes','List of pages to dislay in quick links')";
378 $query[] = "ALTER TABLE `IPv4LB` MODIFY `prio` varchar(255) DEFAULT NULL";
379
380 $query[] = "ALTER TABLE `IPv4Address` ADD COLUMN `comment` char(255) NOT NULL default '' AFTER `name`";
381 $query[] = "ALTER TABLE `IPv6Address` ADD COLUMN `comment` char(255) NOT NULL default '' AFTER `name`";
382
383 // change IP address format of IPv4VS and IPv4RS tables
384 convertSLBTablesToBinIPs();
385
386 // do not allow NULL allocation type
387 $query[] = "ALTER TABLE `IPv4Allocation` MODIFY `type` enum('regular','shared','virtual','router') NOT NULL DEFAULT 'regular'";
388 $query[] = "ALTER TABLE `IPv6Allocation` MODIFY `type` enum('regular','shared','virtual','router') NOT NULL DEFAULT 'regular'";
389
390 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('SEARCH_DOMAINS','','string','yes','no','yes','DNS domain list (comma-separated) to search in FQDN attributes')";
391
392 // update some config variables which changed their defaults in this verison
393 replaceConfigVarValue ('SHOW_LAST_TAB', 'yes');
394 replaceConfigVarValue ('IPV4_TREE_SHOW_USAGE','no');
395 replaceConfigVarValue ('IPV4LB_LISTSRC', 'false', '{$typeid_4}');
396 replaceConfigVarValue ('FILTER_DEFAULT_ANDOR', 'and');
397 replaceConfigVarValue ('FILTER_SUGGEST_EXTRA', 'yes');
398 replaceConfigVarValue ('IPV4_TREE_RTR_AS_CELL', 'no');
399 replaceConfigVarValue ('SSH_OBJS_LISTSRC', 'false', 'none');
400 replaceConfigVarValue ('TELNET_OBJS_LISTSRC', 'false', 'none');
401
402 $query[] = "UPDATE Config SET varvalue = '0.20.0' WHERE varname = 'DB_VERSION'";
403 break;
404 case '0.20.1':
405 // some HW types were moved from the 'Network switch' chapter to the 'Network chassis' chapter
406 // change the type of affected objects to 'Network chassis'
407 $query[] = "UPDATE `Object` SET objtype_id = 1503 WHERE id IN (SELECT object_id FROM `AttributeValue` WHERE attr_id = 2 and uint_value IN (888,889,890,891))";
408
409 // new 'management interface' object type
410 $query[] = "INSERT INTO `Chapter` (`id`,`sticky`,`name`) VALUES (38,'no','management interface type')";
411 $query[] = "INSERT INTO `Attribute` (`id`,`type`,`name`) VALUES (30,'dict','Mgmt type')";
412 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`,`attr_id`,`chapter_id`) VALUES (1787,3,NULL),(1787,14,NULL),(1787,30,38)";
413 $query[] = "UPDATE `Config` SET varvalue = CONCAT(varvalue, ' or {\$typeid_1787}') WHERE varname = 'IPV4OBJ_LISTSRC'";
414
415 $query[] = "INSERT INTO Config VALUES ('8021Q_EXTSYNC_LISTSRC','false','string','yes','no','no','List source: objects with extended 802.1Q sync')";
416
417 // constraints to prevent orphan records
418 $query[] = "DELETE A FROM `Atom` A LEFT JOIN `Molecule` M ON A.molecule_id = M.id LEFT JOIN `Object` O ON A.rack_id = O.id WHERE M.id IS NULL OR O.id IS NULL";
419 $query[] = "DELETE FROM `MountOperation` WHERE old_molecule_id NOT IN (SELECT id FROM `Molecule`) OR new_molecule_id NOT IN (SELECT id FROM `Molecule`)";
420 $query[] = "ALTER TABLE `Atom` ADD CONSTRAINT `Atom-FK-molecule_id` FOREIGN KEY (`molecule_id`) REFERENCES `Molecule` (`id`) ON DELETE CASCADE";
421 $query[] = "ALTER TABLE `Atom` ADD CONSTRAINT `Atom-FK-rack_id` FOREIGN KEY (`rack_id`) REFERENCES `Object` (`id`) ON DELETE CASCADE";
422 $query[] = "ALTER TABLE `MountOperation` ADD CONSTRAINT `MountOperation-FK-old_molecule_id` FOREIGN KEY (`old_molecule_id`) REFERENCES `Molecule` (`id`) ON DELETE CASCADE";
423 $query[] = "ALTER TABLE `MountOperation` ADD CONSTRAINT `MountOperation-FK-new_molecule_id` FOREIGN KEY (`new_molecule_id`) REFERENCES `Molecule` (`id`) ON DELETE CASCADE";
424 # multiple Cacti servers
425 $query[] = "
426 CREATE TABLE `CactiServer` (
427 `id` int(10) unsigned NOT NULL auto_increment,
428 `base_url` char(255) DEFAULT NULL,
429 `username` char(64) DEFAULT NULL,
430 `password` char(64) DEFAULT NULL,
431 PRIMARY KEY (`id`)
432 ) ENGINE=InnoDB
433 ";
434 $query[] = "ALTER TABLE CactiGraph ADD COLUMN server_id int(10) unsigned NOT NULL AFTER object_id";
435
436 $result = $dbxlink->query ('SELECT COUNT(*) AS cnt FROM CactiGraph');
437 $row = $result->fetch (PDO::FETCH_ASSOC);
438 unset ($result);
439
440 $result = $dbxlink->query ("SELECT varvalue FROM Config WHERE varname = 'CACTI_URL'");
441 $cacti_url_row = $result->fetch (PDO::FETCH_ASSOC);
442 unset ($result);
443
444 if ($row['cnt'] != 0 || is_array ($cacti_url_row) && $cacti_url_row['varvalue'] != '')
445 {
446 $query[] = "INSERT INTO CactiServer (id) VALUES (1)";
447 $query[] = "UPDATE CactiServer SET base_url = (SELECT varvalue FROM Config WHERE varname = 'CACTI_URL') WHERE id = 1";
448 $query[] = "UPDATE CactiServer SET username = (SELECT varvalue FROM Config WHERE varname = 'CACTI_USERNAME') WHERE id = 1";
449 $query[] = "UPDATE CactiServer SET password = (SELECT varvalue FROM Config WHERE varname = 'CACTI_USERPASS') WHERE id = 1";
450 $query[] = "UPDATE CactiGraph SET server_id = 1";
451 }
452 $query[] = "ALTER TABLE CactiGraph DROP PRIMARY KEY";
453 $query[] = "ALTER TABLE CactiGraph ADD PRIMARY KEY (server_id, graph_id)";
454 $query[] = "ALTER TABLE CactiGraph ADD KEY (graph_id)";
455 $query[] = "ALTER TABLE CactiGraph ADD CONSTRAINT `CactiGraph-FK-server_id` FOREIGN KEY (server_id) REFERENCES CactiServer (id)";
456 $query[] = "DELETE FROM Config WHERE varname IN('CACTI_URL', 'CACTI_USERNAME', 'CACTI_USERPASS')";
457 $query[] = "UPDATE Config SET varvalue = '0.20.1' WHERE varname = 'DB_VERSION'";
458 break;
459 case '0.20.2':
460 $query[] = "ALTER TABLE TagStorage ADD COLUMN tag_is_assignable ENUM('yes', 'no') NOT NULL default 'yes' AFTER tag_id";
461 $query[] = "ALTER TABLE TagStorage ADD KEY `tag_id-tag_is_assignable` (tag_id, tag_is_assignable)";
462 $query[] = "ALTER TABLE TagTree ADD COLUMN is_assignable ENUM('yes', 'no') NOT NULL default 'yes' AFTER parent_id";
463 $query[] = "ALTER TABLE TagTree ADD KEY `id-is_assignable` (id, is_assignable)";
464 $query[] = "ALTER TABLE TagStorage DROP FOREIGN KEY `TagStorage-FK-tag_id`";
465 $query[] = "ALTER TABLE TagStorage ADD CONSTRAINT `TagStorage-FK-TagTree` FOREIGN KEY (tag_id, tag_is_assignable) REFERENCES TagTree (id, is_assignable)";
466 $query[] = "UPDATE UserAccount SET user_realname = NULL WHERE user_realname = ''";
467 $query[] = "UPDATE Object SET comment = NULL WHERE comment = ''";
468 $query[] = "
469 CREATE TABLE `MuninServer` (
470 `id` int(10) unsigned NOT NULL auto_increment,
471 `base_url` char(255) DEFAULT NULL,
472 PRIMARY KEY (`id`)
473 ) ENGINE=InnoDB
474 ";
475 $query[] = "
476 CREATE TABLE `MuninGraph` (
477 `object_id` int(10) unsigned NOT NULL,
478 `server_id` int(10) unsigned NOT NULL,
479 `graph` char(255) NOT NULL,
480 `caption` char(255) DEFAULT NULL,
481 PRIMARY KEY (`object_id`,`server_id`,`graph`),
482 KEY `server_id` (`server_id`),
483 KEY `graph` (`graph`),
484 CONSTRAINT `MuninGraph-FK-server_id` FOREIGN KEY (`server_id`) REFERENCES `MuninServer` (`id`),
485 CONSTRAINT `MuninGraph-FK-object_id` FOREIGN KEY (`object_id`) REFERENCES `Object` (`id`) ON DELETE CASCADE
486 ) ENGINE=InnoDB
487 ";
488 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('MUNIN_LISTSRC','false','string','yes','no','no','List of object with Munin graphs')";
489 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_MULTILINK_LISTSRC','false','string','yes','no','no','List source: IPv4/IPv6 networks allowing multiple VLANs from same domain')";
490 $query[] = "ALTER TABLE VLANIPv4 ADD UNIQUE `network-domain-vlan` (ipv4net_id, domain_id, vlan_id)";
491 $query[] = "ALTER TABLE VLANIPv4 DROP KEY `network-domain`";
492 $query[] = "ALTER TABLE VLANIPv6 ADD UNIQUE `network-domain-vlan` (ipv6net_id, domain_id, vlan_id)";
493 $query[] = "ALTER TABLE VLANIPv6 DROP KEY `network-domain`";
494 $query[] = "UPDATE Config SET varvalue = '0.20.2' WHERE varname = 'DB_VERSION'";
495 break;
496 case '0.20.3':
497 $query[] = "UPDATE Config SET varvalue = '0.20.3' WHERE varname = 'DB_VERSION'";
498 break;
499 case '0.20.4':
500 $query[] = "ALTER TABLE `FileLink` MODIFY COLUMN `entity_type` ENUM('ipv4net','ipv4rspool','ipv4vs','ipv6net','location','object','rack','row','user') NOT NULL DEFAULT 'object'";
501 $query[] = "ALTER TABLE `RackSpace` MODIFY COLUMN `state` ENUM('A','U','T') NOT NULL default 'A'";
502 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('REVERSED_RACKS_LISTSRC', 'false', 'string', 'yes', 'no', 'no', 'List of racks with reversed (top to bottom) units order')";
503 $query[] = "UPDATE `Config` SET varvalue = CONCAT(varvalue, ' or {\$typeid_965}') WHERE varname = 'IPV4OBJ_LISTSRC'";
504 $query[] = "UPDATE AttributeValue INNER JOIN AttributeMap USING (attr_id) SET AttributeValue.uint_value = 1572 WHERE chapter_id = 12 AND uint_value = 162";
505 $query[] = "UPDATE AttributeValue INNER JOIN AttributeMap USING (attr_id) SET AttributeValue.uint_value = 1710 WHERE chapter_id = 12 AND uint_value = 163";
506 $query[] = "UPDATE Config SET varvalue = '%Y-%m-%d', description='PHP strftime() format to use for date output' WHERE varname = 'DATETIME_FORMAT'";
507 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('NEAREST_RACKS_CHECKBOX', 'yes', 'string', 'yes', 'no', 'yes', 'Enable nearest racks in port list filter by default')";
508 $query[] = "UPDATE Config SET varvalue = '0.20.4' WHERE varname = 'DB_VERSION'";
509 break;
510 case '0.20.5':
511 $query[] = "
512 CREATE OR REPLACE VIEW `Rack` AS SELECT O.id, O.name AS name, O.asset_no, O.has_problems, O.comment,
513 AV_H.uint_value AS height,
514 AV_S.uint_value AS sort_order,
515 RT.thumb_data,
516 R.id AS row_id,
517 R.name AS row_name,
518 L.id AS location_id,
519 L.name AS location_name
520 FROM `Object` O
521 LEFT JOIN `AttributeValue` AV_H ON O.id = AV_H.object_id AND AV_H.attr_id = 27
522 LEFT JOIN `AttributeValue` AV_S ON O.id = AV_S.object_id AND AV_S.attr_id = 29
523 LEFT JOIN `RackThumbnail` RT ON O.id = RT.rack_id
524 LEFT JOIN `EntityLink` RL ON O.id = RL.child_entity_id AND RL.parent_entity_type = 'row' AND RL.child_entity_type = 'rack'
525 INNER JOIN `Object` R ON R.id = RL.parent_entity_id
526 LEFT JOIN `EntityLink` LL ON R.id = LL.child_entity_id AND LL.parent_entity_type = 'location' AND LL.child_entity_type = 'row'
527 LEFT JOIN `Object` L ON L.id = LL.parent_entity_id
528 WHERE O.objtype_id = 1560
529 ";
530
531 // prevent some AttributeMap entries from being deleted
532 $query[] = "ALTER TABLE AttributeMap ADD COLUMN sticky enum('yes','no') default 'no'";
533 $query[] = "UPDATE AttributeMap SET sticky = 'yes' WHERE objtype_id = 4 AND attr_id IN (26,28)"; // Server -> Hypervisor, Slot number
534 $query[] = "UPDATE AttributeMap SET sticky = 'yes' WHERE objtype_id = 8 AND attr_id IN (1,2,4,28)"; // Network switch -> OEM S/N 1, HW type, SW type, Slot number
535 $query[] = "UPDATE AttributeMap SET sticky = 'yes' WHERE objtype_id = 798 AND attr_id = 28"; // Network security -> Slot number
536 $query[] = "UPDATE AttributeMap SET sticky = 'yes' WHERE objtype_id = 1055 AND attr_id = 28"; // FC switch -> Slot number
537 $query[] = "UPDATE AttributeMap SET sticky = 'yes' WHERE objtype_id = 1560 AND attr_id IN (27,29)"; // Rack -> Height, Sort order
538 $query[] = "UPDATE AttributeMap SET sticky = 'yes' WHERE objtype_id = 1787 AND attr_id = 30"; // Management interface -> Mgmt type
539
540 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('RDP_OBJS_LISTSRC','false','string','yes','no','yes','Rackcode filter for RDP-managed objects')";
541
542 // SLB v2 tables
543 $query[] = "
544 CREATE TABLE `VS` (
545 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
546 `name` char(255) DEFAULT NULL,
547 `vsconfig` text,
548 `rsconfig` text,
549 PRIMARY KEY (`id`)
550 ) ENGINE=InnoDB
551 ";
552 $query[] = "
553 CREATE TABLE `VSIPs` (
554 `vs_id` int(10) unsigned NOT NULL,
555 `vip` varbinary(16) NOT NULL,
556 `vsconfig` text,
557 `rsconfig` text,
558 PRIMARY KEY (`vs_id`,`vip`),
559 KEY `vip` (`vip`),
560 CONSTRAINT `VSIPs-vs_id` FOREIGN KEY (`vs_id`) REFERENCES `VS` (`id`) ON DELETE CASCADE
561 ) ENGINE=InnoDB
562 ";
563 $query[] = "
564 CREATE TABLE `VSPorts` (
565 `vs_id` int(10) unsigned NOT NULL,
566 `proto` enum('TCP','UDP','MARK') NOT NULL,
567 `vport` int(10) unsigned NOT NULL,
568 `vsconfig` text,
569 `rsconfig` text,
570 PRIMARY KEY (`vs_id`,`proto`,`vport`),
571 KEY `proto-vport` (`proto`,`vport`),
572 CONSTRAINT `VS-vs_id` FOREIGN KEY (`vs_id`) REFERENCES `VS` (`id`) ON DELETE CASCADE
573 ) ENGINE=InnoDB
574 ";
575 $query[] = "
576 CREATE TABLE `VSEnabledIPs` (
577 `object_id` int(10) unsigned NOT NULL,
578 `vs_id` int(10) unsigned NOT NULL,
579 `vip` varbinary(16) NOT NULL,
580 `rspool_id` int(10) unsigned NOT NULL,
581 `prio` varchar(255) DEFAULT NULL,
582 `vsconfig` text,
583 `rsconfig` text,
584 PRIMARY KEY (`object_id`,`vs_id`,`vip`,`rspool_id`),
585 KEY `vip` (`vip`),
586 KEY `VSEnabledIPs-FK-vs_id-vip` (`vs_id`,`vip`),
587 KEY `VSEnabledIPs-FK-rspool_id` (`rspool_id`),
588 CONSTRAINT `VSEnabledIPs-FK-rspool_id` FOREIGN KEY (`rspool_id`) REFERENCES `IPv4RSPool` (`id`) ON DELETE CASCADE,
589 CONSTRAINT `VSEnabledIPs-FK-vs_id-vip` FOREIGN KEY (`vs_id`, `vip`) REFERENCES `VSIPs` (`vs_id`, `vip`) ON DELETE CASCADE
590 ) ENGINE=InnoDB
591 ";
592 $query[] = "
593 CREATE TABLE `VSEnabledPorts` (
594 `object_id` int(10) unsigned NOT NULL,
595 `vs_id` int(10) unsigned NOT NULL,
596 `proto` enum('TCP','UDP','MARK') NOT NULL,
597 `vport` int(10) unsigned NOT NULL,
598 `rspool_id` int(10) unsigned NOT NULL,
599 `vsconfig` text,
600 `rsconfig` text,
601 PRIMARY KEY (`object_id`,`vs_id`,`proto`,`vport`,`rspool_id`),
602 KEY `VSEnabledPorts-FK-vs_id-proto-vport` (`vs_id`,`proto`,`vport`),
603 KEY `VSEnabledPorts-FK-rspool_id` (`rspool_id`),
604 CONSTRAINT `VSEnabledPorts-FK-object_id` FOREIGN KEY (`object_id`) REFERENCES `Object` (`id`) ON DELETE CASCADE,
605 CONSTRAINT `VSEnabledPorts-FK-rspool_id` FOREIGN KEY (`rspool_id`) REFERENCES `IPv4RSPool` (`id`) ON DELETE CASCADE,
606 CONSTRAINT `VSEnabledPorts-FK-vs_id-proto-vport` FOREIGN KEY (`vs_id`, `proto`, `vport`) REFERENCES `VSPorts` (`vs_id`, `proto`, `vport`) ON DELETE CASCADE
607 ) ENGINE=InnoDB
608 ";
609 $query[] = "ALTER TABLE `EntityLink` MODIFY COLUMN `parent_entity_type` ENUM('ipv4net','ipv4rspool','ipv4vs','ipv6net','location','object','rack','row','user') NOT NULL";
610 $query[] = "ALTER TABLE `FileLink` MODIFY COLUMN `entity_type` ENUM('ipv4net','ipv4rspool','ipv4vs','ipv6net','location','object','rack','row','user') NOT NULL DEFAULT 'object'";
611 $query[] = "ALTER TABLE `TagStorage` MODIFY COLUMN `entity_realm` ENUM('file','ipv4net','ipv4rspool','ipv4vs','ipvs','ipv6net','location','object','rack','user','vst') NOT NULL DEFAULT 'object'";
612 $query[] = "ALTER TABLE `UserConfig` DROP FOREIGN KEY `UserConfig-FK-user`";
613 $query[] = "UPDATE Config SET varvalue = '0.20.5' WHERE varname = 'DB_VERSION'";
614 break;
615 case '0.20.6':
616 // one HW type was moved from the 'Network switch' chapter to the 'Network chassis' chapter
617 // change the type of affected objects to 'Network chassis'
618 $query[] = "UPDATE `Object` SET objtype_id = 1503 WHERE id IN (SELECT object_id FROM `AttributeValue` WHERE attr_id = 2 and uint_value = 935)";
619
620 // convert values of old 'TELNET_OBJS_LISTSRC' 'SSH_OBJS_LISTSRC', 'RDP_OBJS_LISTSRC' variables into 'MGMT_PROTOS'
621 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('MGMT_PROTOS','ssh: {\$typeid_4}; telnet: {\$typeid_8}','string','yes','no','yes','Mapping of management protocol to devices')";
622 if ('' !== $mgmt_converted_var = convertMgmtConfigVars())
623 $query[] = "UPDATE `Config` SET varvalue = '" . mysql_escape_string ($mgmt_converted_var) . "' WHERE varname = 'MGMT_PROTOS'"; // TODO: call of deprecated function
624 $query[] = "DELETE `Config`,`UserConfig` FROM `Config` LEFT JOIN `UserConfig` USING (`varname`) WHERE `Config`.`varname` IN ('TELNET_OBJS_LISTSRC', 'SSH_OBJS_LISTSRC', 'RDP_OBJS_LISTSRC')";
625
626 $query[] = "ALTER TABLE `VSEnabledIPs` ADD CONSTRAINT `VSEnabledIPs-FK-object_id` FOREIGN KEY (`object_id`) REFERENCES `Object` (`id`) ON DELETE CASCADE";
627
628 $query[] = "DELETE FROM Config WHERE varname = 'PORTS_PER_ROW'";
629 $query[] = "UPDATE Config SET varvalue = '0.20.6' WHERE varname = 'DB_VERSION'";
630 break;
631 case '0.20.7':
632 if (! isInnoDBSupported ())
633 {
634 showUpgradeError ('Cannot upgrade because triggers are not supported by your MySQL server.', __FUNCTION__);
635 die;
636 }
637
638 // for the UNIQUE key to work, portb needs to be > porta
639 $result = $dbxlink->query ('SELECT porta, portb FROM `Link` WHERE porta > portb');
640 $links = $result->fetchAll (PDO::FETCH_ASSOC);
641 unset ($result);
642 foreach ($links as $link)
643 $query[] = "UPDATE `Link` SET `porta`=${link['portb']}, `portb`=${link['porta']} WHERE `porta`=${link['porta']} AND `portb`=${link['portb']}";
644
645 // add triggers
646 $query[] = "
647 CREATE TRIGGER `EntityLink-before-insert` BEFORE INSERT ON `EntityLink` FOR EACH ROW
648 EntityLinkTrigger:BEGIN
649 DECLARE parent_objtype, child_objtype, count INTEGER;
650
651 # forbid linking an entity to itself
652 IF NEW.parent_entity_type = NEW.child_entity_type AND NEW.parent_entity_id = NEW.child_entity_id THEN
653 SET NEW.parent_entity_id = NULL;
654 LEAVE EntityLinkTrigger;
655 END IF;
656
657 # in some scenarios, only one parent is allowed
658 CASE CONCAT(NEW.parent_entity_type, '.', NEW.child_entity_type)
659 WHEN 'location.location' THEN
660 SELECT COUNT(*) INTO count FROM EntityLink WHERE parent_entity_type = 'location' AND child_entity_type = 'location' AND child_entity_id = NEW.child_entity_id;
661 WHEN 'location.row' THEN
662 SELECT COUNT(*) INTO count FROM EntityLink WHERE parent_entity_type = 'location' AND child_entity_type = 'row' AND child_entity_id = NEW.child_entity_id;
663 WHEN 'row.rack' THEN
664 SELECT COUNT(*) INTO count FROM EntityLink WHERE parent_entity_type = 'row' AND child_entity_type = 'rack' AND child_entity_id = NEW.child_entity_id;
665 ELSE
666 # some other scenario, assume it is valid
667 SET count = 0;
668 END CASE;
669 IF count > 0 THEN
670 SET NEW.parent_entity_id = NULL;
671 LEAVE EntityLinkTrigger;
672 END IF;
673
674 IF NEW.parent_entity_type = 'object' AND NEW.child_entity_type = 'object' THEN
675 # lock objects to prevent concurrent link establishment
676 SELECT objtype_id INTO parent_objtype FROM Object WHERE id = NEW.parent_entity_id FOR UPDATE;
677 SELECT objtype_id INTO child_objtype FROM Object WHERE id = NEW.child_entity_id FOR UPDATE;
678
679 # only permit the link if object types are compatibile
680 SELECT COUNT(*) INTO count FROM ObjectParentCompat WHERE parent_objtype_id = parent_objtype AND child_objtype_id = child_objtype;
681 IF count = 0 THEN
682 SET NEW.parent_entity_id = NULL;
683 END IF;
684 END IF;
685 END;
686 ";
687 $query[] = "
688 CREATE TRIGGER `EntityLink-before-update` BEFORE UPDATE ON `EntityLink` FOR EACH ROW
689 EntityLinkTrigger:BEGIN
690 DECLARE parent_objtype, child_objtype, count INTEGER;
691
692 # forbid linking an entity to itself
693 IF NEW.parent_entity_type = NEW.child_entity_type AND NEW.parent_entity_id = NEW.child_entity_id THEN
694 SET NEW.parent_entity_id = NULL;
695 LEAVE EntityLinkTrigger;
696 END IF;
697
698 # in some scenarios, only one parent is allowed
699 CASE CONCAT(NEW.parent_entity_type, '.', NEW.child_entity_type)
700 WHEN 'location.location' THEN
701 SELECT COUNT(*) INTO count FROM EntityLink WHERE parent_entity_type = 'location' AND child_entity_type = 'location' AND child_entity_id = NEW.child_entity_id AND id != NEW.id;
702 WHEN 'location.row' THEN
703 SELECT COUNT(*) INTO count FROM EntityLink WHERE parent_entity_type = 'location' AND child_entity_type = 'row' AND child_entity_id = NEW.child_entity_id AND id != NEW.id;
704 WHEN 'row.rack' THEN
705 SELECT COUNT(*) INTO count FROM EntityLink WHERE parent_entity_type = 'row' AND child_entity_type = 'rack' AND child_entity_id = NEW.child_entity_id AND id != NEW.id;
706 ELSE
707 # some other scenario, assume it is valid
708 SET count = 0;
709 END CASE;
710 IF count > 0 THEN
711 SET NEW.parent_entity_id = NULL;
712 LEAVE EntityLinkTrigger;
713 END IF;
714
715 IF NEW.parent_entity_type = 'object' AND NEW.child_entity_type = 'object' THEN
716 # lock objects to prevent concurrent link establishment
717 SELECT objtype_id INTO parent_objtype FROM Object WHERE id = NEW.parent_entity_id FOR UPDATE;
718 SELECT objtype_id INTO child_objtype FROM Object WHERE id = NEW.child_entity_id FOR UPDATE;
719
720 # only permit the link if object types are compatibile
721 SELECT COUNT(*) INTO count FROM ObjectParentCompat WHERE parent_objtype_id = parent_objtype AND child_objtype_id = child_objtype;
722 IF count = 0 THEN
723 SET NEW.parent_entity_id = NULL;
724 END IF;
725 END IF;
726 END;
727 ";
728 $link_trigger_body = <<<ENDOFTRIGGER
729 LinkTrigger:BEGIN
730 DECLARE tmp, porta_type, portb_type, count INTEGER;
731
732 IF NEW.porta = NEW.portb THEN
733 # forbid connecting a port to itself
734 SET NEW.porta = NULL;
735 LEAVE LinkTrigger;
736 ELSEIF NEW.porta > NEW.portb THEN
737 # force porta < portb
738 SET tmp = NEW.porta;
739 SET NEW.porta = NEW.portb;
740 SET NEW.portb = tmp;
741 END IF;
742
743 # lock ports to prevent concurrent link establishment
744 SELECT type INTO porta_type FROM Port WHERE id = NEW.porta FOR UPDATE;
745 SELECT type INTO portb_type FROM Port WHERE id = NEW.portb FOR UPDATE;
746
747 # only permit the link if ports are compatibile
748 SELECT COUNT(*) INTO count FROM PortCompat WHERE (type1 = porta_type AND type2 = portb_type) OR (type1 = portb_type AND type2 = porta_type);
749 IF count = 0 THEN
750 SET NEW.porta = NULL;
751 END IF;
752 END;
753 ENDOFTRIGGER;
754 $query[] = "CREATE TRIGGER `Link-before-insert` BEFORE INSERT ON `Link` FOR EACH ROW $link_trigger_body";
755 $query[] = "CREATE TRIGGER `Link-before-update` BEFORE UPDATE ON `Link` FOR EACH ROW $link_trigger_body";
756
757 // enable IP addressing for all object types unless specifically excluded
758 $query[] = "UPDATE `Config` SET varvalue = 'not ({\$typeid_3} or {\$typeid_9} or {\$typeid_10} or {\$typeid_11})' WHERE varname = 'IPV4OBJ_LISTSRC'";
759
760 $query[] = "ALTER TABLE `EntityLink` MODIFY COLUMN `parent_entity_type` ENUM('location','object','rack','row') NOT NULL";
761 $query[] = "ALTER TABLE `EntityLink` MODIFY COLUMN `child_entity_type` ENUM('location','object','rack','row') NOT NULL";
762
763 $query[] = "UPDATE Config SET description = 'List source: objects for that asset tag should be set' WHERE varname = 'ASSETWARN_LISTSRC'";
764 $query[] = "UPDATE Config SET description = 'List source: objects for that common name should be set' WHERE varname = 'NAMEWARN_LISTSRC'";
765 $query[] = "ALTER TABLE `IPv4NAT` MODIFY COLUMN `proto` ENUM('TCP','UDP','ALL')";
766
767 // add new 'point2point' alloc type
768 $query[] = "ALTER TABLE `IPv4Allocation` MODIFY `type` enum('regular','shared','virtual','router','point2point') NOT NULL DEFAULT 'regular'";
769 $query[] = "ALTER TABLE `IPv6Allocation` MODIFY `type` enum('regular','shared','virtual','router','point2point') NOT NULL DEFAULT 'regular'";
770
771 // update to use utf8_unicode_ci collation
772 // http://bugs.racktables.org/view.php?id=837
773 $query[] = "ALTER DATABASE DEFAULT CHARACTER SET UTF8 COLLATE utf8_unicode_ci";
774
775 $query[] = "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0";
776 $query[] = "ALTER TABLE `Atom` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
777 $query[] = "ALTER TABLE `Attribute` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
778 $query[] = "ALTER TABLE `AttributeMap` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
779 $query[] = "ALTER TABLE `AttributeValue` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
780 $query[] = "ALTER TABLE `CachedPAV` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
781 $query[] = "ALTER TABLE `CachedPNV` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
782 $query[] = "ALTER TABLE `CachedPVM` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
783 $query[] = "ALTER TABLE `CactiGraph` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
784 $query[] = "ALTER TABLE `CactiServer` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
785 $query[] = "ALTER TABLE `Chapter` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
786 $query[] = "ALTER TABLE `Config` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
787 $query[] = "ALTER TABLE `Dictionary` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
788 $query[] = "ALTER TABLE `EntityLink` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
789 $query[] = "ALTER TABLE `File` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
790 $query[] = "ALTER TABLE `FileLink` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
791 $query[] = "ALTER TABLE `IPv4Address` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
792 $query[] = "ALTER TABLE `IPv4Allocation` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
793 $query[] = "ALTER TABLE `IPv4LB` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
794 $query[] = "ALTER TABLE `IPv4Log` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
795 $query[] = "ALTER TABLE `IPv6Log` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
796 $query[] = "ALTER TABLE `IPv4NAT` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
797 $query[] = "ALTER TABLE `IPv4Network` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
798 $query[] = "ALTER TABLE `IPv4RS` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
799 $query[] = "ALTER TABLE `IPv4RSPool` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
800 $query[] = "ALTER TABLE `IPv4VS` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
801 $query[] = "ALTER TABLE `IPv6Address` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
802 $query[] = "ALTER TABLE `IPv6Allocation` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
803 $query[] = "ALTER TABLE `IPv6Network` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
804 $query[] = "ALTER TABLE `LDAPCache` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
805 $query[] = "ALTER TABLE `Link` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
806 $query[] = "ALTER TABLE `Molecule` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
807 $query[] = "ALTER TABLE `MountOperation` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
808 $query[] = "ALTER TABLE `MuninGraph` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
809 $query[] = "ALTER TABLE `MuninServer` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
810 $query[] = "ALTER TABLE `ObjectLog` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
811 $query[] = "ALTER TABLE `ObjectParentCompat` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
812 $query[] = "ALTER TABLE `Port` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
813 $query[] = "ALTER TABLE `PortAllowedVLAN` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
814 $query[] = "ALTER TABLE `PortCompat` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
815 $query[] = "ALTER TABLE `PortInnerInterface` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
816 $query[] = "ALTER TABLE `PortInterfaceCompat` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
817 $query[] = "ALTER TABLE `PortLog` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
818 $query[] = "ALTER TABLE `PortNativeVLAN` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
819 $query[] = "ALTER TABLE `PortVLANMode` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
820 $query[] = "ALTER TABLE `Object` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
821 $query[] = "ALTER TABLE `ObjectHistory` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
822 $query[] = "ALTER TABLE `RackSpace` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
823 $query[] = "ALTER TABLE `RackThumbnail` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
824 $query[] = "ALTER TABLE `Script` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
825 $query[] = "ALTER TABLE `TagStorage` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
826 $query[] = "ALTER TABLE `TagTree` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
827 $query[] = "ALTER TABLE `UserAccount` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
828 $query[] = "ALTER TABLE `UserConfig` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
829 $query[] = "ALTER TABLE `VLANDescription` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
830 $query[] = "ALTER TABLE `VLANDomain` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
831 $query[] = "ALTER TABLE `VLANIPv4` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
832 $query[] = "ALTER TABLE `VLANIPv6` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
833 $query[] = "ALTER TABLE `VLANSTRule` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
834 $query[] = "ALTER TABLE `VLANSwitch` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
835 $query[] = "ALTER TABLE `VLANSwitchTemplate` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
836 $query[] = "ALTER TABLE `VLANValidID` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
837 $query[] = "ALTER TABLE `VS` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
838 $query[] = "ALTER TABLE `VSIPs` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
839 $query[] = "ALTER TABLE `VSPorts` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
840 $query[] = "ALTER TABLE `VSEnabledIPs` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
841 $query[] = "ALTER TABLE `VSEnabledPorts` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
842 $query[] = "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS";
843
844 $query[] = "UPDATE Config SET varvalue = '0.20.7' WHERE varname = 'DB_VERSION'";
845 break;
846 case '0.20.8':
847 $query[] = "ALTER TABLE `VLANSTRule` CHANGE COLUMN `wrt_vlans` `wrt_vlans` text";
848
849 $query[] = "
850 CREATE TABLE `PortOuterInterface` (
851 `id` int(10) unsigned NOT NULL auto_increment,
852 `oif_name` char(48) NOT NULL,
853 PRIMARY KEY (`id`),
854 UNIQUE KEY `oif_name` (`oif_name`)
855 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
856 ";
857 // if upgrading from certain older releases, inject some data to satisfy foreign keys
858 if (version_compare ($dbver, '0.19.2', '<'))
859 $query[] = "INSERT INTO Dictionary (chapter_id,dict_key,dict_value) VALUES (2,1399,'DC')";
860 if (version_compare ($dbver, '0.19.5', '<'))
861 $query[] = "INSERT INTO Dictionary (chapter_id,dict_key,dict_value) VALUES (2,1469,'virtual port')";
862 if (version_compare ($dbver, '0.19.7', '<'))
863 $query[] = "INSERT INTO Dictionary (chapter_id,dict_key,dict_value) VALUES (2,1588,'empty QSFP+')";
864 if (version_compare ($dbver, '0.19.8', '<'))
865 $query[] = "INSERT INTO Dictionary (chapter_id,dict_key,dict_value) VALUES
866 (2,1424,'1000Base-CX'),
867 (2,1425,'10GBase-ER-DWDM40-61.42 (ITU 20)'),
868 (2,1426,'10GBase-ER-DWDM40-60.61 (ITU 21)'),
869 (2,1427,'10GBase-ER-DWDM40-59.79 (ITU 22)'),
870 (2,1428,'10GBase-ER-DWDM40-58.98 (ITU 23)'),
871 (2,1429,'10GBase-ER-DWDM40-58.17 (ITU 24)'),
872 (2,1430,'10GBase-ER-DWDM40-57.36 (ITU 25)'),
873 (2,1431,'10GBase-ER-DWDM40-56.55 (ITU 26)'),
874 (2,1432,'10GBase-ER-DWDM40-55.75 (ITU 27)'),
875 (2,1433,'10GBase-ER-DWDM40-54.94 (ITU 28)'),
876 (2,1434,'10GBase-ER-DWDM40-54.13 (ITU 29)'),
877 (2,1435,'10GBase-ER-DWDM40-53.33 (ITU 30)'),
878 (2,1436,'10GBase-ER-DWDM40-52.52 (ITU 31)'),
879 (2,1437,'10GBase-ER-DWDM40-51.72 (ITU 32)'),
880 (2,1438,'10GBase-ER-DWDM40-50.92 (ITU 33)'),
881 (2,1439,'10GBase-ER-DWDM40-50.12 (ITU 34)'),
882 (2,1440,'10GBase-ER-DWDM40-49.32 (ITU 35)'),
883 (2,1441,'10GBase-ER-DWDM40-48.51 (ITU 36)'),
884 (2,1442,'10GBase-ER-DWDM40-47.72 (ITU 37)'),
885 (2,1443,'10GBase-ER-DWDM40-46.92 (ITU 38)'),
886 (2,1444,'10GBase-ER-DWDM40-46.12 (ITU 39)'),
887 (2,1445,'10GBase-ER-DWDM40-45.32 (ITU 40)'),
888 (2,1446,'10GBase-ER-DWDM40-44.53 (ITU 41)'),
889 (2,1447,'10GBase-ER-DWDM40-43.73 (ITU 42)'),
890 (2,1448,'10GBase-ER-DWDM40-42.94 (ITU 43)'),
891 (2,1449,'10GBase-ER-DWDM40-42.14 (ITU 44)'),
892 (2,1450,'10GBase-ER-DWDM40-41.35 (ITU 45)'),
893 (2,1451,'10GBase-ER-DWDM40-40.56 (ITU 46)'),
894 (2,1452,'10GBase-ER-DWDM40-39.77 (ITU 47)'),
895 (2,1453,'10GBase-ER-DWDM40-38.98 (ITU 48)'),
896 (2,1454,'10GBase-ER-DWDM40-38.19 (ITU 49)'),
897 (2,1455,'10GBase-ER-DWDM40-37.40 (ITU 50)'),
898 (2,1456,'10GBase-ER-DWDM40-36.61 (ITU 51)'),
899 (2,1457,'10GBase-ER-DWDM40-35.82 (ITU 52)'),
900 (2,1458,'10GBase-ER-DWDM40-35.04 (ITU 53)'),
901 (2,1459,'10GBase-ER-DWDM40-34.25 (ITU 54)'),
902 (2,1460,'10GBase-ER-DWDM40-33.47 (ITU 55)'),
903 (2,1461,'10GBase-ER-DWDM40-32.68 (ITU 56)'),
904 (2,1462,'10GBase-ER-DWDM40-31.90 (ITU 57)'),
905 (2,1463,'10GBase-ER-DWDM40-31.12 (ITU 58)'),
906 (2,1464,'10GBase-ER-DWDM40-30.33 (ITU 59)'),
907 (2,1465,'10GBase-ER-DWDM40-29.55 (ITU 60)'),
908 (2,1466,'10GBase-ER-DWDM40-28.77 (ITU 61)')";
909 if (version_compare ($dbver, '0.19.10', '<'))
910 $query[] = "INSERT INTO Dictionary (chapter_id,dict_key,dict_value) VALUES (2,1603,'1000Base-T (HP c-Class)')";
911 if (version_compare ($dbver, '0.19.11', '<'))
912 $query[] = "INSERT INTO Dictionary (chapter_id,dict_key,dict_value) VALUES (2,1642,'10GBase-T')";
913 if (version_compare ($dbver, '0.19.12', '<'))
914 $query[] = "INSERT INTO Dictionary (chapter_id,dict_key,dict_value) VALUES
915 (2,1661,'40GBase-KR4'),
916 (2,1663,'40GBase-SR4'),
917 (2,1664,'40GBase-LR4'),
918 (2,1668,'empty CFP'),
919 (2,1669,'100GBase-SR10'),
920 (2,1670,'100GBase-LR4'),
921 (2,1671,'100GBase-ER4')";
922 $query[] = "INSERT INTO PortOuterInterface SELECT dict_key, dict_value FROM Dictionary WHERE chapter_id = 2";
923 // Previously listed 10GBase-Kx actually means two standards: 10GBase-KX4
924 // and 10GBase-KR. Make respective changes and make primary key auto
925 // increment start at 2000.
926 $query[] = "UPDATE PortOuterInterface SET oif_name = '10GBase-KX4' WHERE id = 41";
927 $query[] = "INSERT INTO PortOuterInterface (id, oif_name) VALUES (1999, '10GBase-KR')";
928 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1999, 1999)";
929 $query[] = "DELETE FROM Dictionary WHERE chapter_id = 2";
930 $query[] = "DELETE FROM Chapter WHERE id = 2";
931 $query[] = "ALTER TABLE PortInterfaceCompat ADD CONSTRAINT `PortInterfaceCompat-FK-oif_id` FOREIGN KEY (oif_id) REFERENCES PortOuterInterface (id)";
932 $query[] = "ALTER TABLE PortCompat ADD CONSTRAINT `PortCompat-FK-oif_id1` FOREIGN KEY (type1) REFERENCES PortOuterInterface (id)";
933 $query[] = "ALTER TABLE PortCompat ADD CONSTRAINT `PortCompat-FK-oif_id2` FOREIGN KEY (type2) REFERENCES PortOuterInterface (id)";
934 // Add more 40G and 100G standards.
935 $query[] = "INSERT INTO PortOuterInterface (id, oif_name) VALUES
936 (1660,'40GBase-FR'),
937 (1662,'40GBase-ER4'),
938 (1672,'100GBase-SR4'),
939 (1673,'100GBase-KR4'),
940 (1674,'100GBase-KP4')";
941 $query[] = "INSERT INTO PortInterfaceCompat (iif_id, oif_id) VALUES
942 (10,1660),
943 (10,1662),
944 (11,1672),
945 (11,1673),
946 (11,1674)";
947 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES
948 (1660,1660),
949 (1662,1662),
950 (1672,1672),
951 (1673,1673),
952 (1674,1674)";
953 // Refine 1G OIF list: fix spelling and add a new standard.
954 $query[] = "UPDATE PortOuterInterface SET oif_name = '1000Base-LX10' WHERE id = 1205";
955 $query[] = "INSERT INTO PortOuterInterface (id, oif_name) VALUES (42, '1000Base-EX')";
956 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (42, 42)";
957 $query[] = "INSERT INTO PortInterfaceCompat (iif_id, oif_id) VALUES (3, 42), (4,42)";
958 // patch cables
959 $query[] = "
960 CREATE TABLE `PatchCableConnector` (
961 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
962 `origin` enum('default','custom') NOT NULL DEFAULT 'custom',
963 `connector` char(32) NOT NULL,
964 PRIMARY KEY (`id`),
965 UNIQUE KEY `connector_per_origin` (`connector`,`origin`)
966 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
967 $query[] = "
968 CREATE TABLE `PatchCableType` (
969 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
970 `origin` enum('default','custom') NOT NULL DEFAULT 'custom',
971 `pctype` char(64) NOT NULL,
972 PRIMARY KEY (`id`),
973 UNIQUE KEY `pctype_per_origin` (`pctype`,`origin`)
974 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
975 $query[] = "
976 CREATE TABLE `PatchCableConnectorCompat` (
977 `pctype_id` int(10) unsigned NOT NULL,
978 `connector_id` int(10) unsigned NOT NULL,
979 PRIMARY KEY (`pctype_id`,`connector_id`),
980 KEY `connector_id` (`connector_id`),
981 CONSTRAINT `PatchCableConnectorCompat-FK-connector_id` FOREIGN KEY (`connector_id`) REFERENCES `PatchCableConnector` (`id`),
982 CONSTRAINT `PatchCableConnectorCompat-FK-pctype_id` FOREIGN KEY (`pctype_id`) REFERENCES `PatchCableType` (`id`)
983 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
984 $query[] = "
985 CREATE TABLE `PatchCableHeap` (
986 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
987 `pctype_id` int(10) unsigned NOT NULL,
988 `end1_conn_id` int(10) unsigned NOT NULL,
989 `end2_conn_id` int(10) unsigned NOT NULL,
990 `amount` smallint(5) unsigned NOT NULL DEFAULT '0',
991 `length` decimal(5,2) unsigned NOT NULL DEFAULT '1.00',
992 `description` char(255) DEFAULT NULL,
993 PRIMARY KEY (`id`),
994 KEY `compat1` (`pctype_id`,`end1_conn_id`),
995 KEY `compat2` (`pctype_id`,`end2_conn_id`),
996 CONSTRAINT `PatchCableHeap-FK-compat1` FOREIGN KEY (`pctype_id`, `end1_conn_id`) REFERENCES `PatchCableConnectorCompat` (`pctype_id`, `connector_id`),
997 CONSTRAINT `PatchCableHeap-FK-compat2` FOREIGN KEY (`pctype_id`, `end2_conn_id`) REFERENCES `PatchCableConnectorCompat` (`pctype_id`, `connector_id`)
998 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
999 $query[] = "
1000 CREATE TABLE `PatchCableHeapLog` (
1001 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
1002 `heap_id` int(10) unsigned NOT NULL,
1003 `date` datetime NOT NULL,
1004 `user` char(64) NOT NULL,
1005 `message` char(255) NOT NULL,
1006 PRIMARY KEY (`id`),
1007 KEY `heap_id-date` (`heap_id`,`date`),
1008 CONSTRAINT `PatchCableHeapLog-FK-heap_id` FOREIGN KEY (`heap_id`) REFERENCES `PatchCableHeap` (`id`) ON DELETE CASCADE
1009 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
1010 $query[] = "
1011 CREATE TABLE `PatchCableOIFCompat` (
1012 `pctype_id` int(10) unsigned NOT NULL,
1013 `oif_id` int(10) unsigned NOT NULL,
1014 PRIMARY KEY (`pctype_id`,`oif_id`),
1015 KEY `oif_id` (`oif_id`),
1016 CONSTRAINT `PatchCableOIFCompat-FK-oif_id` FOREIGN KEY (`oif_id`) REFERENCES `PortOuterInterface` (`id`),
1017 CONSTRAINT `PatchCableOIFCompat-FK-pctype_id` FOREIGN KEY (`pctype_id`) REFERENCES `PatchCableType` (`id`)
1018 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
1019 $query[] = "INSERT INTO PatchCableConnector (id, origin, connector) VALUES
1020 (1,'default','FC/PC'),(2,'default','FC/APC'),
1021 (3,'default','LC/PC'),(4,'default','LC/APC'),
1022 (5,'default','MPO-12/PC'),(6,'default','MPO-12/APC'),
1023 (7,'default','MPO-24/PC'),(8,'default','MPO-24/APC'),
1024 (9,'default','SC/PC'),(10,'default','SC/APC'),
1025 (11,'default','ST/PC'),(12,'default','ST/APC'),
1026 (13,'default','T568/8P8C/RJ45'),
1027 (14,'default','SFP-1000'),
1028 (15,'default','SFP+'),
1029 (999,'default','CX4/SFF-8470')";
1030 $query[] = "INSERT INTO PatchCableType (id, origin, pctype) VALUES
1031 (1,'default','duplex OM1'),
1032 (2,'default','duplex OM2'),
1033 (3,'default','duplex OM3'),
1034 (4,'default','duplex OM4'),
1035 (5,'default','duplex OS1'),
1036 (6,'default','duplex OS2'),
1037 (7,'default','simplex OM1'),
1038 (8,'default','simplex OM2'),
1039 (9,'default','simplex OM3'),
1040 (10,'default','simplex OM4'),
1041 (11,'default','simplex OS1'),
1042 (12,'default','simplex OS2'),
1043 (13,'default','Cat.5 TP'),
1044 (14,'default','Cat.6 TP'),
1045 (15,'default','Cat.6a TP'),
1046 (16,'default','Cat.7 TP'),
1047 (17,'default','Cat.7a TP'),
1048 (18,'default','12-fiber OM3'),
1049 (19,'default','12-fiber OM4'),
1050 (20,'default','10Gb/s CX4 coax'),
1051 (21,'default','24-fiber OM3'),
1052 (22,'default','24-fiber OM4'),
1053 (23,'default','1Gb/s 50cm shielded'),
1054 (24,'default','10Gb/s 24AWG twinax'),
1055 (25,'default','10Gb/s 26AWG twinax'),
1056 (26,'default','10Gb/s 28AWG twinax'),
1057 (27,'default','10Gb/s 30AWG twinax'),
1058 (999,'default','Cat.3 TP')";
1059 $query[] = "INSERT INTO PatchCableOIFCompat (pctype_id, oif_id) VALUES
1060 (13,18),(14,18),(15,18),(16,18),(17,18),(999,18), -- 10Base-T: Cat.3+ TP
1061 (11,1198),(12,1198),(11,1199),(12,1199), -- 100Base-BX10: 1xSMF
1062 (5,1197),(6,1197), -- 100Base-LX10: 2xSMF
1063 (5,1200),(6,1200), -- 100Base-EX: 2xSMF
1064 (5,1201),(6,1201), -- 100Base-ZX: 2xSMF
1065 (1,1195),(2,1195),(3,1195),(4,1195), -- 100Base-FX: 2xMMF
1066 (1,1196),(2,1196),(3,1196),(4,1196), -- 100Base-SX: 2xMMF
1067 (13,19),(14,19),(15,19),(16,19),(17,19), -- 100Base-TX: Cat.5+ TP
1068 (11,1206),(12,1206),(11,1207),(12,1207), -- 1000Base-BX10: 1xSMF
1069 (5,1204),(6,1204), -- 1000Base-LX: 2xSMF
1070 (5,1205),(6,1205), -- 1000Base-LX10: 2xSMF
1071 (1,1202),(2,1202),(3,1202),(4,1202), -- 1000Base-SX: 2xMMF
1072 (1,1203),(2,1203),(3,1203),(4,1203), -- 1000Base-SX+: 2xMMF
1073 (13,24),(14,24),(15,24),(16,24),(17,24), -- 1000Base-T: Cat.5+ TP
1074 (5,34),(6,34), -- 1000Base-ZX: 2xSMF
1075 (23,1077), -- 1000Base direct attach: shielded
1076 (1,30),(2,30),(3,30),(4,30), -- 10GBase-SR: 2xMMF
1077 (5,36),(6,36), -- 10GBase-LR: 2xSMF
1078 (5,35),(6,35), -- 10GBase-ER: 2xSMF
1079 (5,38),(6,38), -- 10GBase-ZR: 2xSMF
1080 (1,39),(2,39),(3,39),(4,39),(5,39),(6,39), -- 10GBase-LX4: 2xMMF/2xSMF
1081 (1,37),(2,37),(3,37),(4,37), -- 10GBase-LRM: 2xMMF
1082 (14,1642),(15,1642),(16,1642),(17,1642), -- 10GBase-T: Cat.6+ TP
1083 (20,40), -- 10GBase-CX4: coax
1084 (24,1084),(25,1084),(26,1084),(27,1084), -- 10GBase direct attach: twinax
1085 (18,1663),(19,1663), -- 40GBase-SR4: 8xMMF
1086 (5,1664),(6,1664), -- 40GBase-LR4: 2xSMF
1087 (5,1662),(6,1662), -- 40GBase-ER4: 2xSMF
1088 (5,1660),(6,1660), -- 40GBase-FR: 2xSMF
1089 (21,1669),(22,1669), -- 100GBase-SR10: 20xMMF
1090 (18,1672),(19,1672), -- 100GBase-SR4: 8xMMF
1091 (5,1670),(6,1670), -- 100GBase-LR4: 2xSMF
1092 (5,1671),(6,1671) -- 100GBase-ER4: 2xSMF";
1093 $query[] = "INSERT INTO PatchCableConnectorCompat (pctype_id, connector_id) VALUES
1094 (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),(9,1),(10,1),(11,1),(12,1), -- FC/PC
1095 (1,2),(2,2),(3,2),(4,2),(5,2),(6,2),(7,2),(8,2),(9,2),(10,2),(11,2),(12,2), -- FC/APC
1096 (1,3),(2,3),(3,3),(4,3),(5,3),(6,3),(7,3),(8,3),(9,3),(10,3),(11,3),(12,3), -- LC/PC
1097 (1,4),(2,4),(3,4),(4,4),(5,4),(6,4),(7,4),(8,4),(9,4),(10,4),(11,4),(12,4), -- LC/APC
1098 (1,9),(2,9),(3,9),(4,9),(5,9),(6,9),(7,9),(8,9),(9,9),(10,9),(11,9),(12,9), -- SC/PC
1099 (1,10),(2,10),(3,10),(4,10),(5,10),(6,10),(7,10),(8,10),(9,10),(10,10),(11,10),(12,10), -- SC/APC
1100 (1,11),(2,11),(3,11),(4,11),(5,11),(6,11),(7,11),(8,11),(9,11),(10,11),(11,11),(12,11), -- ST/PC
1101 (1,12),(2,12),(3,12),(4,12),(5,12),(6,12),(7,12),(8,12),(9,12),(10,12),(11,12),(12,12), -- ST/APC
1102 (13,13),(14,13),(15,13),(16,13),(17,13),(999,13), -- T568
1103 (18,5),(19,5), -- MPO-12/PC
1104 (18,6),(19,6), -- MPO-12/APC
1105 (20,999), -- CX4
1106 (21,7),(22,7), -- MPO-24/PC
1107 (21,8),(22,8), -- MPO-24/APC
1108 (23,14), -- SFP-1000
1109 (24,15),(25,15),(26,15),(27,15) -- SFP+";
1110 // add rules for Cisco UCS objects
1111 $query[] = "INSERT INTO `ObjectParentCompat` (`parent_objtype_id`, `child_objtype_id`) VALUES (1787,8),(1787,1502)";
1112 $query[] = "UPDATE Config SET varvalue = '0.20.8' WHERE varname = 'DB_VERSION'";
1113 break;
1114 case '0.20.9':
1115 $query[] = "ALTER TABLE CactiGraph ADD KEY (server_id)";
1116 $query[] = "ALTER TABLE CactiGraph DROP PRIMARY KEY";
1117 $query[] = "ALTER TABLE CactiGraph ADD PRIMARY KEY (object_id, server_id, graph_id)";
1118 $query[] = "ALTER TABLE CactiGraph DROP KEY `object_id`";
1119 $query[] = "UPDATE Config SET description = 'List of pages to display in quick links' WHERE varname = 'QUICK_LINK_PAGES'";
1120 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('CACTI_RRA_ID','1','uint','no','no','yes','RRA ID for Cacti graphs displayed in RackTables')";
1121 $query[] = "INSERT INTO `Config` (`varname`,`varvalue`,`vartype`,`emptyok`,`is_hidden`,`is_userdefined`,`description`)
1122 VALUES ('SHOW_OBJECTTYPE', 'no', 'string', 'no', 'no', 'yes', 'Show object type column on depot page.')";
1123
1124 $query[] = "INSERT INTO PortInnerInterface (id, iif_name) VALUES (12, 'CFP2'),(13,'CPAK')";
1125 $query[] = "INSERT INTO PortOuterInterface (id, oif_name) VALUES (1589, 'empty CFP2'),(1590,'empty CPAK')";
1126 $query[] = "INSERT INTO PortInterfaceCompat (iif_id, oif_id) VALUES
1127 (12,1589),(12,1669),(12,1670),(12,1671),(12,1672),(12,1673),(12,1674),
1128 (13,1590),(13,1669),(13,1670),(13,1671),(13,1672),(13,1673),(13,1674)";
1129 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1588,1589),(1588,1590),(1589,1589),(1589,1590),(1590,1590)";
1130 $query[] = "UPDATE Config SET varvalue = CONCAT(varvalue, '; 12=1589; 13=1590') WHERE varname = 'DEFAULT_PORT_OIF_IDS'";
1131 $query[] = extendPortCompatQuery();
1132
1133 $query[] = "UPDATE Config SET varvalue = '0.20.9' WHERE varname = 'DB_VERSION'";
1134 break;
1135 case '0.20.10':
1136 $query[] = "UPDATE Config SET varvalue = '0.20.10' WHERE varname = 'DB_VERSION'";
1137 break;
1138 case '0.20.11':
1139 $query[] = "ALTER TABLE VLANDomain ADD COLUMN `group_id` int(10) UNSIGNED DEFAULT NULL AFTER `id`, " .
1140 "ADD CONSTRAINT `VLANDomain-FK-group_id` FOREIGN KEY (`group_id`) REFERENCES `VLANDomain` (`id`) ON DELETE SET NULL";
1141
1142 // new 100GBase port types
1143 $query[] = "INSERT INTO `PortInnerInterface` (`id`, `iif_name`) VALUES (14,'CXP')";
1144 $query[] = "INSERT INTO `PortOuterInterface` (`id`, `oif_name`) VALUES
1145 (1591,'empty CXP'),
1146 (1675,'100GBase-LR10'),
1147 (1676,'100GBase-ER10'),
1148 (1677,'100GBase-CR4'),
1149 (1678,'100GBase-CR10')";
1150 $query[] = "INSERT INTO `PatchCableOIFCompat` (`pctype_id`, `oif_id`) VALUES
1151 (5,1675),(6,1675), -- 100GBase-LR10: 2xSMF
1152 (5,1676),(6,1676) -- 100GBase-ER10: 2xSMF";
1153 $query[] = "INSERT INTO `PortInterfaceCompat` (`iif_id`, `oif_id`) VALUES
1154 (11,1675),(11,1676),
1155 (12,1675),(12,1676),
1156 (13,1675),(13,1676),
1157 (14,1591),(14,1677),(14,1678)";
1158 $query[] = "INSERT INTO `PortCompat` (`type1`, `type2`) VALUES
1159 (1591,1591),
1160 (1675,1675),
1161 (1676,1676),
1162 (1677,1677),
1163 (1678,1678)";
1164 $query[] = "UPDATE Config SET varvalue = CONCAT(varvalue, '; 14=1591') WHERE varname = 'DEFAULT_PORT_OIF_IDS'";
1165
1166 // ABI_ver = 2, invalidate RackCode cache
1167 $query[] = "DELETE FROM Script WHERE script_name='RackCodeCache'";
1168
1169 $query[] = "INSERT INTO Config (varname, varvalue, is_hidden, is_userdefined, description) VALUES ('IPV4_TREE_SHOW_UNALLOCATED', 'yes', 'no', 'yes', 'Show unallocated networks in IPv4 tree'); ";
1170 $query[] = "UPDATE Config SET varvalue = '0.20.11' WHERE varname = 'DB_VERSION'";
1171
1172 break;
1173 case '0.20.12':
1174 // NO_ZERO_DATE
1175 $query[] = "ALTER TABLE LDAPCache MODIFY COLUMN last_retry timestamp NULL DEFAULT NULL";
1176 $query[] = "DELETE FROM LDAPCache";
1177
1178 $query[] = "INSERT INTO ObjectParentCompat (parent_objtype_id, child_objtype_id) VALUES (1787,4)";
1179
1180 $port_trigger_body = <<<ENDOFTRIGGER
1181 PortTrigger:BEGIN
1182 IF (NEW.`l2address` IS NOT NULL AND (SELECT COUNT(*) FROM `Port` WHERE `l2address` = NEW.`l2address` AND `object_id` != NEW.`object_id`) > 0) THEN
1183 CALL `Port-l2address-already-exists-on-another-object`;
1184 END IF;
1185 END;
1186 ENDOFTRIGGER;
1187 $query[] = "CREATE TRIGGER `Port-before-insert` BEFORE INSERT ON `Port` FOR EACH ROW $port_trigger_body";
1188 $query[] = "CREATE TRIGGER `Port-before-update` BEFORE UPDATE ON `Port` FOR EACH ROW $port_trigger_body";
1189
1190 $query[] = "ALTER TABLE UserConfig DROP FOREIGN KEY `UserConfig-FK-varname`";
1191 $query[] = "ALTER TABLE UserConfig ADD CONSTRAINT `UserConfig-FK-varname` FOREIGN KEY (`varname`) REFERENCES `Config` (`varname`) ON DELETE CASCADE ON UPDATE CASCADE";
1192 $query[] = "UPDATE Config SET varname = 'SYNC_8021Q_LISTSRC' WHERE varname = 'SYNC_802Q_LISTSRC'";
1193
1194 // new iif_type 'QSFP28'
1195 $query[] = "INSERT INTO `PortInnerInterface` (`id`, `iif_name`) VALUES (15,'QSFP28')";
1196 $query[] = "UPDATE Config SET varvalue = CONCAT(varvalue, '; 15=1588') WHERE varname = 'DEFAULT_PORT_OIF_IDS' AND 0 = INSTR(varvalue, '15=')";
1197 // rename 'empty QSFP+' to 'empty QSFP'
1198 $query[] = "UPDATE PortOuterInterface SET oif_name='empty QSFP' WHERE id=1588";
1199
1200 $query[] = "INSERT INTO PortOuterInterface (id, oif_name) VALUES
1201 (1088,'1000Base-BX40-D'),
1202 (1089,'1000Base-BX40-U'),
1203 (1090,'1000Base-BX80-D'),
1204 (1091,'1000Base-BX80-U')";
1205 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES
1206 (1088,1089),
1207 (1089,1088),
1208 (1090,1091),
1209 (1091,1090)";
1210 $query[] = "INSERT INTO PortInterfaceCompat (iif_id, oif_id) VALUES
1211 (4,1088), (4,1089), (4,1090), (4,1091),
1212 (15,1588),(15,1660),(15,1662),(15,1663),(15,1664),(15,1670),(15,1671),(15,1672),(15,1673),(15,1674)";
1213 $query[] = "INSERT INTO PatchCableOIFCompat (pctype_id, oif_id) VALUES
1214 (11,1088), (12,1088), (11,1089), (12,1089),
1215 (11,1090), (12,1090), (11,1091), (12,1091)";
1216
1217
1218 $query[] = "UPDATE Config SET varvalue = '0.20.12' WHERE varname = 'DB_VERSION'";
1219 break;
1220 case '0.20.13':
1221 $query[] = "DROP TRIGGER IF EXISTS `Port-before-insert`";
1222 $query[] = "DROP TRIGGER IF EXISTS `Port-before-update`";
1223 $query[] = "UPDATE Config SET varvalue = '0.20.13' WHERE varname = 'DB_VERSION'";
1224 break;
1225 case 'dictionary':
1226 $query = reloadDictionary();
1227 break;
1228 default:
1229 return NULL;
1230 }
1231 return $query;
1232 }
1233
1234 function executeUpgradeBatch ($batchid)
1235 {
1236 global $dbxlink;
1237 $query = getUpgradeBatch($batchid);
1238 if ($query === NULL)
1239 {
1240 showError ("unknown batch '${batchid}'", __FUNCTION__);
1241 die;
1242 }
1243 $failures = array();
1244 echo "<tr><th>Executing batch '${batchid}'</th><td>";
1245 foreach ($query as $q)
1246 {
1247 try
1248 {
1249 $result = $dbxlink->query ($q);
1250 }
1251 catch (PDOException $e)
1252 {
1253 $errorInfo = $dbxlink->errorInfo();
1254 $failures[] = array ($q, $errorInfo[2]);
1255 }
1256 }
1257 if (!count ($failures))
1258 echo "<strong><font color=green>done</font></strong>";
1259 else
1260 {
1261 echo "<strong><font color=red>The following queries failed:</font></strong><br><pre>";
1262 foreach ($failures as $f)
1263 {
1264 list ($q, $i) = $f;
1265 echo "${q} -- ${i}\n";
1266 }
1267 echo "</pre>";
1268 }
1269 echo '</td></tr>';
1270 }
1271
1272 function authenticate_admin ($username, $password)
1273 {
1274 global $dbxlink;
1275 $prepared = $dbxlink->prepare ('SELECT COUNT(*) FROM UserAccount WHERE user_id=1 AND user_name=? AND user_password_hash=?');
1276 if (!$prepared->execute (array ($username, sha1 ($password))))
1277 die ('SQL query failed in ' . __FUNCTION__);
1278 $rows = $prepared->fetchAll (PDO::FETCH_NUM);
1279 return $rows[0][0] == 1;
1280 }
1281
1282 // Database version detector. Should behave corretly on any
1283 // working dataset a user might have.
1284 function getDatabaseVersion ()
1285 {
1286 global $dbxlink;
1287 $prepared = $dbxlink->prepare ('SELECT varvalue FROM Config WHERE varname = "DB_VERSION" and vartype = "string"');
1288 if (! $prepared->execute())
1289 {
1290 $errorInfo = $dbxlink->errorInfo();
1291 die (__FUNCTION__ . ': SQL query failed with error ' . $errorInfo[2]);
1292 }
1293 $rows = $prepared->fetchAll (PDO::FETCH_NUM);
1294 if (count ($rows) != 1 || $rows[0][0] == '')
1295 die (__FUNCTION__ . ': Cannot guess database version. Config table is present, but DB_VERSION is missing or invalid. Giving up.');
1296 $ret = $rows[0][0];
1297 return $ret;
1298 }
1299
1300 function showUpgradeError ($info = '', $location = 'N/A')
1301 {
1302 if (preg_match ('/\.php$/', $location))
1303 $location = basename ($location);
1304 elseif ($location != 'N/A')
1305 $location = $location . '()';
1306 echo "<div class=msg_error>An error has occured in [${location}]. ";
1307 if ($info == '')
1308 echo 'No additional information is available.';
1309 else
1310 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
1311 echo "Go back or try starting from <a href='index.php'>index page</a>.<br></div>\n";
1312 }
1313
1314 // changes the value of config variable. If $old_value_filter is set, value is changed only if current value equals to it.
1315 function replaceConfigVarValue ($varname, $new_value, $old_value_filter = NULL)
1316 {
1317 global $dbxlink;
1318 if (isset ($old_value_filter))
1319 {
1320 $result = $dbxlink->prepare ("SELECT varvalue FROM Config WHERE varname = ?");
1321 $result->execute (array ($varname));
1322 if ($row = $result->fetch (PDO::FETCH_ASSOC))
1323 if ($row['varvalue'] != $old_value_filter)
1324 return;
1325 unset ($result);
1326 }
1327 $result = $dbxlink->prepare ("UPDATE Config set varvalue = ? WHERE varname = ?");
1328 $result->execute (array ($new_value, $varname));
1329 }
1330
1331 function renderUpgraderHTML()
1332 {
1333 global $found_secret_file;
1334 if (! $found_secret_file)
1335 die ('<center>There is no working RackTables instance here, <a href="?module=installer">install</a>?</center>');
1336
1337 try
1338 {
1339 connectDB();
1340 }
1341 catch (RackTablesError $e)
1342 {
1343 die ("Database connection failed:\n\n" . $e->getMessage());
1344 }
1345
1346 if
1347 (
1348 ! isset ($_SERVER['PHP_AUTH_USER']) ||
1349 $_SERVER['PHP_AUTH_USER'] == '' ||
1350 ! isset ($_SERVER['PHP_AUTH_PW']) ||
1351 $_SERVER['PHP_AUTH_PW'] == '' ||
1352 !authenticate_admin ($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
1353 )
1354 {
1355 header ('WWW-Authenticate: Basic realm="RackTables upgrade"');
1356 header ('HTTP/1.0 401 Unauthorized');
1357 ?>
1358 <h1>Trouble logging in?</h1>
1359 You are trying to authenticate for the RackTables upgrade screen. This means that
1360 you must authenticate with the username and password of the main RackTables
1361 administrator. There is only one such account in each installation, its default
1362 username is "admin". RackTables wiki provides more information on this topic.
1363 <?php
1364 die;
1365 }
1366
1367 ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1368 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1369 <head><title>RackTables upgrade script</title>
1370 <style type="text/css">
1371 .tdleft {
1372 text-align: left;
1373 }
1374
1375 .trok {
1376 background-color: #80FF80;
1377 }
1378
1379 .trwarning {
1380 background-color: #FFFF80;
1381 }
1382
1383 .trerror {
1384 background-color: #FF8080;
1385 }
1386 </style>
1387 </head>
1388 <body>
1389 <h1>Platform check status</h1>
1390 <?php
1391
1392 if (!platform_is_ok())
1393 {
1394 echo '<h1>Please resolve the failed (red) item(s) above.</h1>';
1395 die ('</body></html>');
1396 }
1397
1398 echo '<h1>Upgrade status</h1>';
1399 global $dbver;
1400 $dbver = getDatabaseVersion();
1401 echo '<table border=1 cellpadding=5>';
1402 echo "<tr><th>Current status</th><td>Data version: ${dbver}<br>Code version: " . CODE_VERSION . "</td></tr>\n";
1403
1404 $path = getDBUpgradePath ($dbver, CODE_VERSION);
1405 if ($path === NULL)
1406 {
1407 echo "<tr><th>Upgrade path</th><td><font color=red>not found</font></td></tr>\n";
1408 echo "<tr><th>Summary</th><td>Check README for more information. RackTables releases prior to 0.18.0 ";
1409 echo "must be upgraded to 0.18.0 first. RackTables releases prior to 0.20.0 must be upgraded to 0.20.0 first.</td></tr>\n";
1410 }
1411 else
1412 {
1413 if (!count ($path))
1414 echo "<tr><th>Summary</th><td>Come back later.</td></tr>\n";
1415 else
1416 {
1417 echo "<tr><th>Upgrade path</th><td>${dbver} &rarr; " . implode (' &rarr; ', $path) . "</td></tr>\n";
1418 global $relnotes;
1419 foreach ($path as $batchid)
1420 if (isset ($relnotes[$batchid]))
1421 echo "<tr><th>Release notes for ${batchid}</th><td><pre>" . $relnotes[$batchid] . "</pre></td></tr>\n";
1422 if (array_key_exists ('reallyreally', $_REQUEST))
1423 {
1424 foreach ($path as $batchid)
1425 executeUpgradeBatch ($batchid);
1426 executeUpgradeBatch ('dictionary');
1427 echo "<tr><th>Summary</th><td>Upgrade complete, it is Ok to ";
1428 echo "<a href='index.php'>enter</a> the system.</td></tr>\n";
1429 }
1430 else
1431 {
1432 echo '<form method=post action="index.php?module=upgrade"><tr><th>Wait!</th>';
1433 echo '<td><p>RackTables database upgrades sometimes go wrong because of assorted reasons. ';
1434 echo 'It is <strong>highly recommended</strong> to make a database backup before ';
1435 echo 'proceeding any further. <tt>mysqldump</tt> and <tt>PHPMyAdmin</tt> are convenient ';
1436 echo 'tools for doing this.</p>';
1437 echo '<p><input type=checkbox name=reallyreally id=reallyreally><label for=reallyreally>';
1438 echo 'I am ready to bear all risks of this upgrade. I am ready to roll it back in case of ';
1439 echo 'a failure.</label> <input type=submit value="Yes, I am."></p></td></tr></form>';
1440 }
1441 }
1442 }
1443 echo '</table>';
1444 echo '</body></html>';
1445 }
1446
1447 // returns SQL query to make PortCompat symmetric (insert missing reversed-order pairs).
1448 // It should be called each time after the PortCompat table pairs being added during upgrade.
1449 function extendPortCompatQuery()
1450 {
1451 return "INSERT INTO PortCompat SELECT pc1.type2, pc1.type1 FROM PortCompat pc1 LEFT JOIN PortCompat pc2 ON pc1.type1 = pc2.type2 AND pc1.type2 = pc2.type1 WHERE pc2.type1 IS NULL";
1452 }
1453
1454 function convertSLBTablesToBinIPs()
1455 {
1456 global $dbxlink;
1457
1458 $dbxlink->query ("DROP TABLE IF EXISTS `IPv4VS_new`, `IPv4RS_new`, `IPv4VS_old`, `IPv4RS_old`");
1459
1460 $dbxlink->query (<<<END
1461 CREATE TABLE `IPv4VS_new` (
1462 `id` int(10) unsigned NOT NULL auto_increment,
1463 `vip` varbinary(16) NOT NULL,
1464 `vport` smallint(5) unsigned default NULL,
1465 `proto` enum('TCP','UDP','MARK') NOT NULL default 'TCP',
1466 `name` char(255) default NULL,
1467 `vsconfig` text,
1468 `rsconfig` text,
1469 PRIMARY KEY (`id`),
1470 KEY `vip` (`vip`)
1471 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1472 END
1473 );
1474 $result = $dbxlink->query ("SELECT id, vip, vport, proto, name, vsconfig, rsconfig FROM IPv4VS");
1475 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
1476 unset ($result);
1477 foreach ($rows as $row)
1478 {
1479 $row['vip'] = ip4_int2bin ($row['vip']);
1480 usePreparedInsertBlade ('IPv4VS_new', $row);
1481 }
1482
1483 $dbxlink->query (<<<END
1484 CREATE TABLE `IPv4RS_new` (
1485 `id` int(10) unsigned NOT NULL auto_increment,
1486 `inservice` enum('yes','no') NOT NULL default 'no',
1487 `rsip` varbinary(16) NOT NULL,
1488 `rsport` smallint(5) unsigned default NULL,
1489 `rspool_id` int(10) unsigned default NULL,
1490 `rsconfig` text,
1491 `comment` varchar(255) DEFAULT NULL,
1492 PRIMARY KEY (`id`),
1493 KEY `rsip` (`rsip`),
1494 UNIQUE KEY `pool-endpoint` (`rspool_id`,`rsip`,`rsport`),
1495 CONSTRAINT `IPRS-FK` FOREIGN KEY (`rspool_id`) REFERENCES `IPv4RSPool` (`id`) ON DELETE CASCADE
1496 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1497 END
1498 );
1499 $result = $dbxlink->query ("SELECT id, inservice, rsip, rsport, rspool_id, rsconfig FROM IPv4RS");
1500 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
1501 unset ($result);
1502 foreach ($rows as $row)
1503 {
1504 $row['rsip'] = ip4_int2bin ($row['rsip']);
1505 usePreparedInsertBlade ('IPv4RS_new', $row);
1506 }
1507
1508 $dbxlink->query (<<<END
1509 RENAME TABLE
1510 `IPv4VS` TO `IPv4VS_old`,
1511 `IPv4VS_new` TO `IPv4VS`,
1512 `IPv4RS` TO `IPv4RS_old`,
1513 `IPv4RS_new` TO `IPv4RS`
1514 END
1515 );
1516 // re-create foreign key in IPv4LB
1517 $dbxlink->query ("ALTER TABLE `IPv4LB` DROP FOREIGN KEY `IPv4LB-FK-vs_id`");
1518 $dbxlink->query ("ALTER TABLE `IPv4LB` ADD CONSTRAINT `IPv4LB-FK-vs_id` FOREIGN KEY (`vs_id`) REFERENCES `IPv4VS` (`id`)");
1519
1520 $dbxlink->query ("DROP TABLE `IPv4VS_old`, `IPv4RS_old`");
1521
1522 // re-create foreign key in IPv4RS
1523 $dbxlink->query ("ALTER TABLE `IPv4RS` DROP FOREIGN KEY `IPRS-FK`");
1524 $dbxlink->query ("ALTER TABLE `IPv4RS` ADD CONSTRAINT `IPv4RS-FK` FOREIGN KEY (`rspool_id`) REFERENCES `IPv4RSPool` (`id`) ON DELETE CASCADE");
1525 }
1526
1527 // converts the values of old-style config vars TELNET_OBJS_LISTSRC, SSH_OBJS_LISTSRC, RDP_OBJS_LISTSRC
1528 // to the format of MGMT_PROTOS (comma-separated list of "proto: rackcode" pairs)
1529 function convertMgmtConfigVars()
1530 {
1531 global $dbxlink;
1532 $ret = array();
1533 foreach (array ('telnet' => 'TELNET_OBJS_LISTSRC', 'ssh' => 'SSH_OBJS_LISTSRC', 'rdp' => 'RDP_OBJS_LISTSRC') as $proto => $varname)
1534 {
1535 $result = $dbxlink->prepare ("SELECT varvalue FROM Config WHERE varname = ?");
1536 $result->execute (array ($varname));
1537 if ($row = $result->fetch (PDO::FETCH_ASSOC))
1538 if ($row['varvalue'] != 'false' && $row['varvalue'] != '')
1539 $ret[] = "$proto: " . $row['varvalue'];
1540 unset ($result);
1541 }
1542 return implode (',', $ret);
1543 }
1544
1545 ?>