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