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