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