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