r3779 schema: add UNIQUE to RackRow
[racktables] / upgrade.php
1 <?php
2
3 $relnotes = array
4 (
5 '0.17.0' => "LDAP options have been moved to LDAP_options array. This means, that if you were<br>" .
6 "using LDAP authentication for users in version 0.16.x, it will break right after<br>" .
7 "upgrade to 0.17.0. To get things working again, adjust existing secret.php file<br>" .
8 "according to secret-sample.php file provided with 0.17.0 release.<br><br>" .
9 "Another change is the addition of support for file uploads. Files are stored<br>" .
10 "in the database. There are several settings in php.ini which you may need to modify:<br>" .
11 "<ul><li>file_uploads - needs to be On</li>" .
12 "<li>upload_max_filesize - max size for uploaded files</li>" .
13 "<li>post_max_size - max size of all form data submitted via POST (including files)</li></ul><br>" .
14 "Local user accounts used to have 'enabled' flag, which allowed individual blocking and<br>" .
15 "unblocking of each. This flag was dropped in favor of existing mean of access<br>" .
16 "setup (RackCode). An unconditional denying rule is automatically added into RackCode<br>" .
17 "for such blocked account, so the effective security policy remains the same.<br>",
18 );
19
20 // At the moment we assume, that for any two releases we can
21 // sequentally execute all batches, that separate them, and
22 // nothing will break. If this changes one day, the function
23 // below will have to generate smarter upgrade paths, while
24 // the upper layer will remain the same.
25 // Returning an empty array means that no upgrade is necessary.
26 // Returning NULL indicates an error.
27 function getDBUpgradePath ($v1, $v2)
28 {
29 $versionhistory = array
30 (
31 '0.16.4',
32 '0.16.5',
33 '0.16.6',
34 '0.17.0',
35 '0.17.1',
36 '0.17.2',
37 '0.17.3',
38 '0.17.4',
39 '0.17.5',
40 '0.17.6',
41 '0.17.7',
42 '0.17.8',
43 '0.17.9',
44 '0.17.10',
45 '0.17.11',
46 '0.18.0',
47 '0.18.1',
48 '0.18.2',
49 );
50 if (!in_array ($v1, $versionhistory) or !in_array ($v2, $versionhistory))
51 return NULL;
52 $skip = TRUE;
53 $path = NULL;
54 // Now collect all versions > $v1 and <= $v2
55 foreach ($versionhistory as $v)
56 {
57 if ($skip and $v == $v1)
58 {
59 $skip = FALSE;
60 $path = array();
61 continue;
62 }
63 if ($skip)
64 continue;
65 $path[] = $v;
66 if ($v == $v2)
67 break;
68 }
69 return $path;
70 }
71
72 // Upgrade batches are named exactly as the release where they first appear.
73 // That is simple, but seems sufficient for beginning.
74 function executeUpgradeBatch ($batchid)
75 {
76 $query = array();
77 global $dbxlink;
78 switch ($batchid)
79 {
80 case '0.16.5':
81 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4_TREE_SHOW_USAGE','yes','string','no','no','Show address usage in IPv4 tree')";
82 $query[] = "update Config set varvalue = '0.16.5' where varname = 'DB_VERSION'";
83 break;
84 case '0.16.6':
85 $query[] = "update Config set varvalue = '0.16.6' where varname = 'DB_VERSION'";
86 break;
87 case '0.17.0':
88 // create tables for storing files (requires InnoDB support)
89 if (!isInnoDBSupported ())
90 {
91 showFailure ("Cannot upgrade because InnoDB tables are not supported by your MySQL server. See the README for details.", __FILE__);
92 die;
93 }
94
95 $query[] = "alter table Chapter change chapter_no id int(10) unsigned NOT NULL auto_increment";
96 $query[] = "alter table Chapter change chapter_name name char(128) NOT NULL";
97 $query[] = "alter table Chapter drop key chapter_name";
98 $query[] = "alter table Chapter add UNIQUE KEY name (name)";
99 $query[] = "alter table Attribute change attr_id id int(10) unsigned NOT NULL auto_increment";
100 $query[] = "alter table Attribute change attr_type type enum('string','uint','float','dict') default NULL";
101 $query[] = "alter table Attribute change attr_name name char(64) default NULL";
102 $query[] = "alter table Attribute drop key attr_name";
103 $query[] = "alter table Attribute add UNIQUE KEY name (name)";
104 $query[] = "alter table AttributeMap change chapter_no chapter_id int(10) unsigned NOT NULL";
105 $query[] = "alter table Dictionary change chapter_no chapter_id int(10) unsigned NOT NULL";
106 // Only after the above call it is Ok to use reloadDictionary()
107 $query = array_merge ($query, reloadDictionary ($batchid));
108 // schema changes for file management
109 $query[] = "
110 CREATE TABLE `File` (
111 `id` int(10) unsigned NOT NULL auto_increment,
112 `name` char(255) NOT NULL,
113 `type` char(255) NOT NULL,
114 `size` int(10) unsigned NOT NULL,
115 `ctime` datetime NOT NULL,
116 `mtime` datetime NOT NULL,
117 `atime` datetime NOT NULL,
118 `contents` longblob NOT NULL,
119 `comment` text,
120 PRIMARY KEY (`id`),
121 UNIQUE KEY `name` (`name`)
122 ) ENGINE=InnoDB";
123 $query[] = "
124 CREATE TABLE `FileLink` (
125 `id` int(10) unsigned NOT NULL auto_increment,
126 `file_id` int(10) unsigned NOT NULL,
127 `entity_type` enum('ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object',
128 `entity_id` int(10) NOT NULL,
129 PRIMARY KEY (`id`),
130 UNIQUE KEY `FileLink-unique` (`file_id`,`entity_type`,`entity_id`),
131 KEY `FileLink-file_id` (`file_id`),
132 CONSTRAINT `FileLink-File_fkey` FOREIGN KEY (`file_id`) REFERENCES `File` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
133 ) ENGINE=InnoDB";
134 $query[] = "ALTER TABLE TagStorage MODIFY COLUMN target_realm enum('file','ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object'";
135
136 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (24,'no','network security models')";
137 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (25,'no','wireless models')";
138 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,1,0)";
139 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,2,24)";
140 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,3,0)";
141 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,5,0)";
142 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,14,0)";
143 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,16,0)";
144 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,17,0)";
145 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,18,0)";
146 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,20,0)";
147 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,21,0)";
148 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,22,0)";
149 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,24,0)";
150 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,1,0)";
151 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,3,0)";
152 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,2,25)";
153 $query[] = 'alter table IPBonds rename to IPv4Allocation';
154 $query[] = 'alter table PortForwarding rename to IPv4NAT';
155 $query[] = 'alter table IPRanges rename to IPv4Network';
156 $query[] = 'alter table IPAddress rename to IPv4Address';
157 $query[] = 'alter table IPLoadBalancer rename to IPv4LB';
158 $query[] = 'alter table IPRSPool rename to IPv4RSPool';
159 $query[] = 'alter table IPRealServer rename to IPv4RS';
160 $query[] = 'alter table IPVirtualService rename to IPv4VS';
161 $query[] = "alter table TagStorage change column target_realm entity_realm enum('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') NOT NULL default 'object'";
162 $query[] = 'alter table TagStorage change column target_id entity_id int(10) unsigned NOT NULL';
163 $query[] = 'alter table TagStorage drop key entity_tag';
164 $query[] = 'alter table TagStorage drop key target_id';
165 $query[] = 'alter table TagStorage add UNIQUE KEY `entity_tag` (`entity_realm`,`entity_id`,`tag_id`)';
166 $query[] = 'alter table TagStorage add KEY `entity_id` (`entity_id`)';
167 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_MAXCHARS','10240','uint','yes','no','Max chars for text file preview')";
168 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_ROWS','25','uint','yes','no','Rows for text file preview')";
169 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_COLS','80','uint','yes','no','Columns for text file preview')";
170 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_IMAGE_MAXPXS','320','uint','yes','no','Max pixels per axis for image file preview')";
171 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('VENDOR_SIEVE','','string','yes','no','Vendor sieve configuration')";
172 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4LB_LISTSRC','{\$typeid_4}','string','yes','no','List source: IPv4 load balancers')";
173 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4OBJ_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8} or {\$typeid_12} or {\$typeid_445} or {\$typeid_447}','string','yes','no','List source: IPv4-enabled objects')";
174 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4NAT_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: IPv4 NAT performers')";
175 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('ASSETWARN_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: object, for which asset tag should be set')";
176 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('NAMEWARN_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: object, for which common name should be set')";
177 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('RACKS_PER_ROW','12','unit','yes','no','Racks per row')";
178 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_PREDICATE_SIEVE','','string','yes','no','Predicate sieve regex(7)')";
179 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_DEFAULT_ANDOR','or','string','no','no','Default list filter boolean operation (or/and)')";
180 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_ANDOR','yes','string','no','no','Suggest and/or selector in list filter')";
181 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_TAGS','yes','string','no','no','Suggest tags in list filter')";
182 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_PREDICATES','yes','string','no','no','Suggest predicates in list filter')";
183 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_EXTRA','no','string','no','no','Suggest extra expression in list filter')";
184 $query[] = "delete from Config where varname = 'USER_AUTH_SRC'";
185 $query[] = "delete from Config where varname = 'COOKIE_TTL'";
186 $query[] = "delete from Config where varname = 'rtwidth_0'";
187 $query[] = "delete from Config where varname = 'rtwidth_1'";
188 $query[] = "delete from Config where varname = 'rtwidth_2'";
189 $query[] = "delete from Config where varname = 'NAMEFUL_OBJTYPES'";
190 $query[] = "delete from Config where varname = 'REQUIRE_ASSET_TAG_FOR'";
191 $query[] = "delete from Config where varname = 'IPV4_PERFORMERS'";
192 $query[] = "delete from Config where varname = 'NATV4_PERFORMERS'";
193 $query[] = "alter table TagTree add column valid_realm set('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') not null default 'file,ipv4net,ipv4vs,ipv4rspool,object,rack,user' after parent_id";
194 $result = $dbxlink->query ("select user_id, user_name, user_realname from UserAccount where user_enabled = 'no'");
195 while ($row = $result->fetch (PDO::FETCH_ASSOC))
196 $query[] = "update Script set script_text = concat('deny {\$userid_${row['user_id']}} # ${row['user_name']} (${row['user_realname']})\n', script_text) where script_name = 'RackCode'";
197 $query[] = "update Script set script_text = NULL where script_name = 'RackCodeCache'";
198 unset ($result);
199 $query[] = "alter table UserAccount drop column user_enabled";
200
201 $query[] = "CREATE TABLE RackRow ( id int(10) unsigned NOT NULL auto_increment, name char(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM";
202
203 $result = $dbxlink->query ("select dict_key, dict_value from Dictionary where chapter_no = 3");
204 while($row = $result->fetch(PDO::FETCH_NUM))
205 $query[] = "insert into RackRow set id=${row[0]}, name='${row[1]}'";
206 unset ($result);
207 $query[] = "delete from Dictionary where chapter_id = 3";
208 $query[] = "delete from Chapter where id = 3";
209 $query[] = "
210 CREATE TABLE `LDAPCache` (
211 `presented_username` char(64) NOT NULL,
212 `successful_hash` char(40) NOT NULL,
213 `first_success` timestamp NOT NULL default CURRENT_TIMESTAMP,
214 `last_retry` timestamp NOT NULL default '0000-00-00 00:00:00',
215 `displayed_name` char(128) default NULL,
216 `memberof` text,
217 UNIQUE KEY `presented_username` (`presented_username`),
218 KEY `scanidx` (`presented_username`,`successful_hash`)
219 ) ENGINE=InnoDB;";
220 $query[] = "alter table UserAccount modify column user_password_hash char(40) NULL";
221 $query[] = 'ALTER TABLE Rack DROP COLUMN deleted';
222 $query[] = 'ALTER TABLE RackHistory DROP COLUMN deleted';
223 $query[] = 'ALTER TABLE RackObject DROP COLUMN deleted';
224 $query[] = 'ALTER TABLE RackObjectHistory DROP COLUMN deleted';
225 // Can't be added straight due to many duplicates, even in "dictbase" data.
226 $result = $dbxlink->query ('SELECT type1, type2, count(*) - 1 as excess FROM PortCompat GROUP BY type1, type2 HAVING excess > 0');
227 while ($row = $result->fetch (PDO::FETCH_ASSOC))
228 $query[] = "DELETE FROM PortCompat WHERE type1 = ${row['type1']} AND type2 = ${row['type2']} limit ${row['excess']}";
229 unset ($result);
230 $query[] = 'ALTER TABLE PortCompat DROP KEY type1';
231 $query[] = 'ALTER TABLE PortCompat ADD UNIQUE `type1_2` (type1, type2)';
232 $query[] = "UPDATE Config SET varvalue = '0.17.0' WHERE varname = 'DB_VERSION'";
233
234 break;
235 case '0.17.1':
236 $query[] = "ALTER TABLE Dictionary DROP KEY `chap_to_key`";
237 $query = array_merge ($query, reloadDictionary ($batchid));
238 // Token set has changed, so the cache isn't valid any more.
239 $query[] = "UPDATE Script SET script_text = NULL WHERE script_name = 'RackCodeCache'";
240 $query[] = "UPDATE Config SET varvalue = '0.17.1' WHERE varname = 'DB_VERSION'";
241 break;
242 case '0.17.2':
243 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (26,'no','fibre channel switch models')";
244 $query = array_merge ($query, reloadDictionary ($batchid));
245 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1055,2,26)";
246 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('DEFAULT_SNMP_COMMUNITY','public','string','no','no','Default SNMP Community string')";
247 // wipe irrelevant records (ticket:250)
248 $query[] = "DELETE FROM TagStorage WHERE entity_realm = 'file' AND entity_id NOT IN (SELECT id FROM File)";
249 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4_ENABLE_KNIGHT','yes','string','no','no','Enable IPv4 knight feature')";
250 $query[] = "ALTER TABLE IPv4Network ADD COLUMN comment text AFTER name";
251 $query[] = "ALTER TABLE Port ADD INDEX comment (reservation_comment)";
252 $query[] = "ALTER TABLE Port DROP KEY l2address"; // UNIQUE
253 $query[] = "ALTER TABLE Port ADD KEY (l2address)"; // not UNIQUE
254 $query[] = "ALTER TABLE Port DROP KEY object_id";
255 $query[] = "ALTER TABLE Port ADD UNIQUE KEY per_object (object_id, name, type)";
256 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (20,1083)";
257 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (21,1083)";
258 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1077,1077)";
259 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,20)";
260 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,21)";
261 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,1083)";
262 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1087,1087)";
263 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (27,'no','PDU models')";
264 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (2,2,27)";
265 $query[] = "UPDATE Config SET varvalue = '0.17.2' WHERE varname = 'DB_VERSION'";
266 break;
267 case '0.17.3':
268 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_TOPLIST_SIZE','50','uint','yes','no','Tags top list size')";
269 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_SIZE','20','uint','no','no','Tags quick list size')";
270 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_THRESHOLD','50','uint','yes','no','Tags quick list threshold')";
271 $query[] = "ALTER TABLE AttributeMap MODIFY COLUMN chapter_id int(10) unsigned NULL";
272 $query[] = "UPDATE AttributeMap SET chapter_id = NULL WHERE attr_id IN (SELECT id FROM Attribute WHERE type != 'dict')";
273 // ticket:239
274 $query[] = 'UPDATE AttributeValue SET uint_value = 1018 WHERE uint_value = 731 AND attr_id IN (SELECT attr_id FROM AttributeMap WHERE chapter_id = 12)';
275 $query[] = 'DELETE FROM Dictionary WHERE dict_key = 731';
276 $query = array_merge ($query, reloadDictionary ($batchid));
277 $query[] = "UPDATE Config SET vartype='uint' WHERE varname='RACKS_PER_ROW'";
278 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('ENABLE_MULTIPORT_FORM','no','string','no','no','Enable \"Add/update multiple ports\" form')";
279 $query[] = "UPDATE Config SET varvalue = '0.17.3' WHERE varname = 'DB_VERSION'";
280 break;
281 case '0.17.4':
282 $query[] = "ALTER TABLE Link ENGINE=InnoDB";
283 $query[] = "ALTER TABLE Port ENGINE=InnoDB";
284 $query[] = "ALTER TABLE IPv4RS ENGINE=InnoDB";
285 $query[] = "ALTER TABLE IPv4RSPool ENGINE=InnoDB";
286 $query[] = "ALTER TABLE AttributeValue ENGINE=InnoDB";
287 $query[] = "ALTER TABLE RackObject ENGINE=InnoDB";
288 $query[] = "ALTER TABLE IPv4NAT ENGINE=InnoDB";
289 $query[] = "ALTER TABLE IPv4LB ENGINE=InnoDB";
290 $query[] = "ALTER TABLE IPv4VS ENGINE=InnoDB";
291 $query[] = "DELETE FROM IPv4RS WHERE rspool_id NOT IN (SELECT id FROM IPv4RSPool)";
292 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-b` FOREIGN KEY (portb) REFERENCES Port (id)";
293 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-a` FOREIGN KEY (porta) REFERENCES Port (id)";
294 $query[] = "ALTER TABLE IPv4RS ADD CONSTRAINT `IPv4RS-FK` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id) ON DELETE CASCADE";
295 $query[] = "ALTER TABLE AttributeValue ADD CONSTRAINT `AttributeValue-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
296 $query[] = "ALTER TABLE IPv4NAT ADD CONSTRAINT `IPv4NAT-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
297 $query[] = "ALTER TABLE Port ADD CONSTRAINT `Port-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
298 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-rspool_id` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id)";
299 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
300 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-vs_id` FOREIGN KEY (vs_id) REFERENCES IPv4VS (id)";
301 $query = array_merge ($query, reloadDictionary ($batchid));
302 $query[] = "UPDATE Config SET varvalue = '0.17.4' WHERE varname = 'DB_VERSION'";
303 break;
304 case '0.17.5':
305 $query[] = "ALTER TABLE TagTree ENGINE=InnoDB";
306 $query[] = "ALTER TABLE TagStorage ENGINE=InnoDB";
307 $query[] = "ALTER TABLE TagStorage ADD CONSTRAINT `TagStorage-FK-tag_id` FOREIGN KEY (tag_id) REFERENCES TagTree (id)";
308 $query[] = "ALTER TABLE TagTree ADD CONSTRAINT `TagTree-K-parent_id` FOREIGN KEY (parent_id) REFERENCES TagTree (id)";
309 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (21,1195)';
310 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (22,1196)';
311 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (23,1196)';
312 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (20,1195)';
313 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (25,1202)';
314 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (26,1202)';
315 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (27,1204)';
316 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (28,1204)';
317 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1083,1195)';
318 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1084,1084)';
319 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,20)';
320 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,21)';
321 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,1083)';
322 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,1195)';
323 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1196,22)';
324 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1196,23)';
325 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1196,1196)';
326 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1197,1197)';
327 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1198,1199)';
328 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1199,1198)';
329 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1200,1200)';
330 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1201,1201)';
331 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1202,25)';
332 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1202,26)';
333 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1202,1202)';
334 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1203,1203)';
335 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1204,27)';
336 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1204,28)';
337 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1204,1204)';
338 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1205,1205)';
339 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1206,1207)';
340 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1207,1206)';
341 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1316,1316)';
342 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (16, 1322)';
343 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1322, 16)';
344 $query[] = 'DELETE FROM PortCompat WHERE type1 = 16 AND type2 = 16';
345 for ($i = 1209; $i <= 1300; $i++)
346 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (${i}, ${i})";
347 $query[] = "
348 CREATE TABLE `PortInnerInterface` (
349 `id` int(10) unsigned NOT NULL,
350 `iif_name` char(16) NOT NULL,
351 PRIMARY KEY (`id`),
352 UNIQUE KEY `iif_name` (`iif_name`)
353 ) ENGINE=InnoDB";
354 $query[] = "INSERT INTO `PortInnerInterface` VALUES (1,'hardwired')";
355 $query[] = "INSERT INTO `PortInnerInterface` VALUES (2,'SFP-100')";
356 $query[] = "INSERT INTO `PortInnerInterface` VALUES (3,'GBIC')";
357 $query[] = "INSERT INTO `PortInnerInterface` VALUES (4,'SFP-1000')";
358 $query[] = "INSERT INTO `PortInnerInterface` VALUES (5,'XENPAK')";
359 $query[] = "INSERT INTO `PortInnerInterface` VALUES (6,'X2')";
360 $query[] = "INSERT INTO `PortInnerInterface` VALUES (7,'XPAK')";
361 $query[] = "INSERT INTO `PortInnerInterface` VALUES (8,'XFP')";
362 $query[] = "INSERT INTO `PortInnerInterface` VALUES (9,'SFP+')";
363 $query[] = "
364 CREATE TABLE `PortInterfaceCompat` (
365 `iif_id` int(10) unsigned NOT NULL,
366 `oif_id` int(10) unsigned NOT NULL,
367 UNIQUE KEY `pair` (`iif_id`,`oif_id`),
368 CONSTRAINT `PortInterfaceCompat-FK-iif_id` FOREIGN KEY (`iif_id`) REFERENCES `PortInnerInterface` (`id`)
369 ) ENGINE=InnoDB";
370 $query[] = "ALTER TABLE Port ADD COLUMN iif_id int unsigned NOT NULL AFTER name"; // will set iif_id to 0
371 $query[] = "UPDATE Port SET iif_id = 2 WHERE type = 1208";
372 $query[] = "UPDATE Port SET iif_id = 3 WHERE type = 1078";
373 $query[] = "UPDATE Port SET iif_id = 4 WHERE type = 1077";
374 $query[] = "UPDATE Port SET iif_id = 5 WHERE type = 1079";
375 $query[] = "UPDATE Port SET iif_id = 6 WHERE type = 1080";
376 $query[] = "UPDATE Port SET iif_id = 7 WHERE type = 1081";
377 $query[] = "UPDATE Port SET iif_id = 8 WHERE type = 1082";
378 $query[] = "UPDATE Port SET iif_id = 9 WHERE type = 1084";
379 $query[] = "UPDATE Port SET iif_id = 1 WHERE iif_id = 0";
380 $query[] = 'ALTER TABLE Port ADD UNIQUE `object_iif_oif_name` (object_id, iif_id, type, name)';
381 $query[] = 'ALTER TABLE Port DROP KEY `per_object`';
382 $base1000 = array (24, 34, 1202, 1203, 1204, 1205, 1206, 1207);
383 $base10000 = array (30, 35, 36, 37, 38, 39, 40);
384 $PICdata = array
385 (
386 1 => array (16, 19, 24, 29, 31, 33, 446, 681, 682, 1322),
387 2 => array (1208, 1195, 1196, 1197, 1198, 1199, 1200, 1201),
388 3 => array_merge (array (1078), $base1000),
389 4 => array_merge (array (1077), $base1000),
390 5 => array_merge (array (1079), $base10000),
391 6 => array_merge (array (1080), $base10000),
392 7 => array_merge (array (1081), $base10000),
393 8 => array_merge (array (1082), $base10000),
394 9 => array_merge (array (1084), $base10000),
395 );
396 // make sure all IIF/OIF pairs referenced from Port exist in PortInterfaceCompat before enabling FK
397 // iif_id doesn't exist at this point
398 $result = $dbxlink->query ('SELECT DISTINCT type FROM Port WHERE type NOT IN (1208, 1078, 1077, 1079, 1080, 1081, 1082, 1084)');
399 while ($row = $result->fetch (PDO::FETCH_ASSOC))
400 if (FALSE === array_search ($row['type'], $PICdata[1]))
401 array_push ($PICdata[1], $row['type']);
402 unset ($result);
403 foreach ($PICdata as $iif_id => $oif_ids)
404 foreach ($oif_ids as $oif_id)
405 $query[] = "INSERT INTO PortInterfaceCompat (iif_id, oif_id) VALUES (${iif_id}, ${oif_id})";
406 $query[] = "ALTER TABLE Port ADD CONSTRAINT `Port-FK-iif-oif` FOREIGN KEY (`iif_id`, `type`) REFERENCES `PortInterfaceCompat` (`iif_id`, `oif_id`)";
407 $query[] = 'UPDATE Port SET type = 1322 WHERE type = 16 AND (SELECT objtype_id FROM RackObject WHERE id = object_id) IN (2, 12)';
408 $query = array_merge ($query, reloadDictionary ($batchid));
409 $query[] = "DELETE FROM Config WHERE varname = 'default_port_type'";
410 $query[] = "INSERT INTO Config VALUES ('DEFAULT_PORT_IIF_ID','1','uint','no','no','Default port inner interface ID')";
411 $query[] = "INSERT INTO Config VALUES ('DEFAULT_PORT_OIF_IDS','1=24; 3=1078; 4=1077; 5=1079; 6=1080; 8=1082; 9=1084','string','no','no','Default port outer interface IDs')";
412 $query[] = "INSERT INTO Config VALUES ('IPV4_TREE_RTR_AS_CELL','yes','string','no','no','Show full router info for each network in IPv4 tree view')";
413 $query[] = "UPDATE Chapter SET name = 'PortOuterInterface' WHERE id = 2";
414 // remap refs to duplicate records, which will be discarded (ticket:286)
415 $query[] = 'UPDATE AttributeValue SET uint_value = 147 WHERE uint_value = 1020 AND attr_id = 2';
416 $query[] = 'UPDATE AttributeValue SET uint_value = 377 WHERE uint_value = 1021 AND attr_id = 2';
417 $query[] = 'INSERT INTO AttributeMap (objtype_id, attr_id) VALUES (2, 1), (2, 3), (2, 5)';
418 $query[] = "UPDATE Config SET varvalue = '0.17.5' WHERE varname = 'DB_VERSION'";
419 break;
420 case '0.17.6':
421 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (28,'no','Voice/video hardware')";
422 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,1,NULL)";
423 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,2,28)";
424 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,3,NULL)";
425 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,5,NULL)";
426 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PROXIMITY_RANGE','0','uint','yes','no','Proximity range (0 is current rack only)')";
427 $query = array_merge ($query, reloadDictionary ($batchid));
428 $query[] = "UPDATE Config SET varvalue = '0.17.6' WHERE varname = 'DB_VERSION'";
429 break;
430 case '0.17.7':
431 $query[] = "UPDATE Config SET varvalue = '0.17.7' WHERE varname = 'DB_VERSION'";
432 break;
433 case '0.17.8':
434 $query = array_merge ($query, reloadDictionary ($batchid));
435 $query[] = "ALTER TABLE TagTree DROP COLUMN valid_realm";
436 $query[] = "UPDATE Config SET varvalue = '0.17.8' WHERE varname = 'DB_VERSION'";
437 break;
438 case '0.17.9':
439 $query[] = "ALTER table Config add `is_userdefined` enum('yes','no') NOT NULL default 'no' AFTER `is_hidden`";
440 $query[] = "
441 CREATE TABLE `UserConfig` (
442 `varname` char(32) NOT NULL,
443 `varvalue` char(255) NOT NULL,
444 `user` char(64) NOT NULL,
445 UNIQUE KEY `user_varname` (`user`,`varname`)
446 ) TYPE=InnoDB";
447 $query[] = "UPDATE Config SET is_userdefined = 'yes' WHERE varname IN
448 (
449 'MASSCOUNT',
450 'MAXSELSIZE',
451 'ROW_SCALE',
452 'PORTS_PER_ROW',
453 'IPV4_ADDRS_PER_PAGE',
454 'DEFAULT_RACK_HEIGHT',
455 'DEFAULT_SLB_VS_PORT',
456 'DEFAULT_SLB_RS_PORT',
457 'DETECT_URLS',
458 'RACK_PRESELECT_THRESHOLD',
459 'DEFAULT_IPV4_RS_INSERVICE',
460 'DEFAULT_OBJECT_TYPE',
461 'SHOW_EXPLICIT_TAGS',
462 'SHOW_IMPLICIT_TAGS',
463 'SHOW_AUTOMATIC_TAGS',
464 'IPV4_AUTO_RELEASE',
465 'SHOW_LAST_TAB',
466 'EXT_IPV4_VIEW',
467 'TREE_THRESHOLD',
468 'ADDNEW_AT_TOP',
469 'IPV4_TREE_SHOW_USAGE',
470 'PREVIEW_TEXT_MAXCHARS',
471 'PREVIEW_TEXT_ROWS',
472 'PREVIEW_TEXT_COLS',
473 'PREVIEW_IMAGE_MAXPXS',
474 'VENDOR_SIEVE',
475 'RACKS_PER_ROW'
476 )";
477 $query[] = "UPDATE Config SET varvalue = '0.17.9' WHERE varname = 'DB_VERSION'";
478 break;
479 case '0.17.10':
480 $query = array_merge ($query, reloadDictionary ($batchid));
481 $query[] = "ALTER TABLE MountOperation ADD KEY (object_id)";
482 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('STATIC_FILTER','yes','string','no','no','yes','Enable Filter Caching');";
483 $query[] = "UPDATE Config SET varvalue = '0.17.10' WHERE varname = 'DB_VERSION'";
484 case '0.17.11':
485 $query = array_merge ($query, reloadDictionary ($batchid));
486 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('ENABLE_BULKPORT_FORM','yes','string','no','no','yes','Enable \"Bulk Port\" form');";
487 $query[] = "DELETE AttributeValue FROM AttributeValue JOIN Attribute where AttributeValue.attr_id = Attribute.id AND Attribute.type = 'dict' AND AttributeValue.uint_value = 0";
488 $query[] = "UPDATE Config SET varvalue = '0.17.11' WHERE varname = 'DB_VERSION'";
489 break;
490 case '0.18.0':
491 $query = array_merge ($query, reloadDictionary ($batchid));
492 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('VLANSWITCH_LISTSRC', '', 'string', 'yes', 'no', 'yes', 'List of VLAN running switches')";
493 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('VLANIPV4NET_LISTSRC', '', 'string', 'yes', 'no', 'yes', 'List of VLAN-based IPv4 networks')";
494 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('DEFAULT_VDOM_ID','','uint','yes','no','yes','Default VLAN domain ID')";
495 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('DEFAULT_VST_ID','','uint','yes','no','yes','Default VLAN switch template ID')";
496 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_DEPLOY_MINAGE','300','uint','no','no','no','802.1Q deploy minimum age')";
497 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_DEPLOY_MAXAGE','3600','uint','no','no','no','802.1Q deploy maximum age')";
498 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_DEPLOY_RETRY','10800','uint','no','no','no','802.1Q deploy retry timer')";
499 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_WRI_AFTER_CONFT','no','string','no','no','no','802.1Q: save device configuration after deploy')";
500 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_INSTANT_DEPLOY','no','string','no','no','yes','802.1Q: instant deploy')";
501 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('IPV4_TREE_SHOW_VLAN','yes','string','no','no','yes','Show VLAN for each network in IPv4 tree')";
502 $query[] = "ALTER TABLE IPv4Network ENGINE=InnoDB";
503 $query[] = "SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0";
504 $query[] = "
505 CREATE TABLE `CachedPAV` (
506 `object_id` int(10) unsigned NOT NULL,
507 `port_name` char(255) NOT NULL,
508 `vlan_id` int(10) unsigned NOT NULL default '0',
509 PRIMARY KEY (`object_id`,`port_name`,`vlan_id`),
510 KEY `vlan_id` (`vlan_id`),
511 CONSTRAINT `CachedPAV-FK-object-port` FOREIGN KEY (`object_id`, `port_name`) REFERENCES `CachedPVM` (`object_id`, `port_name`) ON DELETE CASCADE,
512 CONSTRAINT `CachedPAV-FK-vlan_id` FOREIGN KEY (`vlan_id`) REFERENCES `VLANValidID` (`vlan_id`)
513 ) ENGINE=InnoDB
514 ";
515 $query[] = "
516 CREATE TABLE `CachedPNV` (
517 `object_id` int(10) unsigned NOT NULL,
518 `port_name` char(255) NOT NULL,
519 `vlan_id` int(10) unsigned NOT NULL default '0',
520 PRIMARY KEY (`object_id`,`port_name`,`vlan_id`),
521 UNIQUE KEY `port_id` (`object_id`,`port_name`),
522 CONSTRAINT `CachedPNV-FK-compound` FOREIGN KEY (`object_id`, `port_name`, `vlan_id`) REFERENCES `CachedPAV` (`object_id`, `port_name`, `vlan_id`) ON DELETE CASCADE
523 ) ENGINE=InnoDB
524 ";
525 $query[] = "
526 CREATE TABLE `CachedPVM` (
527 `object_id` int(10) unsigned NOT NULL,
528 `port_name` char(255) NOT NULL,
529 `vlan_mode` enum('access','trunk') NOT NULL default 'access',
530 PRIMARY KEY (`object_id`,`port_name`),
531 CONSTRAINT `CachedPVM-FK-object_id` FOREIGN KEY (`object_id`) REFERENCES `RackObject` (`id`) ON DELETE CASCADE
532 ) ENGINE=InnoDB
533 ";
534 $query[] = "
535 CREATE TABLE `PortAllowedVLAN` (
536 `object_id` int(10) unsigned NOT NULL,
537 `port_name` char(255) NOT NULL,
538 `vlan_id` int(10) unsigned NOT NULL default '0',
539 PRIMARY KEY (`object_id`,`port_name`,`vlan_id`),
540 KEY `vlan_id` (`vlan_id`),
541 CONSTRAINT `PortAllowedVLAN-FK-object-port` FOREIGN KEY (`object_id`, `port_name`) REFERENCES `PortVLANMode` (`object_id`, `port_name`) ON DELETE CASCADE,
542 CONSTRAINT `PortAllowedVLAN-FK-vlan_id` FOREIGN KEY (`vlan_id`) REFERENCES `VLANValidID` (`vlan_id`)
543 ) ENGINE=InnoDB
544 ";
545 $query[] = "
546 CREATE TABLE `PortNativeVLAN` (
547 `object_id` int(10) unsigned NOT NULL,
548 `port_name` char(255) NOT NULL,
549 `vlan_id` int(10) unsigned NOT NULL default '0',
550 PRIMARY KEY (`object_id`,`port_name`,`vlan_id`),
551 UNIQUE KEY `port_id` (`object_id`,`port_name`),
552 CONSTRAINT `PortNativeVLAN-FK-compound` FOREIGN KEY (`object_id`, `port_name`, `vlan_id`) REFERENCES `PortAllowedVLAN` (`object_id`, `port_name`, `vlan_id`) ON DELETE CASCADE
553 ) ENGINE=InnoDB
554 ";
555 $query[] = "
556 CREATE TABLE `PortVLANMode` (
557 `object_id` int(10) unsigned NOT NULL,
558 `port_name` char(255) NOT NULL,
559 `vlan_mode` enum('access','trunk') NOT NULL default 'access',
560 PRIMARY KEY (`object_id`,`port_name`),
561 CONSTRAINT `PortVLANMode-FK-object-port` FOREIGN KEY (`object_id`, `port_name`) REFERENCES `CachedPVM` (`object_id`, `port_name`)
562 ) ENGINE=InnoDB
563 ";
564 $query[] = "
565 CREATE TABLE `VLANDescription` (
566 `domain_id` int(10) unsigned NOT NULL,
567 `vlan_id` int(10) unsigned NOT NULL default '0',
568 `vlan_type` enum('ondemand','compulsory','alien') NOT NULL default 'ondemand',
569 `vlan_descr` char(255) default NULL,
570 PRIMARY KEY (`domain_id`,`vlan_id`),
571 KEY `vlan_id` (`vlan_id`),
572 CONSTRAINT `VLANDescription-FK-domain_id` FOREIGN KEY (`domain_id`) REFERENCES `VLANDomain` (`id`) ON DELETE CASCADE,
573 CONSTRAINT `VLANDescription-FK-vlan_id` FOREIGN KEY (`vlan_id`) REFERENCES `VLANValidID` (`vlan_id`)
574 ) ENGINE=InnoDB
575 ";
576 $query[] = "
577 CREATE TABLE `VLANDomain` (
578 `id` int(10) unsigned NOT NULL auto_increment,
579 `description` char(255) default NULL,
580 PRIMARY KEY (`id`),
581 UNIQUE KEY `description` (`description`)
582 ) ENGINE=InnoDB
583 ";
584 $query[] = "
585 CREATE TABLE `VLANIPv4` (
586 `domain_id` int(10) unsigned NOT NULL,
587 `vlan_id` int(10) unsigned NOT NULL,
588 `ipv4net_id` int(10) unsigned NOT NULL,
589 UNIQUE KEY `network-domain` (`ipv4net_id`,`domain_id`),
590 KEY `VLANIPv4-FK-compound` (`domain_id`,`vlan_id`),
591 CONSTRAINT `VLANIPv4-FK-compound` FOREIGN KEY (`domain_id`, `vlan_id`) REFERENCES `VLANDescription` (`domain_id`, `vlan_id`) ON DELETE CASCADE,
592 CONSTRAINT `VLANIPv4-FK-ipv4net_id` FOREIGN KEY (`ipv4net_id`) REFERENCES `IPv4Network` (`id`) ON DELETE CASCADE
593 ) ENGINE=InnoDB
594 ";
595 $query[] = "
596 CREATE TABLE `VLANSTRule` (
597 `vst_id` int(10) unsigned NOT NULL,
598 `rule_no` int(10) unsigned NOT NULL,
599 `port_pcre` char(255) NOT NULL,
600 `port_role` enum('access','trunk','uplink','downlink','none') NOT NULL default 'none',
601 `wrt_vlans` char(255) default NULL,
602 `description` char(255) default NULL,
603 UNIQUE KEY `vst-rule` (`vst_id`,`rule_no`),
604 CONSTRAINT `VLANSTRule-FK-vst_id` FOREIGN KEY (`vst_id`) REFERENCES `VLANSwitchTemplate` (`id`) ON DELETE CASCADE
605 ) ENGINE=InnoDB
606 ";
607 $query[] = "
608 CREATE TABLE `VLANSwitch` (
609 `object_id` int(10) unsigned NOT NULL,
610 `domain_id` int(10) unsigned NOT NULL,
611 `template_id` int(10) unsigned NOT NULL,
612 `mutex_rev` int(10) unsigned NOT NULL default '0',
613 `out_of_sync` enum('yes','no') NOT NULL default 'yes',
614 `last_errno` int(10) unsigned NOT NULL default '0',
615 `last_change` timestamp NOT NULL default '0000-00-00 00:00:00',
616 `last_push_started` timestamp NOT NULL default '0000-00-00 00:00:00',
617 `last_push_finished` timestamp NOT NULL default '0000-00-00 00:00:00',
618 `last_error_ts` timestamp NOT NULL default '0000-00-00 00:00:00',
619 UNIQUE KEY `object_id` (`object_id`),
620 KEY `domain_id` (`domain_id`),
621 KEY `template_id` (`template_id`),
622 KEY `out_of_sync` (`out_of_sync`),
623 KEY `last_errno` (`last_errno`),
624 CONSTRAINT `VLANSwitch-FK-domain_id` FOREIGN KEY (`domain_id`) REFERENCES `VLANDomain` (`id`),
625 CONSTRAINT `VLANSwitch-FK-object_id` FOREIGN KEY (`object_id`) REFERENCES `RackObject` (`id`),
626 CONSTRAINT `VLANSwitch-FK-template_id` FOREIGN KEY (`template_id`) REFERENCES `VLANSwitchTemplate` (`id`)
627 ) ENGINE=InnoDB
628 ";
629 $query[] = "
630 CREATE TABLE `VLANSwitchTemplate` (
631 `id` int(10) unsigned NOT NULL auto_increment,
632 `max_local_vlans` int(10) unsigned default NULL,
633 `description` char(255) default NULL,
634 PRIMARY KEY (`id`),
635 UNIQUE KEY `description` (`description`)
636 ) ENGINE=InnoDB
637 ";
638 $query[] = "
639 CREATE TABLE `VLANValidID` (
640 `vlan_id` int(10) unsigned NOT NULL default '1',
641 PRIMARY KEY (`vlan_id`)
642 ) ENGINE=InnoDB
643 ";
644 $query[] = "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS";
645 for ($i = 1; $i <= 4094; $i++)
646 $query[] = "INSERT INTO VLANValidID (vlan_id) VALUES (${i})";
647 $query[] = "UPDATE Config SET varvalue = '0.18.0' WHERE varname = 'DB_VERSION'";
648 break;
649 case '0.18.1':
650 $query = array_merge ($query, reloadDictionary ($batchid));
651 $query[] = "ALTER TABLE Atom ENGINE=InnoDB";
652 $query[] = "ALTER TABLE AttributeMap ENGINE=InnoDB";
653 $query[] = "ALTER TABLE Config ENGINE=InnoDB";
654 $query[] = "ALTER TABLE IPv4Address ENGINE=InnoDB";
655 $query[] = "ALTER TABLE IPv4Allocation ENGINE=InnoDB";
656 $query[] = "ALTER TABLE Molecule ENGINE=InnoDB";
657 $query[] = "ALTER TABLE MountOperation ENGINE=InnoDB";
658 $query[] = "ALTER TABLE PortCompat ENGINE=InnoDB";
659 $query[] = "ALTER TABLE Rack ENGINE=InnoDB";
660 $query[] = "ALTER TABLE RackHistory ENGINE=InnoDB";
661 $query[] = "ALTER TABLE RackObjectHistory ENGINE=InnoDB";
662 $query[] = "ALTER TABLE RackRow ENGINE=InnoDB";
663 $query[] = "ALTER TABLE RackSpace ENGINE=InnoDB";
664 $query[] = "ALTER TABLE Script ENGINE=InnoDB";
665 $query[] = "ALTER TABLE AttributeValue DROP FOREIGN KEY `AttributeValue-FK-object_id`";
666 $query[] = "ALTER TABLE AttributeValue ADD CONSTRAINT `AttributeValue-FK-object_id` FOREIGN KEY (`object_id`) REFERENCES `RackObject` (`id`) ON DELETE CASCADE";
667 $query[] = "ALTER TABLE RackObjectHistory ADD KEY (id)";
668 $query[] = "ALTER TABLE RackObjectHistory ADD CONSTRAINT `RackObjectHistory-FK-object_id` FOREIGN KEY (id) REFERENCES `RackObject` (`id`) ON DELETE CASCADE";
669 $query[] = "ALTER TABLE MountOperation ADD CONSTRAINT `MountOperation-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id) ON DELETE CASCADE";
670 $query[] = "ALTER TABLE RackSpace ADD CONSTRAINT `RackSpace-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id) ON DELETE CASCADE";
671 $query[] = "ALTER TABLE Link DROP FOREIGN KEY `Link-FK-a`";
672 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-a` FOREIGN KEY (`porta`) REFERENCES `Port` (`id`) ON DELETE CASCADE";
673 $query[] = "ALTER TABLE Link DROP FOREIGN KEY `Link-FK-b`";
674 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-b` FOREIGN KEY (`portb`) REFERENCES `Port` (`id`) ON DELETE CASCADE";
675 $query[] = "ALTER TABLE Port DROP FOREIGN KEY `Port-FK-object_id`";
676 $query[] = "ALTER TABLE Port ADD CONSTRAINT `Port-FK-object_id` FOREIGN KEY (`object_id`) REFERENCES `RackObject` (`id`) ON DELETE CASCADE";
677 $query[] = "ALTER TABLE AttributeMap MODIFY `chapter_id` int(10) unsigned default NULL";
678 $query[] = "ALTER TABLE IPv4Address MODIFY `ip` int(10) unsigned NOT NULL default '0'";
679 $query[] = "ALTER TABLE IPv4Address MODIFY `name` char(255) NOT NULL default ''";
680 $query[] = "ALTER TABLE IPv4Allocation MODIFY `object_id` int(10) unsigned NOT NULL default '0'";
681 $query[] = "ALTER TABLE IPv4Allocation MODIFY `ip` int(10) unsigned NOT NULL default '0'";
682 $query[] = "ALTER TABLE IPv4Allocation MODIFY `name` char(255) NOT NULL default ''";
683 $query[] = "ALTER TABLE IPv4NAT MODIFY `object_id` int(10) unsigned NOT NULL default '0'";
684 $query[] = "ALTER TABLE IPv4NAT MODIFY `proto` enum('TCP','UDP') NOT NULL default 'TCP'";
685 $query[] = "ALTER TABLE IPv4NAT MODIFY `localip` int(10) unsigned NOT NULL default '0'";
686 $query[] = "ALTER TABLE IPv4NAT MODIFY `localport` smallint(5) unsigned NOT NULL default '0'";
687 $query[] = "ALTER TABLE IPv4NAT MODIFY `remoteip` int(10) unsigned NOT NULL default '0'";
688 $query[] = "ALTER TABLE IPv4NAT MODIFY `remoteport` smallint(5) unsigned NOT NULL default '0'";
689 $query[] = "ALTER TABLE IPv4Network MODIFY `ip` int(10) unsigned NOT NULL default '0'";
690 $query[] = "ALTER TABLE IPv4Network MODIFY `mask` int(10) unsigned NOT NULL default '0'";
691 $query[] = "ALTER TABLE Link MODIFY `porta` int(10) unsigned NOT NULL default '0'";
692 $query[] = "ALTER TABLE Link MODIFY `portb` int(10) unsigned NOT NULL default '0'";
693 $query[] = "ALTER TABLE MountOperation MODIFY `object_id` int(10) unsigned NOT NULL default '0'";
694 $query[] = "ALTER TABLE MountOperation MODIFY `ctime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP";
695 $query[] = "ALTER TABLE Port MODIFY `object_id` int(10) unsigned NOT NULL default '0'";
696 $query[] = "ALTER TABLE Port MODIFY `name` char(255) NOT NULL default ''";
697 $query[] = "ALTER TABLE Port MODIFY `type` int(10) unsigned NOT NULL default '0'";
698 $query[] = "ALTER TABLE PortCompat MODIFY `type1` int(10) unsigned NOT NULL default '0'";
699 $query[] = "ALTER TABLE PortCompat MODIFY `type2` int(10) unsigned NOT NULL default '0'";
700 $query[] = "ALTER TABLE RackHistory MODIFY `ctime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP";
701 $query[] = "ALTER TABLE RackObjectHistory MODIFY `ctime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP";
702 $query[] = "ALTER TABLE TagStorage MODIFY `tag_id` int(10) unsigned NOT NULL default '0'";
703 $query[] = "ALTER TABLE UserAccount MODIFY `user_name` char(64) NOT NULL default ''";
704 $query[] = "UPDATE Config SET varvalue = '0.18.1' WHERE varname = 'DB_VERSION'";
705 break;
706 case '0.18.2':
707 $query = array_merge ($query, reloadDictionary ($batchid));
708 $query[] = "ALTER TABLE Rack ADD CONSTRAINT `Rack-FK-row_id` FOREIGN KEY (row_id) REFERENCES RackRow (id)";
709 $query[] = "ALTER TABLE RackRow ADD UNIQUE KEY `name` (name)";
710 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('CDP_RUNNERS_LISTSRC', '', 'string', 'yes', 'no', 'no', 'List of devices running CDP')";
711 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('LLDP_RUNNERS_LISTSRC', '', 'string', 'yes', 'no', 'no', 'List of devices running LLDP')";
712 $query[] = "UPDATE Config SET varvalue = '0.18.2' WHERE varname = 'DB_VERSION'";
713 break;
714 default:
715 showFailure ("executeUpgradeBatch () failed, because batch '${batchid}' isn't defined", __FILE__);
716 die;
717 break;
718 }
719 $failures = array();
720 echo "<tr><th>Executing batch '${batchid}'</th><td>";
721 foreach ($query as $q)
722 {
723 $result = $dbxlink->query ($q);
724 if ($result == NULL)
725 {
726 $errorInfo = $dbxlink->errorInfo();
727 $failures[] = array ($q, $errorInfo[2]);
728 }
729 }
730 if (!count ($failures))
731 echo "<strong><font color=green>done</font></strong>";
732 else
733 {
734 echo "<strong><font color=red>The following queries failed:</font></strong><br><pre>";
735 foreach ($failures as $f)
736 {
737 list ($q, $i) = $f;
738 echo "${q} -- ${i}\n";
739 }
740 echo "</pre>";
741 }
742 echo '</td></tr>';
743 }
744
745 // ******************************************************************
746 //
747 // Execution starts here
748 //
749 // ******************************************************************
750
751 // a clone of showError() to drop dependency on interface.php
752 function showFailure ($info = '', $location = 'N/A')
753 {
754 if (preg_match ('/\.php$/', $location))
755 $location = basename ($location);
756 elseif ($location != 'N/A')
757 $location = $location . '()';
758 echo "<div class=msg_error>An error has occured in [${location}]. ";
759 if (empty ($info))
760 echo 'No additional information is available.';
761 else
762 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
763 echo "This failure is most probably fatal.<br></div>\n";
764 }
765
766 require_once 'inc/config.php'; // for CODE_VERSION
767 require_once 'inc/database.php'; // for getDatabaseVersion()
768 require_once 'inc/dictionary.php';
769 // Enforce default value for now, releases prior to 0.17.0 didn't support 'httpd' auth source.
770 $user_auth_src = 'database';
771
772 if (file_exists ('inc/secret.php'))
773 require_once 'inc/secret.php';
774 else
775 die ("Database connection parameters are read from inc/secret.php file, " .
776 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
777 "inc/secret.php and modify to your setup.\n\nThen reload the page.");
778
779 try
780 {
781 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
782 }
783 catch (PDOException $e)
784 {
785 die ("Database connection failed:\n\n" . $e->getMessage());
786 }
787
788 // Now we need to be sure that the current user is the administrator.
789 // The rest doesn't matter within this context.
790
791 function authenticate_admin ($username, $password)
792 {
793 global $dbxlink;
794 $hash = sha1 ($password);
795 $query = "select count(*) from UserAccount where user_id = 1 and user_name = '${username}' and user_password_hash = '${hash}'";
796 if (($result = $dbxlink->query ($query)) == NULL)
797 die ('SQL query failed in ' . __FUNCTION__);
798 $rows = $result->fetchAll (PDO::FETCH_NUM);
799 return $rows[0][0] == 1;
800 }
801
802 switch ($user_auth_src)
803 {
804 case 'database':
805 case 'ldap': // authenticate against DB as well
806 if
807 (
808 !isset ($_SERVER['PHP_AUTH_USER']) or
809 !strlen ($_SERVER['PHP_AUTH_USER']) or
810 !isset ($_SERVER['PHP_AUTH_PW']) or
811 !strlen ($_SERVER['PHP_AUTH_PW']) or
812 !authenticate_admin (escapeString ($_SERVER['PHP_AUTH_USER']), escapeString ($_SERVER['PHP_AUTH_PW']))
813 )
814 {
815 header ('WWW-Authenticate: Basic realm="RackTables upgrade"');
816 header ('HTTP/1.0 401 Unauthorized');
817 showFailure ('You must be authenticated as an administrator to complete the upgrade.', __FILE__);
818 die;
819 }
820 break; // cleared
821 case 'httpd':
822 if
823 (
824 !isset ($_SERVER['REMOTE_USER']) or
825 !strlen ($_SERVER['REMOTE_USER'])
826 )
827 {
828 showFailure ('System misconfiguration. The web-server didn\'t authenticate the user, although ought to do.');
829 die;
830 }
831 break; // cleared
832 default:
833 showFailure ('authentication source misconfiguration', __FILE__);
834 die;
835 }
836
837 $dbver = getDatabaseVersion();
838 echo '<table border=1>';
839 echo "<tr><th>Current status</th><td>Data version: ${dbver}<br>Code version: " . CODE_VERSION . "</td></tr>\n";
840
841 $path = getDBUpgradePath ($dbver, CODE_VERSION);
842 if ($path === NULL)
843 {
844 echo "<tr><th>Upgrade path</th><td><font color=red>not found</font></td></tr>\n";
845 echo "<tr><th>Summary</th><td>Check README for more information. RackTables releases prior to 0.16.4 ";
846 echo "must be upgraded to 0.16.4 first.</td></tr>\n";
847 }
848 else
849 {
850 if (!count ($path))
851 echo "<tr><th>Summary</th><td>Come back later.</td></tr>\n";
852 else
853 {
854 echo "<tr><th>Upgrade path</th><td>${dbver} &rarr; " . implode (' &rarr; ', $path) . "</td></tr>\n";
855 foreach ($path as $batchid)
856 {
857 executeUpgradeBatch ($batchid);
858 if (isset ($relnotes[$batchid]))
859 echo "<tr><th>Release notes for ${batchid}</th><td>" . $relnotes[$batchid] . "</td></tr>\n";
860 }
861 echo "<tr><th>Summary</th><td>Upgrade complete, it is Ok to <a href='index.php'>enter</a> the system.</td></tr>\n";
862 }
863 }
864 echo '</table>';
865
866 ?>