Created normal editors for Agent Config Policy and Log Parser Policy. Updated log...
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade.cpp
CommitLineData
6f33021d 1/*
5039dede 2** nxdbmgr - NetXMS database manager
c908b63b 3** Copyright (C) 2004-2016 Victor Kirhenshtein
5039dede
AK
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"
bf6fb6c3 24#include <nxevent.h>
5039dede 25
4f50e45c
VK
26/**
27 * Externals
28 */
7a41a06e
VK
29BOOL MigrateMaps();
30
50963ced
VK
31/**
32 * Pre-defined GUID mapping for GenerateGUID
33 */
34struct GUID_MAPPING
35{
36 UINT32 id;
37 const TCHAR *guid;
38};
39
271e3971
VK
40/**
41 * Generate GUIDs
42 */
50963ced 43static bool GenerateGUID(const TCHAR *table, const TCHAR *idColumn, const TCHAR *guidColumn, const GUID_MAPPING *mapping)
271e3971
VK
44{
45 TCHAR query[256];
46 _sntprintf(query, 256, _T("SELECT %s FROM %s"), idColumn, table);
47 DB_RESULT hResult = SQLSelect(query);
48 if (hResult == NULL)
49 return false;
50
50963ced
VK
51 uuid_t guid;
52 TCHAR buffer[64];
53
271e3971
VK
54 int count = DBGetNumRows(hResult);
55 for(int i = 0; i < count; i++)
56 {
50963ced
VK
57 const TCHAR *guidText = NULL;
58 UINT32 id = DBGetFieldULong(hResult, i, 0);
59 if (mapping != NULL)
60 {
61 for(int j = 0; mapping[j].guid != NULL; j++)
62 if (mapping[j].id == id)
63 {
64 guidText = mapping[j].guid;
65 break;
66 }
67 }
68 if (guidText == NULL)
69 {
70 _uuid_generate(guid);
71 guidText = _uuid_to_string(guid, buffer);
72 }
73 _sntprintf(query, 256, _T("UPDATE %s SET %s='%s' WHERE %s=%d"), table, guidColumn, guidText, idColumn, id);
271e3971
VK
74 if (!SQLQuery(query))
75 {
76 DBFreeResult(hResult);
77 return false;
78 }
79 }
80 DBFreeResult(hResult);
81 return true;
82}
83
4f50e45c
VK
84/**
85 * Create table
86 */
c85c8ef2 87static bool CreateTable(const TCHAR *pszQuery)
5039dede 88{
7618e362 89 String query(pszQuery);
5039dede 90
2a964810
VK
91 query.replace(_T("$SQL:TEXT"), g_pszSqlType[g_dbSyntax][SQL_TYPE_TEXT]);
92 query.replace(_T("$SQL:TXT4K"), g_pszSqlType[g_dbSyntax][SQL_TYPE_TEXT4K]);
93 query.replace(_T("$SQL:INT64"), g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64]);
94 if (g_dbSyntax == DB_SYNTAX_MYSQL)
7618e362 95 query += g_pszTableSuffix;
c85c8ef2 96 return SQLQuery(query);
5039dede
AK
97}
98
4f50e45c 99/**
c85c8ef2 100 * Check if cnfiguration variable exist
4f50e45c 101 */
c85c8ef2 102static bool IsConfigurationVariableExist(const TCHAR *name)
5039dede 103{
c85c8ef2
VK
104 bool found = false;
105 TCHAR query[256];
106 _sntprintf(query, 256, _T("SELECT var_value FROM config WHERE var_name='%s'"), name);
107 DB_RESULT hResult = DBSelect(g_hCoreDB, query);
5039dede
AK
108 if (hResult != 0)
109 {
c85c8ef2 110 found = (DBGetNumRows(hResult) > 0);
5039dede
AK
111 DBFreeResult(hResult);
112 }
c85c8ef2
VK
113 return found;
114}
5039dede 115
c85c8ef2
VK
116/**
117 * Create configuration parameter if it doesn't exist (unless bForceUpdate set to true)
118 */
119bool CreateConfigParam(const TCHAR *name, const TCHAR *value, const TCHAR *description, char dataType, bool isVisible, bool needRestart, bool isPublic, bool forceUpdate)
120{
121 bool success = true;
2589cc10 122 TCHAR szQuery[3024];
c85c8ef2 123 if (!IsConfigurationVariableExist(name))
5039dede 124 {
2589cc10 125 _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)"),
c85c8ef2 126 (const TCHAR *)DBPrepareString(g_hCoreDB, name, 63),
2589cc10 127 (const TCHAR *)DBPrepareString(g_hCoreDB, value, 2000), isVisible ? 1 : 0, needRestart ? 1 : 0,
c85c8ef2
VK
128 isPublic ? _T('Y') : _T('N'), dataType, (const TCHAR *)DBPrepareString(g_hCoreDB, description, 255));
129 success = SQLQuery(szQuery);
5039dede 130 }
c85c8ef2 131 else if (forceUpdate)
5039dede 132 {
2589cc10 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));
c85c8ef2 135 success = SQLQuery(szQuery);
5039dede 136 }
c85c8ef2
VK
137 return success;
138}
139
140/**
141 * Create configuration parameter if it doesn't exist (unless bForceUpdate set to true)
142 */
143bool CreateConfigParam(const TCHAR *name, const TCHAR *value, bool isVisible, bool needRestart, bool forceUpdate)
144{
145 bool success = true;
2589cc10 146 TCHAR szQuery[3024];
c85c8ef2
VK
147 if (!IsConfigurationVariableExist(name))
148 {
2589cc10 149 _sntprintf(szQuery, 3024, _T("INSERT INTO config (var_name,var_value,is_visible,need_server_restart) VALUES (%s,%s,%d,%d)"),
c85c8ef2 150 (const TCHAR *)DBPrepareString(g_hCoreDB, name, 63),
2589cc10 151 (const TCHAR *)DBPrepareString(g_hCoreDB, value, 2000), isVisible ? 1 : 0, needRestart ? 1 : 0);
c85c8ef2
VK
152 success = SQLQuery(szQuery);
153 }
154 else if (forceUpdate)
155 {
2589cc10 156 _sntprintf(szQuery, 3024, _T("UPDATE config SET var_value=%s WHERE var_name=%s"),
157 (const TCHAR *)DBPrepareString(g_hCoreDB, value, 2000), (const TCHAR *)DBPrepareString(g_hCoreDB, name, 63));
c85c8ef2
VK
158 success = SQLQuery(szQuery);
159 }
160 return success;
5039dede
AK
161}
162
4f50e45c
VK
163/**
164 * Set primary key constraint
165 */
5039dede
AK
166static BOOL SetPrimaryKey(const TCHAR *table, const TCHAR *key)
167{
168 TCHAR query[4096];
169
2a964810 170 if (g_dbSyntax == DB_SYNTAX_SQLITE)
5039dede 171 return TRUE; // SQLite does not support adding constraints
6f33021d 172
5039dede
AK
173 _sntprintf(query, 4096, _T("ALTER TABLE %s ADD PRIMARY KEY (%s)"), table, key);
174 return SQLQuery(query);
175}
176
4f50e45c
VK
177/**
178 * Drop primary key from table
179 */
9bfc9a6b
VK
180static BOOL DropPrimaryKey(const TCHAR *table)
181{
182 TCHAR query[1024];
183 DB_RESULT hResult;
184 BOOL success;
185
2a964810 186 switch(g_dbSyntax)
9bfc9a6b 187 {
c849da55
VK
188 case DB_SYNTAX_DB2:
189 case DB_SYNTAX_INFORMIX:
9bfc9a6b 190 case DB_SYNTAX_MYSQL:
c849da55 191 case DB_SYNTAX_ORACLE:
9bfc9a6b
VK
192 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP PRIMARY KEY"), table);
193 success = SQLQuery(query);
194 break;
195 case DB_SYNTAX_PGSQL:
196 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s_pkey"), table, table);
197 success = SQLQuery(query);
198 break;
199 case DB_SYNTAX_MSSQL:
200 success = FALSE;
201 _sntprintf(query, 1024, _T("SELECT name FROM sysobjects WHERE xtype='PK' AND parent_obj=OBJECT_ID('%s')"), table);
202 hResult = SQLSelect(query);
203 if (hResult != NULL)
204 {
205 if (DBGetNumRows(hResult) > 0)
206 {
207 TCHAR objName[512];
208
209 DBGetField(hResult, 0, 0, objName, 512);
210 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s"), table, objName);
211 success = SQLQuery(query);
212 }
213 DBFreeResult(hResult);
214 }
215 break;
216 default: // Unsupported DB engine
217 success = FALSE;
218 break;
219 }
c849da55
VK
220
221 if ((g_dbSyntax == DB_SYNTAX_DB2) && success)
222 {
223 _sntprintf(query, 1024, _T("CALL Sysproc.admin_cmd('REORG TABLE %s')"), table);
224 success = SQLQuery(query);
225 }
9bfc9a6b
VK
226 return success;
227}
228
4f50e45c
VK
229/**
230 * Convert strings from # encoded form to normal form
231 */
d6bf58f9 232static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *idColumn2, const TCHAR *column, bool isStringId)
643c9dcb
VK
233{
234 DB_RESULT hResult;
235 TCHAR *query;
4e0e77e6 236 size_t queryLen = 512;
643c9dcb
VK
237 BOOL success = FALSE;
238
c29fb885 239 query = (TCHAR *)malloc(queryLen * sizeof(TCHAR));
fe12a1ea 240
2a964810 241 switch(g_dbSyntax)
a4743a0f
VK
242 {
243 case DB_SYNTAX_MSSQL:
9f6712bc 244 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE CAST(%s AS nvarchar(4000))=N'#00'"), table, column, column);
a4743a0f
VK
245 break;
246 case DB_SYNTAX_ORACLE:
247 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE to_char(%s)='#00'"), table, column, column);
248 break;
249 default:
250 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE %s='#00'"), table, column, column);
251 break;
252 }
fe12a1ea
VK
253 if (!SQLQuery(query))
254 {
255 free(query);
256 return FALSE;
257 }
258
6f33021d 259 _sntprintf(query, queryLen, _T("SELECT %s,%s%s%s FROM %s WHERE %s LIKE '%%#%%'"),
98cd01bb 260 idColumn, column, (idColumn2 != NULL) ? _T(",") : _T(""), (idColumn2 != NULL) ? idColumn2 : _T(""), table, column);
643c9dcb
VK
261 hResult = SQLSelect(query);
262 if (hResult == NULL)
263 {
264 free(query);
265 return FALSE;
266 }
267
268 int count = DBGetNumRows(hResult);
269 for(int i = 0; i < count; i++)
270 {
643c9dcb 271 TCHAR *value = DBGetField(hResult, i, 1, NULL, 0);
035a4d73 272 if (_tcschr(value, _T('#')) != NULL)
643c9dcb 273 {
035a4d73
VK
274 DecodeSQLString(value);
275 String newValue = DBPrepareString(g_hCoreDB, value);
4e0e77e6 276 if (newValue.length() + 256 > queryLen)
035a4d73 277 {
4e0e77e6 278 queryLen = newValue.length() + 256;
c29fb885 279 query = (TCHAR *)realloc(query, queryLen * sizeof(TCHAR));
035a4d73 280 }
d6bf58f9 281 if (isStringId)
98cd01bb 282 {
d6bf58f9
VK
283 TCHAR *id = DBGetField(hResult, i, 0, NULL, 0);
284 if (idColumn2 != NULL)
285 {
286 TCHAR *id2 = DBGetField(hResult, i, 2, NULL, 0);
287 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s AND %s=%s"),
288 table, column, (const TCHAR *)newValue,
6f33021d 289 idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id),
d6bf58f9
VK
290 idColumn2, (const TCHAR *)DBPrepareString(g_hCoreDB, id2));
291 }
292 else
293 {
294 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s"), table, column,
295 (const TCHAR *)newValue, idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id));
296 }
297 free(id);
98cd01bb
VK
298 }
299 else
300 {
d6bf58f9
VK
301 INT64 id = DBGetFieldInt64(hResult, i, 0);
302 if (idColumn2 != NULL)
303 {
304 INT64 id2 = DBGetFieldInt64(hResult, i, 2);
6f33021d 305 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT _T(" AND %s=") INT64_FMT,
d6bf58f9
VK
306 table, column, (const TCHAR *)newValue, idColumn, id, idColumn2, id2);
307 }
308 else
309 {
310 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT, table, column,
311 (const TCHAR *)newValue, idColumn, id);
312 }
98cd01bb 313 }
035a4d73
VK
314 if (!SQLQuery(query))
315 goto cleanup;
643c9dcb 316 }
643c9dcb
VK
317 }
318 success = TRUE;
319
320cleanup:
321 DBFreeResult(hResult);
a4743a0f 322 free(query);
643c9dcb
VK
323 return success;
324}
325
98cd01bb
VK
326static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *column)
327{
d6bf58f9 328 return ConvertStrings(table, idColumn, NULL, column, false);
98cd01bb
VK
329}
330
4f50e45c 331/**
70ffb771 332 * Set column nullable (currently only Oracle and PostgreSQL)
4f50e45c 333 */
480e036b 334static BOOL SetColumnNullable(const TCHAR *table, const TCHAR *column)
1024e962
VK
335{
336 TCHAR query[1024] = _T("");
337
2a964810 338 switch(g_dbSyntax)
1024e962
VK
339 {
340 case DB_SYNTAX_ORACLE:
341 _sntprintf(query, 1024, _T("DECLARE already_null EXCEPTION; ")
342 _T("PRAGMA EXCEPTION_INIT(already_null, -1451); ")
f57209fd
VK
343 _T("BEGIN EXECUTE IMMEDIATE 'ALTER TABLE %s MODIFY %s null'; ")
344 _T("EXCEPTION WHEN already_null THEN null; END;"), table, column);
1024e962 345 break;
480e036b
VK
346 case DB_SYNTAX_PGSQL:
347 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL"), table, column);
348 break;
1024e962
VK
349 default:
350 break;
351 }
352
353 return (query[0] != 0) ? SQLQuery(query) : TRUE;
354}
355
480e036b
VK
356/**
357 * Resize varchar column
358 */
b69710b9 359static BOOL ResizeColumn(const TCHAR *table, const TCHAR *column, int newSize, bool nullable)
480e036b
VK
360{
361 TCHAR query[1024];
362
2a964810 363 switch(g_dbSyntax)
480e036b
VK
364 {
365 case DB_SYNTAX_DB2:
366 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE varchar(%d)"), table, column, newSize);
367 break;
368 case DB_SYNTAX_MSSQL:
b69710b9 369 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s varchar(%d) %s NULL"), table, column, newSize, nullable ? _T("") : _T("NOT"));
480e036b
VK
370 break;
371 case DB_SYNTAX_PGSQL:
372 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s TYPE varchar(%d)"), table, column, newSize);
373 break;
374 case DB_SYNTAX_SQLITE:
375 /* TODO: add SQLite support */
376 query[0] = 0;
377 break;
378 default:
379 _sntprintf(query, 1024, _T("ALTER TABLE %s MODIFY %s varchar(%d)"), table, column, newSize);
380 break;
381 }
382
383 return (query[0] != 0) ? SQLQuery(query) : TRUE;
384}
385
4f50e45c
VK
386/**
387 * Create new event template
388 */
aa16f82b
VK
389static BOOL CreateEventTemplate(int code, const TCHAR *name, int severity, int flags, const TCHAR *message, const TCHAR *description)
390{
480e036b 391 TCHAR query[4096];
aa16f82b 392
480e036b
VK
393 _sntprintf(query, 4096, _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (%d,'%s',%d,%d,%s,%s)"),
394 code, name, severity, flags, (const TCHAR *)DBPrepareString(g_hCoreDB, message),
395 (const TCHAR *)DBPrepareString(g_hCoreDB, description));
aa16f82b
VK
396 return SQLQuery(query);
397}
398
22aaa779
VK
399/**
400 * Re-create TDATA tables
401 */
2f1bc68b 402static BOOL RecreateTData(const TCHAR *className, bool multipleTables, bool indexFix)
22aaa779 403{
1d4f7890 404 TCHAR query[1024];
22aaa779
VK
405 _sntprintf(query, 256, _T("SELECT id FROM %s"), className);
406 DB_RESULT hResult = SQLSelect(query);
407 if (hResult != NULL)
408 {
409 int count = DBGetNumRows(hResult);
410 for(int i = 0; i < count; i++)
411 {
2f1bc68b 412 bool recreateTables = true;
22aaa779
VK
413 DWORD id = DBGetFieldULong(hResult, i, 0);
414
2f1bc68b 415 if (indexFix)
1d4f7890 416 {
2f1bc68b
VK
417 _sntprintf(query, 256, _T("SELECT count(*) FROM dc_tables WHERE node_id=%d"), id);
418 DB_RESULT hResultCount = SQLSelect(query);
419 if (hResultCount != NULL)
22aaa779 420 {
7d4f8ace 421 recreateTables = (DBGetFieldLong(hResultCount, 0, 0) == 0);
2f1bc68b
VK
422 DBFreeResult(hResultCount);
423 }
424
425 if (!recreateTables)
426 {
a9f5aa55 427 _sntprintf(query, 256, _T("CREATE INDEX idx_tdata_rec_%d_id ON tdata_records_%d(record_id)"), id, id);
2f1bc68b
VK
428 if (!SQLQuery(query))
429 {
430 if (!g_bIgnoreErrors)
431 {
432 DBFreeResult(hResult);
433 return FALSE;
434 }
435 }
22aaa779
VK
436 }
437 }
438
2f1bc68b 439 if (recreateTables)
22aaa779 440 {
2f1bc68b 441 if (multipleTables)
22aaa779 442 {
2f1bc68b
VK
443 _sntprintf(query, 1024, _T("DROP TABLE tdata_rows_%d\nDROP TABLE tdata_records_%d\nDROP TABLE tdata_%d\n<END>"), id, id, id);
444 }
445 else
446 {
447 _sntprintf(query, 256, _T("DROP TABLE tdata_%d\n<END>"), id);
448 }
449 if (!SQLBatch(query))
450 {
451 if (!g_bIgnoreErrors)
452 {
453 DBFreeResult(hResult);
454 return FALSE;
455 }
456 }
457
458 if (!CreateTDataTables(id))
459 {
460 if (!g_bIgnoreErrors)
461 {
462 DBFreeResult(hResult);
463 return FALSE;
464 }
22aaa779
VK
465 }
466 }
467 }
468 DBFreeResult(hResult);
469 }
470 else
471 {
472 if (!g_bIgnoreErrors)
473 return FALSE;
474 }
385b1f20 475 return TRUE;
476}
477
c75e9ee4
VK
478/**
479 * Convert network masks from dotted decimal format to number of bits
480 */
481static BOOL ConvertNetMasks(const TCHAR *table, const TCHAR *column, const TCHAR *idColumn, const TCHAR *idColumn2 = NULL, const TCHAR *condition = NULL)
482{
483 TCHAR query[256];
fcf91b2e 484
c75e9ee4
VK
485 if (idColumn2 != NULL)
486 _sntprintf(query, 256, _T("SELECT %s,%s,%s FROM %s"), idColumn, column, idColumn2, table);
487 else
488 _sntprintf(query, 256, _T("SELECT %s,%s FROM %s"), idColumn, column, table);
489 DB_RESULT hResult = SQLSelect(query);
490 if (hResult == NULL)
491 return FALSE;
492
a19fa589 493 BOOL success = SQLDropColumn(table, column);
fcf91b2e 494
c75e9ee4
VK
495 if (success)
496 {
497 _sntprintf(query, 256, _T("ALTER TABLE %s ADD %s integer"), table, column);
498 success = SQLQuery(query);
499 }
500
501 if (success)
502 {
503 int count = DBGetNumRows(hResult);
504 for(int i = 0; (i < count) && success; i++)
505 {
506 if (idColumn2 != NULL)
507 {
508 TCHAR id2[256];
fcf91b2e 509 _sntprintf(query, 256, _T("UPDATE %s SET %s=%d WHERE %s=%d AND %s='%s'"),
c75e9ee4
VK
510 table, column, BitsInMask(DBGetFieldIPAddr(hResult, i, 1)), idColumn, DBGetFieldLong(hResult, i, 0),
511 idColumn2, DBGetField(hResult, i, 2, id2, 256));
512 }
513 else
514 {
fcf91b2e 515 _sntprintf(query, 256, _T("UPDATE %s SET %s=%d WHERE %s=%d"),
c75e9ee4
VK
516 table, column, BitsInMask(DBGetFieldIPAddr(hResult, i, 1)), idColumn, DBGetFieldLong(hResult, i, 0));
517 }
518 success = SQLQuery(query);
519 }
520 }
521
522 DBFreeResult(hResult);
523 return success;
524}
525
b2a15edc
VK
526/**
527 * Convert object tool macros to new format
528 */
529static bool ConvertObjectToolMacros(UINT32 id, const TCHAR *text, const TCHAR *column)
530{
531 if (_tcschr(text, _T('%')) == NULL)
532 return true; // nothing to convert
533
534 String s;
535 for(const TCHAR *p = text; *p != 0; p++)
536 {
537 if (*p == _T('%'))
538 {
539 TCHAR name[256];
540 int i = 0;
541 for(p++; (*p != _T('%')) && (*p != 0); p++)
542 {
543 if (i < 255)
544 name[i++] = *p;
545 }
546 if (*p == 0)
547 break; // malformed string
548 name[i] = 0;
549 if (!_tcscmp(name, _T("ALARM_ID")))
550 {
551 s.append(_T("%Y"));
552 }
553 else if (!_tcscmp(name, _T("ALARM_MESSAGE")))
554 {
555 s.append(_T("%A"));
556 }
557 else if (!_tcscmp(name, _T("ALARM_SEVERITY")))
558 {
559 s.append(_T("%s"));
560 }
561 else if (!_tcscmp(name, _T("ALARM_SEVERITY_TEXT")))
562 {
563 s.append(_T("%S"));
564 }
565 else if (!_tcscmp(name, _T("ALARM_STATE")))
566 {
567 s.append(_T("%y"));
568 }
569 else if (!_tcscmp(name, _T("OBJECT_ID")))
570 {
571 s.append(_T("%I"));
572 }
573 else if (!_tcscmp(name, _T("OBJECT_IP_ADDR")))
574 {
575 s.append(_T("%a"));
576 }
577 else if (!_tcscmp(name, _T("OBJECT_NAME")))
578 {
579 s.append(_T("%n"));
580 }
581 else if (!_tcscmp(name, _T("USERNAME")))
582 {
583 s.append(_T("%U"));
584 }
585 else
586 {
587 s.append(_T("%{"));
588 s.append(name);
589 s.append(_T('}'));
590 }
591 }
592 else
593 {
594 s.append(*p);
595 }
596 }
597
598 String query = _T("UPDATE object_tools SET ");
599 query.append(column);
600 query.append(_T('='));
601 query.append(DBPrepareString(g_hCoreDB, s));
602 query.append(_T(" WHERE tool_id="));
603 query.append(id);
604 return SQLQuery(query);
605}
8a1519ce 606
83aa5ebd
VK
607/**
608 * Create library script
609 */
610static bool CreateLibraryScript(UINT32 id, const TCHAR *name, const TCHAR *code)
611{
612 // Check if script exists
613 TCHAR query[256];
614 _sntprintf(query, 256, _T("SELECT script_id FROM script_library WHERE script_id=%d OR script_name=%s"),
615 id, (const TCHAR *)DBPrepareString(g_hCoreDB, name));
616 DB_RESULT hResult = SQLSelect(query);
617 if (hResult == NULL)
618 return false;
619 bool exist = (DBGetNumRows(hResult) > 0);
620 DBFreeResult(hResult);
621 if (exist)
622 return true;
623
624 DB_STATEMENT hStmt = DBPrepare(g_hCoreDB, _T("INSERT INTO script_library (script_id,script_name,script_code) VALUES (?,?,?)"));
625 if (hStmt == NULL)
626 return false;
627
628 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, id);
629 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, name, DB_BIND_STATIC);
630 DBBind(hStmt, 3, DB_SQLTYPE_TEXT, code, DB_BIND_STATIC);
631
632 bool success = SQLExecute(hStmt);
633 DBFreeStatement(hStmt);
634 return success;
635}
636
e2a9c5fc
VK
637/**
638 * Check if an event pair is handled by any EPP rules
639 */
640static bool IsEventPairInUse(UINT32 code1, UINT32 code2)
641{
642 TCHAR query[256];
643 _sntprintf(query, 256, _T("SELECT count(*) FROM policy_event_list WHERE event_code=%d OR event_code=%d"), code1, code2);
644 DB_RESULT hResult = SQLSelect(query);
645 if (hResult == NULL)
646 return false;
647 bool inUse = (DBGetFieldLong(hResult, 0, 0) > 0);
648 DBFreeResult(hResult);
649 return inUse;
650}
651
652/**
653 * Return the next free EPP rule ID
654 */
655static int NextFreeEPPruleID()
656{
657 int ruleId = 1;
658 DB_RESULT hResult = SQLSelect(_T("SELECT max(rule_id) FROM event_policy"));
659 if (hResult != NULL)
660 {
661 ruleId = DBGetFieldLong(hResult, 0, 0) + 1;
662 DBFreeResult(hResult);
663 }
664 return ruleId;
665}
666
50963ced
VK
667/**
668 * Upgrade from V396 to V397
669 */
670static BOOL H_UpgradeFromV396(int currVersion, int newVersion)
671{
672 static GUID_MAPPING eventGuidMapping[] = {
673 { EVENT_NODE_ADDED, _T("8d34acfd-dad6-4f6e-b6a8-1189683591ef") },
674 { EVENT_SUBNET_ADDED, _T("75fc3f8b-768f-46b4-bf44-72949436a679") },
675 { EVENT_INTERFACE_ADDED, _T("33cb8f9c-9427-459c-8a71-45c73f5cc183") },
676 { EVENT_INTERFACE_UP, _T("09ee209a-0e75-434f-b8c8-399d93305d7b") },
677 { EVENT_INTERFACE_DOWN, _T("d9a6d46d-97f8-48eb-a86a-2c0f6b150d0d") },
678 { EVENT_INTERFACE_UNKNOWN, _T("ecb47be1-f911-4c1f-9b00-d0d21694071d") },
679 { EVENT_INTERFACE_DISABLED, _T("2f3123a2-425f-47db-9c6b-9bc05a7fba2d") },
680 { EVENT_INTERFACE_TESTING, _T("eb500e5c-3560-4394-8f5f-80aa67036f13") },
681 { EVENT_INTERFACE_UNEXPECTED_UP, _T("ff21a165-9253-4ecc-929a-ffd1e388d504") },
682 { EVENT_INTERFACE_EXPECTED_DOWN, _T("911358f4-d2a1-4465-94d7-ce4bc5c38860") },
683 { EVENT_NODE_NORMAL, _T("03bc11c0-ec20-43be-be45-e60846f744dc") },
684 { EVENT_NODE_WARNING, _T("1c80deab-aafb-43a7-93a7-1330dd563b47") },
685 { EVENT_NODE_MINOR, _T("84eaea00-4ed7-41eb-9079-b783e5c60651") },
686 { EVENT_NODE_MAJOR, _T("27035c88-c27a-4c16-97b3-4658d34a5f63") },
687 { EVENT_NODE_CRITICAL, _T("8f2e98f8-1cd4-4e12-b41f-48b5c60ebe8e") },
688 { EVENT_NODE_UNKNOWN, _T("6933cce0-fe1f-4123-817f-af1fb9f0eab4") },
689 { EVENT_NODE_UNMANAGED, _T("a8356ba7-51b7-4487-b74e-d12132db233c") },
690 { EVENT_NODE_FLAGS_CHANGED, _T("b04e39f5-d3a7-4d9a-b594-37132f5eaf34") },
691 { EVENT_SNMP_FAIL, _T("d2fc3b0c-1215-4a92-b8f3-47df5d753602") },
692 { EVENT_AGENT_FAIL, _T("ba484457-3594-418e-a72a-65336055d025") },
693 { EVENT_INTERFACE_DELETED, _T("ad7e9856-e361-4095-9361-ccc462d93624") },
694 { EVENT_THRESHOLD_REACHED, _T("05161c3d-7ceb-406f-af0a-af5c77f324a5") },
695 { EVENT_THRESHOLD_REARMED, _T("25eef3a7-6158-4c5e-b4e3-8a7aa7ade73c") },
696 { EVENT_SUBNET_DELETED, _T("af188eb3-e84f-4fd9-aecf-f1ba934a9f1a") },
697 { EVENT_THREAD_HANGS, _T("df247d13-a63a-43fe-bb02-cb41718ee387") },
698 { EVENT_THREAD_RUNNING, _T("5589f6ce-7133-44db-8e7a-e1452d636a9a") },
699 { EVENT_SMTP_FAILURE, _T("1e376009-0d26-4b86-87a2-f4715a02fb38") },
700 { EVENT_MAC_ADDR_CHANGED, _T("61916ef0-1eee-4df7-a95b-150928d47962") },
701 { EVENT_INCORRECT_NETMASK, _T("86c08c55-416e-4ac4-bf2b-302b5fddbd68") },
702 { EVENT_NODE_DOWN, _T("ce34f0d0-5b21-48c2-8788-8ed5ee547023") },
703 { EVENT_NODE_UP, _T("05f180b6-62e7-4bc4-8a8d-34540214254b") },
704 { EVENT_SERVICE_DOWN, _T("89caacb5-d2cf-493b-862f-cddbfecac5b6") },
705 { EVENT_SERVICE_UP, _T("ab35e7c7-2428-44db-ad43-57fe551bb8cc") },
706 { EVENT_SERVICE_UNKNOWN, _T("d891adae-49fe-4442-a8f3-0ca37ca8820a") },
707 { EVENT_SMS_FAILURE, _T("c349bf75-458a-4d43-9c27-f71ea4bb06e2") },
708 { EVENT_SNMP_OK, _T("a821086b-1595-40db-9148-8d770d30a54b") },
709 { EVENT_AGENT_OK, _T("9c15299a-f2e3-4440-84c5-b17dea87ae2a") },
710 { EVENT_SCRIPT_ERROR, _T("2cc78efe-357a-4278-932f-91e36754c775") },
711 { EVENT_CONDITION_ACTIVATED, _T("16a86780-b73a-4601-929c-0c503bd06401") },
712 { EVENT_CONDITION_DEACTIVATED, _T("926d15d2-9761-4bb6-a1ce-64175303796f") },
713 { EVENT_DB_CONNECTION_LOST, _T("0311e9c8-8dcf-4a5b-9036-8cff034409ff") },
714 { EVENT_DB_CONNECTION_RESTORED, _T("d36259a7-5f6b-4e3c-bb6f-17d1f8ac950d") },
715 { EVENT_CLUSTER_RESOURCE_MOVED, _T("44abe5f3-a7c9-4fbd-8d10-53be172eae83") },
716 { EVENT_CLUSTER_RESOURCE_DOWN, _T("c3b1d4b5-2e41-4a2f-b379-9d74ebba3a25") },
717 { EVENT_CLUSTER_RESOURCE_UP, _T("ef6fff96-0cbb-4030-aeba-2473a80c6568") },
718 { EVENT_CLUSTER_DOWN, _T("8f14d0f7-08d4-4422-92f4-469e5eef93ef") },
719 { EVENT_CLUSTER_UP, _T("4a9cdb65-aa44-42f2-99b0-1e302aec10f6") },
720 { EVENT_ALARM_TIMEOUT, _T("4ae4f601-327b-4ef8-9740-8600a1ba2acd") },
721 { EVENT_LOG_RECORD_MATCHED, _T("d9326159-5c60-410f-990e-fae88df7fdd4") },
722 { EVENT_EVENT_STORM_DETECTED, _T("c98d8575-d134-4044-ba67-75c5f5d0f6e0") },
723 { EVENT_EVENT_STORM_ENDED, _T("dfd5e3ba-3182-4deb-bc32-9e6b8c1c6546") },
724 { EVENT_NETWORK_CONNECTION_LOST, _T("3cb0921a-87a1-46e4-8be1-82ad2dda0015") },
725 { EVENT_NETWORK_CONNECTION_RESTORED, _T("1c61b3e0-389a-47ac-8469-932a907392bc") },
726 { EVENT_DB_QUERY_FAILED, _T("5f35d646-63b6-4dcd-b94a-e2ccd060686a") },
727 { EVENT_DCI_UNSUPPORTED, _T("28367b5b-1541-4526-8cbe-91a17ed31ba4") },
728 { EVENT_DCI_DISABLED, _T("50196042-0619-4420-9471-16b7c25c0213") },
729 { EVENT_DCI_ACTIVE, _T("740b6810-b355-46f4-a921-65118504af18") },
730 { EVENT_IP_ADDRESS_CHANGED, _T("517b6d2a-f5c6-46aa-969d-48e62e05e3bf") },
731 { EVENT_CONTAINER_AUTOBIND, _T("611133e2-1f76-446f-b278-9d500a823611") },
732 { EVENT_CONTAINER_AUTOUNBIND, _T("e57603be-0d81-41aa-b07c-12d08640561c") },
733 { EVENT_TEMPLATE_AUTOAPPLY, _T("bf084945-f928-4428-8c6c-d2965addc832") },
734 { EVENT_TEMPLATE_AUTOREMOVE, _T("8f7a4b4a-d0a2-4404-9b66-fdbc029f42cf") },
735 { EVENT_NODE_UNREACHABLE, _T("47bba2ce-c795-4e56-ad44-cbf05741e1ff") },
736 { EVENT_TABLE_THRESHOLD_ACTIVATED, _T("c08a1cfe-3128-40c2-99cc-378e7ef91f79") },
737 { EVENT_TABLE_THRESHOLD_DEACTIVATED, _T("479085e7-e1d1-4f2a-9d96-1d522f51b26a") },
738 { EVENT_IF_PEER_CHANGED, _T("a3a5c1df-9d96-4e98-9e06-b3157dbf39f0") },
739 { EVENT_AP_ADOPTED, _T("5aaee261-0c5d-44e0-b2f0-223bbba5297d") },
740 { EVENT_AP_UNADOPTED, _T("846a3581-aad1-4e17-9c55-9bd2e6b1247b") },
741 { EVENT_AP_DOWN, _T("2c8c6208-d3ab-4b8c-926a-872f4d8abcee") },
742 { EVENT_IF_MASK_CHANGED, _T("f800e593-057e-4aec-9e47-be0f7718c5c4") },
743 { EVENT_IF_IPADDR_ADDED, _T("475bdca6-543e-410b-9aff-c217599e0fe6") },
744 { EVENT_IF_IPADDR_DELETED, _T("ef477387-eb50-4a1a-bf90-717502b9873c") },
745 { EVENT_MAINTENANCE_MODE_ENTERED, _T("5f6c8b1c-f162-413e-8028-80e7ad2c362d") },
746 { EVENT_MAINTENANCE_MODE_LEFT, _T("cab06848-a622-430d-8b4c-addeea732657") },
747 { EVENT_SNMP_UNMATCHED_TRAP, _T("fc3613f7-d151-4221-9acd-d28b6f804335") },
748 { EVENT_SNMP_COLD_START, _T("39920e99-97bd-4d61-a462-43f89ba6fbdf") },
749 { EVENT_SNMP_WARM_START, _T("0aa888c1-eba6-4fe7-a37a-b85f2b373bdc") },
750 { EVENT_SNMP_LINK_DOWN, _T("b71338cc-137d-473c-a0f1-6b131086af56") },
751 { EVENT_SNMP_LINK_UP, _T("03da14a7-e39c-4a46-a7cb-4bf77ec7936c") },
752 { EVENT_SNMP_AUTH_FAILURE, _T("37020cb0-dde7-487b-9cfb-0d5ee771aa13") },
753 { EVENT_SNMP_EGP_NL, _T("aecf5fa4-390c-4125-be10-df8b0e669fe1") },
754 { 0, NULL }
755 };
756
757 CHK_EXEC(SQLQuery(_T("ALTER TABLE event_cfg ADD guid varchar(36)")));
758 CHK_EXEC(GenerateGUID(_T("event_cfg"), _T("event_code"), _T("guid"), eventGuidMapping));
759 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='397' WHERE var_name='SchemaVersion'")));
760 return TRUE;
761}
762
3df8bccd
Z
763/**
764 * Upgrade from V395 to V396
765 */
766static BOOL H_UpgradeFromV395(int currVersion, int newVersion)
767{
768 CHK_EXEC(CreateTable(
769 _T("CREATE TABLE ap_log_parser (")
770 _T(" policy_id integer not null,")
771 _T(" file_content $SQL:TEXT null,")
772 _T(" PRIMARY KEY(policy_id))")));
773
774 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='396' WHERE var_name='SchemaVersion'")));
775 return TRUE;
776}
777
fdef3439
Z
778/**
779 * Upgrade from V394 to V395
780 */
781static BOOL H_UpgradeFromV394(int currVersion, int newVersion)
782{
f6456d80 783 CHK_EXEC(SQLQuery(_T("UPDATE config SET need_server_restart='1' WHERE var_name='OfflineDataRelevanceTime'")));
fdef3439
Z
784 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='395' WHERE var_name='SchemaVersion'")));
785 return TRUE;
786}
787
7f418694
Z
788/**
789 * Upgrade from V393 to V394
790 */
791static BOOL H_UpgradeFromV393(int currVersion, int newVersion)
792{
c0e0623b 793 CHK_EXEC(CreateConfigParam(_T("OfflineDataRelevanceTime"), _T("86400"), _T("Time period in seconds within which received offline data still relevant for threshold validation"), 'I', true, false, false, false));
7f418694
Z
794 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='394' WHERE var_name='SchemaVersion'")));
795 return TRUE;
796}
797
c908b63b
VK
798/**
799 * Upgrade from V392 to V393
800 */
801static BOOL H_UpgradeFromV392(int currVersion, int newVersion)
802{
803 CHK_EXEC(CreateConfigParam(_T("DefaultInterfaceExpectedState"), _T("0"), _T("Default expected state for new interface objects"), 'C', true, false, false, false));
804 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='393' WHERE var_name='SchemaVersion'")));
805 return TRUE;
806}
807
808/**
03b96461
VK
809 * Upgrade from V391 to V392
810 */
811static BOOL H_UpgradeFromV391(int currVersion, int newVersion)
812{
813 CHK_EXEC(CreateConfigParam(_T("ImportConfigurationOnStartup"), _T("0"), _T("Import configuration from local files on server startup"), 'B', true, true, false, false));
814 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='392' WHERE var_name='SchemaVersion'")));
815 return TRUE;
816}
817
c908b63b 818/**
f61f151c
AK
819 * Upgrade from V390 to V391
820 */
821static BOOL H_UpgradeFromV390(int currVersion, int newVersion)
822{
03b96461
VK
823 CHK_EXEC(CreateTable(
824 _T("CREATE TABLE zmq_subscription (")
825 _T(" object_id integer not null,")
826 _T(" subscription_type char(1) not null,")
827 _T(" ignore_items integer not null,")
828 _T(" items $SQL:TEXT,")
829 _T(" PRIMARY KEY(object_id, subscription_type))")));
f61f151c
AK
830
831 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='391' WHERE var_name='SchemaVersion'")));
832 return TRUE;
833}
834
23464115
VK
835/**
836 * Upgrade from V389 to V390
837 */
838static BOOL H_UpgradeFromV389(int currVersion, int newVersion)
839{
840 CHK_EXEC(CreateTable(
841 _T("CREATE TABLE object_containers (")
842 _T(" id integer not null,")
843 _T(" object_class integer not null,")
844 _T(" flags integer not null,")
845 _T(" auto_bind_filter $SQL:TEXT null,")
846 _T(" PRIMARY KEY(id))")));
847
848 DB_RESULT hResult = SQLSelect(_T("SELECT id,object_class,flags,auto_bind_filter FROM containers"));
849 if (hResult != NULL)
850 {
851 int count = DBGetNumRows(hResult);
852 if (count > 0)
853 {
854 DB_STATEMENT hStmt = DBPrepare(g_hCoreDB, _T("INSERT INTO object_containers (id,object_class,flags,auto_bind_filter) VALUES (?,?,?,?)"));
855 if (hStmt != NULL)
856 {
857 for(int i = 0; i < count; i++)
858 {
859 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
860 DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, DBGetFieldLong(hResult, i, 1));
861 DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 2));
862 DBBind(hStmt, 4, DB_SQLTYPE_TEXT, DBGetField(hResult, i, 3, NULL, 0), DB_BIND_DYNAMIC);
863 if (!SQLExecute(hStmt))
864 {
865 if (!g_bIgnoreErrors)
866 {
867 DBFreeStatement(hStmt);
868 DBFreeResult(hResult);
869 return FALSE;
870 }
871 }
872 }
873 DBFreeStatement(hStmt);
874 }
875 else if (!g_bIgnoreErrors)
876 {
877 DBFreeResult(hResult);
878 return FALSE;
879 }
880 }
881 DBFreeResult(hResult);
882 }
883 else
884 {
885 if (!g_bIgnoreErrors)
886 return FALSE;
887 }
888
889 CHK_EXEC(SQLQuery(_T("DROP TABLE containers")));
890 CHK_EXEC(SQLQuery(_T("DROP TABLE container_categories")));
891 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='390' WHERE var_name='SchemaVersion'")));
892 return TRUE;
893}
894
b120cd0f
VK
895/**
896 * Upgrade from V388 to V389
897 */
898static BOOL H_UpgradeFromV388(int currVersion, int newVersion)
899{
900 static TCHAR batch[] =
901 _T("ALTER TABLE racks ADD top_bottom_num char(1)\n")
902 _T("UPDATE racks SET top_bottom_num='0'\n")
903 _T("<END>");
904 CHK_EXEC(SQLBatch(batch));
905 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='389' WHERE var_name='SchemaVersion'")));
906 return TRUE;
907}
908
65ce7452
VK
909/**
910 * Upgrade from V387 to V388
911 */
912static BOOL H_UpgradeFromV387(int currVersion, int newVersion)
913{
914 static TCHAR batch[] =
915 _T("ALTER TABLE alarms ADD dci_id integer\n")
916 _T("UPDATE alarms SET dci_id=0\n")
917 _T("ALTER TABLE event_log ADD dci_id integer\n")
918 _T("UPDATE event_log SET dci_id=0\n")
919 _T("<END>");
920 CHK_EXEC(SQLBatch(batch));
921 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='388' WHERE var_name='SchemaVersion'")));
922 return TRUE;
923}
924
53e39123
VK
925/**
926 * Upgrade from V386 to V387
927 */
928static BOOL H_UpgradeFromV386(int currVersion, int newVersion)
929{
930 DB_RESULT hResult = SQLSelect(_T("SELECT id,flags,filter FROM network_maps WHERE filter IS NOT NULL"));
931 if (hResult != NULL)
932 {
933 int count = DBGetNumRows(hResult);
934 for(int i = 0; i < count; i++)
935 {
936 TCHAR *filter = DBGetField(hResult, i, 2, NULL, 0);
937 if ((filter != NULL) && (filter[0] != 0))
938 {
939 TCHAR query[256];
940 _sntprintf(query, 256, _T("UPDATE network_maps SET flags=%d WHERE id=%d"),
941 DBGetFieldULong(hResult, i, 1) | MF_FILTER_OBJECTS, DBGetFieldULong(hResult, i, 0));
942 CHK_EXEC(SQLQuery(query));
943 }
944 free(filter);
945 }
946 DBFreeResult(hResult);
947 }
948 else
949 {
950 if (!g_bIgnoreErrors)
951 return FALSE;
952 }
953 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='387' WHERE var_name='SchemaVersion'")));
954 return TRUE;
955}
956
e2a9c5fc
VK
957/**
958 * Upgrade from V385 to V386
959 */
960static BOOL H_UpgradeFromV385(int currVersion, int newVersion)
961{
962 TCHAR query[1024];
963 int ruleId = NextFreeEPPruleID();
964
965 if (!IsEventPairInUse(EVENT_THREAD_HANGS, EVENT_THREAD_RUNNING))
966 {
967 _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);
968 CHK_EXEC(SQLQuery(query));
969 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_THREAD_HANGS);
970 CHK_EXEC(SQLQuery(query));
971 ruleId++;
972 _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);
973 CHK_EXEC(SQLQuery(query));
974 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_THREAD_RUNNING);
975 CHK_EXEC(SQLQuery(query));
976 ruleId++;
977 }
978
979 if (!IsEventPairInUse(EVENT_MAINTENANCE_MODE_ENTERED, EVENT_MAINTENANCE_MODE_LEFT))
980 {
981 _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);
982 CHK_EXEC(SQLQuery(query));
983 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_MAINTENANCE_MODE_ENTERED);
984 CHK_EXEC(SQLQuery(query));
985 ruleId++;
986 _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);
987 CHK_EXEC(SQLQuery(query));
988 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_MAINTENANCE_MODE_LEFT);
989 CHK_EXEC(SQLQuery(query));
990 ruleId++;
991 }
992
993 if (!IsEventPairInUse(EVENT_AGENT_FAIL, EVENT_AGENT_OK))
994 {
995 _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);
996 CHK_EXEC(SQLQuery(query));
997 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_AGENT_FAIL);
998 CHK_EXEC(SQLQuery(query));
999 ruleId++;
1000 _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);
1001 CHK_EXEC(SQLQuery(query));
1002 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_AGENT_OK);
1003 CHK_EXEC(SQLQuery(query));
1004 ruleId++;
1005 }
1006
1007 if (!IsEventPairInUse(EVENT_SNMP_FAIL, EVENT_SNMP_OK))
1008 {
1009 _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);
1010 CHK_EXEC(SQLQuery(query));
1011 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_SNMP_FAIL);
1012 CHK_EXEC(SQLQuery(query));
1013 ruleId++;
1014 _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);
1015 CHK_EXEC(SQLQuery(query));
1016 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_SNMP_OK);
1017 CHK_EXEC(SQLQuery(query));
1018 ruleId++;
1019 }
1020
1021 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET severity='3' WHERE event_code=14 OR event_code=15")));
1022 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='386' WHERE var_name='SchemaVersion'")));
1023 return TRUE;
1024}
1025
4e7ebbb4
VK
1026/**
1027 * Upgrade from V384 to V385
1028 */
1029static BOOL H_UpgradeFromV384(int currVersion, int newVersion)
1030{
1031 CHK_EXEC(SQLQuery(_T("UPDATE config SET is_visible=1,need_server_restart=0 WHERE var_name='SNMPPorts'")));
1032 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='385' WHERE var_name='SchemaVersion'")));
1033 return TRUE;
1034}
1035
aed41472 1036/**
7bae90f4 1037 * Upgrade from V383 to V384
aed41472 1038 */
1039static BOOL H_UpgradeFromV383(int currVersion, int newVersion)
1040{
1041 CHK_EXEC(ConvertStrings(_T("graphs"), _T("graph_id"), _T("config")));
1042 static TCHAR batch[] =
1043 _T("ALTER TABLE graphs ADD flags integer\n")
1044 _T("ALTER TABLE graphs ADD filters $SQL:TEXT\n")
1045 _T("<END>");
1046 CHK_EXEC(SQLBatch(batch));
1047 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='384' WHERE var_name='SchemaVersion'")));
1048 return TRUE;
1049}
1050
55f6cb79
VK
1051/**
1052 * Upgrade from V382 to V383
1053 */
1054static BOOL H_UpgradeFromV382(int currVersion, int newVersion)
1055{
1056 CHK_EXEC(ResizeColumn(_T("nodes"), _T("primary_ip"), 48, false));
1057 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='383' WHERE var_name='SchemaVersion'")));
1058 return TRUE;
1059}
1060
83aa5ebd
VK
1061/**
1062 * Upgrade from V381 to V382
1063 */
1064static BOOL H_UpgradeFromV381(int currVersion, int newVersion)
1065{
7bae90f4
VK
1066 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")));
1067 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")));
1068 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")));
1069 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")));
1070 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")));
1071 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")));
83aa5ebd
VK
1072 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='382' WHERE var_name='SchemaVersion'")));
1073 return TRUE;
1074}
1075
98ef8e4a
VK
1076/**
1077 * Upgrade from V380 to V381
1078 */
1079static BOOL H_UpgradeFromV380(int currVersion, int newVersion)
1080{
1081 static TCHAR batch[] =
1082 _T("ALTER TABLE items ADD guid varchar(36)\n")
1083 _T("ALTER TABLE dc_tables ADD guid varchar(36)\n")
1084 _T("<END>");
1085 CHK_EXEC(SQLBatch(batch));
50963ced
VK
1086 CHK_EXEC(GenerateGUID(_T("items"), _T("item_id"), _T("guid"), NULL));
1087 CHK_EXEC(GenerateGUID(_T("dc_tables"), _T("item_id"), _T("guid"), NULL));
98ef8e4a
VK
1088 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='381' WHERE var_name='SchemaVersion'")));
1089 return TRUE;
1090}
1091
9b4a2a0e
VK
1092/**
1093 * Upgrade from V379 to V380
1094 */
1095static BOOL H_UpgradeFromV379(int currVersion, int newVersion)
1096{
1097 static TCHAR batch[] =
1098 _T("UPDATE object_properties SET maint_event_id=0 WHERE maint_event_id IS NULL\n")
1099 _T("UPDATE nodes SET last_agent_comm_time=0 WHERE last_agent_comm_time IS NULL\n")
1100 _T("<END>");
1101 CHK_EXEC(SQLBatch(batch));
1102 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='380' WHERE var_name='SchemaVersion'")));
1103 return TRUE;
1104}
1105
ec5829b2
VK
1106/**
1107 * Upgrade from V378 to V379
1108 */
1109static BOOL H_UpgradeFromV378(int currVersion, int newVersion)
1110{
1111 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfDatabaseWriters'")));
1112 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='379' WHERE var_name='SchemaVersion'")));
1113 return TRUE;
1114}
1115
9bd1bace
VK
1116/**
1117 * Upgrade from V377 to V378
1118 */
1119static BOOL H_UpgradeFromV377(int currVersion, int newVersion)
1120{
1121 static TCHAR batch[] =
1122 _T("DELETE FROM config WHERE var_name='EnableMultipleDBConnections'\n")
1123 _T("UPDATE config SET var_name='DBConnectionPoolBaseSize' WHERE var_name='ConnectionPoolBaseSize'\n")
1124 _T("UPDATE config SET var_name='DBConnectionPoolMaxSize' WHERE var_name='ConnectionPoolMaxSize'\n")
1125 _T("UPDATE config SET var_name='DBConnectionPoolCooldownTime' WHERE var_name='ConnectionPoolCooldownTime'\n")
1126 _T("UPDATE config SET var_name='DBConnectionPoolMaxLifetime' WHERE var_name='ConnectionPoolMaxLifetime'\n")
1127 _T("<END>");
1128 CHK_EXEC(SQLBatch(batch));
1129 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='378' WHERE var_name='SchemaVersion'")));
1130 return TRUE;
1131}
1132
9d7771b8
VK
1133/**
1134 * Upgrade from V376 to V377
1135 */
1136static BOOL H_UpgradeFromV376(int currVersion, int newVersion)
1137{
1138 CHK_EXEC(CreateConfigParam(_T("DefaultSubnetMaskIPv4"), _T("24"), _T("Default mask for synthetic IPv4 subnets"), 'I', true, false, false, false));
1139 CHK_EXEC(CreateConfigParam(_T("DefaultSubnetMaskIPv6"), _T("64"), _T("Default mask for synthetic IPv6 subnets"), 'I', true, false, false, false));
1140 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='377' WHERE var_name='SchemaVersion'")));
1141 return TRUE;
1142}
1143
4e3133ee 1144/**
1145 * Upgrade from V375 to V376
1146 */
1147static BOOL H_UpgradeFromV375(int currVersion, int newVersion)
1148{
1149 static TCHAR batch[] =
1150 _T("ALTER TABLE nodes ADD last_agent_comm_time integer\n")
9b4a2a0e 1151 _T("UPDATE nodes SET last_agent_comm_time=0\n")
4e3133ee 1152 _T("<END>");
1153 CHK_EXEC(SQLBatch(batch));
1154
1155 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='376' WHERE var_name='SchemaVersion'")));
1156 return TRUE;
1157}
1158
203cb23c
VK
1159/**
1160 * Upgrade from V374 to V375
1161 */
1162static BOOL H_UpgradeFromV374(int currVersion, int newVersion)
1163{
1164 static TCHAR batch[] =
1165 _T("ALTER TABLE object_tools_input_fields ADD sequence_num integer\n")
1166 _T("UPDATE object_tools_input_fields SET sequence_num=-1\n")
1167 _T("<END>");
1168 CHK_EXEC(SQLBatch(batch));
1169
1170 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='375' WHERE var_name='SchemaVersion'")));
1171 return TRUE;
1172}
1173
97afcb6e
VK
1174/**
1175 * Upgrade from V373 to V374
1176 */
1177static BOOL H_UpgradeFromV373(int currVersion, int newVersion)
1178{
1179 static TCHAR batch[] =
1180 _T("ALTER TABLE object_properties ADD maint_event_id $SQL:INT64\n")
9b4a2a0e 1181 _T("UPDATE object_properties SET maint_mode='0',maint_event_id=0\n")
97afcb6e
VK
1182 _T("<END>");
1183 CHK_EXEC(SQLBatch(batch));
1184
1185 CHK_EXEC(CreateEventTemplate(EVENT_MAINTENANCE_MODE_ENTERED, _T("SYS_MAINTENANCE_MODE_ENTERED"), SEVERITY_NORMAL, EF_LOG,
1186 _T("Entered maintenance mode"),
1187 _T("Generated when node, cluster, or mobile device enters maintenance mode.")));
1188
1189 CHK_EXEC(CreateEventTemplate(EVENT_MAINTENANCE_MODE_LEFT, _T("SYS_MAINTENANCE_MODE_LEFT"), SEVERITY_NORMAL, EF_LOG,
1190 _T("Left maintenance mode"),
1191 _T("Generated when node, cluster, or mobile device leaves maintenance mode.")));
1192
1193 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='374' WHERE var_name='SchemaVersion'")));
1194 return TRUE;
1195}
1196
b3beb7f4 1197/**
1198 * Upgrade from V372 to V373
1199 */
1200static BOOL H_UpgradeFromV372(int currVersion, int newVersion)
1201{
1202 CHK_EXEC(SQLQuery(_T("ALTER TABLE scheduled_tasks ADD object_id integer")));
1203 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='373' WHERE var_name='SchemaVersion'")));
1204 return TRUE;
1205}
1206
89fd6fa4
VK
1207/**
1208 * Upgrade from V371 to V372
1209 */
1210static BOOL H_UpgradeFromV371(int currVersion, int newVersion)
1211{
1212 static TCHAR batch[] =
1213 _T("ALTER TABLE object_properties ADD maint_mode char(1)\n")
1214 _T("UPDATE object_properties SET maint_mode='0'\n")
1215 _T("<END>");
1216 CHK_EXEC(SQLBatch(batch));
1217 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='372' WHERE var_name='SchemaVersion'")));
1218 return TRUE;
1219}
1220
90e3031f
VK
1221/**
1222 * Upgrade from V370 to V371
1223 */
1224static BOOL H_UpgradeFromV370(int currVersion, int newVersion)
1225{
1226 int defaultPollingInterval = ConfigReadInt(_T("DefaultDCIPollingInterval"), 60);
1227 int defaultRetentionTime = ConfigReadInt(_T("DefaultDCIRetentionTime"), 30);
1228
1229 TCHAR query[256];
1230 _sntprintf(query, 256, _T("UPDATE items SET polling_interval=0 WHERE polling_interval=%d"), defaultPollingInterval);
1231 CHK_EXEC(SQLQuery(query));
1232
1233 _sntprintf(query, 256, _T("UPDATE items SET retention_time=0 WHERE retention_time=%d"), defaultRetentionTime);
1234 CHK_EXEC(SQLQuery(query));
1235
1236 _sntprintf(query, 256, _T("UPDATE dc_tables SET polling_interval=0 WHERE polling_interval=%d"), defaultPollingInterval);
1237 CHK_EXEC(SQLQuery(query));
1238
1239 _sntprintf(query, 256, _T("UPDATE dc_tables SET retention_time=0 WHERE retention_time=%d"), defaultRetentionTime);
1240 CHK_EXEC(SQLQuery(query));
1241
1242 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='371' WHERE var_name='SchemaVersion'")));
1243 return TRUE;
1244}
1245
8bdd26dc
VK
1246/**
1247 * Upgrade from V369 to V370
1248 */
1249static BOOL H_UpgradeFromV369(int currVersion, int newVersion)
1250{
1251 CHK_EXEC(CreateTable(
1252 _T("CREATE TABLE dashboard_associations (")
1253 _T(" object_id integer not null,")
1254 _T(" dashboard_id integer not null,")
1255 _T(" PRIMARY KEY(object_id,dashboard_id))")));
1256 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='370' WHERE var_name='SchemaVersion'")));
1257 return TRUE;
1258}
1259
0a145c10 1260/**
1261 * Upgrade from V368 to V369
1262 */
1263static BOOL H_UpgradeFromV368(int currVersion, int newVersion)
1264{
1265 CHK_EXEC(CreateTable(
c6e191d2 1266 _T("CREATE TABLE scheduled_tasks (")
0a145c10 1267 _T(" id integer not null,")
1268 _T(" taskId varchar(255) null,")
c6e191d2 1269 _T(" schedule varchar(127) null,")
0a145c10 1270 _T(" params varchar(1023) null,")
1271 _T(" execution_time integer not null,")
1272 _T(" last_execution_time integer not null,")
1273 _T(" flags integer not null,")
c6e191d2 1274 _T(" owner integer not null,")
0a145c10 1275 _T(" PRIMARY KEY(id))")));
1276 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='369' WHERE var_name='SchemaVersion'")));
1277 return TRUE;
1278}
1279
de674bb6
VK
1280/**
1281 * Upgrade from V367 to V368
1282 */
1283static BOOL H_UpgradeFromV367(int currVersion, int newVersion)
1284{
1285 static TCHAR batch[] =
1286 _T("ALTER TABLE nodes ADD rack_height integer\n")
1287 _T("UPDATE nodes SET rack_height=1\n")
1288 _T("<END>");
1289 CHK_EXEC(SQLBatch(batch));
de674bb6
VK
1290 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='368' WHERE var_name='SchemaVersion'")));
1291 return TRUE;
1292}
1293
cf38357f
VK
1294/**
1295 * Upgrade from V366 to V367
1296 */
1297static BOOL H_UpgradeFromV366(int currVersion, int newVersion)
1298{
cd071c26 1299 CHK_EXEC(CreateConfigParam(_T("TrapSourcesInAllZones"), _T("0"), _T("Search all zones to match trap/syslog source address to node"), 'B', true, true, false, false));
cf38357f
VK
1300
1301 CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD snmp_sys_contact varchar(127)")));
1302 CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD snmp_sys_location varchar(127)")));
1303
1304 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='367' WHERE var_name='SchemaVersion'")));
1305 return TRUE;
1306}
1307
48905317
VK
1308/**
1309 * Upgrade from V365 to V366
1310 */
1311static BOOL H_UpgradeFromV365(int currVersion, int newVersion)
1312{
b3643ffe 1313 CHK_EXEC(CreateConfigParam(_T("ServerCommandOutputTimeout"), _T("60"), true, false));
48905317
VK
1314
1315 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='366' WHERE var_name='SchemaVersion'")));
1316 return TRUE;
1317}
1318
d0a2ada6 1319/**
1320 * Upgrade from V364 to V365
1321 */
1322static BOOL H_UpgradeFromV364(int currVersion, int newVersion)
1323{
4e7ebbb4 1324 CHK_EXEC(CreateConfigParam(_T("SNMPPorts"), _T("161"), true, false));
d0a2ada6 1325 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='365' WHERE var_name='SchemaVersion'")));
1326 return TRUE;
1327}
1328
3d37f7bf
VK
1329/**
1330 * Upgrade from V363 to V364
1331 */
1332static BOOL H_UpgradeFromV363(int currVersion, int newVersion)
1333{
1334 static TCHAR batch[] =
1335 _T("ALTER TABLE interfaces ADD speed $SQL:INT64\n")
1336 _T("ALTER TABLE interfaces ADD iftable_suffix varchar(127)\n")
1337 _T("UPDATE interfaces SET speed=0\n")
1338 _T("<END>");
1339 CHK_EXEC(SQLBatch(batch));
1340
1341 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='364' WHERE var_name='SchemaVersion'")));
1342 return TRUE;
1343}
1344
2589cc10 1345/**
1346 * Upgrade from V362 to V363
1347 */
1348static BOOL H_UpgradeFromV362(int currVersion, int newVersion)
1349{
1350 ResizeColumn(_T("config"), _T("var_value"), 2000, true);
1351 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='363' WHERE var_name='SchemaVersion'")));
1352 return TRUE;
1353}
1354
8a1519ce
VK
1355/**
1356 * Upgrade from V361 to V362
1357 */
1358static BOOL H_UpgradeFromV361(int currVersion, int newVersion)
1359{
1360 CHK_EXEC(CreateConfigParam(_T("CaseInsensitiveLoginNames"), _T("0"), _T("Enable/disable case insensitive login names"), 'B', true, true, false));
1361 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='362' WHERE var_name='SchemaVersion'")));
1362 return TRUE;
1363}
1364
b576249a
VK
1365/**
1366 * Upgrade from V360 to V361
1367 */
1368static BOOL H_UpgradeFromV360(int currVersion, int newVersion)
1369{
1370 CHK_EXEC(CreateTable(
1371 _T("CREATE TABLE object_tools_input_fields (")
1372 _T(" tool_id integer not null,")
1373 _T(" name varchar(31) not null,")
1374 _T(" input_type char(1) not null,")
1375 _T(" display_name varchar(127) null,")
1376 _T(" config $SQL:TEXT null,")
1377 _T(" PRIMARY KEY(tool_id,name))")));
1378
1379 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='361' WHERE var_name='SchemaVersion'")));
1380 return TRUE;
1381}
1382
b2a15edc
VK
1383/**
1384 * Upgrade from V359 to V360
1385 */
1386static BOOL H_UpgradeFromV359(int currVersion, int newVersion)
1387{
1388 DB_RESULT hResult = SQLSelect(_T("SELECT tool_id,tool_type,tool_data,confirmation_text FROM object_tools"));
1389 if (hResult != NULL)
1390 {
1391 int count = DBGetNumRows(hResult);
1392 for(int i = 0; i < count; i++)
1393 {
1394 UINT32 id = DBGetFieldULong(hResult, i, 0);
2589cc10 1395
b2a15edc
VK
1396 TCHAR *text = DBGetField(hResult, i, 3, NULL, 0);
1397 if (text != NULL)
1398 {
1399 ConvertObjectToolMacros(id, text, _T("confirmation_text"));
1400 free(text);
1401 }
2589cc10 1402
b2a15edc
VK
1403 int type = DBGetFieldLong(hResult, i, 1);
1404 if ((type == 1) || (type == 4) || (type == 5))
1405 {
1406 text = DBGetField(hResult, i, 2, NULL, 0);
1407 if (text != NULL)
1408 {
1409 ConvertObjectToolMacros(id, text, _T("tool_data"));
1410 free(text);
1411 }
1412 }
1413 }
1414 DBFreeResult(hResult);
1415 }
1416 else
1417 {
1418 if (!g_bIgnoreErrors)
1419 return FALSE;
1420 }
1421 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='360' WHERE var_name='SchemaVersion'")));
1422 return TRUE;
1423}
1424
7a8aa001
VK
1425/**
1426 * Upgrade from V358 to V359
1427 */
1428static BOOL H_UpgradeFromV358(int currVersion, int newVersion)
1429{
1430 static TCHAR batch[] =
1431 _T("ALTER TABLE network_maps ADD object_display_mode integer\n")
1432 _T("UPDATE network_maps SET object_display_mode=0\n")
1433 _T("<END>");
1434 CHK_EXEC(SQLBatch(batch));
1435
1436 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='359' WHERE var_name='SchemaVersion'")));
1437 return TRUE;
1438}
1439
c85c8ef2
VK
1440/**
1441 * Upgrade from V357 to V358
1442 */
1443static BOOL H_UpgradeFromV357(int currVersion, int newVersion)
1444{
1445 static TCHAR batch[] =
1446 _T("ALTER TABLE config ADD data_type char(1) not null default 'S'\n")
1447 _T("ALTER TABLE config ADD is_public char(1) not null default 'N'\n")
1448 _T("ALTER TABLE config ADD description varchar(255)\n")
1449 _T("ALTER TABLE config ADD possible_values $SQL:TEXT\n")
1450 _T("<END>");
1451 static TCHAR batchOracle[] =
1452 _T("ALTER TABLE config ADD data_type char(1) default 'S' not null\n")
1453 _T("ALTER TABLE config ADD is_public char(1) default 'N' not null\n")
1454 _T("ALTER TABLE config ADD description varchar(255)\n")
1455 _T("ALTER TABLE config ADD possible_values $SQL:TEXT\n")
1456 _T("<END>");
1457 CHK_EXEC(SQLBatch((g_dbSyntax == DB_SYNTAX_ORACLE) ? batchOracle : batch));
1458
1459 CHK_EXEC(CreateConfigParam(_T("DashboardDataExportEnableInterpolation"), _T("1"), _T("Enable/disable data interpolation in dashboard data export"), 'B', true, false, true));
1460
1461 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='358' WHERE var_name='SchemaVersion'")));
1462 return TRUE;
1463}
1464
9db92307 1465/**
1466 * Upgrade from V356 to V357
1467 */
1468static BOOL H_UpgradeFromV356(int currVersion, int newVersion)
1469{
1470 TCHAR comunityString[256];
1471 comunityString[0] = 0;
1472 DB_RESULT hResult = SQLSelect(_T("SELECT var_value FROM config WHERE var_name='DefaultCommunityString'"));
1473 if (hResult != NULL)
1474 {
1475 if(DBGetNumRows(hResult) > 0)
1476 {
1477 DBGetField(hResult, 0, 0, comunityString, 256);
1478 }
1479 DBFreeResult(hResult);
1480 }
1481
c85c8ef2 1482 if (comunityString[0] != 0)
9db92307 1483 {
1484 DB_RESULT hResult = SQLSelect(_T("SELECT id, community FROM snmp_communities"));
1485 if (hResult != NULL)
1486 {
1487 CHK_EXEC(SQLQuery(_T("DELETE FROM snmp_communities")));
1488
1489 TCHAR query[1024];
1490 _sntprintf(query, 1024, _T("INSERT INTO snmp_communities (id,community) VALUES(%d,'%s')"), 1, comunityString);
1491 CHK_EXEC(SQLQuery(query));
1492
1493 int count = DBGetNumRows(hResult);
1494 for(int i = 0; i < count; i++)
1495 {
1496 _sntprintf(query, 1024, _T("INSERT INTO snmp_communities (id,community) VALUES(%d,'%s')"), i + 2, DBGetField(hResult, i, 1, comunityString, 256));
1497 CHK_EXEC(SQLQuery(query));
1498 }
1499 }
1500 }
1501
1502 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='DefaultCommunityString'")));
1503
1504 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='357' WHERE var_name='SchemaVersion'")));
1505 return TRUE;
1506}
1507
e1d2050f
VK
1508/**
1509 * Upgrade from V355 to V356
1510 */
1511static BOOL H_UpgradeFromV355(int currVersion, int newVersion)
1512{
1513 static TCHAR batch[] =
1514 _T("DELETE FROM config WHERE var_name='NumberOfBusinessServicePollers'\n")
1515 _T("DELETE FROM config WHERE var_name='NumberOfConditionPollers'\n")
1516 _T("DELETE FROM config WHERE var_name='NumberOfConfigurationPollers'\n")
1517 _T("DELETE FROM config WHERE var_name='NumberOfDiscoveryPollers'\n")
1518 _T("DELETE FROM config WHERE var_name='NumberOfInstancePollers'\n")
1519 _T("DELETE FROM config WHERE var_name='NumberOfRoutingTablePollers'\n")
1520 _T("DELETE FROM config WHERE var_name='NumberOfStatusPollers'\n")
1521 _T("DELETE FROM config WHERE var_name='NumberOfTopologyTablePollers'\n")
1522 _T("<END>");
1523 CHK_EXEC(SQLBatch(batch));
1524
c85c8ef2
VK
1525 CHK_EXEC(CreateConfigParam(_T("PollerThreadPoolBaseSize"), _T("10"), true, true));
1526 CHK_EXEC(CreateConfigParam(_T("PollerThreadPoolMaxSize"), _T("250"), true, true));
e1d2050f
VK
1527
1528 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='356' WHERE var_name='SchemaVersion'")));
1529 return TRUE;
1530}
1531
9708eff4
VK
1532/**
1533 * Upgrade from V354 to V355
1534 */
1535static BOOL H_UpgradeFromV354(int currVersion, int newVersion)
1536{
1537 static TCHAR batch[] =
1538 _T("ALTER TABLE nodes ADD agent_cache_mode char(1)\n")
55d81770 1539 _T("UPDATE nodes SET agent_cache_mode='0'\n")
e9902466 1540 _T("DELETE FROM config WHERE var_name='ServerID'\n")
9708eff4
VK
1541 _T("<END>");
1542 CHK_EXEC(SQLBatch(batch));
1543
c85c8ef2 1544 CHK_EXEC(CreateConfigParam(_T("DefaultAgentCacheMode"), _T("2"), true, true));
9708eff4
VK
1545
1546 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='355' WHERE var_name='SchemaVersion'")));
1547 return TRUE;
1548}
1549
981d246a
VK
1550/**
1551 * Upgrade from V353 to V354
1552 */
1553static BOOL H_UpgradeFromV353(int currVersion, int newVersion)
1554{
1555 CHK_EXEC(ResizeColumn(_T("users"), _T("password"), 127, false));
1556 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='354' WHERE var_name='SchemaVersion'")));
1557 return TRUE;
1558}
1559
2096c8f0
VK
1560/**
1561 * Upgrade from V352 to V353
1562 */
1563static BOOL H_UpgradeFromV352(int currVersion, int newVersion)
1564{
1565 CHK_EXEC(SQLQuery(_T("ALTER TABLE dci_summary_tables ADD guid varchar(36)")));
50963ced 1566 CHK_EXEC(GenerateGUID(_T("dci_summary_tables"), _T("id"), _T("guid"), NULL));
2096c8f0
VK
1567 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='353' WHERE var_name='SchemaVersion'")));
1568 return TRUE;
1569}
1570
271e3971
VK
1571/**
1572 * Upgrade from V351 to V352
1573 */
1574static BOOL H_UpgradeFromV351(int currVersion, int newVersion)
1575{
1576 CHK_EXEC(SQLQuery(_T("ALTER TABLE object_tools ADD guid varchar(36)")));
50963ced 1577 CHK_EXEC(GenerateGUID(_T("object_tools"), _T("tool_id"), _T("guid"), NULL));
271e3971
VK
1578 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='352' WHERE var_name='SchemaVersion'")));
1579 return TRUE;
1580}
1581
ac58ffe9
VK
1582/**
1583 * Upgrade from V350 to V351
1584 */
1585static BOOL H_UpgradeFromV350(int currVersion, int newVersion)
1586{
1587 static TCHAR batch[] =
1588 _T("ALTER TABLE access_points ADD ap_index integer\n")
1589 _T("UPDATE access_points SET ap_index=0\n")
1590 _T("<END>");
1591 CHK_EXEC(SQLBatch(batch));
1592
1593 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='351' WHERE var_name='SchemaVersion'")));
1594 return TRUE;
1595}
1596
fcf91b2e 1597/**
1598 * Upgrade from V349 to V350
1599 */
1600static BOOL H_UpgradeFromV349(int currVersion, int newVersion)
1601{
1602 switch(g_dbSyntax)
1603 {
1604 case DB_SYNTAX_ORACLE:
a8a0793d
VK
1605 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)")));
1606 break;
fcf91b2e 1607 case DB_SYNTAX_DB2:
a8a0793d
VK
1608 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 <> '')")));
1609 break;
96e03924 1610 case DB_SYNTAX_MSSQL:
b69710b9 1611 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)")));
fcf91b2e 1612 break;
1613 case DB_SYNTAX_PGSQL:
a8a0793d 1614 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 <> '')")));
fcf91b2e 1615 break;
1616 case DB_SYNTAX_SQLITE:
a8a0793d 1617 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 <> '')")));
fcf91b2e 1618 break;
1619 case DB_SYNTAX_MYSQL:
a8a0793d 1620 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)")));
fcf91b2e 1621 break;
1622 default:
96e03924 1623 break;
fcf91b2e 1624 }
1625
3f543e56 1626 CHK_EXEC(SQLDropColumn(_T("ap_common"), _T("description")));
fcf91b2e 1627 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='350' WHERE var_name='SchemaVersion'")));
1628 return TRUE;
1629}
1630
b06ae508
VK
1631/**
1632 * Upgrade from V348 to V349
1633 */
1634static BOOL H_UpgradeFromV348(int currVersion, int newVersion)
1635{
1636 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='HouseKeepingInterval'")));
c85c8ef2 1637 CHK_EXEC(CreateConfigParam(_T("HousekeeperStartTime"), _T("02:00"), true, true));
b06ae508
VK
1638 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='349' WHERE var_name='SchemaVersion'")));
1639 return TRUE;
1640}
1641
c30c0c0f
VK
1642/**
1643 * Upgrade from V347 to V348
1644 */
1645static BOOL H_UpgradeFromV347(int currVersion, int newVersion)
1646{
1647 CHK_EXEC(CreateEventTemplate(EVENT_IF_IPADDR_ADDED, _T("SYS_IF_IPADDR_ADDED"), SEVERITY_NORMAL, EF_LOG,
1648 _T("IP address %3/%4 added to interface \"%2\""),
1649 _T("Generated when IP address added to interface.\r\n")
1650 _T("Parameters:\r\n")
1651 _T(" 1) Interface object ID\r\n")
1652 _T(" 2) Interface name\r\n")
1653 _T(" 3) IP address\r\n")
1654 _T(" 4) Network mask\r\n")
1655 _T(" 5) Interface index")));
1656
1657 CHK_EXEC(CreateEventTemplate(EVENT_IF_IPADDR_DELETED, _T("SYS_IF_IPADDR_DELETED"), SEVERITY_NORMAL, EF_LOG,
1658 _T("IP address %3/%4 deleted from interface \"%2\""),
1659 _T("Generated when IP address deleted from interface.\r\n")
1660 _T("Parameters:\r\n")
1661 _T(" 1) Interface object ID\r\n")
1662 _T(" 2) Interface name\r\n")
1663 _T(" 3) IP address\r\n")
1664 _T(" 4) Network mask\r\n")
1665 _T(" 5) Interface index")));
1666
1667 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='348' WHERE var_name='SchemaVersion'")));
1668 return TRUE;
1669}
1670
1671/**
1672 * Upgrade from V346 to V347
1673 */
1674static BOOL H_UpgradeFromV346(int currVersion, int newVersion)
1675{
1676 CHK_EXEC(CreateTable(
1677 _T("CREATE TABLE interface_address_list (")
1678 _T(" iface_id integer not null,")
1679 _T(" ip_addr varchar(48) not null,")
1680 _T(" ip_netmask integer not null,")
1681 _T(" PRIMARY KEY(iface_id,ip_addr))")));
1682
1683 DB_RESULT hResult = SQLSelect(_T("SELECT id,ip_addr,ip_netmask FROM interfaces WHERE ip_addr<>'0.0.0.0'"));
1684 if (hResult != NULL)
1685 {
1686 int count = DBGetNumRows(hResult);
1687 for(int i = 0; i < count; i++)
1688 {
1689 TCHAR query[256], addr[64];
1690 _sntprintf(query, 256, _T("INSERT INTO interface_address_list (iface_id,ip_addr,ip_netmask) VALUES (%d,'%s',%d)"),
1691 DBGetFieldLong(hResult, i, 0), DBGetField(hResult, i, 1, addr, 64), DBGetFieldLong(hResult, i, 2));
1692 CHK_EXEC(SQLQuery(query));
1693 }
1694 DBFreeResult(hResult);
1695 }
1696 else
1697 {
1698 if (!g_bIgnoreErrors)
1699 return FALSE;
1700 }
1701
1702 static TCHAR batch[] =
1703 _T("ALTER TABLE interfaces DROP COLUMN ip_addr\n")
1704 _T("ALTER TABLE interfaces DROP COLUMN ip_netmask\n")
1705 _T("<END>");
1706 CHK_EXEC(SQLBatch(batch));
1707
1708 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='347' WHERE var_name='SchemaVersion'")));
1709 return TRUE;
1710}
1711
c75e9ee4
VK
1712/**
1713 * Upgrade from V345 to V346
1714 */
1715static BOOL H_UpgradeFromV345(int currVersion, int newVersion)
1716{
b69710b9
VK
1717 if (g_dbSyntax == DB_SYNTAX_MSSQL)
1718 {
1719 CHK_EXEC(DropPrimaryKey(_T("cluster_sync_subnets")));
1720 CHK_EXEC(DropPrimaryKey(_T("address_lists")));
1721 CHK_EXEC(DropPrimaryKey(_T("vpn_connector_networks")));
1722 }
1723
1724 CHK_EXEC(ResizeColumn(_T("cluster_sync_subnets"), _T("subnet_addr"), 48, false));
1725 CHK_EXEC(ResizeColumn(_T("cluster_resources"), _T("ip_addr"), 48, false));
1726 CHK_EXEC(ResizeColumn(_T("subnets"), _T("ip_addr"), 48, false));
1727 CHK_EXEC(ResizeColumn(_T("interfaces"), _T("ip_addr"), 48, false));
1728 CHK_EXEC(ResizeColumn(_T("network_services"), _T("ip_bind_addr"), 48, false));
1729 CHK_EXEC(ResizeColumn(_T("vpn_connector_networks"), _T("ip_addr"), 48, false));
1730 CHK_EXEC(ResizeColumn(_T("snmp_trap_log"), _T("ip_addr"), 48, false));
1731 CHK_EXEC(ResizeColumn(_T("address_lists"), _T("addr1"), 48, false));
1732 CHK_EXEC(ResizeColumn(_T("address_lists"), _T("addr2"), 48, false));
1733 CHK_EXEC(ResizeColumn(_T("nodes"), _T("primary_ip"), 48, false));
c75e9ee4
VK
1734
1735 CHK_EXEC(ConvertNetMasks(_T("cluster_sync_subnets"), _T("subnet_mask"), _T("cluster_id")));
1736 CHK_EXEC(ConvertNetMasks(_T("subnets"), _T("ip_netmask"), _T("id")));
1737 CHK_EXEC(ConvertNetMasks(_T("interfaces"), _T("ip_netmask"), _T("id")));
1738 CHK_EXEC(ConvertNetMasks(_T("vpn_connector_networks"), _T("ip_netmask"), _T("vpn_id"), _T("ip_addr")));
1739
ba889094 1740 DB_RESULT hResult = SQLSelect(_T("SELECT community_id,list_type,addr1,addr2 FROM address_lists WHERE addr_type=0"));
c75e9ee4
VK
1741 if (hResult != NULL)
1742 {
1743 int count = DBGetNumRows(hResult);
1744 if (count > 0)
1745 {
ba889094 1746 CHK_EXEC(SQLQuery(_T("DELETE FROM address_lists WHERE addr_type=0")));
c75e9ee4
VK
1747
1748 for(int i = 0; i < count; i++)
1749 {
1750 TCHAR query[256], addr[64];
ba889094 1751 _sntprintf(query, 256, _T("INSERT INTO address_lists (addr_type,community_id,list_type,addr1,addr2) VALUES (0,%d,%d,'%s','%d')"),
c75e9ee4
VK
1752 DBGetFieldLong(hResult, i, 0), DBGetFieldLong(hResult, i, 1), DBGetField(hResult, i, 2, addr, 64),
1753 BitsInMask(DBGetFieldIPAddr(hResult, i, 3)));
1754 CHK_EXEC(SQLQuery(query));
1755 }
1756 }
1757 DBFreeResult(hResult);
1758 }
1759 else
1760 {
1761 if (!g_bIgnoreErrors)
1762 return FALSE;
1763 }
1764
b69710b9
VK
1765 if (g_dbSyntax == DB_SYNTAX_MSSQL)
1766 {
1767 CHK_EXEC(SQLQuery(_T("ALTER TABLE cluster_sync_subnets ADD CONSTRAINT pk_cluster_sync_subnets PRIMARY KEY (cluster_id,subnet_addr)")));
1768 CHK_EXEC(SQLQuery(_T("ALTER TABLE address_lists ADD CONSTRAINT pk_address_lists PRIMARY KEY (list_type,community_id,addr_type,addr1,addr2)")));
1769 CHK_EXEC(SQLQuery(_T("ALTER TABLE vpn_connector_networks ADD CONSTRAINT pk_vpn_connector_networks PRIMARY KEY (vpn_id,ip_addr)")));
1770 }
1771
c75e9ee4
VK
1772 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='346' WHERE var_name='SchemaVersion'")));
1773 return TRUE;
1774}
1775
805171de
VK
1776/**
1777 * Upgrade from V344 to V345
1778 */
1779static BOOL H_UpgradeFromV344(int currVersion, int newVersion)
1780{
1781 CHK_EXEC(CreateConfigParam(_T("NumberOfInstancePollers"), _T("10"), 1, 1));
1782 CHK_EXEC(CreateConfigParam(_T("InstancePollingInterval"), _T("600"), 1, 1));
1783 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='345' WHERE var_name='SchemaVersion'")));
1784 return TRUE;
1785}
1786
e95680e5
VK
1787/**
1788 * Upgrade from V343 to V344
1789 */
1790static BOOL H_UpgradeFromV343(int currVersion, int newVersion)
1791{
c30c0c0f 1792 static TCHAR batch[] =
e95680e5
VK
1793 _T("ALTER TABLE interfaces ADD mtu integer\n")
1794 _T("ALTER TABLE interfaces ADD alias varchar(255)\n")
1795 _T("UPDATE interfaces SET mtu=0\n")
1796 _T("<END>");
1797 CHK_EXEC(SQLBatch(batch));
1798 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='344' WHERE var_name='SchemaVersion'")));
1799 return TRUE;
1800}
1801
d140955e
VK
1802/**
1803 * Upgrade from V342 to V343
1804 */
1805static BOOL H_UpgradeFromV342(int currVersion, int newVersion)
1806{
1807 if (g_dbSyntax != DB_SYNTAX_MSSQL)
1808 {
1809 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'")));
5096f5a5 1810 CHK_EXEC(DBCommit(g_hCoreDB)); // do reindexing outside current transaction
d140955e 1811 ReindexIData();
5096f5a5 1812 CHK_EXEC(DBBegin(g_hCoreDB));
d140955e
VK
1813 }
1814 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='343' WHERE var_name='SchemaVersion'")));
1815 return TRUE;
1816}
1817
4b70cb26 1818/**
1819 * Upgrade from V341 to V342
1820 */
1821static BOOL H_UpgradeFromV341(int currVersion, int newVersion)
1822{
d140955e 1823 CHK_EXEC(SQLQuery(_T("ALTER TABLE object_tools ADD tool_filter $SQL:TEXT")));
4b70cb26 1824 DB_RESULT hResult = SQLSelect(_T("SELECT tool_id, matching_oid FROM object_tools"));
1825 if (hResult != NULL)
1826 {
1827 int count = DBGetNumRows(hResult);
1828 for(int i = 0; i < count; i++)
1829 {
1830 TCHAR *oid = DBGetField(hResult, i, 1, NULL, 0);
1831 if (oid == NULL || !_tcscmp(oid, _T(" ")))
1832 {
1833 oid = _tcsdup(_T(""));
1834 }
1835 else
1836 {
1837 TCHAR *newConfig = (TCHAR *)malloc((_tcslen(oid) + 512) * sizeof(TCHAR));
1838 _tcscpy(newConfig, _T("<objectToolFilter>"));
1839 _tcscat(newConfig, _T("<snmpOid>"));
1840 _tcscat(newConfig, oid);
1841 _tcscat(newConfig, _T("</snmpOid>"));
1842 _tcscat(newConfig, _T("</objectToolFilter>"));
1843
1844 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE object_tools SET tool_filter=? WHERE tool_id=?"));
1845 if (statment != NULL)
1846 {
1847 DBBind(statment, 1, DB_SQLTYPE_TEXT, newConfig, DB_BIND_STATIC);
1848 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
1849 CHK_EXEC(DBExecute(statment));
1850 DBFreeStatement(statment);
1851 }
1852 else
1853 {
1854 if (!g_bIgnoreErrors)
1855 return FALSE;
1856 }
1857 }
1858 }
1859 }
a19fa589 1860 CHK_EXEC(SQLDropColumn(_T("object_tools"), _T("matching_oid"))); //delete old column
4b70cb26 1861 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='342' WHERE var_name='SchemaVersion'")));
1862 return TRUE;
1863}
1864
56fa1092
VK
1865/**
1866 * Upgrade from V340 to V341
1867 */
1868static BOOL H_UpgradeFromV340(int currVersion, int newVersion)
1869{
1870 static TCHAR batch[] =
1871 _T("ALTER TABLE object_properties ADD country varchar(63)\n")
1872 _T("ALTER TABLE object_properties ADD city varchar(63)\n")
1873 _T("ALTER TABLE object_properties ADD street_address varchar(255)\n")
1874 _T("ALTER TABLE object_properties ADD postcode varchar(31)\n")
1875 _T("<END>");
1876 CHK_EXEC(SQLBatch(batch));
1877 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='341' WHERE var_name='SchemaVersion'")));
1878 return TRUE;
1879}
1880
b00236dd 1881/**
1882 * Upgrade from V339 to V340
1883 */
1884static BOOL H_UpgradeFromV339(int currVersion, int newVersion)
1885{
1886 CHK_EXEC(CreateConfigParam(_T("LdapPageSize"), _T("1000"), 1, 0));
1887 CHK_EXEC(SQLQuery(_T("UPDATE config SET var_value='1' WHERE var_name='LdapUserDeleteAction'")));
1888 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='340' WHERE var_name='SchemaVersion'")));
1889 return TRUE;
1890}
1891
815638fe
VK
1892/**
1893 * Upgrade from V338 to V339
1894 */
1895static BOOL H_UpgradeFromV338(int currVersion, int newVersion)
1896{
1897 CHK_EXEC(CreateConfigParam(_T("EscapeLocalCommands"), _T("0"), 1, 0));
1898 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='339' WHERE var_name='SchemaVersion'")));
1899 return TRUE;
1900}
1901
9208c84b
VK
1902/**
1903 * Upgrade from V337 to V338
1904 */
1905static BOOL H_UpgradeFromV337(int currVersion, int newVersion)
1906{
1907 static TCHAR batch[] =
1908 _T("ALTER TABLE nodes ADD icmp_proxy integer\n")
1909 _T("UPDATE nodes SET icmp_proxy=0\n")
1910 _T("<END>");
1911 CHK_EXEC(SQLBatch(batch));
1912 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='338' WHERE var_name='SchemaVersion'")));
1913 return TRUE;
1914}
1915
d368074d
VK
1916/**
1917 * Upgrade from V336 to V337
1918 */
1919static BOOL H_UpgradeFromV336(int currVersion, int newVersion)
1920{
1921 CHK_EXEC(CreateConfigParam(_T("SyslogNodeMatchingPolicy"), _T("0"), 1, 1));
1922 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='337' WHERE var_name='SchemaVersion'")));
1923 return TRUE;
1924}
1925
2b01f47c 1926/**
1927 * Upgrade from V335 to V336
1928 */
1929static BOOL H_UpgradeFromV335(int currVersion, int newVersion)
1930{
b69710b9
VK
1931 CHK_EXEC(ResizeColumn(_T("network_map_links"), _T("connector_name1"), 255, true));
1932 CHK_EXEC(ResizeColumn(_T("network_map_links"), _T("connector_name2"), 255, true));
2b01f47c 1933 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='336' WHERE var_name='SchemaVersion'")));
1934 return TRUE;
1935}
1936
b30bb1bc 1937/**
1938 * Upgrade from V334 to V335
1939 */
1940static BOOL H_UpgradeFromV334(int currVersion, int newVersion)
1941{
17b15353
VK
1942 CHK_EXEC(CreateEventTemplate(EVENT_IF_MASK_CHANGED, _T("SYS_IF_MASK_CHANGED"), SEVERITY_NORMAL, EF_LOG,
1943 _T("Interface \"%2\" changed mask from %6 to %4 (IP Addr: %3/%4, IfIndex: %5)"),
1944 _T("Generated when when network mask on interface is changed.\r\n")
1945 _T("Parameters:\r\n")
1946 _T(" 1) Interface object ID\r\n")
1947 _T(" 2) Interface name\r\n")
1948 _T(" 3) IP address\r\n")
1949 _T(" 4) New network mask\r\n")
1950 _T(" 5) Interface index\r\n")
1951 _T(" 6) Old network mask")));
b30bb1bc 1952 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='335' WHERE var_name='SchemaVersion'")));
1953 return TRUE;
1954}
1955
39e60274
AK
1956/**
1957 * Upgrade from V333 to V334
1958 */
1959static BOOL H_UpgradeFromV333(int currVersion, int newVersion)
1960{
1961 CHK_EXEC(SetColumnNullable(_T("user_groups"), _T("description")));
1962 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='334' WHERE var_name='SchemaVersion'")));
1963 return TRUE;
1964}
1965
4899db4d 1966/**
1967 * Upgrade from V332 to V333
1968 */
1969static BOOL H_UpgradeFromV332(int currVersion, int newVersion)
1970{
1971 static TCHAR batch[] =
1972 _T("INSERT INTO metadata (var_name,var_value)")
1973 _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")
1974 _T("<END>");
1975 CHK_EXEC(SQLBatch(batch));
1976 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='333' WHERE var_name='SchemaVersion'")));
1977 return TRUE;
1978}
1979
a6312bd6
VK
1980/**
1981 * Upgrade from V331 to V332
1982 */
1983static BOOL H_UpgradeFromV331(int currVersion, int newVersion)
1984{
1985 CHK_EXEC(SQLQuery(_T("UPDATE items SET instd_data=instance WHERE node_id=template_id AND instd_method=0")));
1986 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='332' WHERE var_name='SchemaVersion'")));
1987 return TRUE;
1988}
1989
2a964810
VK
1990/**
1991 * Upgrade from V330 to V331
1992 */
1993static BOOL H_UpgradeFromV330(int currVersion, int newVersion)
1994{
1995 if (g_dbSyntax == DB_SYNTAX_ORACLE)
1996 {
1997 CHK_EXEC(SQLQuery(_T("ALTER TABLE audit_log ADD session_id integer DEFAULT 0 NOT NULL")));
1998 }
1999 else
2000 {
2001 CHK_EXEC(SQLQuery(_T("ALTER TABLE audit_log ADD session_id integer NOT NULL DEFAULT 0")));
2002 }
2003 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='331' WHERE var_name='SchemaVersion'")));
2004 return TRUE;
2005}
2006
b66098ac
VK
2007/**
2008 * Upgrade from V329 to V330
2009 */
2010static BOOL H_UpgradeFromV329(int currVersion, int newVersion)
2011{
2012 CHK_EXEC(CreateConfigParam(_T("AlarmListDisplayLimit"), _T("4096"), 1, 0));
2013 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='330' WHERE var_name='SchemaVersion'")));
2014 return TRUE;
2015}
2016
4016c0df 2017/**
2018 * Upgrade from V328 to V329
2019 */
2020static BOOL H_UpgradeFromV328(int currVersion, int newVersion)
2021{
b6005694
VK
2022 CHK_EXEC(SQLQuery(_T("ALTER TABLE items ADD comments $SQL:TEXT")));
2023 CHK_EXEC(SQLQuery(_T("ALTER TABLE dc_tables ADD comments $SQL:TEXT")));
4016c0df 2024 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='329' WHERE var_name='SchemaVersion'")));
2025 return TRUE;
2026}
2027
385b1f20 2028/**
2029 * Upgrade from V327 to V328
2030 */
2031static BOOL H_UpgradeFromV327(int currVersion, int newVersion)
2032{
d368074d 2033 CHK_EXEC(CreateConfigParam(_T("ResolveDNSToIPOnStatusPoll"), _T("0"), 1, 1));
385b1f20 2034 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='328' WHERE var_name='SchemaVersion'")));
2035 return TRUE;
2036}
173c189d
VK
2037
2038/**
2039 * Upgrade from V326 to V327
2040 */
2041static BOOL H_UpgradeFromV326(int currVersion, int newVersion)
2042{
3fa5e14a
VK
2043 CHK_EXEC(DropPrimaryKey(_T("network_map_links")));
2044 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_network_map_links_map_id ON network_map_links(map_id)")));
173c189d 2045 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='327' WHERE var_name='SchemaVersion'")));
22aaa779
VK
2046 return TRUE;
2047}
2048
ade20a86 2049/**
2050 * Upgrade from V325 to V326
2051 */
2052static BOOL H_UpgradeFromV325(int currVersion, int newVersion)
2053{
ade20a86 2054 static TCHAR batch[] =
2055 _T("ALTER TABLE network_map_links DROP COLUMN color\n")
2056 _T("ALTER TABLE network_map_links DROP COLUMN status_object\n")
2057 _T("ALTER TABLE network_map_links DROP COLUMN routing\n")
2058 _T("ALTER TABLE network_map_links DROP COLUMN bend_points\n")
2059 _T("<END>");
2060 CHK_EXEC(SQLBatch(batch));
2061
c849da55
VK
2062 if (g_dbSyntax == DB_SYNTAX_DB2)
2063 {
2064 CHK_EXEC(SQLQuery(_T("CALL Sysproc.admin_cmd('REORG TABLE network_map_links')")));
2065 }
2066
ade20a86 2067 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='326' WHERE var_name='SchemaVersion'")));
2068 return TRUE;
2069}
2070
2071/**
2072 * Upgrade from V324 to V325
2073 */
2074static BOOL H_UpgradeFromV324(int currVersion, int newVersion)
2075{
2076 //move map link configuration to xml
2077
2078 DB_RESULT hResult = SQLSelect(_T("SELECT map_id, element1, element2, element_data, color, status_object, routing, bend_points FROM network_map_links"));
2079 if (hResult != NULL)
2080 {
2081 int count = DBGetNumRows(hResult);
2082 for(int i = 0; i < count; i++)
2083 {
c55ab1c2
VK
2084 TCHAR *config = DBGetField(hResult, i, 3, NULL, 0);
2085 if (config == NULL)
2086 config = _tcsdup(_T(""));
ade20a86 2087 UINT32 color = DBGetFieldULong(hResult, i, 4);
2088 UINT32 statusObject = DBGetFieldULong(hResult, i, 5);
2089 UINT32 routing = DBGetFieldULong(hResult, i, 6);
2090 TCHAR bendPoints[1024];
2091 DBGetField(hResult, i, 7, bendPoints, 1024);
2092
c55ab1c2 2093 TCHAR *newConfig = (TCHAR *)malloc((_tcslen(config) + 4096) * sizeof(TCHAR));
ade20a86 2094 _tcscpy(newConfig, _T("<config>"));
2095 TCHAR* c1 = _tcsstr(config, _T("<dciList"));
2096 TCHAR* c2 = _tcsstr(config, _T("</dciList>"));
2097 if(c1 != NULL && c2!= NULL)
2098 {
2099 *c2 = 0;
2100 _tcscat(newConfig, c1);
2101 _tcscat(newConfig, _T("</dciList>"));
2102 }
2103
2104 TCHAR tmp[2048];
2105 _sntprintf(tmp, 2048, _T("<color>%d</color>"), color),
2106 _tcscat(newConfig, tmp);
2107
c55ab1c2 2108 if (statusObject != 0)
ade20a86 2109 {
2110 _sntprintf(tmp, 2048, _T("<objectStatusList length=\"1\"><long>%d</long></objectStatusList>"), statusObject);
2111 _tcscat(newConfig, tmp);
2112 }
2113
2114 _sntprintf(tmp, 2048, _T("<routing>%d</routing>"), routing);
2115 _tcscat(newConfig, tmp);
2116
c55ab1c2 2117 if (routing == 3 && bendPoints[0] != 0)
ade20a86 2118 {
2119 count = 1;
30b86580 2120 for(size_t j = 0; j < _tcslen(bendPoints); j++)
ade20a86 2121 {
c55ab1c2 2122 if (bendPoints[j] == _T(','))
ade20a86 2123 count++;
2124 }
2125 _sntprintf(tmp, 2048, _T("<bendPoints length=\"%d\">%s</bendPoints>"), count, bendPoints);
2126 _tcscat(newConfig, tmp);
2127 }
2128 _tcscat(newConfig, _T("</config>"));
2129
2130 safe_free(config);
2131 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE network_map_links SET element_data=? WHERE map_id=? AND element1=? AND element2=?"));
e196e5d8
VK
2132 if (statment != NULL)
2133 {
2134 DBBind(statment, 1, DB_SQLTYPE_TEXT, newConfig, DB_BIND_STATIC);
2135 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
2136 DBBind(statment, 3, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 1));
2137 DBBind(statment, 4, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 2));
2138 CHK_EXEC(DBExecute(statment));
2139 DBFreeStatement(statment);
2140 }
2141 else
2142 {
2143 if (!g_bIgnoreErrors)
2144 return FALSE;
2145 }
081bba09 2146 safe_free(newConfig);
ade20a86 2147 }
2148 DBFreeResult(hResult);
2149 }
2150
2151 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='325' WHERE var_name='SchemaVersion'")));
2152 return TRUE;
2153}
2154
2f1bc68b
VK
2155/**
2156 * Upgrade from V323 to V324
2157 */
2158static BOOL H_UpgradeFromV323(int currVersion, int newVersion)
2159{
a9f5aa55 2160 if (!MetaDataReadInt(_T("ValidTDataIndex"), 0)) // check if schema is already correct
2f1bc68b
VK
2161 {
2162 TCHAR query[1024];
ade20a86 2163 _sntprintf(query, 1024,
66717659 2164 _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'"),
2a964810 2165 g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64], g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64]);
2f1bc68b
VK
2166 CHK_EXEC(SQLQuery(query));
2167
2168 RecreateTData(_T("nodes"), true, true);
2169 RecreateTData(_T("clusters"), true, true);
2170 RecreateTData(_T("mobile_devices"), true, true);
2171 }
2172
a9f5aa55 2173 CHK_EXEC(SQLQuery(_T("DELETE FROM metadata WHERE var_name='ValidTDataIndex'")));
2f1bc68b
VK
2174 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='324' WHERE var_name='SchemaVersion'")));
2175 return TRUE;
2176}
2177
c8076b19
VK
2178/**
2179 * Upgrade from V322 to V323
2180 */
2181static BOOL H_UpgradeFromV322(int currVersion, int newVersion)
2182{
2183 CHK_EXEC(CreateConfigParam(_T("ProcessTrapsFromUnmanagedNodes"), _T("0"), 1, 1));
2184 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='323' WHERE var_name='SchemaVersion'")));
2185 return TRUE;
2186}
2187
f4d8fe18 2188/**
2189 * Upgrade from V321 to V322
2190 */
2191static BOOL H_UpgradeFromV321(int currVersion, int newVersion)
2192{
2a964810 2193 switch(g_dbSyntax)
f4d8fe18 2194 {
2195 case DB_SYNTAX_DB2:
87104c6a
VK
2196 CHK_EXEC(SQLBatch(
2197 _T("ALTER TABLE users ALTER COLUMN system_access SET DATA TYPE $SQL:INT64\n")
2198 _T("ALTER TABLE user_groups ALTER COLUMN system_access SET DATA TYPE $SQL:INT64\n")
2199 _T("<END>")));
f4d8fe18 2200 break;
2201 case DB_SYNTAX_MSSQL:
87104c6a
VK
2202 CHK_EXEC(SQLBatch(
2203 _T("ALTER TABLE users ALTER COLUMN system_access $SQL:INT64\n")
2204 _T("ALTER TABLE user_groups ALTER COLUMN system_access $SQL:INT64\n")
2205 _T("<END>")));
f4d8fe18 2206 break;
2207 case DB_SYNTAX_PGSQL:
87104c6a 2208 CHK_EXEC(SQLBatch(
d27fb10d
VK
2209 _T("ALTER TABLE users ALTER COLUMN system_access TYPE $SQL:INT64\n")
2210 _T("ALTER TABLE user_groups ALTER COLUMN system_access TYPE $SQL:INT64\n")
87104c6a 2211 _T("<END>")));
f4d8fe18 2212 break;
2213 case DB_SYNTAX_SQLITE:
87104c6a
VK
2214 CHK_EXEC(SQLBatch(
2215 _T("CREATE TABLE temp_users AS SELECT * FROM users\n")
2216 _T("DROP TABLE users\n")
2217 _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,")
2218 _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,")
2219 _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,")
2220 _T(" xmpp_id varchar(127) null, ldap_dn $SQL:TEXT null, PRIMARY KEY(id))\n")
2221 _T("INSERT INTO users SELECT * FROM temp_users\n")
2222 _T("DROP TABLE temp_users\n")
2223 _T("CREATE TABLE temp_user_groups AS SELECT * FROM user_groups\n")
2224 _T("DROP TABLE user_groups\n")
2225 _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,")
2226 _T(" description varchar(255) not null, ldap_dn $SQL:TEXT null, PRIMARY KEY(id))\n")
2227 _T("INSERT INTO user_groups SELECT * FROM temp_user_groups\n")
2228 _T("DROP TABLE temp_user_groups\n")
2229 _T("<END>")));
f4d8fe18 2230 break;
52162395
VK
2231 case DB_SYNTAX_ORACLE:
2232 // no changes needed
2233 break;
f4d8fe18 2234 default:
87104c6a
VK
2235 CHK_EXEC(SQLBatch(
2236 _T("ALTER TABLE users MODIFY system_access $SQL:INT64\n")
2237 _T("ALTER TABLE user_groups MODIFY system_access $SQL:INT64\n")
2238 _T("<END>")));
f4d8fe18 2239 break;
2240 }
2241
2242 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='322' WHERE var_name='SchemaVersion'")));
2243 return TRUE;
2244}
2245
2246/**
d4ce93a2
VK
2247 * Upgrade from V320 to V321
2248 */
2249static BOOL H_UpgradeFromV320(int currVersion, int newVersion)
2250{
2251 static TCHAR batch[] =
2252 _T("ALTER TABLE object_tools ADD command_short_name varchar(31)\n")
2253 _T("UPDATE object_tools SET command_short_name='Shutdown' WHERE tool_id=1\n")
2254 _T("UPDATE object_tools SET command_short_name='Restart' WHERE tool_id=2\n")
2255 _T("UPDATE object_tools SET command_short_name='Wakeup' WHERE tool_id=3\n")
2256 _T("<END>");
2257 CHK_EXEC(SQLBatch(batch));
2258 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='321' WHERE var_name='SchemaVersion'")));
2259 return TRUE;
2260}
2261
ac14e3e6 2262/**
d4ce93a2 2263 * Upgrade from V319 to V320
d9177dd1 2264 */
2265static BOOL H_UpgradeFromV319(int currVersion, int newVersion)
2266{
6c352bb5 2267 CHK_EXEC(CreateConfigParam(_T("LdapConnectionString"), _T("ldap://localhost:389"), 1, 0));
d9177dd1 2268 CHK_EXEC(CreateConfigParam(_T("LdapSyncUser"), _T(""), 1, 0));
2269 CHK_EXEC(CreateConfigParam(_T("LdapSyncUserPassword"), _T(""), 1, 0));
2270 CHK_EXEC(CreateConfigParam(_T("LdapSearchBase"), _T(""), 1, 0));
2271 CHK_EXEC(CreateConfigParam(_T("LdapSearchFilter"), _T(""), 1, 0));
2272 CHK_EXEC(CreateConfigParam(_T("LdapUserDeleteAction"), _T("1"), 1, 0));
f70a8b3c 2273 CHK_EXEC(CreateConfigParam(_T("LdapMappingName"), _T(""), 1, 0));
d9177dd1 2274 CHK_EXEC(CreateConfigParam(_T("LdapMappingFullName"), _T("displayName"), 1, 0));
2575f149 2275 CHK_EXEC(CreateConfigParam(_T("LdapMappingDescription"), _T(""), 1, 0));
d9177dd1 2276 CHK_EXEC(CreateConfigParam(_T("LdapGroupClass"), _T(""), 1, 0));
2277 CHK_EXEC(CreateConfigParam(_T("LdapUserClass"), _T(""), 1, 0));
2278 CHK_EXEC(CreateConfigParam(_T("LdapSyncInterval"), _T("0"), 1, 0));
2279
2280 static TCHAR batch[] =
2281 _T("ALTER TABLE users ADD ldap_dn $SQL:TEXT\n")
2282 _T("ALTER TABLE user_groups ADD ldap_dn $SQL:TEXT\n")
2283 _T("<END>");
2284 CHK_EXEC(SQLBatch(batch));
2285
2286 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='320' WHERE var_name='SchemaVersion'")));
2287 return TRUE;
2288}
d4ce93a2 2289
d9177dd1 2290/*
ac14e3e6
VK
2291 * Upgrade from V318 to V319
2292 */
2293static BOOL H_UpgradeFromV318(int currVersion, int newVersion)
2294{
2295 static TCHAR batch[] =
2296 _T("ALTER TABLE object_tools ADD icon $SQL:TEXT\n")
2297 _T("ALTER TABLE object_tools ADD command_name varchar(255)\n")
2298 _T("UPDATE object_tools SET flags=74,command_name='Shutdown system',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c086488000002bf49444154388d95933f689c7518c73fbf3f7779efaede995c137369d54123ba4843070b1204c10ed24db182939b90c1c1e2eee05871e8d0d2b98a8ba0c52ee2d0cb622acd99222dd1084d9a18cd3597f7bd6bdebbdffbfbe7701d82e8e0071ebec303dfe7e1e1fb883fea7542aba54c080dac153c261ed1a30540b51a8f359bd9c9e565afedd4943ab6b4f4d56873f38d5014ff6af04f95950aaad5fa7e7d7dfdbcee1d1c3cd95d59395b5a5c7c82ffc1fd4ee7acc9f349fd683090b1db15499ae2013b3f8f1c0e296f6f539c380140796707333747a856296d6ca081d1e1a138cc73a95d8cc28f468834459f3ecd7367cee0b38ccd7bf7787e711180dfaf5ee599850544a3c1760898d5556c51e06314d2c5288be150186b995d58404bc9eef5ebb87e86140229257690b17be33b4a4a3173ea14236b71d60a17a3901684b59652b34952ab31dcda6470f76794c9b0b6c0160665320eefae317ab04552ad529e9ec6c78003292dc861bf2f4408e369fb7b948a8cb2cd7085c115868998936887eb75514a617a3db66eb68505211d30f86b97dde536420844a341b17e8bf8db0a21ed12d23ddcda0ff46f7e4dac24482939b8b386b3060f4207206a457afb16be9f519f7f91f22baf52f9e91bfca7ef00829a4fb1af9fa3fed2cbf8419f6c75054a0a0fc800a025f151cafdcb17514af3ecc79f939fbf40d69c259d9ca1ffd687cc7d7411a5145b573e230e52d0120f68ffd8400ad8b97685c9934f31f9ee07b4de5e227ff37d8c311c4f12aad50afb5f5c62e7da65a400519204408f37108408de471e5cfa04fbe3b74c9d7b8ff2d32f1042805f7e25bdf1257fdeee103c8408528d53afa356c85a42b107d6812920bdd3c16f7448cae3d81a0b837cdc2b1c380f724203445d8ff161767cb66df1afe5380a0d3d05ca8d0f148110c02bb035b013109b1a17747b06baa20d3c84897dc93420feeb0b8f22203603dd19307f037f0665861328b32e0000000049454e44ae426082' WHERE tool_id=1\n")
2299 _T("UPDATE object_tools SET flags=74,command_name='Restart system',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c0864880000029849444154388d85934d8b1c5514869f73eeadaaae6ea77bda388999882241fc5c0e09ae4484c4857b4177fa0b5474256edcfa075cbacbc23f6074a12332a0a2200a2189e8c468a23d93e99e9eaeaaae7beb1e1733f9902c7ce06c0ebc2f2fe7f0ca5fe311fdd71e77d93332922e08b749dcc5fe3bc9f72d5d1fcf861f7dd6f9f295353778ebdd0b71ff977374ad60f7888e1003bbb3379ceb930f4e7fbe7be5a7573da7f65697db17cf2ff2e757fe6e06a00e1141040aab59ebb5dc47076efbdb739cacc63edc9aabee4f64796248efb10dbcf738e750556268e97eff14b937ce6d1607126e55eae33c4956d5f4f72f21ae40d6cfe0bc4755c9f39cd4ccc0d27d7a695ae23c896fe7a645d5482c26340f0d19e639beb9811463c85788610a29c1d11d4284cc416a82848589861a49754bab390fac3f4ba69174f963ba7040d745249f4136033f63efd859769f78933a792c244265ea436d9a9a99e86895bc28b0e90fc03632bd88e463acb787580696f3e0fa299ade09e275a5feed2b09b5898f4ba35bdc40bb6b0034f5357cda4277bec354400d0b0d5d75406a5e42caa751d90596c425e22d00aa48771933a3c99e6230a8d1b241dcd1eb03a4a2c4563600f07615bc622da80510149aefa1b982ef3dc24d7d071b7afc71f0c781d58c83d107e48347d1f62a1a7f44f4d0c0130c4c4196d89fefa1273f215f7d9d4b379fa4dfdf22cb32bc7f99b5f533c45893edbc4fe75a8c0116c05b008b408434fd9a327f031b7d08c73670ee2c65595296259afe20fbe76de2fc9ba39e1cd6c6a7e4b0ae87d5200a4cbea4acce3318bd8865cfe1a283dd9fe9a65f901615a982d400c96360be9eda4ebbfdf0a6ec752f741ac11f1a89db02d9ba5bc8d483ae877587e2f0abdfac2b26b209488fa218b07627d7ff636dc524d52cff0513e53f37235ac3190000000049454e44ae426082' WHERE tool_id=2\n")
2300 _T("UPDATE object_tools SET flags=64,command_name='Wakeup node using Wake-On-LAN',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c086488000000097048597300000dd700000dd70142289b780000001974455874536f667477617265007777772e696e6b73636170652e6f72679bee3c1a0000023649444154388d8d924f48545114c67ff7bd37ff7cf9071bc70a4d47271ca15c848d448185b40a89204890362d8568eb2270d7a2762d5cb7711504d1ae10c195218895a488528e4e06a653d338de7bdfbcf75ac84ce38c901f9ccdb9e7fbce39f77c627ce6872df6dd71f01f781e1d9c00866003215efaf99de7d6763afb1078721262053a800908ed5a5aa9b1e3bb0802a600c0717d3cdf3fae6cccd24a25abb302a80b990c265a009859d941299763249296d6b2a6732468d25a1f24156f00e0cbd62e9b5a71a0dd9a490cad14a570b4266c780cf546797cab1b1317139747435ddcec69266c78385a53c9b1b45265b548d022d51563f45a9c778b69ce35850058de928c0cb4933fd04c7ffece812e9639e5158480865098ebc9181fbfeef07a6e9dc68805c0af8243f45480ab174e33bb9426e7484a9b942710020c3b40e24c236f3facb1bd9b634d3a00d8e100ab992cb7af7421bc225aa9b280a195a414524972054d5f679488e5a394442949d8f4b8d4d14caea09115f55a490cad155a2b9452ecfdcef37e619ddef6287706ba89c76ce2319be1fe4e926d51663e6d90cdeda3d42147ebaa4fcc161da6a61739df52cfe88d8b0ca712f8be871d0e31bb94666a7a916c2e8feb7aff3cd33ef2f4c8612dd3a0a5d1a6bfa78d544f1bbeef33bf9a617e65939fb902c50a328068bd3bb10c1c71a3210401cb24143cbc82d2459c62ad8980154b2b3909bca87e91c09fea642d26ad67f7fb32afe6bebd5958dd1c2c48ddf45f8a10d87591bdcb89b3b3f7063a337f01f30f1c1c580292640000000049454e44ae426082' WHERE tool_id=3\n")
2301 _T("<END>");
2302 CHK_EXEC(SQLBatch(batch));
2303 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='319' WHERE var_name='SchemaVersion'")));
2304 return TRUE;
2305}
2306
56d5289b
VK
2307/**
2308 * Upgrade from V317 to V318
2309 */
2310static BOOL H_UpgradeFromV317(int currVersion, int newVersion)
2311{
2312 CHK_EXEC(CreateEventTemplate(EVENT_AP_DOWN, _T("SYS_AP_DOWN"), SEVERITY_CRITICAL, EF_LOG,
2313 _T("Access point %2 changed state to DOWN"),
2314 _T("Generated when access point state changes to DOWN.\r\n")
2315 _T("Parameters:\r\n")
2316 _T(" 1) Access point object ID\r\n")
2317 _T(" 2) Access point name\r\n")
2318 _T(" 3) Access point MAC address\r\n")
2319 _T(" 4) Access point IP address\r\n")
2320 _T(" 5) Access point vendor name\r\n")
2321 _T(" 6) Access point model\r\n")
2322 _T(" 7) Access point serial number")));
2323
2324 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='318' WHERE var_name='SchemaVersion'")));
2325 return TRUE;
2326}
2327
8deadd79
VK
2328/**
2329 * Upgrade from V316 to V317
2330 */
2331static BOOL H_UpgradeFromV316(int currVersion, int newVersion)
2332{
2333 CHK_EXEC(CreateConfigParam(_T("MinViewRefreshInterval"), _T("1000"), 1, 0));
2334 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='317' WHERE var_name='SchemaVersion'")));
2335 return TRUE;
2336}
2337
23ed00c4
VK
2338/**
2339 * Upgrade from V315 to V316
2340 */
2341static BOOL H_UpgradeFromV315(int currVersion, int newVersion)
2342{
2343 static TCHAR batch[] =
2344 _T("ALTER TABLE access_points ADD ap_state integer\n")
2345 _T("UPDATE access_points SET ap_state=0\n")
2346 _T("<END>");
2347 CHK_EXEC(SQLBatch(batch));
2348
2349 CHK_EXEC(CreateEventTemplate(EVENT_AP_ADOPTED, _T("SYS_AP_ADOPTED"), SEVERITY_NORMAL, EF_LOG,
2350 _T("Access point %2 changed state to ADOPTED"),
2351 _T("Generated when access point state changes to ADOPTED.\r\n")
2352 _T("Parameters:\r\n")
2353 _T(" 1) Access point object ID\r\n")
2354 _T(" 2) Access point name\r\n")
2355 _T(" 3) Access point MAC address\r\n")
2356 _T(" 4) Access point IP address\r\n")
2357 _T(" 5) Access point vendor name\r\n")
2358 _T(" 6) Access point model\r\n")
2359 _T(" 7) Access point serial number")));
2360
2361 CHK_EXEC(CreateEventTemplate(EVENT_AP_UNADOPTED, _T("SYS_AP_UNADOPTED"), SEVERITY_MAJOR, EF_LOG,
2362 _T("Access point %2 changed state to UNADOPTED"),
2363 _T("Generated when access point state changes to UNADOPTED.\r\n")
2364 _T("Parameters:\r\n")
2365 _T(" 1) Access point object ID\r\n")
2366 _T(" 2) Access point name\r\n")
2367 _T(" 3) Access point MAC address\r\n")
2368 _T(" 4) Access point IP address\r\n")
2369 _T(" 5) Access point vendor name\r\n")
2370 _T(" 6) Access point model\r\n")
2371 _T(" 7) Access point serial number")));
2372
2373 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='316' WHERE var_name='SchemaVersion'")));
2374 return TRUE;
2375}
2376
b2042b58
VK
2377/**
2378 * Upgrade from V314 to V315
2379 */
2380static BOOL H_UpgradeFromV314(int currVersion, int newVersion)
2381{
2382 static TCHAR batch[] =
2383 _T("ALTER TABLE thresholds ADD match_count integer\n")
2384 _T("UPDATE thresholds SET match_count=0 WHERE current_state=0\n")
2385 _T("UPDATE thresholds SET match_count=1 WHERE current_state<>0\n")
2386 _T("<END>");
2387 CHK_EXEC(SQLBatch(batch));
2388 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='315' WHERE var_name='SchemaVersion'")));
2389 return TRUE;
2390}
2391
2d7ee2e0 2392/**
2393 * Upgrade from V313 to V314
2394 */
2395static BOOL H_UpgradeFromV313(int currVersion, int newVersion)
2396{
b2042b58 2397 // Replace double backslash with single backslash in all "file download" (code 7) object tools
2d7ee2e0 2398 DB_RESULT hResult = SQLSelect(_T("SELECT tool_id, tool_data FROM object_tools WHERE tool_type=7"));
2399 if (hResult != NULL)
2400 {
2401 int count = DBGetNumRows(hResult);
2402 for(int i = 0; i < count; i++)
2403 {
2404 TCHAR* toolData = DBGetField(hResult, i, 1, NULL, 0);
2405 TranslateStr(toolData, _T("\\\\"), _T("\\"));
2406
2407 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE object_tools SET tool_data=? WHERE tool_id=?"));
2408 if (statment == NULL)
2409 return FALSE;
2410 DBBind(statment, 1, DB_SQLTYPE_TEXT, toolData, DB_BIND_DYNAMIC);
2411 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
2412 if(!DBExecute(statment))
2413 return FALSE;
2414 DBFreeStatement(statment);
2415 }
2416 DBFreeResult(hResult);
2417 }
2418
2419 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='314' WHERE var_name='SchemaVersion'")));
2420 return TRUE;
2421}
2422
2423/**
2424 * Upgrade from V312 to V313
2425 */
2426static BOOL H_UpgradeFromV312(int currVersion, int newVersion)
2427{
4dfa78b6
VK
2428 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("tool_name")));
2429 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("tool_data")));
2430 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("description")));
2431 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("confirmation_text")));
2432 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("matching_oid")));
2433 CHK_EXEC(SetColumnNullable(_T("object_tools_table_columns"), _T("col_name")));
2d7ee2e0 2434 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("tool_name")));
2435 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("tool_data")));
2436 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("description")));
2437 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("confirmation_text")));
2438 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("matching_oid")));
2439 CHK_EXEC(ConvertStrings(_T("object_tools_table_columns"), _T("tool_id"), _T("col_number"), _T("col_name"), false));
2440 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='313' WHERE var_name='SchemaVersion'")));
2441 return TRUE;
2442}
2443
6498e05a
VK
2444/**
2445 * Upgrade from V311 to V312
2446 */
2447static BOOL H_UpgradeFromV311(int currVersion, int newVersion)
2448{
2449 CHK_EXEC(CreateConfigParam(_T("EnableReportingServer"), _T("0"), 1, 1));
2450 CHK_EXEC(CreateConfigParam(_T("ReportingServerHostname"), _T("localhost"), 1, 1));
2451 CHK_EXEC(CreateConfigParam(_T("ReportingServerPort"), _T("4710"), 1, 1));
2452 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='312' WHERE var_name='SchemaVersion'")));
2453 return TRUE;
2454}
2455
9262b092
VK
2456/**
2457 * Upgrade from V310 to V311
2458 */
2459static BOOL H_UpgradeFromV310(int currVersion, int newVersion)
2460{
2461 IntegerArray<UINT32> deleteList;
2462
2463 // reports
2464 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM reports"));
2465 if (hResult != NULL)
2466 {
2467 int count = DBGetNumRows(hResult);
2468 for(int i = 0; i < count; i++)
2469 deleteList.add(DBGetFieldULong(hResult, i, 0));
2470 DBFreeResult(hResult);
2471 }
2472
2473 // report groups
2474 hResult = SQLSelect(_T("SELECT id FROM containers WHERE object_class=25"));
2475 if (hResult != NULL)
2476 {
2477 int count = DBGetNumRows(hResult);
2478 for(int i = 0; i < count; i++)
2479 deleteList.add(DBGetFieldULong(hResult, i, 0));
2480 DBFreeResult(hResult);
2481 }
2482
2483 for(int i = 0; i < deleteList.size(); i++)
2484 {
2485 TCHAR query[256];
2486
2487 _sntprintf(query, 256, _T("DELETE FROM object_properties WHERE object_id=%d"), deleteList.get(i));
2488 CHK_EXEC(SQLQuery(query));
2489
2490 _sntprintf(query, 256, _T("DELETE FROM object_custom_attributes WHERE object_id=%d"), deleteList.get(i));
2491 CHK_EXEC(SQLQuery(query));
2492
2493 _sntprintf(query, 256, _T("DELETE FROM acl WHERE object_id=%d"), deleteList.get(i));
2494 CHK_EXEC(SQLQuery(query));
2495
2496 _sntprintf(query, 256, _T("DELETE FROM container_members WHERE object_id=%d OR container_id=%d"), deleteList.get(i), deleteList.get(i));
2497 CHK_EXEC(SQLQuery(query));
2498 }
2499
2500 static TCHAR batch[] =
2501 _T("DROP TABLE reports\n")
2502 _T("DROP TABLE report_results\n")
2503 _T("DELETE FROM containers WHERE object_class=25\n")
2504 _T("DELETE FROM object_properties WHERE object_id=8\n")
2505 _T("<END>");
2506 CHK_EXEC(SQLBatch(batch));
2507 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='311' WHERE var_name='SchemaVersion'")));
2508 return TRUE;
2509}
2510
32745683
VK
2511/**
2512 * Upgrade from V309 to V310
2513 */
2514static BOOL H_UpgradeFromV309(int currVersion, int newVersion)
2515{
2516 static TCHAR batch[] =
2517 _T("ALTER TABLE interfaces ADD peer_proto integer\n")
2518 _T("UPDATE interfaces SET peer_proto=0\n")
2519 _T("<END>");
2520 CHK_EXEC(SQLBatch(batch));
2521 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='310' WHERE var_name='SchemaVersion'")));
2522 return TRUE;
2523}
2524
34a320ad
VK
2525/**
2526 * Upgrade from V308 to V309
2527 */
2528static BOOL H_UpgradeFromV308(int currVersion, int newVersion)
2529{
2530 CHK_EXEC(CreateConfigParam(_T("HelpDeskLink"), _T("none"), 1, 1));
2531 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='309' WHERE var_name='SchemaVersion'")));
2532 return TRUE;
2533}
2534
2ab9314f 2535/**
2536 * Upgrade from V307 to V308
2537 */
2538static BOOL H_UpgradeFromV307(int currVersion, int newVersion)
2539{
2540 static TCHAR batch[] =
32745683
VK
2541 _T("ALTER TABLE network_map_elements ADD flags integer\n")
2542 _T("UPDATE network_map_elements SET flags=0\n") //set all elements like manually generated
2543 _T("ALTER TABLE network_map_links ADD element_data $SQL:TEXT\n")
2544 _T("ALTER TABLE network_map_links ADD flags integer\n")
2545 _T("UPDATE network_map_links SET flags=0\n") //set all elements like manually generated
2546 _T("<END>");
2ab9314f 2547 CHK_EXEC(SQLBatch(batch));
2548
32745683
VK
2549 // it is assumed that now all autogenerated maps contain only autogenerated objects and links
2550 // get elements from autogenerated maps and set their flags to AUTO_GENERATED for map elements and map links
2ab9314f 2551 TCHAR query[256];
2552 _sntprintf(query, 256, _T("SELECT id FROM network_maps WHERE map_type=%d OR map_type=%d"),
2553 MAP_TYPE_LAYER2_TOPOLOGY, MAP_TYPE_IP_TOPOLOGY);
2554 DB_RESULT hResult = SQLSelect(query);
2555 if (hResult != NULL)
2556 {
2557 int count = DBGetNumRows(hResult);
2558 for(int i = 0; i < count; i++)
2559 {
2560 _sntprintf(query, 256, _T("UPDATE network_map_elements SET flags='1' WHERE map_id=%d"),
2561 DBGetFieldULong(hResult, i, 0));
2562 CHK_EXEC(SQLQuery(query));
2563 _sntprintf(query, 256, _T("UPDATE network_map_links SET flags='1' WHERE map_id=%d"),
2564 DBGetFieldULong(hResult, i, 0));
2565 CHK_EXEC(SQLQuery(query));
2566 }
2567 DBFreeResult(hResult);
2568 }
2569
2570 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='308' WHERE var_name='SchemaVersion'")));
2571 return TRUE;
2572}
2573
b3eb058b
VK
2574/**
2575 * Upgrade from V306 to V307
2576 */
2577static BOOL H_UpgradeFromV306(int currVersion, int newVersion)
2578{
2579 CHK_EXEC(SetColumnNullable(_T("config_clob"), _T("var_value")));
2580 CHK_EXEC(ConvertStrings(_T("config_clob"), _T("var_name"), NULL, _T("var_value"), true));
2581 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='307' WHERE var_name='SchemaVersion'")));
2582 return TRUE;
2583}
2584
d1730ccf
VK
2585/**
2586 * Upgrade from V305 to V306
2587 */
2588static BOOL H_UpgradeFromV305(int currVersion, int newVersion)
2589{
2590 CHK_EXEC(CreateConfigParam(_T("ExtendedLogQueryAccessControl"), _T("0"), 1, 0));
2591 CHK_EXEC(CreateConfigParam(_T("EnableTimedAlarmAck"), _T("1"), 1, 1));
9a1f7c45 2592 CHK_EXEC(CreateConfigParam(_T("EnableCheckPointSNMP"), _T("0"), 1, 0));
d1730ccf
VK
2593 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='306' WHERE var_name='SchemaVersion'")));
2594 return TRUE;
2595}
2596
035745fc
VK
2597/**
2598 * Upgrade from V304 to V305
2599 */
2600static BOOL H_UpgradeFromV304(int currVersion, int newVersion)
2601{
2ab9314f 2602 CHK_EXEC(CreateEventTemplate(EVENT_IF_PEER_CHANGED, _T("SYS_IF_PEER_CHANGED"), SEVERITY_NORMAL, EF_LOG,
035745fc
VK
2603 _T("New peer for interface %3 is %7 interface %10 (%12)"),
2604 _T("Generated when peer information for interface changes.\r\n")
2605 _T("Parameters:\r\n")
2606 _T(" 1) Local interface object ID\r\n")
2607 _T(" 2) Local interface index\r\n")
2608 _T(" 3) Local interface name\r\n")
2609 _T(" 4) Local interface IP address\r\n")
2610 _T(" 5) Local interface MAC address\r\n")
2611 _T(" 6) Peer node object ID\r\n")
2612 _T(" 7) Peer node name\r\n")
2613 _T(" 8) Peer interface object ID\r\n")
2614 _T(" 9) Peer interface index\r\n")
2615 _T(" 10) Peer interface name\r\n")
2616 _T(" 11) Peer interface IP address\r\n")
2617 _T(" 12) Peer interface MAC address")));
2618 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='305' WHERE var_name='SchemaVersion'")));
2619 return TRUE;
2620}
2621
fce4295c 2622/**
2623 * Upgrade from V303 to V304
2624 */
2625static BOOL H_UpgradeFromV303(int currVersion, int newVersion)
2626{
d184506f 2627 CHK_EXEC(CreateConfigParam(_T("StrictAlarmStatusFlow"), _T("0"), 1, 0));
fce4295c 2628 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='304' WHERE var_name='SchemaVersion'")));
2629 return TRUE;
2630}
2631
6f33021d 2632/**
2633 * Upgrade from V302 to V303
2634 */
2635static BOOL H_UpgradeFromV302(int currVersion, int newVersion)
2636{
2637 static TCHAR batch[] =
2638 _T("ALTER TABLE alarms ADD ack_timeout integer\n")
2639 _T("UPDATE alarms SET ack_timeout='0'\n")
2640 _T("<END>");
2641 CHK_EXEC(SQLBatch(batch));
2642
2643 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='303' WHERE var_name='SchemaVersion'")));
2644 return TRUE;
2645}
2646
27088c41
AK
2647/**
2648 * Upgrade from V301 to V302
2649 */
2650static BOOL H_UpgradeFromV301(int currVersion, int newVersion)
2651{
2652 static TCHAR batch[] =
2653 _T("DELETE FROM config WHERE var_name='DisableVacuum'\n")
2654 _T("<END>");
2655 CHK_EXEC(SQLBatch(batch));
2656
2657 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='302' WHERE var_name='SchemaVersion'")));
2658 return TRUE;
2659}
2660
1d34c533
VK
2661/**
2662 * Upgrade from V300 to V301
2663 */
2664static BOOL H_UpgradeFromV300(int currVersion, int newVersion)
2665{
2666 static TCHAR batch[] =
2667 _T("ALTER TABLE thresholds ADD script $SQL:TEXT\n")
2668 _T("ALTER TABLE thresholds ADD sample_count integer\n")
2669 _T("UPDATE thresholds SET sample_count=parameter_1\n")
2670 _T("ALTER TABLE thresholds DROP COLUMN parameter_1\n")
2671 _T("ALTER TABLE thresholds DROP COLUMN parameter_2\n")
2672 _T("<END>");
2673 CHK_EXEC(SQLBatch(batch));
2674
2675 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='301' WHERE var_name='SchemaVersion'")));
2676 return TRUE;
2677}
2678
244c65ef
VK
2679/**
2680 * Upgrade from V299 to V300
2681 */
2682static BOOL H_UpgradeFromV299(int currVersion, int newVersion)
2683{
2684 CHK_EXEC(CreateConfigParam(_T("EnableXMPPConnector"), _T("0"), 1, 1));
2685 CHK_EXEC(CreateConfigParam(_T("XMPPLogin"), _T("netxms@localhost"), 1, 1));
2686 CHK_EXEC(CreateConfigParam(_T("XMPPPassword"), _T("netxms"), 1, 1));
2687 CHK_EXEC(CreateConfigParam(_T("XMPPServer"), _T("localhost"), 1, 1));
2688 CHK_EXEC(CreateConfigParam(_T("XMPPPort"), _T("5222"), 1, 1));
2689
2690 SetColumnNullable(_T("users"), _T("full_name"));
2691 SetColumnNullable(_T("users"), _T("description"));
2692 SetColumnNullable(_T("users"), _T("cert_mapping_data"));
2693 SetColumnNullable(_T("user_groups"), _T("description"));
2694 SetColumnNullable(_T("userdb_custom_attributes"), _T("attr_value"));
2695
2696 ConvertStrings(_T("users"), _T("id"), _T("full_name"));
2697 ConvertStrings(_T("users"), _T("id"), _T("description"));
2698 ConvertStrings(_T("users"), _T("id"), _T("cert_mapping_data"));
2699 ConvertStrings(_T("user_groups"), _T("id"), _T("description"));
2700 ConvertStrings(_T("userdb_custom_attributes"), _T("object_id"), _T("attr_name"), _T("attr_name"), true);
2701 ConvertStrings(_T("userdb_custom_attributes"), _T("object_id"), _T("attr_name"), _T("attr_value"), true);
2702
2703 CHK_EXEC(SQLQuery(_T("ALTER TABLE users ADD xmpp_id varchar(127)")));
2704
2705 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='300' WHERE var_name='SchemaVersion'")));
2706 return TRUE;
2707}
2708
ce47611c
VK
2709/**
2710 * Upgrade from V298 to V299
2711 */
2712static BOOL H_UpgradeFromV298(int currVersion, int newVersion)
2713{
2714 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET message='Subnet %2 added',description='")
2715 _T("Generated when subnet object added to the database.\r\n")
2716 _T("Parameters:\r\n")
2717 _T(" 1) Subnet object ID\r\n")
2718 _T(" 2) Subnet name\r\n")
2719 _T(" 3) IP address\r\n")
2720 _T(" 4) Network mask")
2721 _T("' WHERE event_code=2")));
2722 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET message='Subnet %2 deleted',description='")
2723 _T("Generated when subnet object deleted from the database.\r\n")
2724 _T("Parameters:\r\n")
2725 _T(" 1) Subnet object ID\r\n")
2726 _T(" 2) Subnet name\r\n")
2727 _T(" 3) IP address\r\n")
2728 _T(" 4) Network mask")
2729 _T("' WHERE event_code=19")));
2730 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='299' WHERE var_name='SchemaVersion'")));
2731 return TRUE;
2732}
2733
af21affe
VK
2734/**
2735 * Upgrade from V297 to V298
2736 */
2737static BOOL H_UpgradeFromV297(int currVersion, int newVersion)
2738{
2739 CHK_EXEC(CreateConfigParam(_T("AgentDefaultSharedSecret"), _T("netxms"), 1, 0));
2740 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='298' WHERE var_name='SchemaVersion'")));
2741 return TRUE;
2742}
2743
e02953a4
VK
2744/**
2745 * Upgrade from V296 to V297
2746 */
2747static BOOL H_UpgradeFromV296(int currVersion, int newVersion)
2748{
2749 CHK_EXEC(CreateConfigParam(_T("UseSNMPTrapsForDiscovery"), _T("0"), 1, 1));
2750 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='297' WHERE var_name='SchemaVersion'")));
2751 return TRUE;
2752}
2753
71e4ed3a
VK
2754/**
2755 * Upgrade from V295 to V296
2756 */
2757static BOOL H_UpgradeFromV295(int currVersion, int newVersion)
2758{
2759 static TCHAR batch[] =
2760 _T("ALTER TABLE nodes ADD boot_time integer\n")
2761 _T("UPDATE nodes SET boot_time=0\n")
2762 _T("<END>");
2763 CHK_EXEC(SQLBatch(batch));
2764
2765 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='296' WHERE var_name='SchemaVersion'")));
2766 return TRUE;
2767}
2768
c59466d2
VK
2769/**
2770 * Upgrade from V294 to V295
2771 */
2772static BOOL H_UpgradeFromV294(int currVersion, int newVersion)
2773{
2774 CHK_EXEC(CreateConfigParam(_T("IcmpPingTimeout"), _T("1500"), 1, 1));
2775 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='295' WHERE var_name='SchemaVersion'")));
2776 return TRUE;
2777}
2778
1d4f7890
VK
2779/**
2780 * Upgrade from V293 to V294
2781 */
2782static BOOL H_UpgradeFromV293(int currVersion, int newVersion)
2783{
2784 static TCHAR batch[] =
2785 _T("DELETE FROM metadata WHERE var_name LIKE 'TDataTableCreationCommand_%'\n")
2786 _T("INSERT INTO metadata (var_name,var_value)")
2787 _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")
2788 _T("INSERT INTO metadata (var_name,var_value)")
a9f5aa55 2789 _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")
1d4f7890
VK
2790 _T("INSERT INTO metadata (var_name,var_value)")
2791 _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")
a9f5aa55
VK
2792 _T("INSERT INTO metadata (var_name,var_value)")
2793 _T(" VALUES ('TDataIndexCreationCommand_2','CREATE INDEX idx_tdata_rec_%d_id ON tdata_records_%d(record_id)')\n")
1d4f7890
VK
2794 _T("<END>");
2795 CHK_EXEC(SQLBatch(batch));
2796
2f1bc68b
VK
2797 RecreateTData(_T("nodes"), true, false);
2798 RecreateTData(_T("clusters"), true, false);
2799 RecreateTData(_T("mobile_devices"), true, false);
1d4f7890 2800
a9f5aa55 2801 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('ValidTDataIndex','1')")));
1d4f7890
VK
2802 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='294' WHERE var_name='SchemaVersion'")));
2803 return TRUE;
2804}
2805
bc969eaf
VK
2806/**
2807 * Upgrade from V292 to V293
2808 */
2809static BOOL H_UpgradeFromV292(int currVersion, int newVersion)
2810{
2811 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleShortTimeFormat"), _T("HH:mm"), 1, 0));
2812 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='293' WHERE var_name='SchemaVersion'")));
2813 return TRUE;
2814}
2815
badf9a95
VK
2816/**
2817 * Upgrade from V291 to V292
2818 */
2819static BOOL H_UpgradeFromV291(int currVersion, int newVersion)
2820{
2821 CHK_EXEC(SQLQuery(_T("ALTER TABLE event_policy ADD rule_guid varchar(36)")));
50963ced 2822 CHK_EXEC(GenerateGUID(_T("event_policy"), _T("rule_id"), _T("rule_guid"), NULL));
badf9a95
VK
2823 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='292' WHERE var_name='SchemaVersion'")));
2824 return TRUE;
2825}
2826
48d50116
VK
2827/**
2828 * Upgrade from V290 to V291
2829 */
2830static BOOL H_UpgradeFromV290(int currVersion, int newVersion)
2831{
2832 CHK_EXEC(SQLQuery(_T("UPDATE network_services SET service_type=7 WHERE service_type=6")));
2833 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='291' WHERE var_name='SchemaVersion'")));
2834 return TRUE;
2835}
2836
701726bc
VK
2837/**
2838 * Upgrade from V289 to V290
2839 */
2840static BOOL H_UpgradeFromV289(int currVersion, int newVersion)
2841{
2842 CHK_EXEC(SQLQuery(_T("ALTER TABLE network_maps ADD filter $SQL:TEXT")));
2843 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='290' WHERE var_name='SchemaVersion'")));
2844 return TRUE;
2845}
2846
90297892
VK
2847/**
2848 * Upgrade from V288 to V289
2849 */
2850static BOOL H_UpgradeFromV288(int currVersion, int newVersion)
2851{
d95c3dad
A
2852 static TCHAR batch[] =
2853 _T("ALTER TABLE dct_thresholds DROP COLUMN current_state\n")
2854 _T("<END>");
2855 CHK_EXEC(SQLBatch(batch));
90297892
VK
2856 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='289' WHERE var_name='SchemaVersion'")));
2857 return TRUE;
2858}
2859
a0dc14f9
VK
2860/**
2861 * Upgrade from V287 to V288
2862 */
2863static BOOL H_UpgradeFromV287(int currVersion, int newVersion)
2864{
2865 CHK_EXEC(CreateEventTemplate(EVENT_TABLE_THRESHOLD_ACTIVATED, _T("SYS_TABLE_THRESHOLD_ACTIVATED"), EVENT_SEVERITY_MINOR, EF_LOG,
2866 _T("Threshold activated on table \"%2\" row %4 (%5)"),
2867 _T("Generated when table threshold is activated.\r\n")
2868 _T("Parameters:\r\n")
2869 _T(" 1) Table DCI name\r\n")
2870 _T(" 2) Table DCI description\r\n")
2871 _T(" 3) Table DCI ID\r\n")
2872 _T(" 4) Table row\r\n")
2873 _T(" 5) Instance")));
2874
2875 CHK_EXEC(CreateEventTemplate(EVENT_TABLE_THRESHOLD_DEACTIVATED, _T("SYS_TABLE_THRESHOLD_DEACTIVATED"), EVENT_SEVERITY_NORMAL, EF_LOG,
2876 _T("Threshold deactivated on table \"%2\" row %4 (%5)"),
2877 _T("Generated when table threshold is deactivated.\r\n")
2878 _T("Parameters:\r\n")
2879 _T(" 1) Table DCI name\r\n")
2880 _T(" 2) Table DCI description\r\n")
2881 _T(" 3) Table DCI ID\r\n")
2882 _T(" 4) Table row\r\n")
2883 _T(" 5) Instance")));
2884
2885 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='288' WHERE var_name='SchemaVersion'")));
2886 return TRUE;
2887}
2888
9387bc59
VK
2889/**
2890 * Upgrade from V286 to V287
2891 */
2892static BOOL H_UpgradeFromV286(int currVersion, int newVersion)
2893{
2894 static TCHAR batch[] =
2895 _T("ALTER TABLE dc_table_columns ADD sequence_number integer\n")
2896 _T("UPDATE dc_table_columns SET sequence_number=0\n")
2897 _T("<END>");
2898 CHK_EXEC(SQLBatch(batch));
2899
2900 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='287' WHERE var_name='SchemaVersion'")));
2901 return TRUE;
2902}
2903
9098ad59
VK
2904/**
2905 * Upgrade from V285 to V286
2906 */
2907static BOOL H_UpgradeFromV285(int currVersion, int newVersion)
2908{
2909 CHK_EXEC(CreateTable(
2910 _T("CREATE TABLE dct_thresholds (")
2911 _T("id integer not null,")
2912 _T("table_id integer not null,")
2913 _T("sequence_number integer not null,")
2914 _T("current_state char(1) not null,")
2915 _T("activation_event integer not null,")
2916 _T("deactivation_event integer not null,")
2917 _T("PRIMARY KEY(id))")));
2918
2919 CHK_EXEC(CreateTable(
2920 _T("CREATE TABLE dct_threshold_conditions (")
2921 _T("threshold_id integer not null,")
2922 _T("group_id integer not null,")
2923 _T("sequence_number integer not null,")
2924 _T("column_name varchar(63) null,")
2925 _T("check_operation integer not null,")
2926 _T("check_value varchar(255) null,")
2927 _T("PRIMARY KEY(threshold_id,group_id,sequence_number))")));
2928
2929 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='286' WHERE var_name='SchemaVersion'")));
2930 return TRUE;
2931}
2932
17017844
VK
2933/**
2934 * Upgrade from V284 to V285
2935 */
2936static BOOL H_UpgradeFromV284(int currVersion, int newVersion)
2937{
2938 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_items_node_id ON items(node_id)")));
2939 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_dc_tables_node_id ON dc_tables(node_id)")));
2940
2941 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='285' WHERE var_name='SchemaVersion'")));
2942 return TRUE;
2943}
2944
eb4822b6
VK
2945/**
2946 * Upgrade from V283 to V284
2947 */
2948static BOOL H_UpgradeFromV283(int currVersion, int newVersion)
2949{
2950 CHK_EXEC(CreateConfigParam(_T("SNMPTrapPort"), _T("162"), 1, 1));
2951
2952 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='284' WHERE var_name='SchemaVersion'")));
2953 return TRUE;
2954}
2955
0b77b73e
VK
2956/**
2957 * Upgrade from V282 to V283
2958 */
2959static BOOL H_UpgradeFromV282(int currVersion, int newVersion)
2960{
2961 static TCHAR batch[] =
2962 _T("ALTER TABLE dc_table_columns ADD display_name varchar(255)\n")
2963 _T("UPDATE dc_table_columns SET display_name=column_name\n")
2964 _T("<END>");
2965 CHK_EXEC(SQLBatch(batch));
2966
2967 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='283' WHERE var_name='SchemaVersion'")));
2968 return TRUE;
2969}
2970
549f48b3
VK
2971/**
2972 * Upgrade from V281 to V282
2973 */
2974static BOOL H_UpgradeFromV281(int currVersion, int newVersion)
2975{
2976 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='WindowsConsoleUpgradeURL'")));
2977 CHK_EXEC(CreateConfigParam(_T("EnableObjectTransactions"), _T("0"), 1, 1));
2978
2979 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='282' WHERE var_name='SchemaVersion'")));
2980 return TRUE;
2981}
2982
22aaa779
VK
2983/**
2984 * Upgrade from V280 to V281
2985 */
2986static BOOL H_UpgradeFromV280(int currVersion, int newVersion)
2987{
2988 static TCHAR batch[] =
2989 _T("DELETE FROM metadata WHERE var_name='TDataTableCreationCommand'\n")
2990 _T("INSERT INTO metadata (var_name,var_value)")
2991 _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")
2992 _T("INSERT INTO metadata (var_name,var_value)")
2993 _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")
2994 _T("INSERT INTO metadata (var_name,var_value)")
2995 _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")
2996 _T("INSERT INTO metadata (var_name,var_value)")
2997 _T(" VALUES ('TDataIndexCreationCommand_1','CREATE INDEX idx_tdata_rec_%d_instance ON tdata_records_%d(instance)')\n")
2998 _T("<END>");
2999 CHK_EXEC(SQLBatch(batch));
3000
2f1bc68b
VK
3001 RecreateTData(_T("nodes"), false, false);
3002 RecreateTData(_T("clusters"), false, false);
3003 RecreateTData(_T("mobile_devices"), false, false);
22aaa779
VK
3004
3005 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='281' WHERE var_name='SchemaVersion'")));
3006 return TRUE;
3007}
3008
3009/**
3010 * Upgrade from V279 to V280
3011 */
3012static BOOL H_UpgradeFromV279(int currVersion, int newVersion)
3013{
3014 static TCHAR batch[] =
3015 _T("ALTER TABLE dc_table_columns ADD flags integer\n")
3016 _T("UPDATE dc_table_columns SET flags=data_type\n")
3017 _T("ALTER TABLE dc_table_columns DROP COLUMN data_type\n")
3018 _T("<END>");
3019 CHK_EXEC(SQLBatch(batch));
3020
3021 DB_RESULT hResult = SQLSelect(_T("SELECT item_id,instance_column FROM dc_tables"));
3022 if (hResult != NULL)
3023 {
3024 int count = DBGetNumRows(hResult);
3025 for(int i = 0; i < count; i++)
3026 {
3027 TCHAR columnName[MAX_COLUMN_NAME] = _T("");
3028 DBGetField(hResult, i, 1, columnName, MAX_COLUMN_NAME);
3029 if (columnName[0] != 0)
3030 {
3031 TCHAR query[256];
3032 _sntprintf(query, 256, _T("UPDATE dc_table_columns SET flags=flags+256 WHERE table_id=%d AND column_name=%s"),
3033 DBGetFieldLong(hResult, i, 0), (const TCHAR *)DBPrepareString(g_hCoreDB, columnName));
3034 CHK_EXEC(SQLQuery(query));
3035 }
3036 }
3037 DBFreeResult(hResult);
3038 }
3039 else
3040 {
3041 if (!g_bIgnoreErrors)
3042 return FALSE;
3043 }
3044
3045 CHK_EXEC(SQLQuery(_T("ALTER TABLE dc_tables DROP COLUMN instance_column")));
3046
3047 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='280' WHERE var_name='SchemaVersion'")));
3048 return TRUE;
3049}
3050
22ee6d97
VK
3051/**
3052 * Upgrade from V278 to V279
3053 */
3054static BOOL H_UpgradeFromV278(int currVersion, int newVersion)
3055{
3056 CHK_EXEC(CreateConfigParam(_T("DeleteEventsOfDeletedObject"), _T("1"), 1, 0));
3057 CHK_EXEC(CreateConfigParam(_T("DeleteAlarmsOfDeletedObject"), _T("1"), 1, 0));
3058
3059 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='279' WHERE var_name='SchemaVersion'")));
3060 return TRUE;
3061}
3062
85ae39bc
VK
3063/**
3064 * Upgrade from V277 to V278
3065 */
3066static BOOL H_UpgradeFromV277(int currVersion, int newVersion)
3067{
3068 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM clusters"));
3069 if (hResult != NULL)
3070 {
3071 int count = DBGetNumRows(hResult);
3072 for(int i = 0; i < count; i++)
3073 {
3074 DWORD id = DBGetFieldULong(hResult, i, 0);
3075 if (!CreateIDataTable(id))
3076 {
3077 if (!g_bIgnoreErrors)
3078 {
3079 DBFreeResult(hResult);
3080 return FALSE;
3081 }
3082 }
22aaa779 3083 if (!CreateTDataTable_preV281(id))
85ae39bc
VK
3084 {
3085 if (!g_bIgnoreErrors)
3086 {
3087 DBFreeResult(hResult);
3088 return FALSE;
3089 }
3090 }
3091 }
3092 DBFreeResult(hResult);
3093 }
3094 else
3095 {
3096 if (!g_bIgnoreErrors)
3097 return FALSE;
3098 }
3099
3100 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='278' WHERE var_name='SchemaVersion'")));
3101 return TRUE;
3102}
3103
62f41164
VK
3104/**
3105 * Upgrade from V276 to V277
3106 */
3107static BOOL H_UpgradeFromV276(int currVersion, int newVersion)
3108{
b4c2a628
VK
3109 CHK_EXEC(CreateTable(_T("CREATE TABLE dci_summary_tables (")
3110 _T("id integer not null,")
3111 _T("menu_path varchar(255) not null,")
3112 _T("title varchar(127) null,")
3113 _T("node_filter $SQL:TEXT null,")
3114 _T("flags integer not null,")
3115 _T("columns $SQL:TEXT null,")
3116 _T("PRIMARY KEY(id))")));
3117
62f41164
VK
3118 CHK_EXEC(CreateConfigParam(_T("DefaultMapBackgroundColor"), _T("0xffffff"), 1, 0));
3119
3120 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='277' WHERE var_name='SchemaVersion'")));
3121 return TRUE;
3122}
3123
55bdca5a
VK
3124/**
3125 * Upgrade from V275 to V276
3126 */
3127static BOOL H_UpgradeFromV275(int currVersion, int newVersion)
3128{
3129 static TCHAR batch[] =
3130 _T("ALTER TABLE dc_table_columns DROP COLUMN transformation_script\n")
3131 _T("ALTER TABLE dc_tables ADD transformation_script $SQL:TEXT\n")
3132 _T("<END>");
3133 CHK_EXEC(SQLBatch(batch));
3134
3135 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='276' WHERE var_name='SchemaVersion'")));
3136 return TRUE;
3137}
3138
8715a84c
VK
3139/**
3140 * Upgrade from V274 to V275
3141 */
3142static BOOL H_UpgradeFromV274(int currVersion, int newVersion)
3143{
3144 static TCHAR batch[] =
3145 _T("ALTER TABLE nodes ADD rack_image varchar(36)\n")
3146 _T("ALTER TABLE nodes ADD rack_position integer\n")
3147 _T("ALTER TABLE nodes ADD rack_id integer\n")
3148 _T("UPDATE nodes SET rack_image='00000000-0000-0000-0000-000000000000',rack_position=0,rack_id=0\n")
3149 _T("<END>");
3150 CHK_EXEC(SQLBatch(batch));
3151
3152 CHK_EXEC(CreateTable(_T("CREATE TABLE access_points (")
3153 _T("id integer not null,")
3154 _T("node_id integer not null,")
f1989a3a 3155 _T("mac_address varchar(12) null,")
8715a84c
VK
3156 _T("vendor varchar(64) null,")
3157 _T("model varchar(128) null,")
3158 _T("serial_number varchar(64) null,")
3159 _T("PRIMARY KEY(id))")));
3160
3161 CHK_EXEC(CreateTable(_T("CREATE TABLE racks (")
3162 _T("id integer not null,")
3163 _T("height integer not null,")
3164 _T("PRIMARY KEY(id))")));
3165
3166 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='275' WHERE var_name='SchemaVersion'")));
3167 return TRUE;
3168}
3169
d02f6b92
VK
3170/**
3171 * Upgrade from V273 to V274
3172 */
3173static BOOL H_UpgradeFromV273(int currVersion, int newVersion)
3174{
3175 static TCHAR batch[] =
3176 _T("ALTER TABLE items ADD samples integer\n")
3177 _T("UPDATE items SET samples=0\n")
3178 _T("<END>");
3179 CHK_EXEC(SQLBatch(batch));
3180
3181 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='274' WHERE var_name='SchemaVersion'")));
3182 return TRUE;
3183}
3184
f05a8a45
VK
3185/**
3186 * Upgrade from V272 to V273
3187 */
3188static BOOL H_UpgradeFromV272(int currVersion, int newVersion)
3189{
3190 CHK_EXEC(CreateConfigParam(_T("DefaultDCIRetentionTime"), _T("30"), 1, 0));
3191 CHK_EXEC(CreateConfigParam(_T("DefaultDCIPollingInterval"), _T("60"), 1, 0));
3192 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='273' WHERE var_name='SchemaVersion'")));
3193 return TRUE;
3194}
3195
0f75f8fc
VK
3196/**
3197 * Upgrade from V271 to V272
3198 */
3199static BOOL H_UpgradeFromV271(int currVersion, int newVersion)
3200{
3201 CHK_EXEC(CreateConfigParam(_T("SNMPTrapLogRetentionTime"), _T("90"), 1, 0));
28e1575f 3202 CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD driver_name varchar(32)\n")));
0f75f8fc
VK
3203 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='272' WHERE var_name='SchemaVersion'")));
3204 return TRUE;
3205}
3206
86634c2c
VK
3207/**
3208 * Upgrade from V270 to V271
3209 */
3210static BOOL H_UpgradeFromV270(int currVersion, int newVersion)
3211{
3212 static TCHAR batch[] =
3213 _T("ALTER TABLE object_properties ADD location_accuracy integer\n")
f2d5b2c4
VK
3214 _T("ALTER TABLE object_properties ADD location_timestamp integer\n")
3215 _T("UPDATE object_properties SET location_accuracy=0,location_timestamp=0\n")
86634c2c
VK
3216 _T("<END>");
3217 CHK_EXEC(SQLBatch(batch));
3218
3219 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='271' WHERE var_name='SchemaVersion'")));
3220 return TRUE;
3221}
3222
afbe5388
VK
3223/**
3224 * Upgrade from V269 to V270
3225 */
3226static BOOL H_UpgradeFromV269(int currVersion, int newVersion)
3227{
6f33021d 3228 static TCHAR batch[] =
afbe5388
VK
3229 _T("ALTER TABLE items ADD instd_method integer\n")
3230 _T("ALTER TABLE items ADD instd_data varchar(255)\n")
3231 _T("ALTER TABLE items ADD instd_filter $SQL:TEXT\n")
3232 _T("UPDATE items SET instd_method=0\n")
3233 _T("<END>");
3234 CHK_EXEC(SQLBatch(batch));
3235
3236 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='270' WHERE var_name='SchemaVersion'")));
3237 return TRUE;
3238}
3239
480e036b
VK
3240/**
3241 * Upgrade from V268 to V269
3242 */
3243static BOOL H_UpgradeFromV268(int currVersion, int newVersion)
3244{
b69710b9
VK
3245 CHK_EXEC(ResizeColumn(_T("alarms"), _T("message"), 2000, true));
3246 CHK_EXEC(ResizeColumn(_T("alarm_events"), _T("message"), 2000, true));
3247 CHK_EXEC(ResizeColumn(_T("event_log"), _T("event_message"), 2000, true));
3248 CHK_EXEC(ResizeColumn(_T("event_cfg"), _T("message"), 2000, true));
3249 CHK_EXEC(ResizeColumn(_T("event_policy"), _T("alarm_message"), 2000, true));
3250 CHK_EXEC(ResizeColumn(_T("items"), _T("name"), 1024, true));
3251 CHK_EXEC(ResizeColumn(_T("dc_tables"), _T("name"), 1024, true));
480e036b
VK
3252
3253 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_key")));
3254 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_message")));
3255 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("comments")));
3256 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("situation_instance")));
3257 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("script")));
3258 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_key")));
3259 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_message")));
3260 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("comments")));
3261 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("situation_instance")));
3262 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("script")));
3263
3264 CHK_EXEC(SetColumnNullable(_T("policy_situation_attr_list"), _T("attr_value")));
3265 // convert strings in policy_situation_attr_list
3266 DB_RESULT hResult = SQLSelect(_T("SELECT rule_id,situation_id,attr_name,attr_value FROM policy_situation_attr_list"));
3267 if (hResult != NULL)
3268 {
3269 if (SQLQuery(_T("DELETE FROM policy_situation_attr_list")))
3270 {
3271 TCHAR name[MAX_DB_STRING], value[MAX_DB_STRING], query[1024];
3272 int count = DBGetNumRows(hResult);
3273 for(int i = 0; i < count; i++)
3274 {
3275 LONG ruleId = DBGetFieldLong(hResult, i, 0);
3276 LONG situationId = DBGetFieldLong(hResult, i, 1);
3277 DBGetField(hResult, i, 2, name, MAX_DB_STRING);
3278 DBGetField(hResult, i, 3, value, MAX_DB_STRING);
3279
3280 DecodeSQLString(name);
3281 DecodeSQLString(value);
3282
3283 if (name[0] == 0)
3284 _tcscpy(name, _T("noname"));
3285
3286 _sntprintf(query, 1024, _T("INSERT INTO policy_situation_attr_list (rule_id,situation_id,attr_name,attr_value) VALUES (%d,%d,%s,%s)"),
3287 ruleId, situationId, (const TCHAR *)DBPrepareString(g_hCoreDB, name), (const TCHAR *)DBPrepareString(g_hCoreDB, value));
3288 if (!SQLQuery(query))
3289 {
3290 if (!g_bIgnoreErrors)
3291 {
3292 DBFreeResult(hResult);
3293 return FALSE;
3294 }
3295 }
3296 }
3297 }
3298 else
3299 {
3300 if (!g_bIgnoreErrors)
3301 {
3302 DBFreeResult(hResult);
3303 return FALSE;
3304 }
3305 }
3306 DBFreeResult(hResult);
3307 }
3308 else
3309 {
3310 if (!g_bIgnoreErrors)
3311 return FALSE;
3312 }
3313
3314 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("description")));
3315 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("message")));
3316 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("description")));
3317 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("message")));
3318
3319 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='269' WHERE var_name='SchemaVersion'")));
3320 return TRUE;
3321}
3322
d6bf58f9
VK
3323/**
3324 * Upgrade from V267 to V268
3325 */
3326static BOOL H_UpgradeFromV267(int currVersion, int newVersion)
3327{
480e036b
VK
3328 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_request")));
3329 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_responce")));
d6bf58f9
VK
3330 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_request")));
3331 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_responce")));
3332
480e036b 3333 CHK_EXEC(SetColumnNullable(_T("config"), _T("var_value")));
d6bf58f9
VK
3334 CHK_EXEC(ConvertStrings(_T("config"), _T("var_name"), NULL, _T("var_value"), true));
3335
480e036b 3336 CHK_EXEC(SetColumnNullable(_T("dci_schedules"), _T("schedule")));
d6bf58f9
VK
3337 CHK_EXEC(ConvertStrings(_T("dci_schedules"), _T("schedule_id"), _T("item_id"), _T("schedule"), false));
3338
3339 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='268' WHERE var_name='SchemaVersion'")));
3340 return TRUE;
3341}
3342
c1482463
VK
3343/**
3344 * Upgrade from V266 to V267
3345 */
3346static BOOL H_UpgradeFromV266(int currVersion, int newVersion)
3347{
3348 CHK_EXEC(CreateEventTemplate(EVENT_NODE_UNREACHABLE, _T("SYS_NODE_UNREACHABLE"), EVENT_SEVERITY_CRITICAL,
3349 EF_LOG, _T("Node unreachable because of network failure"),
3350 _T("Generated when node is unreachable by management server because of network failure.\r\nParameters:\r\n No event-specific parameters")));
3351 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='267' WHERE var_name='SchemaVersion'")));
3352 return TRUE;
3353}
3354
69bb7f47
VK
3355/**
3356 * Upgrade from V265 to V266
3357 */
3358static BOOL H_UpgradeFromV265(int currVersion, int newVersion)
3359{
3360 // create index on root event ID in event log
2a964810 3361 switch(g_dbSyntax)
69bb7f47
VK
3362 {
3363 case DB_SYNTAX_MSSQL:
3364 case DB_SYNTAX_PGSQL:
3365 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id) WHERE root_event_id > 0")));
3366 break;
3367 case DB_SYNTAX_ORACLE:
3368 CHK_EXEC(SQLQuery(_T("CREATE OR REPLACE FUNCTION zero_to_null(id NUMBER) ")
3369 _T("RETURN NUMBER ")
3370 _T("DETERMINISTIC ")
3371 _T("AS BEGIN")
3372 _T(" IF id > 0 THEN")
3373 _T(" RETURN id;")
3374 _T(" ELSE")
3375 _T(" RETURN NULL;")
3376 _T(" END IF;")
3377 _T("END;")));
3378 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(zero_to_null(root_event_id))")));
3379 break;
3380 default:
3381 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id)")));
3382 break;
3383 }
3384
3385 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_tables (")
3386 _T("id integer not null,")
3387 _T("name varchar(63) not null,")
3388 _T("flags integer not null,")
3389 _T("description $SQL:TXT4K null,")
3390 _T("PRIMARY KEY(id))")));
3391
3392 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_data (")
3393 _T("table_id integer not null,")
3394 _T("md_key varchar(63) not null,")
3395 _T("md_value varchar(255) null,")
f41af2c4 3396 _T("description $SQL:TXT4K null,")
69bb7f47
VK
3397 _T("PRIMARY KEY(table_id,md_key))")));
3398
3399 CHK_EXEC(SQLQuery(_T("DROP TABLE deleted_objects")));
3400
3401 CHK_EXEC(CreateConfigParam(_T("FirstFreeObjectId"), _T("100"), 0, 1, FALSE));
3402
3403 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='266' WHERE var_name='SchemaVersion'")));
3404 return TRUE;
3405}
3406
b1e9b6b3
VK
3407/**
3408 * Upgrade from V264 to V265
3409 */
3410static BOOL H_UpgradeFromV264(int currVersion, int newVersion)
3411{
3412 CHK_EXEC(CreateTable(_T("CREATE TABLE alarm_events (")
3413 _T("alarm_id integer not null,")
3414 _T("event_id $SQL:INT64 not null,")
3415 _T("event_code integer not null,")
3416 _T("event_name varchar(63) null,")
3417 _T("severity integer not null,")
3418 _T("source_object_id integer not null,")
3419 _T("event_timestamp integer not null,")
3420 _T("message varchar(255) null,")
3421 _T("PRIMARY KEY(alarm_id,event_id))")));
3422 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_alarm_events_alarm_id ON alarm_events(alarm_id)")));
3423 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='265' WHERE var_name='SchemaVersion'")));
3424 return TRUE;
3425}
3426
9aa67910
VK
3427/**
3428 * Upgrade from V263 to V264
3429 */
3430static BOOL H_UpgradeFromV263(int currVersion, int newVersion)
3431{
3432 CHK_EXEC(CreateTable(_T("CREATE TABLE mobile_devices (")
3433 _T("id integer not null,")
3434 _T("device_id varchar(64) not null,")
3435 _T("vendor varchar(64) null,")
3436 _T("model varchar(128) null,")
3437 _T("serial_number varchar(64) null,")
3438 _T("os_name varchar(32) null,")
3439 _T("os_version varchar(64) null,")
3440 _T("user_id varchar(64) null,")
171c2fd6 3441 _T("battery_level integer not null,")
9aa67910 3442 _T("PRIMARY KEY(id))")));
534e1b83 3443 CHK_EXEC(CreateConfigParam(_T("MobileDeviceListenerPort"), _T("4747"), 1, 1));
9aa67910
VK
3444 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='264' WHERE var_name='SchemaVersion'")));
3445 return TRUE;
3446}
3447
84880c89
VK
3448/**
3449 * Upgrade from V262 to V263
3450 */
3451static BOOL H_UpgradeFromV262(int currVersion, int newVersion)
3452{
3453 CHK_EXEC(SQLQuery(_T("ALTER TABLE network_maps ADD radius integer")));
3454 CHK_EXEC(SQLQuery(_T("UPDATE network_maps SET radius=-1")));
3455 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='263' WHERE var_name='SchemaVersion'")));
3456 return TRUE;
3457}
3458
dc4a1aec
AK
3459/**
3460 * Upgrade from V261 to V262
3461 */
3462static BOOL H_UpgradeFromV261(int currVersion, int newVersion)
3463{
534e1b83 3464 CHK_EXEC(CreateConfigParam(_T("ApplyDCIFromTemplateToDisabledDCI"), _T("0"), 1, 1));
dc4a1aec
AK
3465 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='262' WHERE var_name='SchemaVersion'")));
3466 return TRUE;
3467}
3468
4f50e45c
VK
3469/**
3470 * Upgrade from V260 to V261
3471 */
3472static BOOL H_UpgradeFromV260(int currVersion, int newVersion)
3473{
534e1b83 3474 CHK_EXEC(CreateConfigParam(_T("NumberOfBusinessServicePollers"), _T("10"), 1, 1));
4f50e45c
VK
3475 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfEventProcessors'")));
3476 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='261' WHERE var_name='SchemaVersion'")));
3477 return TRUE;
3478}
a203ea53 3479
4f50e45c
VK
3480/**
3481 * Upgrade from V259 to V260
3482 */
a203ea53
AK
3483static BOOL H_UpgradeFromV259(int currVersion, int newVersion)
3484{
534e1b83 3485 CHK_EXEC(CreateConfigParam(_T("UseFQDNForNodeNames"), _T("1"), 1, 1));
a203ea53
AK
3486 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='260' WHERE var_name='SchemaVersion'")));
3487 return TRUE;
3488}
3489
4f50e45c
VK
3490/**
3491 * Upgrade from V258 to V259
3492 */
5da6ca46
VK
3493static BOOL H_UpgradeFromV258(int currVersion, int newVersion)
3494{
3495 // have to made these columns nullable again because
3496 // because they was forgotten as NOT NULL in schema.in
3497 // and so some databases can still have them as NOT NULL
480e036b
VK
3498 CHK_EXEC(SetColumnNullable(_T("templates"), _T("apply_filter")));
3499 CHK_EXEC(SetColumnNullable(_T("containers"), _T("auto_bind_filter")));
5da6ca46
VK
3500
3501 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='259' WHERE var_name='SchemaVersion'")));
3502 return TRUE;
3503}
3504
4f50e45c
VK
3505/**
3506 * Upgrade from V257 to V258