e4cc396087e8426511dbc86b368efdd19054b165
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade.cpp
1 /*
2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004-2014 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: upgrade.cpp
20 **
21 **/
22
23 #include "nxdbmgr.h"
24
25 /**
26 * Externals
27 */
28 BOOL MigrateMaps();
29
30 /**
31 * Create table
32 */
33 static BOOL CreateTable(const TCHAR *pszQuery)
34 {
35 BOOL bResult;
36 String query(pszQuery);
37
38 query.replace(_T("$SQL:TEXT"), g_pszSqlType[g_dbSyntax][SQL_TYPE_TEXT]);
39 query.replace(_T("$SQL:TXT4K"), g_pszSqlType[g_dbSyntax][SQL_TYPE_TEXT4K]);
40 query.replace(_T("$SQL:INT64"), g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64]);
41 if (g_dbSyntax == DB_SYNTAX_MYSQL)
42 query += g_pszTableSuffix;
43 bResult = SQLQuery(query);
44 return bResult;
45 }
46
47 /**
48 * Create configuration parameter if it doesn't exist (unless bForceUpdate set to true)
49 */
50 BOOL CreateConfigParam(const TCHAR *pszName, const TCHAR *pszValue, int iVisible, int iNeedRestart, BOOL bForceUpdate)
51 {
52 TCHAR szQuery[1024];
53 DB_RESULT hResult;
54 BOOL bVarExist = FALSE, bResult = TRUE;
55
56 // Check for variable existence
57 _sntprintf(szQuery, 1024, _T("SELECT var_value FROM config WHERE var_name='%s'"), pszName);
58 hResult = DBSelect(g_hCoreDB, szQuery);
59 if (hResult != 0)
60 {
61 if (DBGetNumRows(hResult) > 0)
62 bVarExist = TRUE;
63 DBFreeResult(hResult);
64 }
65
66 if (!bVarExist)
67 {
68 _sntprintf(szQuery, 1024, _T("INSERT INTO config (var_name,var_value,is_visible,")
69 _T("need_server_restart) VALUES (%s,%s,%d,%d)"),
70 (const TCHAR *)DBPrepareString(g_hCoreDB, pszName, 63),
71 (const TCHAR *)DBPrepareString(g_hCoreDB, pszValue, 255), iVisible, iNeedRestart);
72 bResult = SQLQuery(szQuery);
73 }
74 else if (bForceUpdate)
75 {
76 _sntprintf(szQuery, 1024, _T("UPDATE config SET var_value=%s WHERE var_name=%s"),
77 (const TCHAR *)DBPrepareString(g_hCoreDB, pszValue, 255), (const TCHAR *)DBPrepareString(g_hCoreDB, pszName, 63));
78 bResult = SQLQuery(szQuery);
79 }
80 return bResult;
81 }
82
83 /**
84 * Set primary key constraint
85 */
86 static BOOL SetPrimaryKey(const TCHAR *table, const TCHAR *key)
87 {
88 TCHAR query[4096];
89
90 if (g_dbSyntax == DB_SYNTAX_SQLITE)
91 return TRUE; // SQLite does not support adding constraints
92
93 _sntprintf(query, 4096, _T("ALTER TABLE %s ADD PRIMARY KEY (%s)"), table, key);
94 return SQLQuery(query);
95 }
96
97 /**
98 * Drop primary key from table
99 */
100 static BOOL DropPrimaryKey(const TCHAR *table)
101 {
102 TCHAR query[1024];
103 DB_RESULT hResult;
104 BOOL success;
105
106 switch(g_dbSyntax)
107 {
108 case DB_SYNTAX_ORACLE:
109 case DB_SYNTAX_MYSQL:
110 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP PRIMARY KEY"), table);
111 success = SQLQuery(query);
112 break;
113 case DB_SYNTAX_PGSQL:
114 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s_pkey"), table, table);
115 success = SQLQuery(query);
116 break;
117 case DB_SYNTAX_MSSQL:
118 success = FALSE;
119 _sntprintf(query, 1024, _T("SELECT name FROM sysobjects WHERE xtype='PK' AND parent_obj=OBJECT_ID('%s')"), table);
120 hResult = SQLSelect(query);
121 if (hResult != NULL)
122 {
123 if (DBGetNumRows(hResult) > 0)
124 {
125 TCHAR objName[512];
126
127 DBGetField(hResult, 0, 0, objName, 512);
128 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s"), table, objName);
129 success = SQLQuery(query);
130 }
131 DBFreeResult(hResult);
132 }
133 break;
134 default: // Unsupported DB engine
135 success = FALSE;
136 break;
137 }
138 return success;
139 }
140
141 /**
142 * Convert strings from # encoded form to normal form
143 */
144 static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *idColumn2, const TCHAR *column, bool isStringId)
145 {
146 DB_RESULT hResult;
147 TCHAR *query;
148 int queryLen = 512;
149 BOOL success = FALSE;
150
151 query = (TCHAR *)malloc(queryLen * sizeof(TCHAR));
152
153 switch(g_dbSyntax)
154 {
155 case DB_SYNTAX_MSSQL:
156 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE CAST(%s AS nvarchar(4000))=N'#00'"), table, column, column);
157 break;
158 case DB_SYNTAX_ORACLE:
159 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE to_char(%s)='#00'"), table, column, column);
160 break;
161 default:
162 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE %s='#00'"), table, column, column);
163 break;
164 }
165 if (!SQLQuery(query))
166 {
167 free(query);
168 return FALSE;
169 }
170
171 _sntprintf(query, queryLen, _T("SELECT %s,%s%s%s FROM %s WHERE %s LIKE '%%#%%'"),
172 idColumn, column, (idColumn2 != NULL) ? _T(",") : _T(""), (idColumn2 != NULL) ? idColumn2 : _T(""), table, column);
173 hResult = SQLSelect(query);
174 if (hResult == NULL)
175 {
176 free(query);
177 return FALSE;
178 }
179
180 int count = DBGetNumRows(hResult);
181 for(int i = 0; i < count; i++)
182 {
183 TCHAR *value = DBGetField(hResult, i, 1, NULL, 0);
184 if (_tcschr(value, _T('#')) != NULL)
185 {
186 DecodeSQLString(value);
187 String newValue = DBPrepareString(g_hCoreDB, value);
188 if ((int)newValue.getSize() + 256 > queryLen)
189 {
190 queryLen = newValue.getSize() + 256;
191 query = (TCHAR *)realloc(query, queryLen * sizeof(TCHAR));
192 }
193 if (isStringId)
194 {
195 TCHAR *id = DBGetField(hResult, i, 0, NULL, 0);
196 if (idColumn2 != NULL)
197 {
198 TCHAR *id2 = DBGetField(hResult, i, 2, NULL, 0);
199 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s AND %s=%s"),
200 table, column, (const TCHAR *)newValue,
201 idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id),
202 idColumn2, (const TCHAR *)DBPrepareString(g_hCoreDB, id2));
203 }
204 else
205 {
206 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s"), table, column,
207 (const TCHAR *)newValue, idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id));
208 }
209 free(id);
210 }
211 else
212 {
213 INT64 id = DBGetFieldInt64(hResult, i, 0);
214 if (idColumn2 != NULL)
215 {
216 INT64 id2 = DBGetFieldInt64(hResult, i, 2);
217 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT _T(" AND %s=") INT64_FMT,
218 table, column, (const TCHAR *)newValue, idColumn, id, idColumn2, id2);
219 }
220 else
221 {
222 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT, table, column,
223 (const TCHAR *)newValue, idColumn, id);
224 }
225 }
226 if (!SQLQuery(query))
227 goto cleanup;
228 }
229 }
230 success = TRUE;
231
232 cleanup:
233 DBFreeResult(hResult);
234 free(query);
235 return success;
236 }
237
238 static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *column)
239 {
240 return ConvertStrings(table, idColumn, NULL, column, false);
241 }
242
243 /**
244 * Set column nullable (currently only Oracle and PostgreSQL)
245 */
246 static BOOL SetColumnNullable(const TCHAR *table, const TCHAR *column)
247 {
248 TCHAR query[1024] = _T("");
249
250 switch(g_dbSyntax)
251 {
252 case DB_SYNTAX_ORACLE:
253 _sntprintf(query, 1024, _T("DECLARE already_null EXCEPTION; ")
254 _T("PRAGMA EXCEPTION_INIT(already_null, -1451); ")
255 _T("BEGIN EXECUTE IMMEDIATE 'ALTER TABLE %s MODIFY %s null'; ")
256 _T("EXCEPTION WHEN already_null THEN null; END;"), table, column);
257 break;
258 case DB_SYNTAX_PGSQL:
259 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL"), table, column);
260 break;
261 default:
262 break;
263 }
264
265 return (query[0] != 0) ? SQLQuery(query) : TRUE;
266 }
267
268 /**
269 * Resize varchar column
270 */
271 static BOOL ResizeColumn(const TCHAR *table, const TCHAR *column, int newSize)
272 {
273 TCHAR query[1024];
274
275 switch(g_dbSyntax)
276 {
277 case DB_SYNTAX_DB2:
278 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE varchar(%d)"), table, column, newSize);
279 break;
280 case DB_SYNTAX_MSSQL:
281 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s varchar(%d)"), table, column, newSize);
282 break;
283 case DB_SYNTAX_PGSQL:
284 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s TYPE varchar(%d)"), table, column, newSize);
285 break;
286 case DB_SYNTAX_SQLITE:
287 /* TODO: add SQLite support */
288 query[0] = 0;
289 break;
290 default:
291 _sntprintf(query, 1024, _T("ALTER TABLE %s MODIFY %s varchar(%d)"), table, column, newSize);
292 break;
293 }
294
295 return (query[0] != 0) ? SQLQuery(query) : TRUE;
296 }
297
298 /**
299 * Create new event template
300 */
301 static BOOL CreateEventTemplate(int code, const TCHAR *name, int severity, int flags, const TCHAR *message, const TCHAR *description)
302 {
303 TCHAR query[4096];
304
305 _sntprintf(query, 4096, _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (%d,'%s',%d,%d,%s,%s)"),
306 code, name, severity, flags, (const TCHAR *)DBPrepareString(g_hCoreDB, message),
307 (const TCHAR *)DBPrepareString(g_hCoreDB, description));
308 return SQLQuery(query);
309 }
310
311 /**
312 * Re-create TDATA tables
313 */
314 static BOOL RecreateTData(const TCHAR *className, bool multipleTables, bool indexFix)
315 {
316 TCHAR query[1024];
317 _sntprintf(query, 256, _T("SELECT id FROM %s"), className);
318 DB_RESULT hResult = SQLSelect(query);
319 if (hResult != NULL)
320 {
321 int count = DBGetNumRows(hResult);
322 for(int i = 0; i < count; i++)
323 {
324 bool recreateTables = true;
325 DWORD id = DBGetFieldULong(hResult, i, 0);
326
327 if (indexFix)
328 {
329 _sntprintf(query, 256, _T("SELECT count(*) FROM dc_tables WHERE node_id=%d"), id);
330 DB_RESULT hResultCount = SQLSelect(query);
331 if (hResultCount != NULL)
332 {
333 recreateTables = (DBGetFieldLong(hResultCount, 0, 0) == 0);
334 DBFreeResult(hResultCount);
335 }
336
337 if (!recreateTables)
338 {
339 _sntprintf(query, 256, _T("CREATE INDEX idx_tdata_rec_%d_id ON tdata_records_%d(record_id)"), id, id);
340 if (!SQLQuery(query))
341 {
342 if (!g_bIgnoreErrors)
343 {
344 DBFreeResult(hResult);
345 return FALSE;
346 }
347 }
348 }
349 }
350
351 if (recreateTables)
352 {
353 if (multipleTables)
354 {
355 _sntprintf(query, 1024, _T("DROP TABLE tdata_rows_%d\nDROP TABLE tdata_records_%d\nDROP TABLE tdata_%d\n<END>"), id, id, id);
356 }
357 else
358 {
359 _sntprintf(query, 256, _T("DROP TABLE tdata_%d\n<END>"), id);
360 }
361 if (!SQLBatch(query))
362 {
363 if (!g_bIgnoreErrors)
364 {
365 DBFreeResult(hResult);
366 return FALSE;
367 }
368 }
369
370 if (!CreateTDataTables(id))
371 {
372 if (!g_bIgnoreErrors)
373 {
374 DBFreeResult(hResult);
375 return FALSE;
376 }
377 }
378 }
379 }
380 DBFreeResult(hResult);
381 }
382 else
383 {
384 if (!g_bIgnoreErrors)
385 return FALSE;
386 }
387 return TRUE;
388 }
389
390 /**
391 * Upgrade from V332 to V333
392 */
393 static BOOL H_UpgradeFromV332(int currVersion, int newVersion)
394 {
395 static TCHAR batch[] =
396 _T("INSERT INTO metadata (var_name,var_value)")
397 _T(" VALUES ('LocationHistory','CREATE TABLE gps_history_%d (latitude varchar(20), longitude varchar(20), accuracy integer not null, start_timestamp integer not null, end_timestamp integer not null, PRIMARY KEY(start_timestamp))')\n")
398 _T("<END>");
399 CHK_EXEC(SQLBatch(batch));
400 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='333' WHERE var_name='SchemaVersion'")));
401 return TRUE;
402 }
403
404 /**
405 * Upgrade from V331 to V332
406 */
407 static BOOL H_UpgradeFromV331(int currVersion, int newVersion)
408 {
409 CHK_EXEC(SQLQuery(_T("UPDATE items SET instd_data=instance WHERE node_id=template_id AND instd_method=0")));
410 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='332' WHERE var_name='SchemaVersion'")));
411 return TRUE;
412 }
413
414 /**
415 * Upgrade from V330 to V331
416 */
417 static BOOL H_UpgradeFromV330(int currVersion, int newVersion)
418 {
419 if (g_dbSyntax == DB_SYNTAX_ORACLE)
420 {
421 CHK_EXEC(SQLQuery(_T("ALTER TABLE audit_log ADD session_id integer DEFAULT 0 NOT NULL")));
422 }
423 else
424 {
425 CHK_EXEC(SQLQuery(_T("ALTER TABLE audit_log ADD session_id integer NOT NULL DEFAULT 0")));
426 }
427 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='331' WHERE var_name='SchemaVersion'")));
428 return TRUE;
429 }
430
431 /**
432 * Upgrade from V329 to V330
433 */
434 static BOOL H_UpgradeFromV329(int currVersion, int newVersion)
435 {
436 CHK_EXEC(CreateConfigParam(_T("AlarmListDisplayLimit"), _T("4096"), 1, 0));
437 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='330' WHERE var_name='SchemaVersion'")));
438 return TRUE;
439 }
440
441 /**
442 * Upgrade from V328 to V329
443 */
444 static BOOL H_UpgradeFromV328(int currVersion, int newVersion)
445 {
446 CHK_EXEC(SQLQuery(_T("ALTER TABLE items ADD comments $SQL:TEXT")));
447 CHK_EXEC(SQLQuery(_T("ALTER TABLE dc_tables ADD comments $SQL:TEXT")));
448 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='329' WHERE var_name='SchemaVersion'")));
449 return TRUE;
450 }
451
452 /**
453 * Upgrade from V327 to V328
454 */
455 static BOOL H_UpgradeFromV327(int currVersion, int newVersion)
456 {
457 if (!CreateConfigParam(_T("ResolveDNSToIPOnStatusPoll"), _T("0"), 1, 1))
458 if (!g_bIgnoreErrors)
459 return FALSE;
460
461 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='328' WHERE var_name='SchemaVersion'")));
462 return TRUE;
463 }
464
465 /**
466 * Upgrade from V326 to V327
467 */
468 static BOOL H_UpgradeFromV326(int currVersion, int newVersion)
469 {
470 CHK_EXEC(DropPrimaryKey(_T("network_map_links")));
471 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_network_map_links_map_id ON network_map_links(map_id)")));
472 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='327' WHERE var_name='SchemaVersion'")));
473 return TRUE;
474 }
475
476 /**
477 * Upgrade from V325 to V326
478 */
479 static BOOL H_UpgradeFromV325(int currVersion, int newVersion)
480 {
481 static TCHAR batch[] =
482 _T("ALTER TABLE network_map_links DROP COLUMN color\n")
483 _T("ALTER TABLE network_map_links DROP COLUMN status_object\n")
484 _T("ALTER TABLE network_map_links DROP COLUMN routing\n")
485 _T("ALTER TABLE network_map_links DROP COLUMN bend_points\n")
486 _T("<END>");
487 CHK_EXEC(SQLBatch(batch));
488
489 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='326' WHERE var_name='SchemaVersion'")));
490 return TRUE;
491 }
492
493 /**
494 * Upgrade from V324 to V325
495 */
496 static BOOL H_UpgradeFromV324(int currVersion, int newVersion)
497 {
498 //move map link configuration to xml
499
500 DB_RESULT hResult = SQLSelect(_T("SELECT map_id, element1, element2, element_data, color, status_object, routing, bend_points FROM network_map_links"));
501 if (hResult != NULL)
502 {
503 int count = DBGetNumRows(hResult);
504 for(int i = 0; i < count; i++)
505 {
506 TCHAR *config = DBGetField(hResult, i, 3, NULL, 0);
507 if (config == NULL)
508 config = _tcsdup(_T(""));
509 UINT32 color = DBGetFieldULong(hResult, i, 4);
510 UINT32 statusObject = DBGetFieldULong(hResult, i, 5);
511 UINT32 routing = DBGetFieldULong(hResult, i, 6);
512 TCHAR bendPoints[1024];
513 DBGetField(hResult, i, 7, bendPoints, 1024);
514
515 TCHAR *newConfig = (TCHAR *)malloc((_tcslen(config) + 4096) * sizeof(TCHAR));
516 _tcscpy(newConfig, _T("<config>"));
517 TCHAR* c1 = _tcsstr(config, _T("<dciList"));
518 TCHAR* c2 = _tcsstr(config, _T("</dciList>"));
519 if(c1 != NULL && c2!= NULL)
520 {
521 *c2 = 0;
522 _tcscat(newConfig, c1);
523 _tcscat(newConfig, _T("</dciList>"));
524 }
525
526 TCHAR tmp[2048];
527 _sntprintf(tmp, 2048, _T("<color>%d</color>"), color),
528 _tcscat(newConfig, tmp);
529
530 if (statusObject != 0)
531 {
532 _sntprintf(tmp, 2048, _T("<objectStatusList length=\"1\"><long>%d</long></objectStatusList>"), statusObject);
533 _tcscat(newConfig, tmp);
534 }
535
536 _sntprintf(tmp, 2048, _T("<routing>%d</routing>"), routing);
537 _tcscat(newConfig, tmp);
538
539 if (routing == 3 && bendPoints[0] != 0)
540 {
541 count = 1;
542 for(int j = 0; j < _tcslen(bendPoints); j++)
543 {
544 if (bendPoints[j] == _T(','))
545 count++;
546 }
547 _sntprintf(tmp, 2048, _T("<bendPoints length=\"%d\">%s</bendPoints>"), count, bendPoints);
548 _tcscat(newConfig, tmp);
549 }
550 _tcscat(newConfig, _T("</config>"));
551
552 safe_free(config);
553 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE network_map_links SET element_data=? WHERE map_id=? AND element1=? AND element2=?"));
554 if (statment != NULL)
555 {
556 DBBind(statment, 1, DB_SQLTYPE_TEXT, newConfig, DB_BIND_STATIC);
557 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
558 DBBind(statment, 3, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 1));
559 DBBind(statment, 4, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 2));
560 CHK_EXEC(DBExecute(statment));
561 DBFreeStatement(statment);
562 }
563 else
564 {
565 if (!g_bIgnoreErrors)
566 return FALSE;
567 }
568 safe_free(newConfig);
569 }
570 DBFreeResult(hResult);
571 }
572
573 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='325' WHERE var_name='SchemaVersion'")));
574 return TRUE;
575 }
576
577 /**
578 * Upgrade from V323 to V324
579 */
580 static BOOL H_UpgradeFromV323(int currVersion, int newVersion)
581 {
582 if (!MetaDataReadInt(_T("ValidTDataIndex"), 0)) // check if schema is already correct
583 {
584 TCHAR query[1024];
585 _sntprintf(query, 1024,
586 _T("UPDATE metadata SET var_value='CREATE TABLE tdata_records_%%d (record_id %s not null,row_id %s not null,instance varchar(255) null,PRIMARY KEY(row_id),FOREIGN KEY (record_id) REFERENCES tdata_%%d(record_id) ON DELETE CASCADE)' WHERE var_name='TDataTableCreationCommand_1'"),
587 g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64], g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64]);
588 CHK_EXEC(SQLQuery(query));
589
590 RecreateTData(_T("nodes"), true, true);
591 RecreateTData(_T("clusters"), true, true);
592 RecreateTData(_T("mobile_devices"), true, true);
593 }
594
595 CHK_EXEC(SQLQuery(_T("DELETE FROM metadata WHERE var_name='ValidTDataIndex'")));
596 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='324' WHERE var_name='SchemaVersion'")));
597 return TRUE;
598 }
599
600 /**
601 * Upgrade from V322 to V323
602 */
603 static BOOL H_UpgradeFromV322(int currVersion, int newVersion)
604 {
605 CHK_EXEC(CreateConfigParam(_T("ProcessTrapsFromUnmanagedNodes"), _T("0"), 1, 1));
606 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='323' WHERE var_name='SchemaVersion'")));
607 return TRUE;
608 }
609
610 /**
611 * Upgrade from V321 to V322
612 */
613 static BOOL H_UpgradeFromV321(int currVersion, int newVersion)
614 {
615 switch(g_dbSyntax)
616 {
617 case DB_SYNTAX_DB2:
618 CHK_EXEC(SQLBatch(
619 _T("ALTER TABLE users ALTER COLUMN system_access SET DATA TYPE $SQL:INT64\n")
620 _T("ALTER TABLE user_groups ALTER COLUMN system_access SET DATA TYPE $SQL:INT64\n")
621 _T("<END>")));
622 break;
623 case DB_SYNTAX_MSSQL:
624 CHK_EXEC(SQLBatch(
625 _T("ALTER TABLE users ALTER COLUMN system_access $SQL:INT64\n")
626 _T("ALTER TABLE user_groups ALTER COLUMN system_access $SQL:INT64\n")
627 _T("<END>")));
628 break;
629 case DB_SYNTAX_PGSQL:
630 CHK_EXEC(SQLBatch(
631 _T("ALTER TABLE users ALTER COLUMN system_access TYPE $SQL:INT64\n")
632 _T("ALTER TABLE user_groups ALTER COLUMN system_access TYPE $SQL:INT64\n")
633 _T("<END>")));
634 break;
635 case DB_SYNTAX_SQLITE:
636 CHK_EXEC(SQLBatch(
637 _T("CREATE TABLE temp_users AS SELECT * FROM users\n")
638 _T("DROP TABLE users\n")
639 _T("CREATE TABLE users (id integer not null, guid varchar(36) not null, name varchar(63) not null, password varchar(48) not null, system_access $SQL:INT64 not null, flags integer not null,")
640 _T(" full_name varchar(127) null, description varchar(255) null, grace_logins integer not null, auth_method integer not null, cert_mapping_method integer not null, cert_mapping_data $SQL:TEXT null,")
641 _T(" auth_failures integer not null, last_passwd_change integer not null, min_passwd_length integer not null, disabled_until integer not null, last_login integer not null, password_history $SQL:TEXT null,")
642 _T(" xmpp_id varchar(127) null, ldap_dn $SQL:TEXT null, PRIMARY KEY(id))\n")
643 _T("INSERT INTO users SELECT * FROM temp_users\n")
644 _T("DROP TABLE temp_users\n")
645 _T("CREATE TABLE temp_user_groups AS SELECT * FROM user_groups\n")
646 _T("DROP TABLE user_groups\n")
647 _T("CREATE TABLE user_groups (id integer not null, guid varchar(36) not null, name varchar(63) not null, system_access $SQL:INT64 not null, flags integer not null,")
648 _T(" description varchar(255) not null, ldap_dn $SQL:TEXT null, PRIMARY KEY(id))\n")
649 _T("INSERT INTO user_groups SELECT * FROM temp_user_groups\n")
650 _T("DROP TABLE temp_user_groups\n")
651 _T("<END>")));
652 break;
653 case DB_SYNTAX_ORACLE:
654 // no changes needed
655 break;
656 default:
657 CHK_EXEC(SQLBatch(
658 _T("ALTER TABLE users MODIFY system_access $SQL:INT64\n")
659 _T("ALTER TABLE user_groups MODIFY system_access $SQL:INT64\n")
660 _T("<END>")));
661 break;
662 }
663
664 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='322' WHERE var_name='SchemaVersion'")));
665 return TRUE;
666 }
667
668 /**
669 * Upgrade from V320 to V321
670 */
671 static BOOL H_UpgradeFromV320(int currVersion, int newVersion)
672 {
673 static TCHAR batch[] =
674 _T("ALTER TABLE object_tools ADD command_short_name varchar(31)\n")
675 _T("UPDATE object_tools SET command_short_name='Shutdown' WHERE tool_id=1\n")
676 _T("UPDATE object_tools SET command_short_name='Restart' WHERE tool_id=2\n")
677 _T("UPDATE object_tools SET command_short_name='Wakeup' WHERE tool_id=3\n")
678 _T("<END>");
679 CHK_EXEC(SQLBatch(batch));
680 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='321' WHERE var_name='SchemaVersion'")));
681 return TRUE;
682 }
683
684 /**
685 * Upgrade from V319 to V320
686 */
687 static BOOL H_UpgradeFromV319(int currVersion, int newVersion)
688 {
689 CHK_EXEC(CreateConfigParam(_T("LdapConnectionString"), _T("ldap://localhost:389"), 1, 0));
690 CHK_EXEC(CreateConfigParam(_T("LdapSyncUser"), _T(""), 1, 0));
691 CHK_EXEC(CreateConfigParam(_T("LdapSyncUserPassword"), _T(""), 1, 0));
692 CHK_EXEC(CreateConfigParam(_T("LdapSearchBase"), _T(""), 1, 0));
693 CHK_EXEC(CreateConfigParam(_T("LdapSearchFilter"), _T(""), 1, 0));
694 CHK_EXEC(CreateConfigParam(_T("LdapUserDeleteAction"), _T("1"), 1, 0));
695 CHK_EXEC(CreateConfigParam(_T("LdapMappingName"), _T("uid"), 1, 0));
696 CHK_EXEC(CreateConfigParam(_T("LdapMappingFullName"), _T("displayName"), 1, 0));
697 CHK_EXEC(CreateConfigParam(_T("LdapMappingDescription"), _T(""), 1, 0));
698 CHK_EXEC(CreateConfigParam(_T("LdapGroupClass"), _T(""), 1, 0));
699 CHK_EXEC(CreateConfigParam(_T("LdapUserClass"), _T(""), 1, 0));
700 CHK_EXEC(CreateConfigParam(_T("LdapSyncInterval"), _T("0"), 1, 0));
701
702 static TCHAR batch[] =
703 _T("ALTER TABLE users ADD ldap_dn $SQL:TEXT\n")
704 _T("ALTER TABLE user_groups ADD ldap_dn $SQL:TEXT\n")
705 _T("<END>");
706 CHK_EXEC(SQLBatch(batch));
707
708 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='320' WHERE var_name='SchemaVersion'")));
709 return TRUE;
710 }
711
712 /*
713 * Upgrade from V318 to V319
714 */
715 static BOOL H_UpgradeFromV318(int currVersion, int newVersion)
716 {
717 static TCHAR batch[] =
718 _T("ALTER TABLE object_tools ADD icon $SQL:TEXT\n")
719 _T("ALTER TABLE object_tools ADD command_name varchar(255)\n")
720 _T("UPDATE object_tools SET flags=74,command_name='Shutdown system',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c086488000002bf49444154388d95933f689c7518c73fbf3f7779efaede995c137369d54123ba4843070b1204c10ed24db182939b90c1c1e2eee05871e8d0d2b98a8ba0c52ee2d0cb622acd99222dd1084d9a18cd3597f7bd6bdebbdffbfbe7701d82e8e0071ebec303dfe7e1e1fb883fea7542aba54c080dac153c261ed1a30540b51a8f359bd9c9e565afedd4943ab6b4f4d56873f38d5014ff6af04f95950aaad5fa7e7d7dfdbcee1d1c3cd95d59395b5a5c7c82ffc1fd4ee7acc9f349fd683090b1db15499ae2013b3f8f1c0e296f6f539c380140796707333747a856296d6ca081d1e1a138cc73a95d8cc28f468834459f3ecd7367cee0b38ccd7bf7787e711180dfaf5ee599850544a3c1760898d5556c51e06314d2c5288be150186b995d58404bc9eef5ebb87e86140229257690b17be33b4a4a3173ea14236b71d60a17a3901684b59652b34952ab31dcda6470f76794c9b0b6c0160665320eefae317ab04552ad529e9ec6c78003292dc861bf2f4408e369fb7b948a8cb2cd7085c115868998936887eb75514a617a3db66eb68505211d30f86b97dde536420844a341b17e8bf8db0a21ed12d23ddcda0ff46f7e4dac24482939b8b386b3060f4207206a457afb16be9f519f7f91f22baf52f9e91bfca7ef00829a4fb1af9fa3fed2cbf8419f6c75054a0a0fc800a025f151cafdcb17514af3ecc79f939fbf40d69c259d9ca1ffd687cc7d7411a5145b573e230e52d0120f68ffd8400ad8b97685c9934f31f9ee07b4de5e227ff37d8c311c4f12aad50afb5f5c62e7da65a400519204408f37108408de471e5cfa04fbe3b74c9d7b8ff2d32f1042805f7e25bdf1257fdeee103c8408528d53afa356c85a42b107d6812920bdd3c16f7448cae3d81a0b837cdc2b1c380f724203445d8ff161767cb66df1afe5380a0d3d05ca8d0f148110c02bb035b013109b1a17747b06baa20d3c84897dc93420feeb0b8f22203603dd19307f037f0665861328b32e0000000049454e44ae426082' WHERE tool_id=1\n")
721 _T("UPDATE object_tools SET flags=74,command_name='Restart system',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c0864880000029849444154388d85934d8b1c5514869f73eeadaaae6ea77bda388999882241fc5c0e09ae4484c4857b4177fa0b5474256edcfa075cbacbc23f6074a12332a0a2200a2189e8c468a23d93e99e9eaeaaae7beb1e1733f9902c7ce06c0ebc2f2fe7f0ca5fe311fdd71e77d93332922e08b749dcc5fe3bc9f72d5d1fcf861f7dd6f9f295353778ebdd0b71ff977374ad60f7888e1003bbb3379ceb930f4e7fbe7be5a7573da7f65697db17cf2ff2e757fe6e06a00e1141040aab59ebb5dc47076efbdb739cacc63edc9aabee4f64796248efb10dbcf738e750556268e97eff14b937ce6d1607126e55eae33c4956d5f4f72f21ae40d6cfe0bc4755c9f39cd4ccc0d27d7a695ae23c896fe7a645d5482c26340f0d19e639beb9811463c85788610a29c1d11d4284cc416a82848589861a49754bab390fac3f4ba69174f963ba7040d745249f4136033f63efd859769f78933a792c244265ea436d9a9a99e86895bc28b0e90fc03632bd88e463acb787580696f3e0fa299ade09e275a5feed2b09b5898f4ba35bdc40bb6b0034f5357cda4277bec354400d0b0d5d75406a5e42caa751d90596c425e22d00aa48771933a3c99e6230a8d1b241dcd1eb03a4a2c4563600f07615bc622da80510149aefa1b982ef3dc24d7d071b7afc71f0c781d58c83d107e48347d1f62a1a7f44f4d0c0130c4c4196d89fefa1273f215f7d9d4b379fa4dfdf22cb32bc7f99b5f533c45893edbc4fe75a8c0116c05b008b408434fd9a327f031b7d08c73670ee2c65595296259afe20fbe76de2fc9ba39e1cd6c6a7e4b0ae87d5200a4cbea4acce3318bd8865cfe1a283dd9fe9a65f901615a982d400c96360be9eda4ebbfdf0a6ec752f741ac11f1a89db02d9ba5bc8d483ae877587e2f0abdfac2b26b209488fa218b07627d7ff636dc524d52cff0513e53f37235ac3190000000049454e44ae426082' WHERE tool_id=2\n")
722 _T("UPDATE object_tools SET flags=64,command_name='Wakeup node using Wake-On-LAN',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c086488000000097048597300000dd700000dd70142289b780000001974455874536f667477617265007777772e696e6b73636170652e6f72679bee3c1a0000023649444154388d8d924f48545114c67ff7bd37ff7cf9071bc70a4d47271ca15c848d448185b40a89204890362d8568eb2270d7a2762d5cb7711504d1ae10c195218895a488528e4e06a653d338de7bdfbcf75ac84ce38c901f9ccdb9e7fbce39f77c627ce6872df6dd71f01f781e1d9c00866003215efaf99de7d6763afb1078721262053a800908ed5a5aa9b1e3bb0802a600c0717d3cdf3fae6cccd24a25abb302a80b990c265a009859d941299763249296d6b2a6732468d25a1f24156f00e0cbd62e9b5a71a0dd9a490cad14a570b4266c780cf546797cab1b1317139747435ddcec69266c78385a53c9b1b45265b548d022d51563f45a9c778b69ce35850058de928c0cb4933fd04c7ffece812e9639e5158480865098ebc9181fbfeef07a6e9dc68805c0af8243f45480ab174e33bb9426e7484a9b942710020c3b40e24c236f3facb1bd9b634d3a00d8e100ab992cb7af7421bc225aa9b280a195a414524972054d5f679488e5a394442949d8f4b8d4d14caea09115f55a490cad155a2b9452ecfdcef37e619ddef6287706ba89c76ce2319be1fe4e926d51663e6d90cdeda3d42147ebaa4fcc161da6a61739df52cfe88d8b0ca712f8be871d0e31bb94666a7a916c2e8feb7aff3cd33ef2f4c8612dd3a0a5d1a6bfa78d544f1bbeef33bf9a617e65939fb902c50a328068bd3bb10c1c71a3210401cb24143cbc82d2459c62ad8980154b2b3909bca87e91c09fea642d26ad67f7fb32afe6bebd5958dd1c2c48ddf45f8a10d87591bdcb89b3b3f7063a337f01f30f1c1c580292640000000049454e44ae426082' WHERE tool_id=3\n")
723 _T("<END>");
724 CHK_EXEC(SQLBatch(batch));
725 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='319' WHERE var_name='SchemaVersion'")));
726 return TRUE;
727 }
728
729 /**
730 * Upgrade from V317 to V318
731 */
732 static BOOL H_UpgradeFromV317(int currVersion, int newVersion)
733 {
734 CHK_EXEC(CreateEventTemplate(EVENT_AP_DOWN, _T("SYS_AP_DOWN"), SEVERITY_CRITICAL, EF_LOG,
735 _T("Access point %2 changed state to DOWN"),
736 _T("Generated when access point state changes to DOWN.\r\n")
737 _T("Parameters:\r\n")
738 _T(" 1) Access point object ID\r\n")
739 _T(" 2) Access point name\r\n")
740 _T(" 3) Access point MAC address\r\n")
741 _T(" 4) Access point IP address\r\n")
742 _T(" 5) Access point vendor name\r\n")
743 _T(" 6) Access point model\r\n")
744 _T(" 7) Access point serial number")));
745
746 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='318' WHERE var_name='SchemaVersion'")));
747 return TRUE;
748 }
749
750 /**
751 * Upgrade from V316 to V317
752 */
753 static BOOL H_UpgradeFromV316(int currVersion, int newVersion)
754 {
755 CHK_EXEC(CreateConfigParam(_T("MinViewRefreshInterval"), _T("1000"), 1, 0));
756 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='317' WHERE var_name='SchemaVersion'")));
757 return TRUE;
758 }
759
760 /**
761 * Upgrade from V315 to V316
762 */
763 static BOOL H_UpgradeFromV315(int currVersion, int newVersion)
764 {
765 static TCHAR batch[] =
766 _T("ALTER TABLE access_points ADD ap_state integer\n")
767 _T("UPDATE access_points SET ap_state=0\n")
768 _T("<END>");
769 CHK_EXEC(SQLBatch(batch));
770
771 CHK_EXEC(CreateEventTemplate(EVENT_AP_ADOPTED, _T("SYS_AP_ADOPTED"), SEVERITY_NORMAL, EF_LOG,
772 _T("Access point %2 changed state to ADOPTED"),
773 _T("Generated when access point state changes to ADOPTED.\r\n")
774 _T("Parameters:\r\n")
775 _T(" 1) Access point object ID\r\n")
776 _T(" 2) Access point name\r\n")
777 _T(" 3) Access point MAC address\r\n")
778 _T(" 4) Access point IP address\r\n")
779 _T(" 5) Access point vendor name\r\n")
780 _T(" 6) Access point model\r\n")
781 _T(" 7) Access point serial number")));
782
783 CHK_EXEC(CreateEventTemplate(EVENT_AP_UNADOPTED, _T("SYS_AP_UNADOPTED"), SEVERITY_MAJOR, EF_LOG,
784 _T("Access point %2 changed state to UNADOPTED"),
785 _T("Generated when access point state changes to UNADOPTED.\r\n")
786 _T("Parameters:\r\n")
787 _T(" 1) Access point object ID\r\n")
788 _T(" 2) Access point name\r\n")
789 _T(" 3) Access point MAC address\r\n")
790 _T(" 4) Access point IP address\r\n")
791 _T(" 5) Access point vendor name\r\n")
792 _T(" 6) Access point model\r\n")
793 _T(" 7) Access point serial number")));
794
795 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='316' WHERE var_name='SchemaVersion'")));
796 return TRUE;
797 }
798
799 /**
800 * Upgrade from V314 to V315
801 */
802 static BOOL H_UpgradeFromV314(int currVersion, int newVersion)
803 {
804 static TCHAR batch[] =
805 _T("ALTER TABLE thresholds ADD match_count integer\n")
806 _T("UPDATE thresholds SET match_count=0 WHERE current_state=0\n")
807 _T("UPDATE thresholds SET match_count=1 WHERE current_state<>0\n")
808 _T("<END>");
809 CHK_EXEC(SQLBatch(batch));
810 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='315' WHERE var_name='SchemaVersion'")));
811 return TRUE;
812 }
813
814 /**
815 * Upgrade from V313 to V314
816 */
817 static BOOL H_UpgradeFromV313(int currVersion, int newVersion)
818 {
819 // Replace double backslash with single backslash in all "file download" (code 7) object tools
820 DB_RESULT hResult = SQLSelect(_T("SELECT tool_id, tool_data FROM object_tools WHERE tool_type=7"));
821 if (hResult != NULL)
822 {
823 int count = DBGetNumRows(hResult);
824 for(int i = 0; i < count; i++)
825 {
826 TCHAR* toolData = DBGetField(hResult, i, 1, NULL, 0);
827 TranslateStr(toolData, _T("\\\\"), _T("\\"));
828
829 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE object_tools SET tool_data=? WHERE tool_id=?"));
830 if (statment == NULL)
831 return FALSE;
832 DBBind(statment, 1, DB_SQLTYPE_TEXT, toolData, DB_BIND_DYNAMIC);
833 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
834 if(!DBExecute(statment))
835 return FALSE;
836 DBFreeStatement(statment);
837 }
838 DBFreeResult(hResult);
839 }
840
841 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='314' WHERE var_name='SchemaVersion'")));
842 return TRUE;
843 }
844
845 /**
846 * Upgrade from V312 to V313
847 */
848 static BOOL H_UpgradeFromV312(int currVersion, int newVersion)
849 {
850 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("tool_name")));
851 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("tool_data")));
852 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("description")));
853 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("confirmation_text")));
854 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("matching_oid")));
855 CHK_EXEC(SetColumnNullable(_T("object_tools_table_columns"), _T("col_name")));
856 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("tool_name")));
857 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("tool_data")));
858 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("description")));
859 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("confirmation_text")));
860 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("matching_oid")));
861 CHK_EXEC(ConvertStrings(_T("object_tools_table_columns"), _T("tool_id"), _T("col_number"), _T("col_name"), false));
862 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='313' WHERE var_name='SchemaVersion'")));
863 return TRUE;
864 }
865
866 /**
867 * Upgrade from V311 to V312
868 */
869 static BOOL H_UpgradeFromV311(int currVersion, int newVersion)
870 {
871 CHK_EXEC(CreateConfigParam(_T("EnableReportingServer"), _T("0"), 1, 1));
872 CHK_EXEC(CreateConfigParam(_T("ReportingServerHostname"), _T("localhost"), 1, 1));
873 CHK_EXEC(CreateConfigParam(_T("ReportingServerPort"), _T("4710"), 1, 1));
874 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='312' WHERE var_name='SchemaVersion'")));
875 return TRUE;
876 }
877
878 /**
879 * Upgrade from V310 to V311
880 */
881 static BOOL H_UpgradeFromV310(int currVersion, int newVersion)
882 {
883 IntegerArray<UINT32> deleteList;
884
885 // reports
886 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM reports"));
887 if (hResult != NULL)
888 {
889 int count = DBGetNumRows(hResult);
890 for(int i = 0; i < count; i++)
891 deleteList.add(DBGetFieldULong(hResult, i, 0));
892 DBFreeResult(hResult);
893 }
894
895 // report groups
896 hResult = SQLSelect(_T("SELECT id FROM containers WHERE object_class=25"));
897 if (hResult != NULL)
898 {
899 int count = DBGetNumRows(hResult);
900 for(int i = 0; i < count; i++)
901 deleteList.add(DBGetFieldULong(hResult, i, 0));
902 DBFreeResult(hResult);
903 }
904
905 for(int i = 0; i < deleteList.size(); i++)
906 {
907 TCHAR query[256];
908
909 _sntprintf(query, 256, _T("DELETE FROM object_properties WHERE object_id=%d"), deleteList.get(i));
910 CHK_EXEC(SQLQuery(query));
911
912 _sntprintf(query, 256, _T("DELETE FROM object_custom_attributes WHERE object_id=%d"), deleteList.get(i));
913 CHK_EXEC(SQLQuery(query));
914
915 _sntprintf(query, 256, _T("DELETE FROM acl WHERE object_id=%d"), deleteList.get(i));
916 CHK_EXEC(SQLQuery(query));
917
918 _sntprintf(query, 256, _T("DELETE FROM container_members WHERE object_id=%d OR container_id=%d"), deleteList.get(i), deleteList.get(i));
919 CHK_EXEC(SQLQuery(query));
920 }
921
922 static TCHAR batch[] =
923 _T("DROP TABLE reports\n")
924 _T("DROP TABLE report_results\n")
925 _T("DELETE FROM containers WHERE object_class=25\n")
926 _T("DELETE FROM object_properties WHERE object_id=8\n")
927 _T("<END>");
928 CHK_EXEC(SQLBatch(batch));
929 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='311' WHERE var_name='SchemaVersion'")));
930 return TRUE;
931 }
932
933 /**
934 * Upgrade from V309 to V310
935 */
936 static BOOL H_UpgradeFromV309(int currVersion, int newVersion)
937 {
938 static TCHAR batch[] =
939 _T("ALTER TABLE interfaces ADD peer_proto integer\n")
940 _T("UPDATE interfaces SET peer_proto=0\n")
941 _T("<END>");
942 CHK_EXEC(SQLBatch(batch));
943 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='310' WHERE var_name='SchemaVersion'")));
944 return TRUE;
945 }
946
947 /**
948 * Upgrade from V308 to V309
949 */
950 static BOOL H_UpgradeFromV308(int currVersion, int newVersion)
951 {
952 CHK_EXEC(CreateConfigParam(_T("HelpDeskLink"), _T("none"), 1, 1));
953 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='309' WHERE var_name='SchemaVersion'")));
954 return TRUE;
955 }
956
957 /**
958 * Upgrade from V307 to V308
959 */
960 static BOOL H_UpgradeFromV307(int currVersion, int newVersion)
961 {
962 static TCHAR batch[] =
963 _T("ALTER TABLE network_map_elements ADD flags integer\n")
964 _T("UPDATE network_map_elements SET flags=0\n") //set all elements like manually generated
965 _T("ALTER TABLE network_map_links ADD element_data $SQL:TEXT\n")
966 _T("ALTER TABLE network_map_links ADD flags integer\n")
967 _T("UPDATE network_map_links SET flags=0\n") //set all elements like manually generated
968 _T("<END>");
969 CHK_EXEC(SQLBatch(batch));
970
971 // it is assumed that now all autogenerated maps contain only autogenerated objects and links
972 // get elements from autogenerated maps and set their flags to AUTO_GENERATED for map elements and map links
973 TCHAR query[256];
974 _sntprintf(query, 256, _T("SELECT id FROM network_maps WHERE map_type=%d OR map_type=%d"),
975 MAP_TYPE_LAYER2_TOPOLOGY, MAP_TYPE_IP_TOPOLOGY);
976 DB_RESULT hResult = SQLSelect(query);
977 if (hResult != NULL)
978 {
979 int count = DBGetNumRows(hResult);
980 for(int i = 0; i < count; i++)
981 {
982 _sntprintf(query, 256, _T("UPDATE network_map_elements SET flags='1' WHERE map_id=%d"),
983 DBGetFieldULong(hResult, i, 0));
984 CHK_EXEC(SQLQuery(query));
985 _sntprintf(query, 256, _T("UPDATE network_map_links SET flags='1' WHERE map_id=%d"),
986 DBGetFieldULong(hResult, i, 0));
987 CHK_EXEC(SQLQuery(query));
988 }
989 DBFreeResult(hResult);
990 }
991
992 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='308' WHERE var_name='SchemaVersion'")));
993 return TRUE;
994 }
995
996 /**
997 * Upgrade from V306 to V307
998 */
999 static BOOL H_UpgradeFromV306(int currVersion, int newVersion)
1000 {
1001 CHK_EXEC(SetColumnNullable(_T("config_clob"), _T("var_value")));
1002 CHK_EXEC(ConvertStrings(_T("config_clob"), _T("var_name"), NULL, _T("var_value"), true));
1003 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='307' WHERE var_name='SchemaVersion'")));
1004 return TRUE;
1005 }
1006
1007 /**
1008 * Upgrade from V305 to V306
1009 */
1010 static BOOL H_UpgradeFromV305(int currVersion, int newVersion)
1011 {
1012 CHK_EXEC(CreateConfigParam(_T("ExtendedLogQueryAccessControl"), _T("0"), 1, 0));
1013 CHK_EXEC(CreateConfigParam(_T("EnableTimedAlarmAck"), _T("1"), 1, 1));
1014 CHK_EXEC(CreateConfigParam(_T("EnableCheckPointSNMP"), _T("0"), 1, 0));
1015 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='306' WHERE var_name='SchemaVersion'")));
1016 return TRUE;
1017 }
1018
1019 /**
1020 * Upgrade from V304 to V305
1021 */
1022 static BOOL H_UpgradeFromV304(int currVersion, int newVersion)
1023 {
1024 CHK_EXEC(CreateEventTemplate(EVENT_IF_PEER_CHANGED, _T("SYS_IF_PEER_CHANGED"), SEVERITY_NORMAL, EF_LOG,
1025 _T("New peer for interface %3 is %7 interface %10 (%12)"),
1026 _T("Generated when peer information for interface changes.\r\n")
1027 _T("Parameters:\r\n")
1028 _T(" 1) Local interface object ID\r\n")
1029 _T(" 2) Local interface index\r\n")
1030 _T(" 3) Local interface name\r\n")
1031 _T(" 4) Local interface IP address\r\n")
1032 _T(" 5) Local interface MAC address\r\n")
1033 _T(" 6) Peer node object ID\r\n")
1034 _T(" 7) Peer node name\r\n")
1035 _T(" 8) Peer interface object ID\r\n")
1036 _T(" 9) Peer interface index\r\n")
1037 _T(" 10) Peer interface name\r\n")
1038 _T(" 11) Peer interface IP address\r\n")
1039 _T(" 12) Peer interface MAC address")));
1040 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='305' WHERE var_name='SchemaVersion'")));
1041 return TRUE;
1042 }
1043
1044 /**
1045 * Upgrade from V303 to V304
1046 */
1047 static BOOL H_UpgradeFromV303(int currVersion, int newVersion)
1048 {
1049 CHK_EXEC(CreateConfigParam(_T("StrictAlarmStatusFlow"), _T("0"), 1, 0));
1050 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='304' WHERE var_name='SchemaVersion'")));
1051 return TRUE;
1052 }
1053
1054 /**
1055 * Upgrade from V302 to V303
1056 */
1057 static BOOL H_UpgradeFromV302(int currVersion, int newVersion)
1058 {
1059 static TCHAR batch[] =
1060 _T("ALTER TABLE alarms ADD ack_timeout integer\n")
1061 _T("UPDATE alarms SET ack_timeout='0'\n")
1062 _T("<END>");
1063 CHK_EXEC(SQLBatch(batch));
1064
1065 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='303' WHERE var_name='SchemaVersion'")));
1066 return TRUE;
1067 }
1068
1069 /**
1070 * Upgrade from V301 to V302
1071 */
1072 static BOOL H_UpgradeFromV301(int currVersion, int newVersion)
1073 {
1074 static TCHAR batch[] =
1075 _T("DELETE FROM config WHERE var_name='DisableVacuum'\n")
1076 _T("<END>");
1077 CHK_EXEC(SQLBatch(batch));
1078
1079 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='302' WHERE var_name='SchemaVersion'")));
1080 return TRUE;
1081 }
1082
1083 /**
1084 * Upgrade from V300 to V301
1085 */
1086 static BOOL H_UpgradeFromV300(int currVersion, int newVersion)
1087 {
1088 static TCHAR batch[] =
1089 _T("ALTER TABLE thresholds ADD script $SQL:TEXT\n")
1090 _T("ALTER TABLE thresholds ADD sample_count integer\n")
1091 _T("UPDATE thresholds SET sample_count=parameter_1\n")
1092 _T("ALTER TABLE thresholds DROP COLUMN parameter_1\n")
1093 _T("ALTER TABLE thresholds DROP COLUMN parameter_2\n")
1094 _T("<END>");
1095 CHK_EXEC(SQLBatch(batch));
1096
1097 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='301' WHERE var_name='SchemaVersion'")));
1098 return TRUE;
1099 }
1100
1101 /**
1102 * Upgrade from V299 to V300
1103 */
1104 static BOOL H_UpgradeFromV299(int currVersion, int newVersion)
1105 {
1106 CHK_EXEC(CreateConfigParam(_T("EnableXMPPConnector"), _T("0"), 1, 1));
1107 CHK_EXEC(CreateConfigParam(_T("XMPPLogin"), _T("netxms@localhost"), 1, 1));
1108 CHK_EXEC(CreateConfigParam(_T("XMPPPassword"), _T("netxms"), 1, 1));
1109 CHK_EXEC(CreateConfigParam(_T("XMPPServer"), _T("localhost"), 1, 1));
1110 CHK_EXEC(CreateConfigParam(_T("XMPPPort"), _T("5222"), 1, 1));
1111
1112 SetColumnNullable(_T("users"), _T("full_name"));
1113 SetColumnNullable(_T("users"), _T("description"));
1114 SetColumnNullable(_T("users"), _T("cert_mapping_data"));
1115 SetColumnNullable(_T("user_groups"), _T("description"));
1116 SetColumnNullable(_T("userdb_custom_attributes"), _T("attr_value"));
1117
1118 ConvertStrings(_T("users"), _T("id"), _T("full_name"));
1119 ConvertStrings(_T("users"), _T("id"), _T("description"));
1120 ConvertStrings(_T("users"), _T("id"), _T("cert_mapping_data"));
1121 ConvertStrings(_T("user_groups"), _T("id"), _T("description"));
1122 ConvertStrings(_T("userdb_custom_attributes"), _T("object_id"), _T("attr_name"), _T("attr_name"), true);
1123 ConvertStrings(_T("userdb_custom_attributes"), _T("object_id"), _T("attr_name"), _T("attr_value"), true);
1124
1125 CHK_EXEC(SQLQuery(_T("ALTER TABLE users ADD xmpp_id varchar(127)")));
1126
1127 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='300' WHERE var_name='SchemaVersion'")));
1128 return TRUE;
1129 }
1130
1131 /**
1132 * Upgrade from V298 to V299
1133 */
1134 static BOOL H_UpgradeFromV298(int currVersion, int newVersion)
1135 {
1136 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET message='Subnet %2 added',description='")
1137 _T("Generated when subnet object added to the database.\r\n")
1138 _T("Parameters:\r\n")
1139 _T(" 1) Subnet object ID\r\n")
1140 _T(" 2) Subnet name\r\n")
1141 _T(" 3) IP address\r\n")
1142 _T(" 4) Network mask")
1143 _T("' WHERE event_code=2")));
1144 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET message='Subnet %2 deleted',description='")
1145 _T("Generated when subnet object deleted from the database.\r\n")
1146 _T("Parameters:\r\n")
1147 _T(" 1) Subnet object ID\r\n")
1148 _T(" 2) Subnet name\r\n")
1149 _T(" 3) IP address\r\n")
1150 _T(" 4) Network mask")
1151 _T("' WHERE event_code=19")));
1152 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='299' WHERE var_name='SchemaVersion'")));
1153 return TRUE;
1154 }
1155
1156 /**
1157 * Upgrade from V297 to V298
1158 */
1159 static BOOL H_UpgradeFromV297(int currVersion, int newVersion)
1160 {
1161 CHK_EXEC(CreateConfigParam(_T("AgentDefaultSharedSecret"), _T("netxms"), 1, 0));
1162 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='298' WHERE var_name='SchemaVersion'")));
1163 return TRUE;
1164 }
1165
1166 /**
1167 * Upgrade from V296 to V297
1168 */
1169 static BOOL H_UpgradeFromV296(int currVersion, int newVersion)
1170 {
1171 CHK_EXEC(CreateConfigParam(_T("UseSNMPTrapsForDiscovery"), _T("0"), 1, 1));
1172 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='297' WHERE var_name='SchemaVersion'")));
1173 return TRUE;
1174 }
1175
1176 /**
1177 * Upgrade from V295 to V296
1178 */
1179 static BOOL H_UpgradeFromV295(int currVersion, int newVersion)
1180 {
1181 static TCHAR batch[] =
1182 _T("ALTER TABLE nodes ADD boot_time integer\n")
1183 _T("UPDATE nodes SET boot_time=0\n")
1184 _T("<END>");
1185 CHK_EXEC(SQLBatch(batch));
1186
1187 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='296' WHERE var_name='SchemaVersion'")));
1188 return TRUE;
1189 }
1190
1191 /**
1192 * Upgrade from V294 to V295
1193 */
1194 static BOOL H_UpgradeFromV294(int currVersion, int newVersion)
1195 {
1196 CHK_EXEC(CreateConfigParam(_T("IcmpPingTimeout"), _T("1500"), 1, 1));
1197 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='295' WHERE var_name='SchemaVersion'")));
1198 return TRUE;
1199 }
1200
1201 /**
1202 * Upgrade from V293 to V294
1203 */
1204 static BOOL H_UpgradeFromV293(int currVersion, int newVersion)
1205 {
1206 static TCHAR batch[] =
1207 _T("DELETE FROM metadata WHERE var_name LIKE 'TDataTableCreationCommand_%'\n")
1208 _T("INSERT INTO metadata (var_name,var_value)")
1209 _T(" VALUES ('TDataTableCreationCommand_0','CREATE TABLE tdata_%d (item_id integer not null,tdata_timestamp integer not null,record_id $SQL:INT64 not null,UNIQUE(record_id))')\n")
1210 _T("INSERT INTO metadata (var_name,var_value)")
1211 _T(" VALUES ('TDataTableCreationCommand_1','CREATE TABLE tdata_records_%d (record_id $SQL:INT64 not null,row_id $SQL:INT64 not null,instance varchar(255) null,PRIMARY KEY(row_id),FOREIGN KEY (record_id) REFERENCES tdata_%d(record_id) ON DELETE CASCADE)')\n")
1212 _T("INSERT INTO metadata (var_name,var_value)")
1213 _T(" VALUES ('TDataTableCreationCommand_2','CREATE TABLE tdata_rows_%d (row_id $SQL:INT64 not null,column_id integer not null,value varchar(255) null,PRIMARY KEY(row_id,column_id),FOREIGN KEY (row_id) REFERENCES tdata_records_%d(row_id) ON DELETE CASCADE)')\n")
1214 _T("INSERT INTO metadata (var_name,var_value)")
1215 _T(" VALUES ('TDataIndexCreationCommand_2','CREATE INDEX idx_tdata_rec_%d_id ON tdata_records_%d(record_id)')\n")
1216 _T("<END>");
1217 CHK_EXEC(SQLBatch(batch));
1218
1219 RecreateTData(_T("nodes"), true, false);
1220 RecreateTData(_T("clusters"), true, false);
1221 RecreateTData(_T("mobile_devices"), true, false);
1222
1223 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('ValidTDataIndex','1')")));
1224 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='294' WHERE var_name='SchemaVersion'")));
1225 return TRUE;
1226 }
1227
1228 /**
1229 * Upgrade from V292 to V293
1230 */
1231 static BOOL H_UpgradeFromV292(int currVersion, int newVersion)
1232 {
1233 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleShortTimeFormat"), _T("HH:mm"), 1, 0));
1234 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='293' WHERE var_name='SchemaVersion'")));
1235 return TRUE;
1236 }
1237
1238 /**
1239 * Upgrade from V291 to V292
1240 */
1241 static BOOL H_UpgradeFromV291(int currVersion, int newVersion)
1242 {
1243 CHK_EXEC(SQLQuery(_T("ALTER TABLE event_policy ADD rule_guid varchar(36)")));
1244
1245 // Generate GUIDs for all objects
1246 DB_RESULT hResult = SQLSelect(_T("SELECT rule_id FROM event_policy"));
1247 if (hResult != NULL)
1248 {
1249 int count = DBGetNumRows(hResult);
1250 for(int i = 0; i < count; i++)
1251 {
1252 uuid_t guid;
1253 TCHAR query[256], buffer[64];
1254
1255 uuid_generate(guid);
1256 _sntprintf(query, 256, _T("UPDATE event_policy SET rule_guid='%s' WHERE rule_id=%d"),
1257 uuid_to_string(guid, buffer), DBGetFieldULong(hResult, i, 0));
1258 CHK_EXEC(SQLQuery(query));
1259 }
1260 DBFreeResult(hResult);
1261 }
1262
1263 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='292' WHERE var_name='SchemaVersion'")));
1264 return TRUE;
1265 }
1266
1267 /**
1268 * Upgrade from V290 to V291
1269 */
1270 static BOOL H_UpgradeFromV290(int currVersion, int newVersion)
1271 {
1272 CHK_EXEC(SQLQuery(_T("UPDATE network_services SET service_type=7 WHERE service_type=6")));
1273 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='291' WHERE var_name='SchemaVersion'")));
1274 return TRUE;
1275 }
1276
1277 /**
1278 * Upgrade from V289 to V290
1279 */
1280 static BOOL H_UpgradeFromV289(int currVersion, int newVersion)
1281 {
1282 CHK_EXEC(SQLQuery(_T("ALTER TABLE network_maps ADD filter $SQL:TEXT")));
1283 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='290' WHERE var_name='SchemaVersion'")));
1284 return TRUE;
1285 }
1286
1287 /**
1288 * Upgrade from V288 to V289
1289 */
1290 static BOOL H_UpgradeFromV288(int currVersion, int newVersion)
1291 {
1292 CHK_EXEC(SQLQuery(_T("ALTER TABLE dct_thresholds DROP COLUMN current_state")));
1293 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='289' WHERE var_name='SchemaVersion'")));
1294 return TRUE;
1295 }
1296
1297 /**
1298 * Upgrade from V287 to V288
1299 */
1300 static BOOL H_UpgradeFromV287(int currVersion, int newVersion)
1301 {
1302 CHK_EXEC(CreateEventTemplate(EVENT_TABLE_THRESHOLD_ACTIVATED, _T("SYS_TABLE_THRESHOLD_ACTIVATED"), EVENT_SEVERITY_MINOR, EF_LOG,
1303 _T("Threshold activated on table \"%2\" row %4 (%5)"),
1304 _T("Generated when table threshold is activated.\r\n")
1305 _T("Parameters:\r\n")
1306 _T(" 1) Table DCI name\r\n")
1307 _T(" 2) Table DCI description\r\n")
1308 _T(" 3) Table DCI ID\r\n")
1309 _T(" 4) Table row\r\n")
1310 _T(" 5) Instance")));
1311
1312 CHK_EXEC(CreateEventTemplate(EVENT_TABLE_THRESHOLD_DEACTIVATED, _T("SYS_TABLE_THRESHOLD_DEACTIVATED"), EVENT_SEVERITY_NORMAL, EF_LOG,
1313 _T("Threshold deactivated on table \"%2\" row %4 (%5)"),
1314 _T("Generated when table threshold is deactivated.\r\n")
1315 _T("Parameters:\r\n")
1316 _T(" 1) Table DCI name\r\n")
1317 _T(" 2) Table DCI description\r\n")
1318 _T(" 3) Table DCI ID\r\n")
1319 _T(" 4) Table row\r\n")
1320 _T(" 5) Instance")));
1321
1322 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='288' WHERE var_name='SchemaVersion'")));
1323 return TRUE;
1324 }
1325
1326 /**
1327 * Upgrade from V286 to V287
1328 */
1329 static BOOL H_UpgradeFromV286(int currVersion, int newVersion)
1330 {
1331 static TCHAR batch[] =
1332 _T("ALTER TABLE dc_table_columns ADD sequence_number integer\n")
1333 _T("UPDATE dc_table_columns SET sequence_number=0\n")
1334 _T("<END>");
1335 CHK_EXEC(SQLBatch(batch));
1336
1337 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='287' WHERE var_name='SchemaVersion'")));
1338 return TRUE;
1339 }
1340
1341 /**
1342 * Upgrade from V285 to V286
1343 */
1344 static BOOL H_UpgradeFromV285(int currVersion, int newVersion)
1345 {
1346 CHK_EXEC(CreateTable(
1347 _T("CREATE TABLE dct_thresholds (")
1348 _T("id integer not null,")
1349 _T("table_id integer not null,")
1350 _T("sequence_number integer not null,")
1351 _T("current_state char(1) not null,")
1352 _T("activation_event integer not null,")
1353 _T("deactivation_event integer not null,")
1354 _T("PRIMARY KEY(id))")));
1355
1356 CHK_EXEC(CreateTable(
1357 _T("CREATE TABLE dct_threshold_conditions (")
1358 _T("threshold_id integer not null,")
1359 _T("group_id integer not null,")
1360 _T("sequence_number integer not null,")
1361 _T("column_name varchar(63) null,")
1362 _T("check_operation integer not null,")
1363 _T("check_value varchar(255) null,")
1364 _T("PRIMARY KEY(threshold_id,group_id,sequence_number))")));
1365
1366 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='286' WHERE var_name='SchemaVersion'")));
1367 return TRUE;
1368 }
1369
1370 /**
1371 * Upgrade from V284 to V285
1372 */
1373 static BOOL H_UpgradeFromV284(int currVersion, int newVersion)
1374 {
1375 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_items_node_id ON items(node_id)")));
1376 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_dc_tables_node_id ON dc_tables(node_id)")));
1377
1378 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='285' WHERE var_name='SchemaVersion'")));
1379 return TRUE;
1380 }
1381
1382 /**
1383 * Upgrade from V283 to V284
1384 */
1385 static BOOL H_UpgradeFromV283(int currVersion, int newVersion)
1386 {
1387 CHK_EXEC(CreateConfigParam(_T("SNMPTrapPort"), _T("162"), 1, 1));
1388
1389 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='284' WHERE var_name='SchemaVersion'")));
1390 return TRUE;
1391 }
1392
1393 /**
1394 * Upgrade from V282 to V283
1395 */
1396 static BOOL H_UpgradeFromV282(int currVersion, int newVersion)
1397 {
1398 static TCHAR batch[] =
1399 _T("ALTER TABLE dc_table_columns ADD display_name varchar(255)\n")
1400 _T("UPDATE dc_table_columns SET display_name=column_name\n")
1401 _T("<END>");
1402 CHK_EXEC(SQLBatch(batch));
1403
1404 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='283' WHERE var_name='SchemaVersion'")));
1405 return TRUE;
1406 }
1407
1408 /**
1409 * Upgrade from V281 to V282
1410 */
1411 static BOOL H_UpgradeFromV281(int currVersion, int newVersion)
1412 {
1413 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='WindowsConsoleUpgradeURL'")));
1414 CHK_EXEC(CreateConfigParam(_T("EnableObjectTransactions"), _T("0"), 1, 1));
1415
1416 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='282' WHERE var_name='SchemaVersion'")));
1417 return TRUE;
1418 }
1419
1420 /**
1421 * Upgrade from V280 to V281
1422 */
1423 static BOOL H_UpgradeFromV280(int currVersion, int newVersion)
1424 {
1425 static TCHAR batch[] =
1426 _T("DELETE FROM metadata WHERE var_name='TDataTableCreationCommand'\n")
1427 _T("INSERT INTO metadata (var_name,var_value)")
1428 _T(" VALUES ('TDataTableCreationCommand_0','CREATE TABLE tdata_%d (item_id integer not null,tdata_timestamp integer not null,record_id $SQL:INT64 not null)')\n")
1429 _T("INSERT INTO metadata (var_name,var_value)")
1430 _T(" VALUES ('TDataTableCreationCommand_1','CREATE TABLE tdata_records_%d (record_id $SQL:INT64 not null,row_id $SQL:INT64 not null,instance varchar(255) null,PRIMARY KEY(record_id,row_id))')\n")
1431 _T("INSERT INTO metadata (var_name,var_value)")
1432 _T(" VALUES ('TDataTableCreationCommand_2','CREATE TABLE tdata_rows_%d (row_id $SQL:INT64 not null,column_id integer not null,value varchar(255) null,PRIMARY KEY(row_id,column_id))')\n")
1433 _T("INSERT INTO metadata (var_name,var_value)")
1434 _T(" VALUES ('TDataIndexCreationCommand_1','CREATE INDEX idx_tdata_rec_%d_instance ON tdata_records_%d(instance)')\n")
1435 _T("<END>");
1436 CHK_EXEC(SQLBatch(batch));
1437
1438 RecreateTData(_T("nodes"), false, false);
1439 RecreateTData(_T("clusters"), false, false);
1440 RecreateTData(_T("mobile_devices"), false, false);
1441
1442 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='281' WHERE var_name='SchemaVersion'")));
1443 return TRUE;
1444 }
1445
1446 /**
1447 * Upgrade from V279 to V280
1448 */
1449 static BOOL H_UpgradeFromV279(int currVersion, int newVersion)
1450 {
1451 static TCHAR batch[] =
1452 _T("ALTER TABLE dc_table_columns ADD flags integer\n")
1453 _T("UPDATE dc_table_columns SET flags=data_type\n")
1454 _T("ALTER TABLE dc_table_columns DROP COLUMN data_type\n")
1455 _T("<END>");
1456 CHK_EXEC(SQLBatch(batch));
1457
1458 DB_RESULT hResult = SQLSelect(_T("SELECT item_id,instance_column FROM dc_tables"));
1459 if (hResult != NULL)
1460 {
1461 int count = DBGetNumRows(hResult);
1462 for(int i = 0; i < count; i++)
1463 {
1464 TCHAR columnName[MAX_COLUMN_NAME] = _T("");
1465 DBGetField(hResult, i, 1, columnName, MAX_COLUMN_NAME);
1466 if (columnName[0] != 0)
1467 {
1468 TCHAR query[256];
1469 _sntprintf(query, 256, _T("UPDATE dc_table_columns SET flags=flags+256 WHERE table_id=%d AND column_name=%s"),
1470 DBGetFieldLong(hResult, i, 0), (const TCHAR *)DBPrepareString(g_hCoreDB, columnName));
1471 CHK_EXEC(SQLQuery(query));
1472 }
1473 }
1474 DBFreeResult(hResult);
1475 }
1476 else
1477 {
1478 if (!g_bIgnoreErrors)
1479 return FALSE;
1480 }
1481
1482 CHK_EXEC(SQLQuery(_T("ALTER TABLE dc_tables DROP COLUMN instance_column")));
1483
1484 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='280' WHERE var_name='SchemaVersion'")));
1485 return TRUE;
1486 }
1487
1488 /**
1489 * Upgrade from V278 to V279
1490 */
1491 static BOOL H_UpgradeFromV278(int currVersion, int newVersion)
1492 {
1493 CHK_EXEC(CreateConfigParam(_T("DeleteEventsOfDeletedObject"), _T("1"), 1, 0));
1494 CHK_EXEC(CreateConfigParam(_T("DeleteAlarmsOfDeletedObject"), _T("1"), 1, 0));
1495
1496 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='279' WHERE var_name='SchemaVersion'")));
1497 return TRUE;
1498 }
1499
1500 /**
1501 * Upgrade from V277 to V278
1502 */
1503 static BOOL H_UpgradeFromV277(int currVersion, int newVersion)
1504 {
1505 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM clusters"));
1506 if (hResult != NULL)
1507 {
1508 int count = DBGetNumRows(hResult);
1509 for(int i = 0; i < count; i++)
1510 {
1511 DWORD id = DBGetFieldULong(hResult, i, 0);
1512 if (!CreateIDataTable(id))
1513 {
1514 if (!g_bIgnoreErrors)
1515 {
1516 DBFreeResult(hResult);
1517 return FALSE;
1518 }
1519 }
1520 if (!CreateTDataTable_preV281(id))
1521 {
1522 if (!g_bIgnoreErrors)
1523 {
1524 DBFreeResult(hResult);
1525 return FALSE;
1526 }
1527 }
1528 }
1529 DBFreeResult(hResult);
1530 }
1531 else
1532 {
1533 if (!g_bIgnoreErrors)
1534 return FALSE;
1535 }
1536
1537 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='278' WHERE var_name='SchemaVersion'")));
1538 return TRUE;
1539 }
1540
1541 /**
1542 * Upgrade from V276 to V277
1543 */
1544 static BOOL H_UpgradeFromV276(int currVersion, int newVersion)
1545 {
1546 CHK_EXEC(CreateTable(_T("CREATE TABLE dci_summary_tables (")
1547 _T("id integer not null,")
1548 _T("menu_path varchar(255) not null,")
1549 _T("title varchar(127) null,")
1550 _T("node_filter $SQL:TEXT null,")
1551 _T("flags integer not null,")
1552 _T("columns $SQL:TEXT null,")
1553 _T("PRIMARY KEY(id))")));
1554
1555 CHK_EXEC(CreateConfigParam(_T("DefaultMapBackgroundColor"), _T("0xffffff"), 1, 0));
1556
1557 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='277' WHERE var_name='SchemaVersion'")));
1558 return TRUE;
1559 }
1560
1561 /**
1562 * Upgrade from V275 to V276
1563 */
1564 static BOOL H_UpgradeFromV275(int currVersion, int newVersion)
1565 {
1566 static TCHAR batch[] =
1567 _T("ALTER TABLE dc_table_columns DROP COLUMN transformation_script\n")
1568 _T("ALTER TABLE dc_tables ADD transformation_script $SQL:TEXT\n")
1569 _T("<END>");
1570 CHK_EXEC(SQLBatch(batch));
1571
1572 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='276' WHERE var_name='SchemaVersion'")));
1573 return TRUE;
1574 }
1575
1576 /**
1577 * Upgrade from V274 to V275
1578 */
1579 static BOOL H_UpgradeFromV274(int currVersion, int newVersion)
1580 {
1581 static TCHAR batch[] =
1582 _T("ALTER TABLE nodes ADD rack_image varchar(36)\n")
1583 _T("ALTER TABLE nodes ADD rack_position integer\n")
1584 _T("ALTER TABLE nodes ADD rack_id integer\n")
1585 _T("UPDATE nodes SET rack_image='00000000-0000-0000-0000-000000000000',rack_position=0,rack_id=0\n")
1586 _T("<END>");
1587 CHK_EXEC(SQLBatch(batch));
1588
1589 CHK_EXEC(CreateTable(_T("CREATE TABLE access_points (")
1590 _T("id integer not null,")
1591 _T("node_id integer not null,")
1592 _T("mac_address varchar(12) null,")
1593 _T("vendor varchar(64) null,")
1594 _T("model varchar(128) null,")
1595 _T("serial_number varchar(64) null,")
1596 _T("PRIMARY KEY(id))")));
1597
1598 CHK_EXEC(CreateTable(_T("CREATE TABLE racks (")
1599 _T("id integer not null,")
1600 _T("height integer not null,")
1601 _T("PRIMARY KEY(id))")));
1602
1603 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='275' WHERE var_name='SchemaVersion'")));
1604 return TRUE;
1605 }
1606
1607 /**
1608 * Upgrade from V273 to V274
1609 */
1610 static BOOL H_UpgradeFromV273(int currVersion, int newVersion)
1611 {
1612 static TCHAR batch[] =
1613 _T("ALTER TABLE items ADD samples integer\n")
1614 _T("UPDATE items SET samples=0\n")
1615 _T("<END>");
1616 CHK_EXEC(SQLBatch(batch));
1617
1618 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='274' WHERE var_name='SchemaVersion'")));
1619 return TRUE;
1620 }
1621
1622 /**
1623 * Upgrade from V272 to V273
1624 */
1625 static BOOL H_UpgradeFromV272(int currVersion, int newVersion)
1626 {
1627 CHK_EXEC(CreateConfigParam(_T("DefaultDCIRetentionTime"), _T("30"), 1, 0));
1628 CHK_EXEC(CreateConfigParam(_T("DefaultDCIPollingInterval"), _T("60"), 1, 0));
1629 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='273' WHERE var_name='SchemaVersion'")));
1630 return TRUE;
1631 }
1632
1633 /**
1634 * Upgrade from V271 to V272
1635 */
1636 static BOOL H_UpgradeFromV271(int currVersion, int newVersion)
1637 {
1638 CHK_EXEC(CreateConfigParam(_T("SNMPTrapLogRetentionTime"), _T("90"), 1, 0));
1639 CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD driver_name varchar(32)\n")));
1640 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='272' WHERE var_name='SchemaVersion'")));
1641 return TRUE;
1642 }
1643
1644 /**
1645 * Upgrade from V270 to V271
1646 */
1647 static BOOL H_UpgradeFromV270(int currVersion, int newVersion)
1648 {
1649 static TCHAR batch[] =
1650 _T("ALTER TABLE object_properties ADD location_accuracy integer\n")
1651 _T("ALTER TABLE object_properties ADD location_timestamp integer\n")
1652 _T("UPDATE object_properties SET location_accuracy=0,location_timestamp=0\n")
1653 _T("<END>");
1654 CHK_EXEC(SQLBatch(batch));
1655
1656 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='271' WHERE var_name='SchemaVersion'")));
1657 return TRUE;
1658 }
1659
1660 /**
1661 * Upgrade from V269 to V270
1662 */
1663 static BOOL H_UpgradeFromV269(int currVersion, int newVersion)
1664 {
1665 static TCHAR batch[] =
1666 _T("ALTER TABLE items ADD instd_method integer\n")
1667 _T("ALTER TABLE items ADD instd_data varchar(255)\n")
1668 _T("ALTER TABLE items ADD instd_filter $SQL:TEXT\n")
1669 _T("UPDATE items SET instd_method=0\n")
1670 _T("<END>");
1671 CHK_EXEC(SQLBatch(batch));
1672
1673 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='270' WHERE var_name='SchemaVersion'")));
1674 return TRUE;
1675 }
1676
1677 /**
1678 * Upgrade from V268 to V269
1679 */
1680 static BOOL H_UpgradeFromV268(int currVersion, int newVersion)
1681 {
1682 CHK_EXEC(ResizeColumn(_T("alarms"), _T("message"), 2000));
1683 CHK_EXEC(ResizeColumn(_T("alarm_events"), _T("message"), 2000));
1684 CHK_EXEC(ResizeColumn(_T("event_log"), _T("event_message"), 2000));
1685 CHK_EXEC(ResizeColumn(_T("event_cfg"), _T("message"), 2000));
1686 CHK_EXEC(ResizeColumn(_T("event_policy"), _T("alarm_message"), 2000));
1687 CHK_EXEC(ResizeColumn(_T("items"), _T("name"), 1024));
1688 CHK_EXEC(ResizeColumn(_T("dc_tables"), _T("name"), 1024));
1689
1690 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_key")));
1691 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_message")));
1692 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("comments")));
1693 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("situation_instance")));
1694 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("script")));
1695 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_key")));
1696 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_message")));
1697 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("comments")));
1698 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("situation_instance")));
1699 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("script")));
1700
1701 CHK_EXEC(SetColumnNullable(_T("policy_situation_attr_list"), _T("attr_value")));
1702 // convert strings in policy_situation_attr_list
1703 DB_RESULT hResult = SQLSelect(_T("SELECT rule_id,situation_id,attr_name,attr_value FROM policy_situation_attr_list"));
1704 if (hResult != NULL)
1705 {
1706 if (SQLQuery(_T("DELETE FROM policy_situation_attr_list")))
1707 {
1708 TCHAR name[MAX_DB_STRING], value[MAX_DB_STRING], query[1024];
1709 int count = DBGetNumRows(hResult);
1710 for(int i = 0; i < count; i++)
1711 {
1712 LONG ruleId = DBGetFieldLong(hResult, i, 0);
1713 LONG situationId = DBGetFieldLong(hResult, i, 1);
1714 DBGetField(hResult, i, 2, name, MAX_DB_STRING);
1715 DBGetField(hResult, i, 3, value, MAX_DB_STRING);
1716
1717 DecodeSQLString(name);
1718 DecodeSQLString(value);
1719
1720 if (name[0] == 0)
1721 _tcscpy(name, _T("noname"));
1722
1723 _sntprintf(query, 1024, _T("INSERT INTO policy_situation_attr_list (rule_id,situation_id,attr_name,attr_value) VALUES (%d,%d,%s,%s)"),
1724 ruleId, situationId, (const TCHAR *)DBPrepareString(g_hCoreDB, name), (const TCHAR *)DBPrepareString(g_hCoreDB, value));
1725 if (!SQLQuery(query))
1726 {
1727 if (!g_bIgnoreErrors)
1728 {
1729 DBFreeResult(hResult);
1730 return FALSE;
1731 }
1732 }
1733 }
1734 }
1735 else
1736 {
1737 if (!g_bIgnoreErrors)
1738 {
1739 DBFreeResult(hResult);
1740 return FALSE;
1741 }
1742 }
1743 DBFreeResult(hResult);
1744 }
1745 else
1746 {
1747 if (!g_bIgnoreErrors)
1748 return FALSE;
1749 }
1750
1751 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("description")));
1752 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("message")));
1753 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("description")));
1754 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("message")));
1755
1756 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='269' WHERE var_name='SchemaVersion'")));
1757 return TRUE;
1758 }
1759
1760 /**
1761 * Upgrade from V267 to V268
1762 */
1763 static BOOL H_UpgradeFromV267(int currVersion, int newVersion)
1764 {
1765 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_request")));
1766 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_responce")));
1767 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_request")));
1768 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_responce")));
1769
1770 CHK_EXEC(SetColumnNullable(_T("config"), _T("var_value")));
1771 CHK_EXEC(ConvertStrings(_T("config"), _T("var_name"), NULL, _T("var_value"), true));
1772
1773 CHK_EXEC(SetColumnNullable(_T("dci_schedules"), _T("schedule")));
1774 CHK_EXEC(ConvertStrings(_T("dci_schedules"), _T("schedule_id"), _T("item_id"), _T("schedule"), false));
1775
1776 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='268' WHERE var_name='SchemaVersion'")));
1777 return TRUE;
1778 }
1779
1780 /**
1781 * Upgrade from V266 to V267
1782 */
1783 static BOOL H_UpgradeFromV266(int currVersion, int newVersion)
1784 {
1785 CHK_EXEC(CreateEventTemplate(EVENT_NODE_UNREACHABLE, _T("SYS_NODE_UNREACHABLE"), EVENT_SEVERITY_CRITICAL,
1786 EF_LOG, _T("Node unreachable because of network failure"),
1787 _T("Generated when node is unreachable by management server because of network failure.\r\nParameters:\r\n No event-specific parameters")));
1788 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='267' WHERE var_name='SchemaVersion'")));
1789 return TRUE;
1790 }
1791
1792 /**
1793 * Upgrade from V265 to V266
1794 */
1795 static BOOL H_UpgradeFromV265(int currVersion, int newVersion)
1796 {
1797 // create index on root event ID in event log
1798 switch(g_dbSyntax)
1799 {
1800 case DB_SYNTAX_MSSQL:
1801 case DB_SYNTAX_PGSQL:
1802 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id) WHERE root_event_id > 0")));
1803 break;
1804 case DB_SYNTAX_ORACLE:
1805 CHK_EXEC(SQLQuery(_T("CREATE OR REPLACE FUNCTION zero_to_null(id NUMBER) ")
1806 _T("RETURN NUMBER ")
1807 _T("DETERMINISTIC ")
1808 _T("AS BEGIN")
1809 _T(" IF id > 0 THEN")
1810 _T(" RETURN id;")
1811 _T(" ELSE")
1812 _T(" RETURN NULL;")
1813 _T(" END IF;")
1814 _T("END;")));
1815 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(zero_to_null(root_event_id))")));
1816 break;
1817 default:
1818 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id)")));
1819 break;
1820 }
1821
1822 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_tables (")
1823 _T("id integer not null,")
1824 _T("name varchar(63) not null,")
1825 _T("flags integer not null,")
1826 _T("description $SQL:TXT4K null,")
1827 _T("PRIMARY KEY(id))")));
1828
1829 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_data (")
1830 _T("table_id integer not null,")
1831 _T("md_key varchar(63) not null,")
1832 _T("md_value varchar(255) null,")
1833 _T("description $SQL:TXT4K null,")
1834 _T("PRIMARY KEY(table_id,md_key))")));
1835
1836 CHK_EXEC(SQLQuery(_T("DROP TABLE deleted_objects")));
1837
1838 CHK_EXEC(CreateConfigParam(_T("FirstFreeObjectId"), _T("100"), 0, 1, FALSE));
1839
1840 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='266' WHERE var_name='SchemaVersion'")));
1841 return TRUE;
1842 }
1843
1844 /**
1845 * Upgrade from V264 to V265
1846 */
1847 static BOOL H_UpgradeFromV264(int currVersion, int newVersion)
1848 {
1849 CHK_EXEC(CreateTable(_T("CREATE TABLE alarm_events (")
1850 _T("alarm_id integer not null,")
1851 _T("event_id $SQL:INT64 not null,")
1852 _T("event_code integer not null,")
1853 _T("event_name varchar(63) null,")
1854 _T("severity integer not null,")
1855 _T("source_object_id integer not null,")
1856 _T("event_timestamp integer not null,")
1857 _T("message varchar(255) null,")
1858 _T("PRIMARY KEY(alarm_id,event_id))")));
1859 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_alarm_events_alarm_id ON alarm_events(alarm_id)")));
1860 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='265' WHERE var_name='SchemaVersion'")));
1861 return TRUE;
1862 }
1863
1864 /**
1865 * Upgrade from V263 to V264
1866 */
1867 static BOOL H_UpgradeFromV263(int currVersion, int newVersion)
1868 {
1869 CHK_EXEC(CreateTable(_T("CREATE TABLE mobile_devices (")
1870 _T("id integer not null,")
1871 _T("device_id varchar(64) not null,")
1872 _T("vendor varchar(64) null,")
1873 _T("model varchar(128) null,")
1874 _T("serial_number varchar(64) null,")
1875 _T("os_name varchar(32) null,")
1876 _T("os_version varchar(64) null,")
1877 _T("user_id varchar(64) null,")
1878 _T("battery_level integer not null,")
1879 _T("PRIMARY KEY(id))")));
1880 CHK_EXEC(CreateConfigParam(_T("MobileDeviceListenerPort"), _T("4747"), 1, 1));
1881 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='264' WHERE var_name='SchemaVersion'")));
1882 return TRUE;
1883 }
1884
1885 /**
1886 * Upgrade from V262 to V263
1887 */
1888 static BOOL H_UpgradeFromV262(int currVersion, int newVersion)
1889 {
1890 CHK_EXEC(SQLQuery(_T("ALTER TABLE network_maps ADD radius integer")));
1891 CHK_EXEC(SQLQuery(_T("UPDATE network_maps SET radius=-1")));
1892 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='263' WHERE var_name='SchemaVersion'")));
1893 return TRUE;
1894 }
1895
1896 /**
1897 * Upgrade from V261 to V262
1898 */
1899 static BOOL H_UpgradeFromV261(int currVersion, int newVersion)
1900 {
1901 CHK_EXEC(CreateConfigParam(_T("ApplyDCIFromTemplateToDisabledDCI"), _T("0"), 1, 1));
1902 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='262' WHERE var_name='SchemaVersion'")));
1903 return TRUE;
1904 }
1905
1906 /**
1907 * Upgrade from V260 to V261
1908 */
1909 static BOOL H_UpgradeFromV260(int currVersion, int newVersion)
1910 {
1911 CHK_EXEC(CreateConfigParam(_T("NumberOfBusinessServicePollers"), _T("10"), 1, 1));
1912 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfEventProcessors'")));
1913 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='261' WHERE var_name='SchemaVersion'")));
1914 return TRUE;
1915 }
1916
1917 /**
1918 * Upgrade from V259 to V260
1919 */
1920 static BOOL H_UpgradeFromV259(int currVersion, int newVersion)
1921 {
1922 CHK_EXEC(CreateConfigParam(_T("UseFQDNForNodeNames"), _T("1"), 1, 1));
1923 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='260' WHERE var_name='SchemaVersion'")));
1924 return TRUE;
1925 }
1926
1927 /**
1928 * Upgrade from V258 to V259
1929 */
1930 static BOOL H_UpgradeFromV258(int currVersion, int newVersion)
1931 {
1932 // have to made these columns nullable again because
1933 // because they was forgotten as NOT NULL in schema.in
1934 // and so some databases can still have them as NOT NULL
1935 CHK_EXEC(SetColumnNullable(_T("templates"), _T("apply_filter")));
1936 CHK_EXEC(SetColumnNullable(_T("containers"), _T("auto_bind_filter")));
1937
1938 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='259' WHERE var_name='SchemaVersion'")));
1939 return TRUE;
1940 }
1941
1942 /**
1943 * Upgrade from V257 to V258
1944 */
1945 static BOOL H_UpgradeFromV257(int currVersion, int newVersion)
1946 {
1947 static TCHAR batch[] =
1948 _T("ALTER TABLE nodes ADD down_since integer\n")
1949 _T("UPDATE nodes SET down_since=0\n")
1950 _T("<END>");
1951
1952 CHK_EXEC(SQLBatch(batch));
1953
1954 CHK_EXEC(CreateConfigParam(_T("DeleteUnreachableNodesPeriod"), _T("0"), 1, 1));
1955
1956 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='258' WHERE var_name='SchemaVersion'")));
1957 return TRUE;
1958 }
1959
1960 /**
1961 * Upgrade from V256 to V257
1962 */
1963 static BOOL H_UpgradeFromV256(int currVersion, int newVersion)
1964 {
1965 static TCHAR batch[] =
1966 _T("ALTER TABLE network_maps ADD bg_color integer\n")
1967 _T("ALTER TABLE network_maps ADD link_routing integer\n")
1968 _T("UPDATE network_maps SET bg_color=16777215,link_routing=1\n")
1969 _T("ALTER TABLE network_map_links ADD routing integer\n")
1970 _T("ALTER TABLE network_map_links ADD bend_points $SQL:TXT4K\n")
1971 _T("UPDATE network_map_links SET routing=0\n")
1972 _T("<END>");
1973
1974 CHK_EXEC(SQLBatch(batch));
1975
1976 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='257' WHERE var_name='SchemaVersion'")));
1977 return TRUE;
1978 }
1979
1980 /**
1981 * Upgrade from V255 to V256
1982 */
1983 static BOOL H_UpgradeFromV255(int currVersion, int newVersion)
1984 {
1985 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleDateFormat"), _T("dd.MM.yyyy"), 1, 0));
1986 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleTimeFormat"), _T("HH:mm:ss"), 1, 0));
1987
1988 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='256' WHERE var_name='SchemaVersion'")));
1989 return TRUE;
1990 }
1991
1992 /**
1993 * Upgrade from V254 to V255
1994 */
1995 static BOOL H_UpgradeFromV254(int currVersion, int newVersion)
1996 {
1997 static TCHAR batch[] =
1998 _T("ALTER TABLE alarms ADD resolved_by integer\n")
1999 _T("UPDATE alarms SET resolved_by=0\n")
2000 _T("UPDATE alarms SET alarm_state=3 WHERE alarm_state=2\n")
2001 _T("<END>");
2002
2003 CHK_EXEC(SQLBatch(batch));
2004
2005 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='255' WHERE var_name='SchemaVersion'")));
2006 return TRUE;
2007 }
2008
2009 /**
2010 * Upgrade from V253 to V254
2011 */
2012 static BOOL H_UpgradeFromV253(int currVersion, int newVersion)
2013 {
2014 static TCHAR batch[] =
2015 _T("ALTER TABLE network_maps ADD flags integer\n")
2016 _T("ALTER TABLE network_maps ADD link_color integer\n")
2017 _T("UPDATE network_maps SET flags=1,link_color=-1\n")
2018 _T("ALTER TABLE network_map_links ADD color integer\n")
2019 _T("ALTER TABLE network_map_links ADD status_object integer\n")
2020 _T("UPDATE network_map_links SET color=-1,status_object=0\n")
2021 _T("<END>");
2022
2023 CHK_EXEC(SQLBatch(batch));
2024
2025 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='254' WHERE var_name='SchemaVersion'")));
2026 return TRUE;
2027 }
2028
2029 /**
2030 * Upgrade from V252 to V253
2031 */
2032 static BOOL H_UpgradeFromV252(int currVersion, int newVersion)
2033 {
2034 CHK_EXEC(SetColumnNullable(_T("templates"), _T("apply_filter")));
2035 CHK_EXEC(ConvertStrings(_T("templates"), _T("id"), _T("apply_filter")));
2036
2037 CHK_EXEC(SetColumnNullable(_T("containers"), _T("auto_bind_filter")));
2038 CHK_EXEC(ConvertStrings(_T("containers"), _T("id"), _T("auto_bind_filter")));
2039
2040 static TCHAR batch[] =
2041 _T("ALTER TABLE templates ADD flags integer\n")
2042 _T("UPDATE templates SET flags=0 WHERE enable_auto_apply=0\n")
2043 _T("UPDATE templates SET flags=3 WHERE enable_auto_apply<>0\n")
2044 _T("ALTER TABLE templates DROP COLUMN enable_auto_apply\n")
2045 _T("ALTER TABLE containers ADD flags integer\n")
2046 _T("UPDATE containers SET flags=0 WHERE enable_auto_bind=0\n")
2047 _T("UPDATE containers SET flags=3 WHERE enable_auto_bind<>0\n")
2048 _T("ALTER TABLE containers DROP COLUMN enable_auto_bind\n")
2049 _T("<END>");
2050 CHK_EXEC(SQLBatch(batch));
2051
2052 CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOBIND, _T("SYS_CONTAINER_AUTOBIND"), EVENT_SEVERITY_NORMAL, 1,
2053 _T("Node %2 automatically bound to container %4"),
2054 _T("Generated when node bound to container object by autobind rule.\r\n")
2055 _T("Parameters:#\r\n")
2056 _T(" 1) Node ID\r\n")
2057 _T(" 2) Node name\r\n")
2058 _T(" 3) Container ID\r\n")
2059 _T(" 4) Container name")
2060 ));
2061
2062 CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOUNBIND, _T("SYS_CONTAINER_AUTOUNBIND"), EVENT_SEVERITY_NORMAL, 1,
2063 _T("Node %2 automatically unbound from container %4"),
2064 _T("Generated when node unbound from container object by autobind rule.\r\n")
2065 _T("Parameters:#\r\n")
2066 _T(" 1) Node ID\r\n")
2067 _T(" 2) Node name\r\n")
2068 _T(" 3) Container ID\r\n")
2069 _T(" 4) Container name")
2070 ));
2071
2072 CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOAPPLY, _T("SYS_TEMPLATE_AUTOAPPLY"), EVENT_SEVERITY_NORMAL, 1,
2073 _T("Template %4 automatically applied to node %2"),
2074 _T("Generated when template applied to node by autoapply rule.\r\n")
2075 _T("Parameters:#\r\n")
2076 _T(" 1) Node ID\r\n")
2077 _T(" 2) Node name\r\n")
2078 _T(" 3) Template ID\r\n")
2079 _T(" 4) Template name")
2080 ));
2081
2082 CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOREMOVE, _T("SYS_TEMPLATE_AUTOREMOVE"), EVENT_SEVERITY_NORMAL, 1,
2083 _T("Template %4 automatically removed from node %2"),
2084 _T("Generated when template removed from node by autoapply rule.\r\n")
2085 _T("Parameters:#\r\n")
2086 _T(" 1) Node ID\r\n")
2087 _T(" 2) Node name\r\n")
2088 _T(" 3) Template ID\r\n")
2089 _T(" 4) Template name")
2090 ));
2091
2092 TCHAR buffer[64];
2093 _sntprintf(buffer, 64, _T("%d"), ConfigReadInt(_T("AllowedCiphers"), 15) + 16);
2094 CreateConfigParam(_T("AllowedCiphers"), buffer, 1, 1, TRUE);
2095
2096 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='253' WHERE var_name='SchemaVersion'")));
2097 return TRUE;
2098 }
2099
2100 /**
2101 * Upgrade from V251 to V252
2102 */
2103 static BOOL H_UpgradeFromV251(int currVersion, int newVersion)
2104 {
2105 static TCHAR batch[] =
2106 _T("ALTER TABLE interfaces ADD admin_state integer\n")
2107 _T("ALTER TABLE interfaces ADD oper_state integer\n")
2108 _T("UPDATE interfaces SET admin_state=0,oper_state=0\n")
2109 _T("<END>");
2110
2111 CHK_EXEC(SQLBatch(batch));
2112
2113 CHK_EXEC(CreateEventTemplate(EVENT_INTERFACE_UNEXPECTED_UP, _T("SYS_IF_UNEXPECTED_UP"), EVENT_SEVERITY_MAJOR, 1,
2114 _T("Interface \"%2\" unexpectedly changed state to UP (IP Addr: %3/%4, IfIndex: %5)"),
2115 _T("Generated when interface goes up but it's expected state set to DOWN.\r\n")
2116 _T("Please note that source of event is node, not an interface itself.\r\n")
2117 _T("Parameters:#\r\n")
2118 _T(" 1) Interface object ID\r\n")
2119 _T(" 2) Interface name\r\n")
2120 _T(" 3) Interface IP address\r\n")
2121 _T(" 4) Interface netmask\r\n")
2122 _T(" 5) Interface index")
2123 ));
2124
2125 CHK_EXEC(CreateEventTemplate(EVENT_INTERFACE_EXPECTED_DOWN, _T("SYS_IF_EXPECTED_DOWN"), EVENT_SEVERITY_NORMAL, 1,
2126 _T("Interface \"%2\" with expected state DOWN changed state to DOWN (IP Addr: %3/%4, IfIndex: %5)"),
2127 _T("Generated when interface goes down and it's expected state is DOWN.\r\n")
2128 _T("Please note that source of event is node, not an interface itself.\r\n")
2129 _T("Parameters:#\r\n")
2130 _T(" 1) Interface object ID\r\n")
2131 _T(" 2) Interface name\r\n")
2132 _T(" 3) Interface IP address\r\n")
2133 _T(" 4) Interface netmask\r\n")
2134 _T(" 5) Interface index")
2135 ));
2136
2137 // Create rule pair in event processing policy
2138 int ruleId = 0;
2139 DB_RESULT hResult = SQLSelect(_T("SELECT max(rule_id) FROM event_policy"));
2140 if (hResult != NULL)
2141 {
2142 ruleId = DBGetFieldLong(hResult, 0, 0) + 1;
2143 DBFreeResult(hResult);
2144 }
2145
2146 TCHAR query[1024];
2147 _sntprintf(query, 1024,
2148 _T("INSERT INTO event_policy (rule_id,flags,comments,alarm_message,alarm_severity,alarm_key,")
2149 _T("script,alarm_timeout,alarm_timeout_event,situation_id,situation_instance) VALUES ")
2150 _T("(%d,7944,'Show alarm when interface is unexpectedly up','%%m',5,'IF_UNEXP_UP_%%i_%%1',")
2151 _T("'#00',0,%d,0,'#00')"), ruleId, EVENT_ALARM_TIMEOUT);
2152 CHK_EXEC(SQLQuery(query));
2153 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_INTERFACE_UNEXPECTED_UP);
2154 CHK_EXEC(SQLQuery(query));
2155 ruleId++;
2156
2157 _sntprintf(query, 1024,
2158 _T("INSERT INTO event_policy (rule_id,flags,comments,alarm_message,alarm_severity,alarm_key,")
2159 _T("script,alarm_timeout,alarm_timeout_event,situation_id,situation_instance) VALUES ")
2160 _T("(%d,7944,'Acknowlege interface unexpectedly up alarms when interface goes down','%%m',")
2161 _T("6,'IF_UNEXP_UP_%%i_%%1','#00',0,%d,0,'#00')"), ruleId, EVENT_ALARM_TIMEOUT);
2162 CHK_EXEC(SQLQuery(query));
2163 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_INTERFACE_EXPECTED_DOWN);
2164 CHK_EXEC(SQLQuery(query));
2165
2166 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='252' WHERE var_name='SchemaVersion'")));
2167 return TRUE;
2168 }
2169
2170 /**
2171 * Upgrade from V250 to V251
2172 */
2173 static BOOL H_UpgradeFromV250(int currVersion, int newVersion)
2174 {
2175 static TCHAR batch[] =
2176 _T("ALTER TABLE thresholds ADD current_severity integer\n")
2177 _T("ALTER TABLE thresholds ADD last_event_timestamp integer\n")
2178 _T("UPDATE thresholds SET current_severity=0,last_event_timestamp=0\n")
2179 _T("<END>");
2180
2181 CHK_EXEC(SQLBatch(batch));
2182
2183 CHK_EXEC(SetColumnNullable(_T("thresholds"), _T("fire_value")));
2184 CHK_EXEC(SetColumnNullable(_T("thresholds"), _T("rearm_value")));
2185 CHK_EXEC(ConvertStrings(_T("thresholds"), _T("threshold_id"), _T("fire_value")));
2186 CHK_EXEC(ConvertStrings(_T("thresholds"), _T("threshold_id"), _T("rearm_value")));
2187
2188 CHK_EXEC(CreateConfigParam(_T("EnableNXSLContainerFunctions"), _T("1"), 1, 1));
2189 CHK_EXEC(CreateConfigParam(_T("UseDNSNameForDiscoveredNodes"), _T("0"), 1, 0));
2190 CHK_EXEC(CreateConfigParam(_T("AllowTrapVarbindsConversion"), _T("1"), 1, 1));
2191
2192 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='251' WHERE var_name='SchemaVersion'")));
2193 return TRUE;
2194 }
2195
2196 /**
2197 * Upgrade from V249 to V250
2198 */
2199 static BOOL H_UpgradeFromV249(int currVersion, int newVersion)
2200 {
2201 CHK_EXEC(CreateTable(_T("CREATE TABLE licenses (")
2202 _T("id integer not null,")
2203 _T("content $SQL:TEXT null,")
2204 _T("PRIMARY KEY(id))")));
2205
2206 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='250' WHERE var_name='SchemaVersion'")));
2207 return TRUE;
2208 }
2209
2210 /**
2211 * Upgrade from V248 to V249
2212 */
2213 #define TDATA_CREATE_QUERY _T("CREATE TABLE tdata_%d (item_id integer not null,tdata_timestamp integer not null,tdata_row integer not null,tdata_column integer not null,tdata_value varchar(255) null)")
2214 #define TDATA_INDEX_MSSQL _T("CREATE CLUSTERED INDEX idx_tdata_%d_id_timestamp ON tdata_%d(item_id,tdata_timestamp)")
2215 #define TDATA_INDEX_PGSQL _T("CREATE INDEX idx_tdata_%d_timestamp_id ON tdata_%d(tdata_timestamp,item_id)")
2216 #define TDATA_INDEX_DEFAULT _T("CREATE INDEX idx_tdata_%d_id_timestamp ON tdata_%d(item_id,tdata_timestamp)")
2217
2218 static BOOL CreateTData(DWORD nodeId)
2219 {
2220 TCHAR query[256];
2221
2222 _sntprintf(query, 256, TDATA_CREATE_QUERY, (int)nodeId);
2223 CHK_EXEC(SQLQuery(query));
2224
2225 switch(g_dbSyntax)
2226 {
2227 case DB_SYNTAX_MSSQL:
2228 _sntprintf(query, 256, TDATA_INDEX_MSSQL, (int)nodeId, (int)nodeId);
2229 break;
2230 case DB_SYNTAX_PGSQL:
2231 _sntprintf(query, 256, TDATA_INDEX_PGSQL, (int)nodeId, (int)nodeId);
2232 break;
2233 default:
2234 _sntprintf(query, 256, TDATA_INDEX_DEFAULT, (int)nodeId, (int)nodeId);
2235 break;
2236 }
2237 CHK_EXEC(SQLQuery(query));
2238
2239 return TRUE;
2240 }
2241
2242 static BOOL H_UpgradeFromV248(int currVersion, int newVersion)
2243 {
2244 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataTableCreationCommand','") TDATA_CREATE_QUERY _T("')")));
2245
2246 switch(g_dbSyntax)
2247 {
2248 case DB_SYNTAX_MSSQL:
2249 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_MSSQL _T("')")));
2250 break;
2251 case DB_SYNTAX_PGSQL:
2252 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_PGSQL _T("')")));
2253 break;
2254 default:
2255 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_DEFAULT _T("')")));
2256 break;
2257 }
2258
2259 CHK_EXEC(CreateTable(_T("CREATE TABLE dct_column_names (")
2260 _T("column_id integer not null,")
2261 _T("column_name varchar(63) not null,")
2262 _T("PRIMARY KEY(column_id))")));
2263
2264 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM nodes"));
2265 if (hResult != NULL)
2266 {
2267 int count = DBGetNumRows(hResult);
2268 for(int i = 0 ; i < count; i++)
2269 {
2270 if (!CreateTData(DBGetFieldULong(hResult, i, 0)))
2271 {
2272 if (!g_bIgnoreErrors)
2273 {
2274 DBFreeResult(hResult);
2275 return FALSE;
2276 }
2277 }
2278 }
2279 DBFreeResult(hResult);
2280 }
2281 else
2282 {
2283 if (!g_bIgnoreErrors)
2284 return FALSE;
2285 }
2286
2287 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='249' WHERE var_name='SchemaVersion'")));
2288 return TRUE;
2289 }
2290
2291 /**
2292 * Upgrade from V247 to V248
2293 */
2294 static BOOL H_UpgradeFromV247(int currVersion, int newVersion)
2295 {
2296 CHK_EXEC(CreateTable(_T("CREATE TABLE dc_tables (")
2297 _T("item_id integer not null,")
2298 _T("node_id integer not null,")
2299 _T("template_id integer not null,")
2300 _T("template_item_id integer not null,")
2301 _T("name varchar(255) null,")
2302 _T("instance_column varchar(63) null,")
2303 _T("description varchar(255) null,")
2304 _T("flags integer not null,")
2305 _T("source integer not null,")
2306 _T("snmp_port integer not null,")
2307 _T("polling_interval integer not null,")
2308 _T("retention_time integer not null,")
2309 _T("status integer not null,")
2310 _T("system_tag varchar(255) null,")
2311 _T("resource_id integer not null,")
2312 _T("proxy_node integer not null,")
2313 _T("perftab_settings $SQL:TEXT null,")
2314 _T("PRIMARY KEY(item_id))")));
2315
2316 CHK_EXEC(CreateTable(_T("CREATE TABLE dc_table_columns (")
2317 _T("table_id integer not null,")
2318 _T("column_name varchar(63) not null,")
2319 _T("snmp_oid varchar(1023) null,")
2320 _T("data_type integer not null,")
2321 _T("transformation_script $SQL:TEXT null,")
2322 _T("PRIMARY KEY(table_id,column_name))")));
2323
2324 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='248' WHERE var_name='SchemaVersion'")));
2325 return TRUE;
2326 }
2327
2328 /**
2329 * Upgrade from V246 to V247
2330 */
2331 static BOOL H_UpgradeFromV246(int currVersion, int newVersion)
2332 {
2333 static TCHAR insertQuery[] = _T("INSERT INTO object_custom_attributes (object_id,attr_name,attr_value) VALUES (?,?,?)");
2334
2335 CHK_EXEC(SetColumnNullable(_T("object_custom_attributes"), _T("attr_value")));
2336
2337 // Convert strings in object_custom_attributes table
2338 DB_RESULT hResult = SQLSelect(_T("SELECT object_id,attr_name,attr_value FROM object_custom_attributes"));
2339 if (hResult != NULL)
2340 {
2341 if (SQLQuery(_T("DELETE FROM object_custom_attributes")))
2342 {
2343 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
2344 DB_STATEMENT hStmt = DBPrepareEx(g_hCoreDB, insertQuery, errorText);
2345 if (hStmt != NULL)
2346 {
2347 TCHAR name[128], *value;
2348 int count = DBGetNumRows(hResult);
2349 for(int i = 0; i < count; i++)
2350 {
2351 UINT32 id = DBGetFieldULong(hResult, i, 0);
2352 DBGetField(hResult, i, 1, name, 128);
2353 DecodeSQLString(name);
2354 value = DBGetField(hResult, i, 2, NULL, 0);
2355 DecodeSQLString(value);
2356
2357 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, id);
2358 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, name, DB_BIND_STATIC);
2359 DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, value, DB_BIND_DYNAMIC);
2360 if (g_bTrace)
2361 ShowQuery(insertQuery);
2362 if (!DBExecuteEx(hStmt, errorText))
2363 {
2364 WriteToTerminalEx(_T("SQL query failed (%s):\n\x1b[33;1m%s\x1b[0m\n"), errorText, insertQuery);
2365 if (!g_bIgnoreErrors)
2366 {
2367 DBFreeStatement(hStmt);
2368 DBFreeResult(hResult);
2369 return FALSE;
2370 }
2371 }
2372 }
2373 DBFreeStatement(hStmt);
2374 }
2375 else
2376 {
2377 WriteToTerminalEx(_T("SQL query failed (%s):\n\x1b[33;1m%s\x1b[0m\n"), errorText, insertQuery);
2378 if (!g_bIgnoreErrors)
2379 {
2380 DBFreeResult(hResult);
2381 return FALSE;
2382 }
2383 }
2384 }
2385 else
2386 {
2387 if (!g_bIgnoreErrors)
2388 {
2389 DBFreeResult(hResult);
2390 return FALSE;
2391 }
2392 }
2393
2394 DBFreeResult(hResult);
2395 }
2396 else
2397 {
2398 if (!g_bIgnoreErrors)
2399 return FALSE;
2400 }
2401
2402 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_ocattr_oid ON object_custom_attributes(object_id)")));
2403 CHK_EXEC(CreateConfigParam(_T("AlarmHistoryRetentionTime"), _T("180"), 1, 0));
2404
2405 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='247' WHERE var_name='SchemaVersion'")));
2406 return TRUE;
2407 }
2408
2409 /**
2410 * Upgrade from V245 to V246
2411 */
2412 static BOOL H_UpgradeFromV245(int currVersion, int newVersion)
2413 {
2414 static TCHAR batch[] =
2415 _T("ALTER TABLE snmp_trap_pmap ADD flags integer\n")
2416 _T("UPDATE snmp_trap_pmap SET flags=0\n")
2417 _T("<END>");
2418
2419 CHK_EXEC(SQLBatch(batch));
2420
2421 CHK_EXEC(SetColumnNullable(_T("snmp_trap_pmap"), _T("description")));
2422 CHK_EXEC(ConvertStrings(_T("snmp_trap_pmap"), _T("trap_id"), _T("parameter"), _T("description"), false));
2423
2424 CHK_EXEC(SetColumnNullable(_T("cluster_resources"), _T("resource_name")));
2425 CHK_EXEC(ConvertStrings(_T("cluster_resources"), _T("cluster_id"), _T("resource_id"), _T("resource_name"), false));
2426
2427 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='246' WHERE var_name='SchemaVersion'")));
2428 return TRUE;
2429 }
2430
2431 /**
2432 * Upgrade from V244 to V245
2433 */
2434 static BOOL H_UpgradeFromV244(int currVersion, int newVersion)
2435 {
2436 static TCHAR batch[] =
2437 _T("ALTER TABLE nodes ADD runtime_flags integer\n")
2438 _T("UPDATE nodes SET runtime_flags=0\n")
2439 _T("<END>");
2440
2441 CHK_EXEC(SQLBatch(batch));
2442
2443 CHK_EXEC(SetColumnNullable(_T("actions"), _T("rcpt_addr")));
2444 CHK_EXEC(SetColumnNullable(_T("actions"), _T("email_subject")));
2445 CHK_EXEC(SetColumnNullable(_T("actions"), _T("action_data")));
2446
2447 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("rcpt_addr")));
2448 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("email_subject")));
2449 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("action_data")));
2450
2451 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='245' WHERE var_name='SchemaVersion'")));
2452 return TRUE;
2453 }
2454
2455
2456 //
2457 // Upgrade from V243 to V244
2458 //
2459
2460 static BOOL H_UpgradeFromV243(int currVersion, int newVersion)
2461 {
2462 static TCHAR batch[] =
2463 _T("ALTER TABLE interfaces ADD dot1x_pae_state integer\n")
2464 _T("ALTER TABLE interfaces ADD dot1x_backend_state integer\n")
2465 _T("UPDATE interfaces SET dot1x_pae_state=0,dot1x_backend_state=0\n")
2466 _T("<END>");
2467
2468 CHK_EXEC(SQLBatch(batch));
2469
2470 CHK_EXEC(CreateEventTemplate(EVENT_8021X_PAE_STATE_CHANGED, _T("SYS_8021X_PAE_STATE_CHANGED"),
2471 EVENT_SEVERITY_NORMAL, 1, _T("Port %6 PAE state changed from %4 to %2"),
2472 _T("Generated when switch port PAE state changed.\r\nParameters:\r\n")
2473 _T(" 1) New PAE state code\r\n")
2474 _T(" 2) New PAE state as text\r\n")
2475 _T(" 3) Old PAE state code\r\n")
2476 _T(" 4) Old PAE state as text\r\n")
2477 _T(" 5) Interface index\r\n")
2478 _T(" 6) Interface name")));
2479
2480 CHK_EXEC(CreateEventTemplate(EVENT_8021X_BACKEND_STATE_CHANGED, _T("SYS_8021X_BACKEND_STATE_CHANGED"),
2481 EVENT_SEVERITY_NORMAL, 1, _T("Port %6 backend authentication state changed from %4 to %2"),
2482 _T("Generated when switch port backend authentication state changed.\r\nParameters:\r\n")
2483 _T(" 1) New backend state code\r\n")
2484 _T(" 2) New backend state as text\r\n")
2485 _T(" 3) Old backend state code\r\n")
2486 _T(" 4) Old backend state as text\r\n")
2487 _T(" 5) Interface index\r\n")
2488 _T(" 6) Interface name")));
2489
2490 CHK_EXEC(CreateEventTemplate(EVENT_8021X_PAE_FORCE_UNAUTH, _T("SYS_8021X_PAE_FORCE_UNAUTH"),
2491 EVENT_SEVERITY_MAJOR, 1, _T("Port %2 switched to force unauthorize state"),
2492 _T("Generated when switch port PAE state changed to FORCE UNAUTHORIZE.\r\nParameters:\r\n")
2493 _T(" 1) Interface index\r\n")
2494 _T(" 2) Interface name")));
2495
2496 CHK_EXEC(CreateEventTemplate(EVENT_8021X_AUTH_FAILED, _T("SYS_8021X_AUTH_FAILED"),
2497 EVENT_SEVERITY_MAJOR, 1, _T("802.1x authentication failed on port %2"),
2498 _T("Generated when switch port backend authentication state changed to FAIL.\r\nParameters:\r\n")
2499 _T(" 1) Interface index\r\n")
2500 _T(" 2) Interface name")));
2501
2502 CHK_EXEC(CreateEventTemplate(EVENT_8021X_AUTH_TIMEOUT, _T("SYS_8021X_AUTH_TIMEOUT"),
2503 EVENT_SEVERITY_MAJOR, 1, _T("802.1x authentication time out on port %2"),
2504 _T("Generated when switch port backend authentication state changed to TIMEOUT.\r\nParameters:\r\n")
2505 _T(" 1) Interface index\r\n")
2506 _T(" 2) Interface name")));
2507
2508 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='244' WHERE var_name='SchemaVersion'")));
2509 return TRUE;
2510 }
2511
2512
2513 //
2514 // Upgrade from V242 to V243
2515 //
2516
2517 static BOOL H_UpgradeFromV242(int currVersion, int newVersion)
2518 {
2519 static TCHAR batch[] =
2520 _T("ALTER TABLE items ADD snmp_raw_value_type integer\n")
2521 _T("UPDATE items SET snmp_raw_value_type=0\n")
2522 _T("ALTER TABLE items ADD flags integer\n")
2523 _T("UPDATE items SET flags=adv_schedule+(all_thresholds*2)\n")
2524 _T("ALTER TABLE items DROP COLUMN adv_schedule\n")
2525 _T("ALTER TABLE items DROP COLUMN all_thresholds\n")
2526 _T("<END>");
2527
2528 CHK_EXEC(SQLBatch(batch));
2529 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='243' WHERE var_name='SchemaVersion'")));
2530 return TRUE;
2531 }
2532
2533
2534 //
2535 // Upgrade from V241 to V242
2536 //
2537
2538 static BOOL H_UpgradeFromV241(int currVersion, int newVersion)
2539 {
2540 static TCHAR batch[] =
2541 _T("DROP TABLE business_service_templates\n")
2542 _T("ALTER TABLE dashboards ADD options integer\n")
2543 _T("UPDATE dashboards SET options=0\n")
2544 _T("<END>");
2545
2546 CHK_EXEC(SQLBatch(batch));
2547 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='242' WHERE var_name='SchemaVersion'")));
2548 return TRUE;
2549 }
2550
2551
2552 //
2553 // Upgrade from V240 to V241
2554 //
2555
2556 static BOOL H_UpgradeFromV240(int currVersion, int newVersion)
2557 {
2558 static TCHAR batch[] =
2559 _T("ALTER TABLE slm_checks ADD template_id integer\n")
2560 _T("ALTER TABLE slm_checks ADD current_ticket integer\n")
2561 _T("UPDATE slm_checks SET template_id=0,current_ticket=0\n")
2562 _T("<END>");
2563
2564 CHK_EXEC(SQLBatch(batch));
2565 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='241' WHERE var_name='SchemaVersion'")));
2566 return TRUE;
2567 }
2568
2569
2570 //
2571 // Upgrade from V239 to V240
2572 //
2573
2574 static BOOL H_UpgradeFromV239(int currVersion, int newVersion)
2575 {
2576 CHK_EXEC(SQLQuery(_T("ALTER TABLE raw_dci_values ADD transformed_value varchar(255)")));
2577 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='240' WHERE var_name='SchemaVersion'")));
2578 return TRUE;
2579 }
2580
2581
2582 //
2583 // Upgrade from V238 to V239
2584 //
2585
2586 static BOOL H_UpgradeFromV238(int currVersion, int newVersion)
2587 {
2588 CHK_EXEC(SQLQuery(
2589 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES ")
2590 _T("(56,'SYS_IP_ADDRESS_CHANGED',1,1,'Primary IP address changed from %2 to %1',")
2591 _T("'Generated when primary IP address changed (usually because of primary name change or DNS change).#0D#0A")
2592 _T("Parameters:#0D#0A 1) New IP address#0D#0A 2) Old IP address#0D#0A 3) Primary host name')")));
2593
2594 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='239' WHERE var_name='SchemaVersion'")));
2595 return TRUE;
2596 }
2597
2598
2599 //
2600 // Upgrade from V232 to V238
2601 //
2602
2603 static BOOL H_UpgradeFromV232toV238(int currVersion, int newVersion)
2604 {
2605 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
2606 _T("id integer not null,")
2607 _T("type integer not null,")
2608 _T("content $SQL:TEXT null,")
2609 _T("threshold_id integer not null,")
2610 _T("reason varchar(255) null,")
2611 _T("is_template integer not null,")
2612 _T("PRIMARY KEY(id))")));
2613
2614 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
2615 _T("ticket_id integer not null,")
2616 _T("service_id integer not null,")
2617 _T("check_id integer not null,")
2618 _T("create_timestamp integer not null,")
2619 _T("close_timestamp integer not null,")
2620 _T("reason varchar(255) null,")
2621 _T("PRIMARY KEY(ticket_id))")));
2622
2623 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_service_history (")
2624 _T("record_id integer not null,")
2625 _T("service_id integer not null,")
2626 _T("change_timestamp integer not null,")
2627 _T("new_status integer not null,")
2628 _T("PRIMARY KEY(record_id))")));
2629
2630 CHK_EXEC(CreateTable(_T("CREATE TABLE report_results (")
2631 _T("report_id integer not null,")
2632 _T("generated integer not null,")
2633 _T("job_id integer not null,")
2634 _T("PRIMARY KEY(report_id,job_id))")));
2635
2636 CHK_EXEC(CreateTable(_T("CREATE TABLE reports (")
2637 _T("id integer not null,")
2638 _T("definition $SQL:TEXT null,")
2639 _T("PRIMARY KEY(id))")));
2640
2641 CHK_EXEC(CreateTable(_T("CREATE TABLE job_history (")
2642 _T("id integer not null,")
2643 _T("time_created integer not null,")
2644 _T("time_started integer not null,")
2645 _T("time_finished integer not null,")
2646 _T("job_type varchar(127) null,")
2647 _T("description varchar(255) null,")
2648 _T("additional_info varchar(255) null,")
2649 _T("node_id integer not null,")
2650 _T("user_id integer not null,")
2651 _T("status integer not null,")
2652 _T("failure_message varchar(255) null,")
2653 _T("PRIMARY KEY(id))")));
2654
2655 CHK_EXEC(CreateTable(_T("CREATE TABLE business_services (")
2656 _T("service_id integer not null,")
2657 _T("PRIMARY KEY(service_id))")));
2658
2659 CHK_EXEC(CreateTable(_T("CREATE TABLE business_service_templates (")
2660 _T("service_id integer not null,")
2661 _T("template_id integer not null,")
2662 _T("PRIMARY KEY(service_id,template_id))")));
2663
2664 CHK_EXEC(CreateTable(_T("CREATE TABLE node_links (")
2665 _T("nodelink_id integer not null,")
2666 _T("node_id integer not null,")
2667 _T("PRIMARY KEY(nodelink_id))")));
2668
2669 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_agreements (")
2670 _T("agreement_id integer not null,")
2671 _T("service_id integer not null,")
2672 _T("org_id integer not null,")
2673 _T("uptime varchar(63) not null,")
2674 _T("period integer not null,")
2675 _T("start_date integer not null,")
2676 _T("notes varchar(255),")
2677 _T("PRIMARY KEY(agreement_id))")));
2678
2679 CHK_EXEC(CreateTable(_T("CREATE TABLE organizations (")
2680 _T("id integer not null,")
2681 _T("parent_id integer not null,")
2682 _T("org_type integer not null,")
2683 _T("name varchar(63) not null,")
2684 _T("description varchar(255),")
2685 _T("manager integer not null,")
2686 _T("PRIMARY KEY(id))")));
2687
2688 CHK_EXEC(CreateTable(_T("CREATE TABLE persons (")
2689 _T("id integer not null,")
2690 _T("org_id integer not null,")
2691 _T("first_name varchar(63),")
2692 _T("last_name varchar(63),")
2693 _T("title varchar(255),")
2694 _T("status integer not null,")
2695 _T("PRIMARY KEY(id))")));
2696
2697 CHK_EXEC(CreateConfigParam(_T("JobHistoryRetentionTime"), _T("90"), 1, 0));
2698
2699 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description=")
2700 _T("'Generated when threshold check is rearmed for specific data collection item.#0D#0A")
2701 _T("Parameters:#0D#0A")
2702 _T(" 1) Parameter name#0D#0A")
2703 _T(" 2) Item description#0D#0A")
2704 _T(" 3) Data collection item ID#0D#0A")
2705 _T(" 4) Instance#0D#0A")
2706 _T(" 5) Threshold value#0D#0A")
2707 _T(" 6) Actual value' WHERE event_code=18")));
2708
2709 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='238' WHERE var_name='SchemaVersion'")));
2710 return TRUE;
2711 }
2712
2713
2714 //
2715 // Upgrade from V237 to V238
2716 //
2717
2718 static BOOL H_UpgradeFromV237(int currVersion, int newVersion)
2719 {
2720 static TCHAR batch[] =
2721 _T("DROP TABLE slm_check_templates\n")
2722 _T("DROP TABLE node_link_checks\n")
2723 _T("DROP TABLE slm_checks\n")
2724 _T("DROP TABLE slm_tickets\n")
2725 _T("<END>");
2726
2727 CHK_EXEC(SQLBatch(batch));
2728
2729 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
2730 _T("id integer not null,")
2731 _T("type integer not null,")
2732 _T("content $SQL:TEXT null,")
2733 _T("threshold_id integer not null,")
2734 _T("reason varchar(255) null,")
2735 _T("is_template integer not null,")
2736 _T("PRIMARY KEY(id))")));
2737
2738 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
2739 _T("ticket_id integer not null,")
2740 _T("service_id integer not null,")
2741 _T("check_id integer not null,")
2742 _T("create_timestamp integer not null,")
2743 _T("close_timestamp integer not null,")
2744 _T("reason varchar(255) null,")
2745 _T("PRIMARY KEY(ticket_id))")));
2746
2747 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_service_history (")
2748 _T("record_id integer not null,")
2749 _T("service_id integer not null,")
2750 _T("change_timestamp integer not null,")
2751 _T("new_status integer not null,")
2752 _T("PRIMARY KEY(record_id))")));
2753
2754 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='238' WHERE var_name='SchemaVersion'")));
2755 return TRUE;
2756 }
2757
2758
2759 //
2760 // Upgrade from V236 to V237
2761 //
2762
2763 static BOOL H_UpgradeFromV236(int currVersion, int newVersion)
2764 {
2765 static TCHAR batch[] =
2766 _T("ALTER TABLE business_services DROP COLUMN name\n")
2767 _T("ALTER TABLE business_services DROP COLUMN parent_id\n")
2768 _T("ALTER TABLE business_services DROP COLUMN status\n")
2769 _T("ALTER TABLE slm_checks DROP COLUMN name\n")
2770 _T("<END>");
2771
2772 CHK_EXEC(SQLBatch(batch));
2773
2774 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='237' WHERE var_name='SchemaVersion'")));
2775 return TRUE;
2776 }
2777
2778
2779 //
2780 // Upgrade from V235 to V236
2781 //
2782
2783 static BOOL H_UpgradeFromV235(int currVersion, int newVersion)
2784 {
2785 CHK_EXEC(CreateTable(_T("CREATE TABLE report_results (")
2786 _T("report_id integer not null,")
2787 _T("generated integer not null,")
2788 _T("job_id integer not null,")
2789 _T("PRIMARY KEY(report_id,job_id))")));
2790
2791 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='236' WHERE var_name='SchemaVersion'")));
2792 return TRUE;
2793 }
2794
2795
2796 //
2797 // Upgrade from V234 to V235
2798 //
2799
2800 static BOOL H_UpgradeFromV234(int currVersion, int newVersion)
2801 {
2802 CHK_EXEC(CreateTable(_T("CREATE TABLE reports (")
2803 _T("id integer not null,")
2804 _T("definition $SQL:TEXT null,")
2805 _T("PRIMARY KEY(id))")));
2806
2807 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='235' WHERE var_name='SchemaVersion'")));
2808 return TRUE;
2809 }
2810
2811
2812 //
2813 // Upgrade from V233 to V234
2814 //
2815
2816 static BOOL H_UpgradeFromV233(int currVersion, int newVersion)
2817 {
2818 CHK_EXEC(CreateTable(_T("CREATE TABLE job_history (")
2819 _T("id integer not null,")
2820 _T("time_created integer not null,")
2821 _T("time_started integer not null,")
2822 _T("time_finished integer not null,")
2823 _T("job_type varchar(127) null,")
2824 _T("description varchar(255) null,")
2825 _T("additional_info varchar(255) null,")
2826 _T("node_id integer not null,")
2827 _T("user_id integer not null,")
2828 _T("status integer not null,")
2829 _T("failure_message varchar(255) null,")
2830 _T("PRIMARY KEY(id))")));
2831
2832 CHK_EXEC(CreateConfigParam(_T("JobHistoryRetentionTime"), _T("90"), 1, 0));
2833
2834 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description=")
2835 _T("'Generated when threshold check is rearmed for specific data collection item.#0D#0A")
2836 _T("Parameters:#0D#0A")
2837 _T(" 1) Parameter name#0D#0A")
2838 _T(" 2) Item description#0D#0A")
2839 _T(" 3) Data collection item ID#0D#0A")
2840 _T(" 4) Instance#0D#0A")
2841 _T(" 5) Threshold value#0D#0A")
2842 _T(" 6) Actual value' WHERE event_code=18")));
2843
2844 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='234' WHERE var_name='SchemaVersion'")));
2845 return TRUE;
2846 }
2847
2848
2849 //
2850 // Upgrade from V232 to V233
2851 //
2852
2853 static BOOL H_UpgradeFromV232(int currVersion, int newVersion)
2854 {
2855 CHK_EXEC(CreateTable(_T("CREATE TABLE business_services (")
2856 _T("service_id integer not null,")
2857 _T("name varchar(63) not null,")
2858 _T("parent_id integer not null,")
2859 _T("status integer not null,")
2860 _T("PRIMARY KEY(service_id))")));
2861
2862 CHK_EXEC(CreateTable(_T("CREATE TABLE business_service_templates (")
2863 _T("service_id integer not null,")
2864 _T("template_id integer not null,")
2865 _T("PRIMARY KEY(service_id,template_id))")));
2866
2867 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
2868 _T("check_id integer not null,")
2869 _T("name varchar(63) not null,")
2870 _T("type integer not null,")
2871 _T("content $SQL:TEXT,")
2872 _T("threshold_id integer not null,")
2873 _T("state integer not null,")
2874 _T("reason varchar(255) not null,")
2875 _T("PRIMARY KEY(check_id))")));
2876
2877 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_check_templates (")
2878 _T("id integer not null,")
2879 _T("name varchar(63) not null,")
2880 _T("type integer not null,")
2881 _T("content $SQL:TEXT,")
2882 _T("threshold_id integer not null,")
2883 _T("PRIMARY KEY(id))")));
2884
2885 CHK_EXEC(CreateTable(_T("CREATE TABLE node_links (")
2886 _T("nodelink_id integer not null,")
2887 _T("node_id integer not null,")
2888 _T("PRIMARY KEY(nodelink_id))")));
2889
2890 CHK_EXEC(CreateTable(_T("CREATE TABLE node_link_checks (")
2891 _T("nodelink_id integer not null,")
2892 _T("check_id integer not null,")
2893 _T("PRIMARY KEY(nodelink_id,check_id))")));
2894
2895 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_agreements (")
2896 _T("agreement_id integer not null,")
2897 _T("service_id integer not null,")
2898 _T("org_id integer not null,")
2899 _T("uptime varchar(63) not null,")
2900 _T("period integer not null,")
2901 _T("start_date integer not null,")
2902 _T("notes varchar(255),")
2903 _T("PRIMARY KEY(agreement_id))")));
2904
2905 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
2906 _T("ticket_id integer not null,")
2907 _T("service_id integer not null,")
2908 _T("create_timestamp integer not null,")
2909 _T("close_timestamp integer not null,")
2910 _T("reason varchar(255) not null,")
2911 _T("PRIMARY KEY(ticket_id))")));
2912
2913 CHK_EXEC(CreateTable(_T("CREATE TABLE organizations (")
2914 _T("id integer not null,")
2915 _T("parent_id integer not null,")
2916 _T("org_type integer not null,")
2917 _T("name varchar(63) not null,")
2918 _T("description varchar(255),")
2919 _T("manager integer not null,")
2920 _T("PRIMARY KEY(id))")));
2921
2922 CHK_EXEC(CreateTable(_T("CREATE TABLE persons (")
2923 _T("id integer not null,")
2924 _T("org_id integer not null,")
2925 _T("first_name varchar(63),")
2926 _T("last_name varchar(63),")
2927 _T("title varchar(255),")
2928 _T("status integer not null,")
2929 _T("PRIMARY KEY(id))")));
2930
2931 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='233' WHERE var_name='SchemaVersion'")));
2932 return TRUE;
2933 }
2934
2935
2936 //
2937 // Upgrade from V231 to V232
2938 //
2939
2940 static BOOL H_UpgradeFromV231(int currVersion, int newVersion)
2941 {
2942 static TCHAR batch[] =
2943 _T("ALTER TABLE object_properties ADD submap_id integer\n")
2944 _T("UPDATE object_properties SET submap_id=0\n")
2945 _T("DROP TABLE maps\n")
2946 _T("DROP TABLE map_access_lists\n")
2947 _T("DROP TABLE submaps\n")
2948 _T("DROP TABLE submap_object_positions\n")
2949 _T("DROP TABLE submap_links\n")
2950 _T("<END>");
2951
2952 CHK_EXEC(SQLBatch(batch));
2953
2954 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='232' WHERE var_name='SchemaVersion'")));
2955 return TRUE;
2956 }
2957
2958
2959 //
2960 // Upgrade from V230 to V231
2961 //
2962
2963 static BOOL H_UpgradeFromV230(int currVersion, int newVersion)
2964 {
2965 static TCHAR batch[] =
2966 _T("ALTER TABLE nodes ADD bridge_base_addr varchar(15)\n")
2967 _T("UPDATE nodes SET bridge_base_addr='000000000000'\n")
2968 _T("<END>");
2969
2970 CHK_EXEC(SQLBatch(batch));
2971
2972 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='231' WHERE var_name='SchemaVersion'")));
2973 return TRUE;
2974 }
2975
2976
2977 //
2978 // Upgrade from V229 to V230
2979 //
2980
2981 static BOOL H_UpgradeFromV229(int currVersion, int newVersion)
2982 {
2983 static TCHAR batch1[] =
2984 _T("ALTER TABLE network_maps ADD bg_latitude varchar(20)\n")
2985 _T("ALTER TABLE network_maps ADD bg_longitude varchar(20)\n")
2986 _T("ALTER TABLE network_maps ADD bg_zoom integer\n")
2987 _T("ALTER TABLE dashboard_elements ADD layout_data $SQL:TEXT\n")
2988 _T("<END>");
2989
2990 CHK_EXEC(SQLBatch(batch1));
2991
2992 DB_RESULT hResult = SQLSelect(_T("SELECT dashboard_id,element_id,horizontal_span,vertical_span,horizontal_alignment,vertical_alignment FROM dashboard_elements"));
2993 if (hResult != NULL)
2994 {
2995 TCHAR query[1024], xml[1024];
2996
2997 int count = DBGetNumRows(hResult);
2998 for(int i = 0; i < count; i++)
2999 {
3000 _sntprintf(xml, 1024, _T("<layout><horizontalSpan>%d</horizontalSpan><verticalSpan>%d</verticalSpan><horizontalAlignment>%d</horizontalAlignment><verticalAlignment>%d</verticalAlignment></layout>"),
3001 (int)DBGetFieldLong(hResult, i, 2), (int)DBGetFieldLong(hResult, i, 3),
3002 (int)DBGetFieldLong(hResult, i, 4), (int)DBGetFieldLong(hResult, i, 5));
3003 _sntprintf(query, 1024, _T("UPDATE dashboard_elements SET layout_data=%s WHERE dashboard_id=%d AND element_id=%d"),
3004 (const TCHAR *)DBPrepareString(g_hCoreDB, xml), (int)DBGetFieldLong(hResult, i, 0), (int)DBGetFieldLong(hResult, i, 1));
3005 CHK_EXEC(SQLQuery(query));
3006 }
3007 DBFreeResult(hResult);
3008 }
3009 else
3010 {
3011 if (!g_bIgnoreErrors)
3012 return FALSE;
3013 }
3014
3015 static TCHAR batch2[] =
3016 _T("ALTER TABLE dashboard_elements DROP COLUMN horizontal_span\n")
3017 _T("ALTER TABLE dashboard_elements DROP COLUMN vertical_span\n")
3018 _T("ALTER TABLE dashboard_elements DROP COLUMN horizontal_alignment\n")
3019 _T("ALTER TABLE dashboard_elements DROP COLUMN vertical_alignment\n")
3020 _T("<END>");
3021
3022 CHK_EXEC(SQLBatch(batch2));
3023
3024 CreateConfigParam(_T("TileServerURL"), _T("http://tile.openstreetmap.org/"), 1, 0);
3025
3026 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='230' WHERE var_name='SchemaVersion'")));
3027 return TRUE;
3028 }
3029
3030
3031 //
3032 // Upgrade from V228 to V229
3033 //
3034
3035 static BOOL H_UpgradeFromV228(int currVersion, int newVersion)
3036 {
3037 CHK_EXEC(CreateTable(_T("CREATE TABLE dashboards (")
3038 _T(" id integer not null,")
3039 _T(" num_columns integer not null,")
3040 _T(" PRIMARY KEY(id))")));
3041
3042 CHK_EXEC(CreateTable(_T("CREATE TABLE dashboard_elements (")
3043 _T(" dashboard_id integer not null,")
3044 _T(" element_id integer not null,")
3045 _T(" element_type integer not null,")
3046 _T(" element_data $SQL:TEXT null,")
3047 _T(" horizontal_span integer not null,")
3048 _T(" vertical_span integer not null,")
3049 _T(" horizontal_alignment integer not null,")
3050 _T(" vertical_alignment integer not null,")
3051 _T(" PRIMARY KEY(dashboard_id,element_id))")));
3052
3053 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='229' WHERE var_name='SchemaVersion'")));
3054 return TRUE;
3055 }
3056
3057 /**
3058 * Upgrade from V227 to V228
3059 */
3060 static BOOL H_UpgradeFromV227(int currVersion, int newVersion)
3061 {
3062 CHK_EXEC(SQLQuery(_T("DROP TABLE web_maps")));
3063 CHK_EXEC(MigrateMaps());
3064 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='228' WHERE var_name='SchemaVersion'")));
3065 return TRUE;
3066 }
3067
3068 /**
3069 * Upgrade from V226 to V227
3070 */
3071 static BOOL H_UpgradeFromV226(int currVersion, int newVersion)
3072 {
3073 static TCHAR batch[] =
3074 _T("ALTER TABLE clusters ADD zone_guid integer\n")
3075 _T("UPDATE clusters SET zone_guid=0\n")
3076 _T("<END>");
3077
3078 CHK_EXEC(SQLBatch(batch));
3079
3080 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='227' WHERE var_name='SchemaVersion'")));
3081 return TRUE;
3082 }
3083
3084
3085 //
3086 // Upgrade from V225 to V226
3087 //
3088
3089 static BOOL H_UpgradeFromV225(int currVersion, int newVersion)
3090 {
3091 static TCHAR batch[] =
3092 _T("ALTER TABLE interfaces ADD flags integer\n")
3093 _T("UPDATE interfaces SET flags=0\n")
3094 _T("UPDATE interfaces SET flags=1 WHERE synthetic_mask<>0\n")
3095 _T("ALTER TABLE interfaces DROP COLUMN synthetic_mask\n")
3096 _T("<END>");
3097
3098 CHK_EXEC(SQLBatch(batch));
3099
3100 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='226' WHERE var_name='SchemaVersion'")));
3101 return TRUE;
3102 }
3103
3104
3105 //
3106 // Upgrade from V224 to V225
3107 //
3108
3109 static BOOL H_UpgradeFromV224(int currVersion, int newVersion)
3110 {
3111 static TCHAR batch[] =
3112 _T("ALTER TABLE interfaces ADD description varchar(255)\n")
3113 _T("UPDATE interfaces SET description=''\n"<