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