improved event correlation for nodes in remote zones
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade.cpp
CommitLineData
5039dede
AK
1/*
2** nxdbmgr - NetXMS database manager
480e036b 3** Copyright (C) 2004-2013 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"
24
4f50e45c
VK
25/**
26 * Externals
27 */
7a41a06e
VK
28BOOL MigrateMaps();
29
4f50e45c
VK
30/**
31 * Create table
32 */
5039dede
AK
33static BOOL CreateTable(const TCHAR *pszQuery)
34{
5039dede 35 BOOL bResult;
7618e362 36 String query(pszQuery);
5039dede 37
6bcafa2d
VK
38 query.replace(_T("$SQL:TEXT"), g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
39 query.replace(_T("$SQL:TXT4K"), g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT4K]);
40 query.replace(_T("$SQL:INT64"), g_pszSqlType[g_iSyntax][SQL_TYPE_INT64]);
5039dede 41 if (g_iSyntax == DB_SYNTAX_MYSQL)
7618e362
VK
42 query += g_pszTableSuffix;
43 bResult = SQLQuery(query);
5039dede
AK
44 return bResult;
45}
46
4f50e45c
VK
47/**
48 * Create configuration parameter if it doesn't exist (unless bForceUpdate set to true)
49 */
a43a5e7d
VK
50BOOL CreateConfigParam(const TCHAR *pszName, const TCHAR *pszValue,
51 int iVisible, int iNeedRestart, BOOL bForceUpdate)
5039dede 52{
42475f3e 53 TCHAR szQuery[1024];
5039dede
AK
54 DB_RESULT hResult;
55 BOOL bVarExist = FALSE, bResult = TRUE;
56
57 // Check for variable existence
08b214c6 58 _sntprintf(szQuery, 1024, _T("SELECT var_value FROM config WHERE var_name='%s'"), pszName);
5039dede
AK
59 hResult = DBSelect(g_hCoreDB, szQuery);
60 if (hResult != 0)
61 {
62 if (DBGetNumRows(hResult) > 0)
63 bVarExist = TRUE;
64 DBFreeResult(hResult);
65 }
66
67 if (!bVarExist)
68 {
08b214c6 69 _sntprintf(szQuery, 1024, _T("INSERT INTO config (var_name,var_value,is_visible,")
42475f3e
VK
70 _T("need_server_restart) VALUES (%s,%s,%d,%d)"),
71 (const TCHAR *)DBPrepareString(g_hCoreDB, pszName, 63),
72 (const TCHAR *)DBPrepareString(g_hCoreDB, pszValue, 255), iVisible, iNeedRestart);
5039dede
AK
73 bResult = SQLQuery(szQuery);
74 }
75 else if (bForceUpdate)
76 {
42475f3e
VK
77 _sntprintf(szQuery, 1024, _T("UPDATE config SET var_value=%s WHERE var_name=%s"),
78 (const TCHAR *)DBPrepareString(g_hCoreDB, pszValue, 255), (const TCHAR *)DBPrepareString(g_hCoreDB, pszName, 63));
5039dede
AK
79 bResult = SQLQuery(szQuery);
80 }
81 return bResult;
82}
83
4f50e45c
VK
84/**
85 * Set primary key constraint
86 */
5039dede
AK
87static BOOL SetPrimaryKey(const TCHAR *table, const TCHAR *key)
88{
89 TCHAR query[4096];
90
91 if (g_iSyntax == DB_SYNTAX_SQLITE)
92 return TRUE; // SQLite does not support adding constraints
93
94 _sntprintf(query, 4096, _T("ALTER TABLE %s ADD PRIMARY KEY (%s)"), table, key);
95 return SQLQuery(query);
96}
97
4f50e45c
VK
98/**
99 * Drop primary key from table
100 */
9bfc9a6b
VK
101static BOOL DropPrimaryKey(const TCHAR *table)
102{
103 TCHAR query[1024];
104 DB_RESULT hResult;
105 BOOL success;
106
107 switch(g_iSyntax)
108 {
109 case DB_SYNTAX_ORACLE:
110 case DB_SYNTAX_MYSQL:
111 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP PRIMARY KEY"), table);
112 success = SQLQuery(query);
113 break;
114 case DB_SYNTAX_PGSQL:
115 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s_pkey"), table, table);
116 success = SQLQuery(query);
117 break;
118 case DB_SYNTAX_MSSQL:
119 success = FALSE;
120 _sntprintf(query, 1024, _T("SELECT name FROM sysobjects WHERE xtype='PK' AND parent_obj=OBJECT_ID('%s')"), table);
121 hResult = SQLSelect(query);
122 if (hResult != NULL)
123 {
124 if (DBGetNumRows(hResult) > 0)
125 {
126 TCHAR objName[512];
127
128 DBGetField(hResult, 0, 0, objName, 512);
129 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s"), table, objName);
130 success = SQLQuery(query);
131 }
132 DBFreeResult(hResult);
133 }
134 break;
135 default: // Unsupported DB engine
136 success = FALSE;
137 break;
138 }
139 return success;
140}
141
4f50e45c
VK
142/**
143 * Convert strings from # encoded form to normal form
144 */
d6bf58f9 145static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *idColumn2, const TCHAR *column, bool isStringId)
643c9dcb
VK
146{
147 DB_RESULT hResult;
148 TCHAR *query;
149 int queryLen = 512;
150 BOOL success = FALSE;
151
c29fb885 152 query = (TCHAR *)malloc(queryLen * sizeof(TCHAR));
fe12a1ea 153
a4743a0f
VK
154 switch(g_iSyntax)
155 {
156 case DB_SYNTAX_MSSQL:
9f6712bc 157 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE CAST(%s AS nvarchar(4000))=N'#00'"), table, column, column);
a4743a0f
VK
158 break;
159 case DB_SYNTAX_ORACLE:
160 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE to_char(%s)='#00'"), table, column, column);
161 break;
162 default:
163 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE %s='#00'"), table, column, column);
164 break;
165 }
fe12a1ea
VK
166 if (!SQLQuery(query))
167 {
168 free(query);
169 return FALSE;
170 }
171
98cd01bb
VK
172 _sntprintf(query, queryLen, _T("SELECT %s,%s%s%s FROM %s WHERE %s LIKE '%%#%%'"),
173 idColumn, column, (idColumn2 != NULL) ? _T(",") : _T(""), (idColumn2 != NULL) ? idColumn2 : _T(""), table, column);
643c9dcb
VK
174 hResult = SQLSelect(query);
175 if (hResult == NULL)
176 {
177 free(query);
178 return FALSE;
179 }
180
181 int count = DBGetNumRows(hResult);
182 for(int i = 0; i < count; i++)
183 {
643c9dcb 184 TCHAR *value = DBGetField(hResult, i, 1, NULL, 0);
035a4d73 185 if (_tcschr(value, _T('#')) != NULL)
643c9dcb 186 {
035a4d73
VK
187 DecodeSQLString(value);
188 String newValue = DBPrepareString(g_hCoreDB, value);
189 if ((int)newValue.getSize() + 256 > queryLen)
190 {
191 queryLen = newValue.getSize() + 256;
c29fb885 192 query = (TCHAR *)realloc(query, queryLen * sizeof(TCHAR));
035a4d73 193 }
d6bf58f9 194 if (isStringId)
98cd01bb 195 {
d6bf58f9
VK
196 TCHAR *id = DBGetField(hResult, i, 0, NULL, 0);
197 if (idColumn2 != NULL)
198 {
199 TCHAR *id2 = DBGetField(hResult, i, 2, NULL, 0);
200 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s AND %s=%s"),
201 table, column, (const TCHAR *)newValue,
202 idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id),
203 idColumn2, (const TCHAR *)DBPrepareString(g_hCoreDB, id2));
204 }
205 else
206 {
207 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s"), table, column,
208 (const TCHAR *)newValue, idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id));
209 }
210 free(id);
98cd01bb
VK
211 }
212 else
213 {
d6bf58f9
VK
214 INT64 id = DBGetFieldInt64(hResult, i, 0);
215 if (idColumn2 != NULL)
216 {
217 INT64 id2 = DBGetFieldInt64(hResult, i, 2);
218 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT _T(" AND %s=") INT64_FMT,
219 table, column, (const TCHAR *)newValue, idColumn, id, idColumn2, id2);
220 }
221 else
222 {
223 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT, table, column,
224 (const TCHAR *)newValue, idColumn, id);
225 }
98cd01bb 226 }
035a4d73
VK
227 if (!SQLQuery(query))
228 goto cleanup;
643c9dcb 229 }
643c9dcb
VK
230 }
231 success = TRUE;
232
233cleanup:
234 DBFreeResult(hResult);
a4743a0f 235 free(query);
643c9dcb
VK
236 return success;
237}
238
98cd01bb
VK
239static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *column)
240{
d6bf58f9 241 return ConvertStrings(table, idColumn, NULL, column, false);
98cd01bb
VK
242}
243
4f50e45c 244/**
70ffb771 245 * Set column nullable (currently only Oracle and PostgreSQL)
4f50e45c 246 */
480e036b 247static BOOL SetColumnNullable(const TCHAR *table, const TCHAR *column)
1024e962
VK
248{
249 TCHAR query[1024] = _T("");
250
251 switch(g_iSyntax)
252 {
253 case DB_SYNTAX_ORACLE:
254 _sntprintf(query, 1024, _T("DECLARE already_null EXCEPTION; ")
255 _T("PRAGMA EXCEPTION_INIT(already_null, -1451); ")
f57209fd
VK
256 _T("BEGIN EXECUTE IMMEDIATE 'ALTER TABLE %s MODIFY %s null'; ")
257 _T("EXCEPTION WHEN already_null THEN null; END;"), table, column);
1024e962 258 break;
480e036b
VK
259 case DB_SYNTAX_PGSQL:
260 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL"), table, column);
261 break;
1024e962
VK
262 default:
263 break;
264 }
265
266 return (query[0] != 0) ? SQLQuery(query) : TRUE;
267}
268
480e036b
VK
269/**
270 * Resize varchar column
271 */
272static BOOL ResizeColumn(const TCHAR *table, const TCHAR *column, int newSize)
273{
274 TCHAR query[1024];
275
276 switch(g_iSyntax)
277 {
278 case DB_SYNTAX_DB2:
279 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE varchar(%d)"), table, column, newSize);
280 break;
281 case DB_SYNTAX_MSSQL:
282 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s varchar(%d)"), table, column, newSize);
283 break;
284 case DB_SYNTAX_PGSQL:
285 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s TYPE varchar(%d)"), table, column, newSize);
286 break;
287 case DB_SYNTAX_SQLITE:
288 /* TODO: add SQLite support */
289 query[0] = 0;
290 break;
291 default:
292 _sntprintf(query, 1024, _T("ALTER TABLE %s MODIFY %s varchar(%d)"), table, column, newSize);
293 break;
294 }
295
296 return (query[0] != 0) ? SQLQuery(query) : TRUE;
297}
298
4f50e45c
VK
299/**
300 * Create new event template
301 */
aa16f82b
VK
302static BOOL CreateEventTemplate(int code, const TCHAR *name, int severity, int flags, const TCHAR *message, const TCHAR *description)
303{
480e036b 304 TCHAR query[4096];
aa16f82b 305
480e036b
VK
306 _sntprintf(query, 4096, _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (%d,'%s',%d,%d,%s,%s)"),
307 code, name, severity, flags, (const TCHAR *)DBPrepareString(g_hCoreDB, message),
308 (const TCHAR *)DBPrepareString(g_hCoreDB, description));
aa16f82b
VK
309 return SQLQuery(query);
310}
311
d02f6b92
VK
312/**
313 * Upgrade from V273 to V274
314 */
315static BOOL H_UpgradeFromV273(int currVersion, int newVersion)
316{
317 static TCHAR batch[] =
318 _T("ALTER TABLE items ADD samples integer\n")
319 _T("UPDATE items SET samples=0\n")
320 _T("<END>");
321 CHK_EXEC(SQLBatch(batch));
322
323 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='274' WHERE var_name='SchemaVersion'")));
324 return TRUE;
325}
326
f05a8a45
VK
327/**
328 * Upgrade from V272 to V273
329 */
330static BOOL H_UpgradeFromV272(int currVersion, int newVersion)
331{
332 CHK_EXEC(CreateConfigParam(_T("DefaultDCIRetentionTime"), _T("30"), 1, 0));
333 CHK_EXEC(CreateConfigParam(_T("DefaultDCIPollingInterval"), _T("60"), 1, 0));
334 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='273' WHERE var_name='SchemaVersion'")));
335 return TRUE;
336}
337
0f75f8fc
VK
338/**
339 * Upgrade from V271 to V272
340 */
341static BOOL H_UpgradeFromV271(int currVersion, int newVersion)
342{
343 CHK_EXEC(CreateConfigParam(_T("SNMPTrapLogRetentionTime"), _T("90"), 1, 0));
28e1575f 344 CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD driver_name varchar(32)\n")));
0f75f8fc
VK
345 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='272' WHERE var_name='SchemaVersion'")));
346 return TRUE;
347}
348
86634c2c
VK
349/**
350 * Upgrade from V270 to V271
351 */
352static BOOL H_UpgradeFromV270(int currVersion, int newVersion)
353{
354 static TCHAR batch[] =
355 _T("ALTER TABLE object_properties ADD location_accuracy integer\n")
f2d5b2c4
VK
356 _T("ALTER TABLE object_properties ADD location_timestamp integer\n")
357 _T("UPDATE object_properties SET location_accuracy=0,location_timestamp=0\n")
86634c2c
VK
358 _T("<END>");
359 CHK_EXEC(SQLBatch(batch));
360
361 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='271' WHERE var_name='SchemaVersion'")));
362 return TRUE;
363}
364
afbe5388
VK
365/**
366 * Upgrade from V269 to V270
367 */
368static BOOL H_UpgradeFromV269(int currVersion, int newVersion)
369{
370 static TCHAR batch[] =
371 _T("ALTER TABLE items ADD instd_method integer\n")
372 _T("ALTER TABLE items ADD instd_data varchar(255)\n")
373 _T("ALTER TABLE items ADD instd_filter $SQL:TEXT\n")
374 _T("UPDATE items SET instd_method=0\n")
375 _T("<END>");
376 CHK_EXEC(SQLBatch(batch));
377
378 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='270' WHERE var_name='SchemaVersion'")));
379 return TRUE;
380}
381
480e036b
VK
382/**
383 * Upgrade from V268 to V269
384 */
385static BOOL H_UpgradeFromV268(int currVersion, int newVersion)
386{
387 CHK_EXEC(ResizeColumn(_T("alarms"), _T("message"), 2000));
388 CHK_EXEC(ResizeColumn(_T("alarm_events"), _T("message"), 2000));
389 CHK_EXEC(ResizeColumn(_T("event_log"), _T("event_message"), 2000));
390 CHK_EXEC(ResizeColumn(_T("event_cfg"), _T("message"), 2000));
391 CHK_EXEC(ResizeColumn(_T("event_policy"), _T("alarm_message"), 2000));
392 CHK_EXEC(ResizeColumn(_T("items"), _T("name"), 1024));
393 CHK_EXEC(ResizeColumn(_T("dc_tables"), _T("name"), 1024));
394
395 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_key")));
396 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_message")));
397 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("comments")));
398 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("situation_instance")));
399 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("script")));
400 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_key")));
401 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_message")));
402 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("comments")));
403 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("situation_instance")));
404 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("script")));
405
406 CHK_EXEC(SetColumnNullable(_T("policy_situation_attr_list"), _T("attr_value")));
407 // convert strings in policy_situation_attr_list
408 DB_RESULT hResult = SQLSelect(_T("SELECT rule_id,situation_id,attr_name,attr_value FROM policy_situation_attr_list"));
409 if (hResult != NULL)
410 {
411 if (SQLQuery(_T("DELETE FROM policy_situation_attr_list")))
412 {
413 TCHAR name[MAX_DB_STRING], value[MAX_DB_STRING], query[1024];
414 int count = DBGetNumRows(hResult);
415 for(int i = 0; i < count; i++)
416 {
417 LONG ruleId = DBGetFieldLong(hResult, i, 0);
418 LONG situationId = DBGetFieldLong(hResult, i, 1);
419 DBGetField(hResult, i, 2, name, MAX_DB_STRING);
420 DBGetField(hResult, i, 3, value, MAX_DB_STRING);
421
422 DecodeSQLString(name);
423 DecodeSQLString(value);
424
425 if (name[0] == 0)
426 _tcscpy(name, _T("noname"));
427
428 _sntprintf(query, 1024, _T("INSERT INTO policy_situation_attr_list (rule_id,situation_id,attr_name,attr_value) VALUES (%d,%d,%s,%s)"),
429 ruleId, situationId, (const TCHAR *)DBPrepareString(g_hCoreDB, name), (const TCHAR *)DBPrepareString(g_hCoreDB, value));
430 if (!SQLQuery(query))
431 {
432 if (!g_bIgnoreErrors)
433 {
434 DBFreeResult(hResult);
435 return FALSE;
436 }
437 }
438 }
439 }
440 else
441 {
442 if (!g_bIgnoreErrors)
443 {
444 DBFreeResult(hResult);
445 return FALSE;
446 }
447 }
448 DBFreeResult(hResult);
449 }
450 else
451 {
452 if (!g_bIgnoreErrors)
453 return FALSE;
454 }
455
456 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("description")));
457 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("message")));
458 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("description")));
459 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("message")));
460
461 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='269' WHERE var_name='SchemaVersion'")));
462 return TRUE;
463}
464
d6bf58f9
VK
465/**
466 * Upgrade from V267 to V268
467 */
468static BOOL H_UpgradeFromV267(int currVersion, int newVersion)
469{
480e036b
VK
470 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_request")));
471 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_responce")));
d6bf58f9
VK
472 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_request")));
473 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_responce")));
474
480e036b 475 CHK_EXEC(SetColumnNullable(_T("config"), _T("var_value")));
d6bf58f9
VK
476 CHK_EXEC(ConvertStrings(_T("config"), _T("var_name"), NULL, _T("var_value"), true));
477
480e036b 478 CHK_EXEC(SetColumnNullable(_T("config"), _T("var_value")));
d6bf58f9
VK
479 CHK_EXEC(ConvertStrings(_T("config"), _T("var_name"), NULL, _T("var_value"), true));
480
480e036b 481 CHK_EXEC(SetColumnNullable(_T("dci_schedules"), _T("schedule")));
d6bf58f9
VK
482 CHK_EXEC(ConvertStrings(_T("dci_schedules"), _T("schedule_id"), _T("item_id"), _T("schedule"), false));
483
484 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='268' WHERE var_name='SchemaVersion'")));
485 return TRUE;
486}
487
c1482463
VK
488/**
489 * Upgrade from V266 to V267
490 */
491static BOOL H_UpgradeFromV266(int currVersion, int newVersion)
492{
493 CHK_EXEC(CreateEventTemplate(EVENT_NODE_UNREACHABLE, _T("SYS_NODE_UNREACHABLE"), EVENT_SEVERITY_CRITICAL,
494 EF_LOG, _T("Node unreachable because of network failure"),
495 _T("Generated when node is unreachable by management server because of network failure.\r\nParameters:\r\n No event-specific parameters")));
496 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='267' WHERE var_name='SchemaVersion'")));
497 return TRUE;
498}
499
69bb7f47
VK
500/**
501 * Upgrade from V265 to V266
502 */
503static BOOL H_UpgradeFromV265(int currVersion, int newVersion)
504{
505 // create index on root event ID in event log
506 switch(g_iSyntax)
507 {
508 case DB_SYNTAX_MSSQL:
509 case DB_SYNTAX_PGSQL:
510 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id) WHERE root_event_id > 0")));
511 break;
512 case DB_SYNTAX_ORACLE:
513 CHK_EXEC(SQLQuery(_T("CREATE OR REPLACE FUNCTION zero_to_null(id NUMBER) ")
514 _T("RETURN NUMBER ")
515 _T("DETERMINISTIC ")
516 _T("AS BEGIN")
517 _T(" IF id > 0 THEN")
518 _T(" RETURN id;")
519 _T(" ELSE")
520 _T(" RETURN NULL;")
521 _T(" END IF;")
522 _T("END;")));
523 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(zero_to_null(root_event_id))")));
524 break;
525 default:
526 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id)")));
527 break;
528 }
529
530 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_tables (")
531 _T("id integer not null,")
532 _T("name varchar(63) not null,")
533 _T("flags integer not null,")
534 _T("description $SQL:TXT4K null,")
535 _T("PRIMARY KEY(id))")));
536
537 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_data (")
538 _T("table_id integer not null,")
539 _T("md_key varchar(63) not null,")
540 _T("md_value varchar(255) null,")
f41af2c4 541 _T("description $SQL:TXT4K null,")
69bb7f47
VK
542 _T("PRIMARY KEY(table_id,md_key))")));
543
544 CHK_EXEC(SQLQuery(_T("DROP TABLE deleted_objects")));
545
546 CHK_EXEC(CreateConfigParam(_T("FirstFreeObjectId"), _T("100"), 0, 1, FALSE));
547
548 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='266' WHERE var_name='SchemaVersion'")));
549 return TRUE;
550}
551
b1e9b6b3
VK
552/**
553 * Upgrade from V264 to V265
554 */
555static BOOL H_UpgradeFromV264(int currVersion, int newVersion)
556{
557 CHK_EXEC(CreateTable(_T("CREATE TABLE alarm_events (")
558 _T("alarm_id integer not null,")
559 _T("event_id $SQL:INT64 not null,")
560 _T("event_code integer not null,")
561 _T("event_name varchar(63) null,")
562 _T("severity integer not null,")
563 _T("source_object_id integer not null,")
564 _T("event_timestamp integer not null,")
565 _T("message varchar(255) null,")
566 _T("PRIMARY KEY(alarm_id,event_id))")));
567 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_alarm_events_alarm_id ON alarm_events(alarm_id)")));
568 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='265' WHERE var_name='SchemaVersion'")));
569 return TRUE;
570}
571
9aa67910
VK
572/**
573 * Upgrade from V263 to V264
574 */
575static BOOL H_UpgradeFromV263(int currVersion, int newVersion)
576{
577 CHK_EXEC(CreateTable(_T("CREATE TABLE mobile_devices (")
578 _T("id integer not null,")
579 _T("device_id varchar(64) not null,")
580 _T("vendor varchar(64) null,")
581 _T("model varchar(128) null,")
582 _T("serial_number varchar(64) null,")
583 _T("os_name varchar(32) null,")
584 _T("os_version varchar(64) null,")
585 _T("user_id varchar(64) null,")
171c2fd6 586 _T("battery_level integer not null,")
9aa67910 587 _T("PRIMARY KEY(id))")));
534e1b83 588 CHK_EXEC(CreateConfigParam(_T("MobileDeviceListenerPort"), _T("4747"), 1, 1));
9aa67910
VK
589 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='264' WHERE var_name='SchemaVersion'")));
590 return TRUE;
591}
592
84880c89
VK
593/**
594 * Upgrade from V262 to V263
595 */
596static BOOL H_UpgradeFromV262(int currVersion, int newVersion)
597{
598 CHK_EXEC(SQLQuery(_T("ALTER TABLE network_maps ADD radius integer")));
599 CHK_EXEC(SQLQuery(_T("UPDATE network_maps SET radius=-1")));
600 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='263' WHERE var_name='SchemaVersion'")));
601 return TRUE;
602}
603
dc4a1aec
AK
604/**
605 * Upgrade from V261 to V262
606 */
607static BOOL H_UpgradeFromV261(int currVersion, int newVersion)
608{
534e1b83 609 CHK_EXEC(CreateConfigParam(_T("ApplyDCIFromTemplateToDisabledDCI"), _T("0"), 1, 1));
dc4a1aec
AK
610 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='262' WHERE var_name='SchemaVersion'")));
611 return TRUE;
612}
613
4f50e45c
VK
614/**
615 * Upgrade from V260 to V261
616 */
617static BOOL H_UpgradeFromV260(int currVersion, int newVersion)
618{
534e1b83 619 CHK_EXEC(CreateConfigParam(_T("NumberOfBusinessServicePollers"), _T("10"), 1, 1));
4f50e45c
VK
620 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfEventProcessors'")));
621 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='261' WHERE var_name='SchemaVersion'")));
622 return TRUE;
623}
a203ea53 624
4f50e45c
VK
625/**
626 * Upgrade from V259 to V260
627 */
a203ea53
AK
628static BOOL H_UpgradeFromV259(int currVersion, int newVersion)
629{
534e1b83 630 CHK_EXEC(CreateConfigParam(_T("UseFQDNForNodeNames"), _T("1"), 1, 1));
a203ea53
AK
631 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='260' WHERE var_name='SchemaVersion'")));
632 return TRUE;
633}
634
4f50e45c
VK
635/**
636 * Upgrade from V258 to V259
637 */
5da6ca46
VK
638static BOOL H_UpgradeFromV258(int currVersion, int newVersion)
639{
640 // have to made these columns nullable again because
641 // because they was forgotten as NOT NULL in schema.in
642 // and so some databases can still have them as NOT NULL
480e036b
VK
643 CHK_EXEC(SetColumnNullable(_T("templates"), _T("apply_filter")));
644 CHK_EXEC(SetColumnNullable(_T("containers"), _T("auto_bind_filter")));
5da6ca46
VK
645
646 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='259' WHERE var_name='SchemaVersion'")));
647 return TRUE;
648}
649
4f50e45c
VK
650/**
651 * Upgrade from V257 to V258
652 */
dd42ad0a
AK
653static BOOL H_UpgradeFromV257(int currVersion, int newVersion)
654{
655 static TCHAR batch[] =
656 _T("ALTER TABLE nodes ADD down_since integer\n")
657 _T("UPDATE nodes SET down_since=0\n")
658 _T("<END>");
659
660 CHK_EXEC(SQLBatch(batch));
661
534e1b83 662 CHK_EXEC(CreateConfigParam(_T("DeleteUnreachableNodesPeriod"), _T("0"), 1, 1));
dd42ad0a
AK
663
664 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='258' WHERE var_name='SchemaVersion'")));
665 return TRUE;
666}
aa16f82b 667
534e1b83
VK
668/**
669 * Upgrade from V256 to V257
670 */
e4acd770
VK
671static BOOL H_UpgradeFromV256(int currVersion, int newVersion)
672{
673 static TCHAR batch[] =
674 _T("ALTER TABLE network_maps ADD bg_color integer\n")
675 _T("ALTER TABLE network_maps ADD link_routing integer\n")
676 _T("UPDATE network_maps SET bg_color=16777215,link_routing=1\n")
677 _T("ALTER TABLE network_map_links ADD routing integer\n")
678 _T("ALTER TABLE network_map_links ADD bend_points $SQL:TXT4K\n")
679 _T("UPDATE network_map_links SET routing=0\n")
680 _T("<END>");
681
682 CHK_EXEC(SQLBatch(batch));
683
684 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='257' WHERE var_name='SchemaVersion'")));
685 return TRUE;
686}
687
534e1b83
VK
688/**
689 * Upgrade from V255 to V256
690 */
aabe5b72
VK
691static BOOL H_UpgradeFromV255(int currVersion, int newVersion)
692{
534e1b83
VK
693 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleDateFormat"), _T("dd.MM.yyyy"), 1, 0));
694 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleTimeFormat"), _T("HH:mm:ss"), 1, 0));
aabe5b72
VK
695
696 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='256' WHERE var_name='SchemaVersion'")));
697 return TRUE;
698}
699
480e036b
VK
700/**
701 * Upgrade from V254 to V255
702 */
5f6bc78c
VK
703static BOOL H_UpgradeFromV254(int currVersion, int newVersion)
704{
705 static TCHAR batch[] =
706 _T("ALTER TABLE alarms ADD resolved_by integer\n")
707 _T("UPDATE alarms SET resolved_by=0\n")
708 _T("UPDATE alarms SET alarm_state=3 WHERE alarm_state=2\n")
709 _T("<END>");
710
711 CHK_EXEC(SQLBatch(batch));
712
3e9e0cc5 713 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='255' WHERE var_name='SchemaVersion'")));
5f6bc78c
VK
714 return TRUE;
715}
40647141
VK
716
717
718//
719// Upgrade from V253 to V254
720//
721
722static BOOL H_UpgradeFromV253(int currVersion, int newVersion)
723{
724 static TCHAR batch[] =
725 _T("ALTER TABLE network_maps ADD flags integer\n")
726 _T("ALTER TABLE network_maps ADD link_color integer\n")
727 _T("UPDATE network_maps SET flags=1,link_color=-1\n")
728 _T("ALTER TABLE network_map_links ADD color integer\n")
729 _T("ALTER TABLE network_map_links ADD status_object integer\n")
730 _T("UPDATE network_map_links SET color=-1,status_object=0\n")
731 _T("<END>");
732
733 CHK_EXEC(SQLBatch(batch));
734
735 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='254' WHERE var_name='SchemaVersion'")));
736 return TRUE;
737}
738
d6bf58f9
VK
739/**
740 * Upgrade from V252 to V253
741 */
feab3324
VK
742static BOOL H_UpgradeFromV252(int currVersion, int newVersion)
743{
480e036b 744 CHK_EXEC(SetColumnNullable(_T("templates"), _T("apply_filter")));
242e9c7f
VK
745 CHK_EXEC(ConvertStrings(_T("templates"), _T("id"), _T("apply_filter")));
746
480e036b 747 CHK_EXEC(SetColumnNullable(_T("containers"), _T("auto_bind_filter")));
242e9c7f
VK
748 CHK_EXEC(ConvertStrings(_T("containers"), _T("id"), _T("auto_bind_filter")));
749
feab3324
VK
750 static TCHAR batch[] =
751 _T("ALTER TABLE templates ADD flags integer\n")
2984ac95
VK
752 _T("UPDATE templates SET flags=0 WHERE enable_auto_apply=0\n")
753 _T("UPDATE templates SET flags=3 WHERE enable_auto_apply<>0\n")
feab3324
VK
754 _T("ALTER TABLE templates DROP COLUMN enable_auto_apply\n")
755 _T("ALTER TABLE containers ADD flags integer\n")
2984ac95
VK
756 _T("UPDATE containers SET flags=0 WHERE enable_auto_bind=0\n")
757 _T("UPDATE containers SET flags=3 WHERE enable_auto_bind<>0\n")
feab3324
VK
758 _T("ALTER TABLE containers DROP COLUMN enable_auto_bind\n")
759 _T("<END>");
feab3324
VK
760 CHK_EXEC(SQLBatch(batch));
761
762 CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOBIND, _T("SYS_CONTAINER_AUTOBIND"), EVENT_SEVERITY_NORMAL, 1,
763 _T("Node %2 automatically bound to container %4"),
764 _T("Generated when node bound to container object by autobind rule.\r\n")
765 _T("Parameters:#\r\n")
766 _T(" 1) Node ID\r\n")
767 _T(" 2) Node name\r\n")
768 _T(" 3) Container ID\r\n")
769 _T(" 4) Container name")
770 ));
771
772 CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOUNBIND, _T("SYS_CONTAINER_AUTOUNBIND"), EVENT_SEVERITY_NORMAL, 1,
773 _T("Node %2 automatically unbound from container %4"),
774 _T("Generated when node unbound from container object by autobind rule.\r\n")
775 _T("Parameters:#\r\n")
776 _T(" 1) Node ID\r\n")
777 _T(" 2) Node name\r\n")
778 _T(" 3) Container ID\r\n")
779 _T(" 4) Container name")
780 ));
781
782 CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOAPPLY, _T("SYS_TEMPLATE_AUTOAPPLY"), EVENT_SEVERITY_NORMAL, 1,
783 _T("Template %4 automatically applied to node %2"),
784 _T("Generated when template applied to node by autoapply rule.\r\n")
785 _T("Parameters:#\r\n")
786 _T(" 1) Node ID\r\n")
787 _T(" 2) Node name\r\n")
788 _T(" 3) Template ID\r\n")
789 _T(" 4) Template name")
790 ));
791
792 CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOREMOVE, _T("SYS_TEMPLATE_AUTOREMOVE"), EVENT_SEVERITY_NORMAL, 1,
793 _T("Template %4 automatically removed from node %2"),
794 _T("Generated when template removed from node by autoapply rule.\r\n")
795 _T("Parameters:#\r\n")
796 _T(" 1) Node ID\r\n")
797 _T(" 2) Node name\r\n")
798 _T(" 3) Template ID\r\n")
799 _T(" 4) Template name")
800 ));
801
65790062
VK
802 TCHAR buffer[64];
803 _sntprintf(buffer, 64, _T("%d"), ConfigReadInt(_T("AllowedCiphers"), 15) + 16);
804 CreateConfigParam(_T("AllowedCiphers"), buffer, 1, 1, TRUE);
805
feab3324
VK
806 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='253' WHERE var_name='SchemaVersion'")));
807 return TRUE;
808}
809
810
1e6b68a6
VK
811//
812// Upgrade from V251 to V252
813//
814
815static BOOL H_UpgradeFromV251(int currVersion, int newVersion)
816{
817 static TCHAR batch[] =
818 _T("ALTER TABLE interfaces ADD admin_state integer\n")
819 _T("ALTER TABLE interfaces ADD oper_state integer\n")
820 _T("UPDATE interfaces SET admin_state=0,oper_state=0\n")
821 _T("<END>");
822
823 CHK_EXEC(SQLBatch(batch));
824
825 CHK_EXEC(CreateEventTemplate(EVENT_INTERFACE_UNEXPECTED_UP, _T("SYS_IF_UNEXPECTED_UP"), EVENT_SEVERITY_MAJOR, 1,
826 _T("Interface \"%2\" unexpectedly changed state to UP (IP Addr: %3/%4, IfIndex: %5)"),
827 _T("Generated when interface goes up but it's expected state set to DOWN.\r\n")
828 _T("Please note that source of event is node, not an interface itself.\r\n")
829 _T("Parameters:#\r\n")
830 _T(" 1) Interface object ID\r\n")
831 _T(" 2) Interface name\r\n")
832 _T(" 3) Interface IP address\r\n")
833 _T(" 4) Interface netmask\r\n")
834 _T(" 5) Interface index")
835 ));
836
837 CHK_EXEC(CreateEventTemplate(EVENT_INTERFACE_EXPECTED_DOWN, _T("SYS_IF_EXPECTED_DOWN"), EVENT_SEVERITY_NORMAL, 1,
838 _T("Interface \"%2\" with expected state DOWN changed state to DOWN (IP Addr: %3/%4, IfIndex: %5)"),
839 _T("Generated when interface goes down and it's expected state is DOWN.\r\n")
840 _T("Please note that source of event is node, not an interface itself.\r\n")
841 _T("Parameters:#\r\n")
842 _T(" 1) Interface object ID\r\n")
843 _T(" 2) Interface name\r\n")
844 _T(" 3) Interface IP address\r\n")
845 _T(" 4) Interface netmask\r\n")
846 _T(" 5) Interface index")
847 ));
848
849 // Create rule pair in event processing policy
850 int ruleId = 0;
851 DB_RESULT hResult = SQLSelect(_T("SELECT max(rule_id) FROM event_policy"));
852 if (hResult != NULL)
853 {
854 ruleId = DBGetFieldLong(hResult, 0, 0) + 1;
855 DBFreeResult(hResult);
856 }
857
858 TCHAR query[1024];
859 _sntprintf(query, 1024,
860 _T("INSERT INTO event_policy (rule_id,flags,comments,alarm_message,alarm_severity,alarm_key,")
861 _T("script,alarm_timeout,alarm_timeout_event,situation_id,situation_instance) VALUES ")
862 _T("(%d,7944,'Show alarm when interface is unexpectedly up','%%m',5,'IF_UNEXP_UP_%%i_%%1',")
863 _T("'#00',0,%d,0,'#00')"), ruleId, EVENT_ALARM_TIMEOUT);
864 CHK_EXEC(SQLQuery(query));
865 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_INTERFACE_UNEXPECTED_UP);
866 CHK_EXEC(SQLQuery(query));
867 ruleId++;
868
869 _sntprintf(query, 1024,
870 _T("INSERT INTO event_policy (rule_id,flags,comments,alarm_message,alarm_severity,alarm_key,")
871 _T("script,alarm_timeout,alarm_timeout_event,situation_id,situation_instance) VALUES ")
872 _T("(%d,7944,'Acknowlege interface unexpectedly up alarms when interface goes down','%%m',")
873 _T("6,'IF_UNEXP_UP_%%i_%%1','#00',0,%d,0,'#00')"), ruleId, EVENT_ALARM_TIMEOUT);
874 CHK_EXEC(SQLQuery(query));
875 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_INTERFACE_EXPECTED_DOWN);
876 CHK_EXEC(SQLQuery(query));
877
878 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='252' WHERE var_name='SchemaVersion'")));
879 return TRUE;
880}
881
9aa67910
VK
882/**
883 * Upgrade from V250 to V251
884 */
711e5e9a
VK
885static BOOL H_UpgradeFromV250(int currVersion, int newVersion)
886{
887 static TCHAR batch[] =
888 _T("ALTER TABLE thresholds ADD current_severity integer\n")
889 _T("ALTER TABLE thresholds ADD last_event_timestamp integer\n")
890 _T("UPDATE thresholds SET current_severity=0,last_event_timestamp=0\n")
891 _T("<END>");
892
893 CHK_EXEC(SQLBatch(batch));
894
480e036b
VK
895 CHK_EXEC(SetColumnNullable(_T("thresholds"), _T("fire_value")));
896 CHK_EXEC(SetColumnNullable(_T("thresholds"), _T("rearm_value")));
711e5e9a
VK
897 CHK_EXEC(ConvertStrings(_T("thresholds"), _T("threshold_id"), _T("fire_value")));
898 CHK_EXEC(ConvertStrings(_T("thresholds"), _T("threshold_id"), _T("rearm_value")));
899
900 CHK_EXEC(CreateConfigParam(_T("EnableNXSLContainerFunctions"), _T("1"), 1, 1));
901 CHK_EXEC(CreateConfigParam(_T("UseDNSNameForDiscoveredNodes"), _T("0"), 1, 0));
902 CHK_EXEC(CreateConfigParam(_T("AllowTrapVarbindsConversion"), _T("1"), 1, 1));
903
904 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='251' WHERE var_name='SchemaVersion'")));
905 return TRUE;
906}
907
9aa67910
VK
908/**
909 * Upgrade from V249 to V250
910 */
ae2a3458
VK
911static BOOL H_UpgradeFromV249(int currVersion, int newVersion)
912{
913 CHK_EXEC(CreateTable(_T("CREATE TABLE licenses (")
914 _T("id integer not null,")
915 _T("content $SQL:TEXT null,")
916 _T("PRIMARY KEY(id))")));
917
918 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='250' WHERE var_name='SchemaVersion'")));
919 return TRUE;
920}
921
9aa67910
VK
922/**
923 * Upgrade from V248 to V249
924 */
d6124fa0
VK
925#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)")
926#define TDATA_INDEX_MSSQL _T("CREATE CLUSTERED INDEX idx_tdata_%d_id_timestamp ON tdata_%d(item_id,tdata_timestamp)")
927#define TDATA_INDEX_PGSQL _T("CREATE INDEX idx_tdata_%d_timestamp_id ON tdata_%d(tdata_timestamp,item_id)")
928#define TDATA_INDEX_DEFAULT _T("CREATE INDEX idx_tdata_%d_id_timestamp ON tdata_%d(item_id,tdata_timestamp)")
929
930static BOOL CreateTData(DWORD nodeId)
931{
932 TCHAR query[256];
933
934 _sntprintf(query, 256, TDATA_CREATE_QUERY, (int)nodeId);
935 CHK_EXEC(SQLQuery(query));
936
937 switch(g_iSyntax)
938 {
939 case DB_SYNTAX_MSSQL:
940 _sntprintf(query, 256, TDATA_INDEX_MSSQL, (int)nodeId, (int)nodeId);
941 break;
942 case DB_SYNTAX_PGSQL:
943 _sntprintf(query, 256, TDATA_INDEX_PGSQL, (int)nodeId, (int)nodeId);
944 break;
945 default:
946 _sntprintf(query, 256, TDATA_INDEX_DEFAULT, (int)nodeId, (int)nodeId);
947 break;
948 }
949 CHK_EXEC(SQLQuery(query));
950
951 return TRUE;
952}
953
954static BOOL H_UpgradeFromV248(int currVersion, int newVersion)
955{
956 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataTableCreationCommand','") TDATA_CREATE_QUERY _T("')")));
957
958 switch(g_iSyntax)
959 {
960 case DB_SYNTAX_MSSQL:
961 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_MSSQL _T("')")));
962 break;
963 case DB_SYNTAX_PGSQL:
964 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_PGSQL _T("')")));
965 break;
966 default:
967 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_DEFAULT _T("')")));
968 break;
969 }
970
971 CHK_EXEC(CreateTable(_T("CREATE TABLE dct_column_names (")
972 _T("column_id integer not null,")
973 _T("column_name varchar(63) not null,")
974 _T("PRIMARY KEY(column_id))")));
975
976 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM nodes"));
977 if (hResult != NULL)
978 {
979 int count = DBGetNumRows(hResult);
980 for(int i = 0 ; i < count; i++)
981 {
982 if (!CreateTData(DBGetFieldULong(hResult, i, 0)))
983 {
984 if (!g_bIgnoreErrors)
985 {
986 DBFreeResult(hResult);
987 return FALSE;
988 }
989 }
990 }
991 DBFreeResult(hResult);
992 }
993 else
994 {
995 if (!g_bIgnoreErrors)
996 return FALSE;
997 }
998
999 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='249' WHERE var_name='SchemaVersion'")));
1000 return TRUE;
1001}
1002
480e036b
VK
1003/**
1004 * Upgrade from V247 to V248
1005 */
cc8ce218
VK
1006static BOOL H_UpgradeFromV247(int currVersion, int newVersion)
1007{
1008 CHK_EXEC(CreateTable(_T("CREATE TABLE dc_tables (")
1009 _T("item_id integer not null,")
1010 _T("node_id integer not null,")
1011 _T("template_id integer not null,")
1012 _T("template_item_id integer not null,")
1013 _T("name varchar(255) null,")
1014 _T("instance_column varchar(63) null,")
1015 _T("description varchar(255) null,")
1016 _T("flags integer not null,")
1017 _T("source integer not null,")
1018 _T("snmp_port integer not null,")
1019 _T("polling_interval integer not null,")
1020 _T("retention_time integer not null,")
1021 _T("status integer not null,")
1022 _T("system_tag varchar(255) null,")
1023 _T("resource_id integer not null,")
1024 _T("proxy_node integer not null,")
1025 _T("perftab_settings $SQL:TEXT null,")
1026 _T("PRIMARY KEY(item_id))")));
1027
1028 CHK_EXEC(CreateTable(_T("CREATE TABLE dc_table_columns (")
1029 _T("table_id integer not null,")
1030 _T("column_name varchar(63) not null,")
1031 _T("snmp_oid varchar(1023) null,")
1032 _T("data_type integer not null,")
1033 _T("transformation_script $SQL:TEXT null,")
1034 _T("PRIMARY KEY(table_id,column_name))")));
1035
1036 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='248' WHERE var_name='SchemaVersion'")));
1037 return TRUE;
1038}
1039
480e036b
VK
1040/**
1041 * Upgrade from V246 to V247
1042 */
ff392ecf
VK
1043static BOOL H_UpgradeFromV246(int currVersion, int newVersion)
1044{
1045 static TCHAR insertQuery[] = _T("INSERT INTO object_custom_attributes (object_id,attr_name,attr_value) VALUES (?,?,?)");
1046
480e036b 1047 CHK_EXEC(SetColumnNullable(_T("object_custom_attributes"), _T("attr_value")));
ff392ecf
VK
1048
1049 // Convert strings in object_custom_attributes table
1050 DB_RESULT hResult = SQLSelect(_T("SELECT object_id,attr_name,attr_value FROM object_custom_attributes"));
1051 if (hResult != NULL)
1052 {
1053 if (SQLQuery(_T("DELETE FROM object_custom_attributes")))
1054 {
1055 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
1056 DB_STATEMENT hStmt = DBPrepareEx(g_hCoreDB, insertQuery, errorText);
1057 if (hStmt != NULL)
1058 {
1059 TCHAR name[128], *value;
1060 int count = DBGetNumRows(hResult);
1061 for(int i = 0; i < count; i++)
1062 {
1063 DWORD id = DBGetFieldULong(hResult, i, 0);
1064 DBGetField(hResult, i, 1, name, 128);
1065 DecodeSQLString(name);
1066 value = DBGetField(hResult, i, 2, NULL, 0);
1067 DecodeSQLString(value);
1068
1069 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, id);
1070 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, name, DB_BIND_STATIC);
1071 DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, value, DB_BIND_DYNAMIC);
1072 if (g_bTrace)
1073 ShowQuery(insertQuery);
1074 if (!DBExecuteEx(hStmt, errorText))
1075 {
1076 WriteToTerminalEx(_T("SQL query failed (%s):\n\x1b[33;1m%s\x1b[0m\n"), errorText, insertQuery);
1077 if (!g_bIgnoreErrors)
1078 {
1079 DBFreeStatement(hStmt);
1080 DBFreeResult(hResult);
1081 return FALSE;
1082 }
1083 }
1084 }
1085 DBFreeStatement(hStmt);
1086 }
1087 else
1088 {
1089 WriteToTerminalEx(_T("SQL query failed (%s):\n\x1b[33;1m%s\x1b[0m\n"), errorText, insertQuery);
1090 if (!g_bIgnoreErrors)
1091 {
1092 DBFreeResult(hResult);
1093 return FALSE;
1094 }
1095 }
1096 }
1097 else
1098 {
1099 if (!g_bIgnoreErrors)
1100 {
1101 DBFreeResult(hResult);
1102 return FALSE;
1103 }
1104 }
1105
1106 DBFreeResult(hResult);
1107 }
1108 else
1109 {
1110 if (!g_bIgnoreErrors)
1111 return FALSE;
1112 }
1113
1114 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_ocattr_oid ON object_custom_attributes(object_id)")));
1115 CHK_EXEC(CreateConfigParam(_T("AlarmHistoryRetentionTime"), _T("180"), 1, 0));
1116
1117 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='247' WHERE var_name='SchemaVersion'")));
1118 return TRUE;
1119}
1120
d6bf58f9
VK
1121/**
1122 * Upgrade from V245 to V246
1123 */
98cd01bb
VK
1124static BOOL H_UpgradeFromV245(int currVersion, int newVersion)
1125{
1126 static TCHAR batch[] =
1127 _T("ALTER TABLE snmp_trap_pmap ADD flags integer\n")
1128 _T("UPDATE snmp_trap_pmap SET flags=0\n")
1129 _T("<END>");
1130
1131 CHK_EXEC(SQLBatch(batch));
1132
480e036b 1133 CHK_EXEC(SetColumnNullable(_T("snmp_trap_pmap"), _T("description")));
d6bf58f9 1134 CHK_EXEC(ConvertStrings(_T("snmp_trap_pmap"), _T("trap_id"), _T("parameter"), _T("description"), false));
98cd01bb 1135
480e036b 1136 CHK_EXEC(SetColumnNullable(_T("cluster_resources"), _T("resource_name")));
d6bf58f9 1137 CHK_EXEC(ConvertStrings(_T("cluster_resources"), _T("cluster_id"), _T("resource_id"), _T("resource_name"), false));
98cd01bb
VK
1138
1139 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='246' WHERE var_name='SchemaVersion'")));
1140 return TRUE;
1141}
1142
d6bf58f9
VK
1143/**
1144 * Upgrade from V244 to V245
1145 */
4866d57b
VK
1146static BOOL H_UpgradeFromV244(int currVersion, int newVersion)
1147{
1148 static TCHAR batch[] =
1149 _T("ALTER TABLE nodes ADD runtime_flags integer\n")
1150 _T("UPDATE nodes SET runtime_flags=0\n")
1151 _T("<END>");
1152
1153 CHK_EXEC(SQLBatch(batch));
1154
480e036b
VK
1155 CHK_EXEC(SetColumnNullable(_T("actions"), _T("rcpt_addr")));
1156 CHK_EXEC(SetColumnNullable(_T("actions"), _T("email_subject")));
1157 CHK_EXEC(SetColumnNullable(_T("actions"), _T("action_data")));
4866d57b
VK
1158
1159 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("rcpt_addr")));
1160 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("email_subject")));
1161 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("action_data")));
1162
1163 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='245' WHERE var_name='SchemaVersion'")));
1164 return TRUE;
1165}
1166
1167
aa16f82b
VK
1168//
1169// Upgrade from V243 to V244
1170//
1171
1172static BOOL H_UpgradeFromV243(int currVersion, int newVersion)
1173{
1174 static TCHAR batch[] =
1175 _T("ALTER TABLE interfaces ADD dot1x_pae_state integer\n")
1176 _T("ALTER TABLE interfaces ADD dot1x_backend_state integer\n")
1177 _T("UPDATE interfaces SET dot1x_pae_state=0,dot1x_backend_state=0\n")
1178 _T("<END>");
1179
1180 CHK_EXEC(SQLBatch(batch));
1181
1182 CHK_EXEC(CreateEventTemplate(EVENT_8021X_PAE_STATE_CHANGED, _T("SYS_8021X_PAE_STATE_CHANGED"),
1183 EVENT_SEVERITY_NORMAL, 1, _T("Port %6 PAE state changed from %4 to %2"),
1184 _T("Generated when switch port PAE state changed.\r\nParameters:\r\n")
1185 _T(" 1) New PAE state code\r\n")
1186 _T(" 2) New PAE state as text\r\n")
1187 _T(" 3) Old PAE state code\r\n")
1188 _T(" 4) Old PAE state as text\r\n")
1189 _T(" 5) Interface index\r\n")
1190 _T(" 6) Interface name")));
1191
1192 CHK_EXEC(CreateEventTemplate(EVENT_8021X_BACKEND_STATE_CHANGED, _T("SYS_8021X_BACKEND_STATE_CHANGED"),
1193 EVENT_SEVERITY_NORMAL, 1, _T("Port %6 backend authentication state changed from %4 to %2"),
1194 _T("Generated when switch port backend authentication state changed.\r\nParameters:\r\n")
1195 _T(" 1) New backend state code\r\n")
1196 _T(" 2) New backend state as text\r\n")
1197 _T(" 3) Old backend state code\r\n")
1198 _T(" 4) Old backend state as text\r\n")
1199 _T(" 5) Interface index\r\n")
1200 _T(" 6) Interface name")));
1201
1202 CHK_EXEC(CreateEventTemplate(EVENT_8021X_PAE_FORCE_UNAUTH, _T("SYS_8021X_PAE_FORCE_UNAUTH"),
1203 EVENT_SEVERITY_MAJOR, 1, _T("Port %2 switched to force unauthorize state"),
1204 _T("Generated when switch port PAE state changed to FORCE UNAUTHORIZE.\r\nParameters:\r\n")
1205 _T(" 1) Interface index\r\n")
1206 _T(" 2) Interface name")));
1207
1208 CHK_EXEC(CreateEventTemplate(EVENT_8021X_AUTH_FAILED, _T("SYS_8021X_AUTH_FAILED"),
1209 EVENT_SEVERITY_MAJOR, 1, _T("802.1x authentication failed on port %2"),
1210 _T("Generated when switch port backend authentication state changed to FAIL.\r\nParameters:\r\n")
1211 _T(" 1) Interface index\r\n")
1212 _T(" 2) Interface name")));
1213
1214 CHK_EXEC(CreateEventTemplate(EVENT_8021X_AUTH_TIMEOUT, _T("SYS_8021X_AUTH_TIMEOUT"),
1215 EVENT_SEVERITY_MAJOR, 1, _T("802.1x authentication time out on port %2"),
1216 _T("Generated when switch port backend authentication state changed to TIMEOUT.\r\nParameters:\r\n")
1217 _T(" 1) Interface index\r\n")
1218 _T(" 2) Interface name")));
1219
1220 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='244' WHERE var_name='SchemaVersion'")));
1221 return TRUE;
1222}
1223
1224
e320f8ce
VK
1225//
1226// Upgrade from V242 to V243
1227//
1228
1229static BOOL H_UpgradeFromV242(int currVersion, int newVersion)
1230{
1231 static TCHAR batch[] =
242e9c7f
VK
1232 _T("ALTER TABLE items ADD snmp_raw_value_type integer\n")
1233 _T("UPDATE items SET snmp_raw_value_type=0\n")
e320f8ce
VK
1234 _T("ALTER TABLE items ADD flags integer\n")
1235 _T("UPDATE items SET flags=adv_schedule+(all_thresholds*2)\n")
1236 _T("ALTER TABLE items DROP COLUMN adv_schedule\n")
1237 _T("ALTER TABLE items DROP COLUMN all_thresholds\n")
e320f8ce
VK
1238 _T("<END>");
1239
1240 CHK_EXEC(SQLBatch(batch));
1241 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='243' WHERE var_name='SchemaVersion'")));
1242 return TRUE;
1243}
1244
1245
f5e80875
VK
1246//
1247// Upgrade from V241 to V242
1248//
1249
1250static BOOL H_UpgradeFromV241(int currVersion, int newVersion)
1251{
1252 static TCHAR batch[] =
1253 _T("DROP TABLE business_service_templates\n")
1254 _T("ALTER TABLE dashboards ADD options integer\n")
1255 _T("UPDATE dashboards SET options=0\n")
1256 _T("<END>");
1257
1258 CHK_EXEC(SQLBatch(batch));
1259 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='242' WHERE var_name='SchemaVersion'")));
1260 return TRUE;
1261}
1262
1263
6ff21d27
VK
1264//
1265// Upgrade from V240 to V241
1266//
1267
1268static BOOL H_UpgradeFromV240(int currVersion, int newVersion)
1269{
1270 static TCHAR batch[] =
1271 _T("ALTER TABLE slm_checks ADD template_id integer\n")
1272 _T("ALTER TABLE slm_checks ADD current_ticket integer\n")
1273 _T("UPDATE slm_checks SET template_id=0,current_ticket=0\n")
1274 _T("<END>");
1275
1276 CHK_EXEC(SQLBatch(batch));
1277 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='241' WHERE var_name='SchemaVersion'")));
1278 return TRUE;
1279}
1280
1281
5fe19f9e
VK
1282//
1283// Upgrade from V239 to V240
1284//
1285
1286static BOOL H_UpgradeFromV239(int currVersion, int newVersion)
1287{
1288 CHK_EXEC(SQLQuery(_T("ALTER TABLE raw_dci_values ADD transformed_value varchar(255)")));
1289 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='240' WHERE var_name='SchemaVersion'")));
1290 return TRUE;
1291}
1292
1293
5a7d6a10
VK
1294//
1295// Upgrade from V238 to V239
1296//
1297
1298static BOOL H_UpgradeFromV238(int currVersion, int newVersion)
1299{
1300 CHK_EXEC(SQLQuery(
1301 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES ")
1302 _T("(56,'SYS_IP_ADDRESS_CHANGED',1,1,'Primary IP address changed from %2 to %1',")
1303 _T("'Generated when primary IP address changed (usually because of primary name change or DNS change).#0D#0A")
1304 _T("Parameters:#0D#0A 1) New IP address#0D#0A 2) Old IP address#0D#0A 3) Primary host name')")));
1305
1306 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='239' WHERE var_name='SchemaVersion'")));
1307 return TRUE;
1308}
1309
1310
34ccf87f
VK
1311//
1312// Upgrade from V232 to V238
1313//
1314
1315static BOOL H_UpgradeFromV232toV238(int currVersion, int newVersion)
1316{
1317 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
1318 _T("id integer not null,")
1319 _T("type integer not null,")
1320 _T("content $SQL:TEXT null,")
1321 _T("threshold_id integer not null,")
1322 _T("reason varchar(255) null,")
1323 _T("is_template integer not null,")
1324 _T("PRIMARY KEY(id))")));
1325
1326 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
1327 _T("ticket_id integer not null,")
1328 _T("service_id integer not null,")
1329 _T("check_id integer not null,")
1330 _T("create_timestamp integer not null,")
1331 _T("close_timestamp integer not null,")
1332 _T("reason varchar(255) null,")
1333 _T("PRIMARY KEY(ticket_id))")));
1334
1335 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_service_history (")
1336 _T("record_id integer not null,")
1337 _T("service_id integer not null,")
1338 _T("change_timestamp integer not null,")
1339 _T("new_status integer not null,")
1340 _T("PRIMARY KEY(record_id))")));
1341
1342 CHK_EXEC(CreateTable(_T("CREATE TABLE report_results (")
1343 _T("report_id integer not null,")
1344 _T("generated integer not null,")
1345 _T("job_id integer not null,")
1346 _T("PRIMARY KEY(report_id,job_id))")));
1347
1348 CHK_EXEC(CreateTable(_T("CREATE TABLE reports (")
1349 _T("id integer not null,")
1350 _T("definition $SQL:TEXT null,")
1351 _T("PRIMARY KEY(id))")));
1352
1353 CHK_EXEC(CreateTable(_T("CREATE TABLE job_history (")
1354 _T("id integer not null,")
1355 _T("time_created integer not null,")
1356 _T("time_started integer not null,")
1357 _T("time_finished integer not null,")
1358 _T("job_type varchar(127) null,")
1359 _T("description varchar(255) null,")
1360 _T("additional_info varchar(255) null,")
1361 _T("node_id integer not null,")
1362 _T("user_id integer not null,")
1363 _T("status integer not null,")
1364 _T("failure_message varchar(255) null,")
1365 _T("PRIMARY KEY(id))")));
1366
1367 CHK_EXEC(CreateTable(_T("CREATE TABLE business_services (")
1368 _T("service_id integer not null,")
1369 _T("PRIMARY KEY(service_id))")));
1370
1371 CHK_EXEC(CreateTable(_T("CREATE TABLE business_service_templates (")
1372 _T("service_id integer not null,")
1373 _T("template_id integer not null,")
1374 _T("PRIMARY KEY(service_id,template_id))")));
1375
1376 CHK_EXEC(CreateTable(_T("CREATE TABLE node_links (")
1377 _T("nodelink_id integer not null,")
1378 _T("node_id integer not null,")
1379 _T("PRIMARY KEY(nodelink_id))")));
1380
1381 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_agreements (")
1382 _T("agreement_id integer not null,")
1383 _T("service_id integer not null,")
1384 _T("org_id integer not null,")
1385 _T("uptime varchar(63) not null,")
1386 _T("period integer not null,")
1387 _T("start_date integer not null,")
1388 _T("notes varchar(255),")
1389 _T("PRIMARY KEY(agreement_id))")));
1390
1391 CHK_EXEC(CreateTable(_T("CREATE TABLE organizations (")
1392 _T("id integer not null,")
1393 _T("parent_id integer not null,")
1394 _T("org_type integer not null,")
1395 _T("name varchar(63) not null,")
1396 _T("description varchar(255),")
1397 _T("manager integer not null,")
1398 _T("PRIMARY KEY(id))")));
1399
1400 CHK_EXEC(CreateTable(_T("CREATE TABLE persons (")
1401 _T("id integer not null,")
1402 _T("org_id integer not null,")
1403 _T("first_name varchar(63),")
1404 _T("last_name varchar(63),")
1405 _T("title varchar(255),")
1406 _T("status integer not null,")
1407 _T("PRIMARY KEY(id))")));
1408
1409 CHK_EXEC(CreateConfigParam(_T("JobHistoryRetentionTime"), _T("90"), 1, 0));
1410
1411 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description=")
1412 _T("'Generated when threshold check is rearmed for specific data collection item.#0D#0A")
1413 _T("Parameters:#0D#0A")
1414 _T(" 1) Parameter name#0D#0A")
1415 _T(" 2) Item description#0D#0A")
1416 _T(" 3) Data collection item ID#0D#0A")
1417 _T(" 4) Instance#0D#0A")
1418 _T(" 5) Threshold value#0D#0A")
1419 _T(" 6) Actual value' WHERE event_code=18")));
1420
1421 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='238' WHERE var_name='SchemaVersion'")));
1422 return TRUE;
1423}
1424
1425
9c529287
VK
1426//
1427// Upgrade from V237 to V238
1428//
1429
1430static BOOL H_UpgradeFromV237(int currVersion, int newVersion)
1431{
1432 static TCHAR batch[] =
1433 _T("DROP TABLE slm_check_templates\n")
1434 _T("DROP TABLE node_link_checks\n")
1435 _T("DROP TABLE slm_checks\n")
1436 _T("DROP TABLE slm_tickets\n")
1437 _T("<END>");
1438
1439 CHK_EXEC(SQLBatch(batch));
1440
1441 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
1442 _T("id integer not null,")
1443 _T("type integer not null,")
1444 _T("content $SQL:TEXT null,")
1445 _T("threshold_id integer not null,")
1446 _T("reason varchar(255) null,")
1447 _T("is_template integer not null,")
1448 _T("PRIMARY KEY(id))")));
1449
1450 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
1451 _T("ticket_id integer not null,")
1452 _T("service_id integer not null,")
1453 _T("check_id integer not null,")
1454 _T("create_timestamp integer not null,")
1455 _T("close_timestamp integer not null,")
1456 _T("reason varchar(255) null,")
1457 _T("PRIMARY KEY(ticket_id))")));
1458
1459 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_service_history (")
1460 _T("record_id integer not null,")
1461 _T("service_id integer not null,")
1462 _T("change_timestamp integer not null,")
1463 _T("new_status integer not null,")
1464 _T("PRIMARY KEY(record_id))")));
1465
1466 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='238' WHERE var_name='SchemaVersion'")));
1467 return TRUE;
1468}
1469
1470
f5c9fdc5
VK
1471//
1472// Upgrade from V236 to V237
1473//
1474
1475static BOOL H_UpgradeFromV236(int currVersion, int newVersion)
1476{
1477 static TCHAR batch[] =
1478 _T("ALTER TABLE business_services DROP COLUMN name\n")
1479 _T("ALTER TABLE business_services DROP COLUMN parent_id\n")
1480 _T("ALTER TABLE business_services DROP COLUMN status\n")
1481 _T("ALTER TABLE slm_checks DROP COLUMN name\n")
1482 _T("<END>");
1483
1484 CHK_EXEC(SQLBatch(batch));
1485
1486 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='237' WHERE var_name='SchemaVersion'")));
1487 return TRUE;
1488}
1489
1490
5418326b
AK
1491//
1492// Upgrade from V235 to V236
1493//
1494
1495static BOOL H_UpgradeFromV235(int currVersion, int newVersion)
1496{
1497 CHK_EXEC(CreateTable(_T("CREATE TABLE report_results (")
1498 _T("report_id integer not null,")
1499 _T("generated integer not null,")
1500 _T("job_id integer not null,")
1501 _T("PRIMARY KEY(report_id,job_id))")));
1502
1503 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='236' WHERE var_name='SchemaVersion'")));
1504 return TRUE;
1505}
1506
1507
845b8121
VK
1508//
1509// Upgrade from V234 to V235
1510//
1511
1512static BOOL H_UpgradeFromV234(int currVersion, int newVersion)
1513{
1514 CHK_EXEC(CreateTable(_T("CREATE TABLE reports (")
1515 _T("id integer not null,")
1516 _T("definition $SQL:TEXT null,")
1517 _T("PRIMARY KEY(id))")));
1518
845b8121
VK
1519 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='235' WHERE var_name='SchemaVersion'")));
1520 return TRUE;
1521}
1522
1523
878b4261
VK
1524//
1525// Upgrade from V233 to V234
1526//
1527
1528static BOOL H_UpgradeFromV233(int currVersion, int newVersion)
1529{
1530 CHK_EXEC(CreateTable(_T("CREATE TABLE job_history (")
1531 _T("id integer not null,")
1532 _T("time_created integer not null,")
1533 _T("time_started integer not null,")
1534 _T("time_finished integer not null,")
1535 _T("job_type varchar(127) null,")
1536 _T("description varchar(255) null,")
24dc5346 1537 _T("additional_info varchar(255) null,")
878b4261
VK
1538 _T("node_id integer not null,")
1539 _T("user_id integer not null,")
1540 _T("status integer not null,")
1541 _T("failure_message varchar(255) null,")
1542 _T("PRIMARY KEY(id))")));
1543
1544 CHK_EXEC(CreateConfigParam(_T("JobHistoryRetentionTime"), _T("90"), 1, 0));
1545
1546 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description=")
1547 _T("'Generated when threshold check is rearmed for specific data collection item.#0D#0A")
1548 _T("Parameters:#0D#0A")
1549 _T(" 1) Parameter name#0D#0A")
1550 _T(" 2) Item description#0D#0A")
1551 _T(" 3) Data collection item ID#0D#0A")
1552 _T(" 4) Instance#0D#0A")
1553 _T(" 5) Threshold value#0D#0A")
1554 _T(" 6) Actual value' WHERE event_code=18")));
1555
1556 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='234' WHERE var_name='SchemaVersion'")));
1557 return TRUE;
1558}
1559
1560
128681c8
VK
1561//
1562// Upgrade from V232 to V233
1563//
1564
1565static BOOL H_UpgradeFromV232(int currVersion, int newVersion)
1566{
1567 CHK_EXEC(CreateTable(_T("CREATE TABLE business_services (")
1568 _T("service_id integer not null,")
1569 _T("name varchar(63) not null,")
2498594e
VK
1570 _T("parent_id integer not null,")
1571 _T("status integer not null,")
128681c8
VK
1572 _T("PRIMARY KEY(service_id))")));
1573
128681c8
VK
1574 CHK_EXEC(CreateTable(_T("CREATE TABLE business_service_templates (")
1575 _T("service_id integer not null,")
1576 _T("template_id integer not null,")
1577 _T("PRIMARY KEY(service_id,template_id))")));
1578
1579 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
1580 _T("check_id integer not null,")
1581 _T("name varchar(63) not null,")
2498594e 1582 _T("type integer not null,")
128681c8 1583 _T("content $SQL:TEXT,")
2498594e 1584 _T("threshold_id integer not null,")
128681c8
VK
1585 _T("state integer not null,")
1586 _T("reason varchar(255) not null,")
1587 _T("PRIMARY KEY(check_id))")));
1588
1589 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_check_templates (")
1590 _T("id integer not null,")
1591 _T("name varchar(63) not null,")
2498594e 1592 _T("type integer not null,")
128681c8 1593 _T("content $SQL:TEXT,")
2498594e 1594 _T("threshold_id integer not null,")
128681c8
VK
1595 _T("PRIMARY KEY(id))")));
1596
1597 CHK_EXEC(CreateTable(_T("CREATE TABLE node_links (")
1598 _T("nodelink_id integer not null,")
1599 _T("node_id integer not null,")
1600 _T("PRIMARY KEY(nodelink_id))")));
1601
1602 CHK_EXEC(CreateTable(_T("CREATE TABLE node_link_checks (")
1603 _T("nodelink_id integer not null,")
1604 _T("check_id integer not null,")
1605 _T("PRIMARY KEY(nodelink_id,check_id))")));
1606
1607 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_agreements (")
1608 _T("agreement_id integer not null,")
1609 _T("service_id integer not null,")
1610 _T("org_id integer not null,")
1611 _T("uptime varchar(63) not null,")
1612 _T("period integer not null,")
1613 _T("start_date integer not null,")
1614 _T("notes varchar(255),")
1615 _T("PRIMARY KEY(agreement_id))")));
1616
1617 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
1618 _T("ticket_id integer not null,")
1619 _T("service_id integer not null,")
1620 _T("create_timestamp integer not null,")
1621 _T("close_timestamp integer not null,")
1622 _T("reason varchar(255) not null,")
1623 _T("PRIMARY KEY(ticket_id))")));
1624
1625 CHK_EXEC(CreateTable(_T("CREATE TABLE organizations (")
1626 _T("id integer not null,")
1627 _T("parent_id integer not null,")
1628 _T("org_type integer not null,")
1629 _T("name varchar(63) not null,")
1630 _T("description varchar(255),")
1631 _T("manager integer not null,")
1632 _T("PRIMARY KEY(id))")));
1633
1634 CHK_EXEC(CreateTable(_T("CREATE TABLE persons (")
1635 _T("id integer not null,")
1636 _T("org_id integer not null,")
1637 _T("first_name varchar(63),")
1638 _T("last_name varchar(63),")
1639 _T("title varchar(255),")
1640 _T("status integer not null,")
1641 _T("PRIMARY KEY(id))")));
1642
1643 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='233' WHERE var_name='SchemaVersion'")));
1644 return TRUE;
1645}
1646
1647
8f245200
VK
1648//
1649// Upgrade from V231 to V232
1650//
1651
1652static BOOL H_UpgradeFromV231(int currVersion, int newVersion)
1653{
1654 static TCHAR batch[] =
1655 _T("ALTER TABLE object_properties ADD submap_id integer\n")
1656 _T("UPDATE object_properties SET submap_id=0\n")
1657 _T("DROP TABLE maps\n")
1658 _T("DROP TABLE map_access_lists\n")
1659 _T("DROP TABLE submaps\n")
1660 _T("DROP TABLE submap_object_positions\n")
1661 _T("DROP TABLE submap_links\n")
1662 _T("<END>");
1663
1664 CHK_EXEC(SQLBatch(batch));
1665
1666 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='232' WHERE var_name='SchemaVersion'")));
1667 return TRUE;
1668}
1669
1670
3eab63f2
VK
1671//
1672// Upgrade from V230 to V231
1673//
1674
1675static BOOL H_UpgradeFromV230(int currVersion, int newVersion)
1676{
1677 static TCHAR batch[] =
1678 _T("ALTER TABLE nodes ADD bridge_base_addr varchar(15)\n")
1679 _T("UPDATE nodes SET bridge_base_addr='000000000000'\n")
1680 _T("<END>");
1681
1682 CHK_EXEC(SQLBatch(batch));
1683
1684 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='231' WHERE var_name='SchemaVersion'")));
1685 return TRUE;
1686}
1687
1688
fceea457
VK
1689//
1690// Upgrade from V229 to V230
1691//
1692
1693static BOOL H_UpgradeFromV229(int currVersion, int newVersion)
1694{
1695 static TCHAR batch1[] =
1a74dc37
VK
1696 _T("ALTER TABLE network_maps ADD bg_latitude varchar(20)\n")
1697 _T("ALTER TABLE network_maps ADD bg_longitude varchar(20)\n")
1698 _T("ALTER TABLE network_maps ADD bg_zoom integer\n")
fceea457
VK
1699 _T("ALTER TABLE dashboard_elements ADD layout_data $SQL:TEXT\n")
1700 _T("<END>");
1701
1702 CHK_EXEC(SQLBatch(batch1));
1703
1704 DB_RESULT hResult = SQLSelect(_T("SELECT dashboard_id,element_id,horizontal_span,vertical_span,horizontal_alignment,vertical_alignment FROM dashboard_elements"));
1705 if (hResult != NULL)
1706 {
1707 TCHAR query[1024], xml[1024];
1708
1709 int count = DBGetNumRows(hResult);
1710 for(int i = 0; i < count; i++)
1711 {
1712 _sntprintf(xml, 1024, _T("<layout><horizontalSpan>%d</horizontalSpan><verticalSpan>%d</verticalSpan><horizontalAlignment>%d</horizontalAlignment><verticalAlignment>%d</verticalAlignment></layout>"),
1713 (int)DBGetFieldLong(hResult, i, 2), (int)DBGetFieldLong(hResult, i, 3),
1714 (int)DBGetFieldLong(hResult, i, 4), (int)DBGetFieldLong(hResult, i, 5));
1715 _sntprintf(query, 1024, _T("UPDATE dashboard_elements SET layout_data=%s WHERE dashboard_id=%d AND element_id=%d"),
1716 (const TCHAR *)DBPrepareString(g_hCoreDB, xml), (int)DBGetFieldLong(hResult, i, 0), (int)DBGetFieldLong(hResult, i, 1));
1717 CHK_EXEC(SQLQuery(query));
1718 }
1719 DBFreeResult(hResult);
1720 }
1721 else
1722 {
1723 if (!g_bIgnoreErrors)
1724 return FALSE;
1725 }
1726
1727 static TCHAR batch2[] =
1728 _T("ALTER TABLE dashboard_elements DROP COLUMN horizontal_span\n")
1729 _T("ALTER TABLE dashboard_elements DROP COLUMN vertical_span\n")
1730 _T("ALTER TABLE dashboard_elements DROP COLUMN horizontal_alignment\n")
1731 _T("ALTER TABLE dashboard_elements DROP COLUMN vertical_alignment\n")
1732 _T("<END>");
1733
1734 CHK_EXEC(SQLBatch(batch2));
1735
1736 CreateConfigParam(_T("TileServerURL"), _T("http://tile.openstreetmap.org/"), 1, 0);
1737
1738 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='230' WHERE var_name='SchemaVersion'")));
1739 return TRUE;
1740}
1741
1742
926e8ce7
VK
1743//
1744// Upgrade from V228 to V229
1745//
1746
1747static BOOL H_UpgradeFromV228(int currVersion, int newVersion)
1748{
1749 CHK_EXEC(CreateTable(_T("CREATE TABLE dashboards (")
1750 _T(" id integer not null,")
1751 _T(" num_columns integer not null,")
1752 _T(" PRIMARY KEY(id))")));
1753
1754 CHK_EXEC(CreateTable(_T("CREATE TABLE dashboard_elements (")
1755 _T(" dashboard_id integer not null,")
1756 _T(" element_id integer not null,")
1757 _T(" element_type integer not null,")
1758 _T(" element_data $SQL:TEXT null,")
1759 _T(" horizontal_span integer not null,")
1760 _T(" vertical_span integer not null,")
1761 _T(" horizontal_alignment integer not null,")
1762 _T(" vertical_alignment integer not null,")
1763 _T(" PRIMARY KEY(dashboard_id,element_id))")));
1764
1765 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='229' WHERE var_name='SchemaVersion'")));
1766 return TRUE;
1767}
1768
1769
7a41a06e
VK
1770//
1771// Upgrade from V227 to V228
1772//
1773
1774static BOOL H_UpgradeFromV227(int currVersion, int newVersion)
1775{
1776 CHK_EXEC(SQLQuery(_T("DROP TABLE web_maps")));
1777 CHK_EXEC(MigrateMaps());
1778 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='228' WHERE var_name='SchemaVersion'")));
1779 return TRUE;
1780}
1781
b1553daa 1782
89135050
VK
1783//
1784// Upgrade from V226 to V227
1785//
1786
1787static BOOL H_UpgradeFromV226(int currVersion, int newVersion)
1788{
1789 static TCHAR batch[] =
1790 _T("ALTER TABLE clusters ADD zone_guid integer\n")
1791 _T("UPDATE clusters SET zone_guid=0\n")
1792 _T("<END>");
1793
1794 CHK_EXEC(SQLBatch(batch));
1795
1796 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='227' WHERE var_name='SchemaVersion'")));
1797 return TRUE;
1798}
1799
1800
4c16cdc7
VK
1801//
1802// Upgrade from V225 to V226
1803//
1804
1805static BOOL H_UpgradeFromV225(int currVersion, int newVersion)
1806{
1807 static TCHAR batch[] =
1808 _T("ALTER TABLE interfaces ADD flags integer\n")
1809 _T("UPDATE interfaces SET flags=0\n")
1810 _T("UPDATE interfaces SET flags=1 WHERE synthetic_mask<>0\n")
1811 _T("ALTER TABLE interfaces DROP COLUMN synthetic_mask\n")
1812 _T("<END>");
1813
1814 CHK_EXEC(SQLBatch(batch));
1815
1816 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='226' WHERE var_name='SchemaVersion'")));
1817 return TRUE;
1818}
1819
1820
478d4ff4
VK
1821//
1822// Upgrade from V224 to V225
1823//
1824
1825static BOOL H_UpgradeFromV224(int currVersion, int newVersion)
1826{
1827 static TCHAR batch[] =
1828 _T("ALTER TABLE interfaces ADD description varchar(255)\n")
1829 _T("UPDATE interfaces SET description=''\n")
1830 _T("<END>");
1831
1832 CHK_EXEC(SQLBatch(batch));
1833
1834 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='225' WHERE var_name='SchemaVersion'")));
1835 return TRUE;
1836}
1837
1838
52c31148
VK
1839//
1840// Upgrade from V223 to V224
1841//
1842
1843static BOOL H_UpgradeFromV223(int currVersion, int newVersion)
1844{
1845 static TCHAR batch[] =
1846 _T("DROP TABLE zone_ip_addr_list\n")
1847 _T("ALTER TABLE zones DROP COLUMN zone_type\n")
1848 _T("ALTER TABLE zones DROP COLUMN controller_ip\n")
1849 _T("ALTER TABLE zones ADD agent_proxy integer\n")
1850 _T("ALTER TABLE zones ADD snmp_proxy integer\n")
1851 _T("ALTER TABLE zones ADD icmp_proxy integer\n")
1852 _T("UPDATE zones SET agent_proxy=0,snmp_proxy=0,icmp_proxy=0\n")
1853 _T("<END>");
1854
1855 CHK_EXEC(SQLBatch(batch));
1856
1857 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='224' WHERE var_name='SchemaVersion'")));
1858 return TRUE;
1859}
1860
1861
5ad2167d
VK
1862//
1863// Upgrade from V222 to V223
1864//
1865
1866static BOOL H_UpgradeFromV222(int currVersion, int newVersion)
1867{
1868 static TCHAR batch[] =
1869 _T("DROP TABLE oid_to_type\n")
1870 _T("ALTER TABLE nodes DROP COLUMN node_type\n")
1871 _T("ALTER TABLE nodes ADD primary_name varchar(255)\n")
1872 _T("UPDATE nodes SET primary_name=primary_ip\n")
1873 _T("<END>");
1874
1875 CHK_EXEC(SQLBatch(batch));
1876
1877 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='223' WHERE var_name='SchemaVersion'")));
1878 return TRUE;
1879}
1880
1881
465b3f2d 1882//
f25b0d12
VK
1883// Upgrade from V221 to V222
1884//
1885
1886static BOOL H_UpgradeFromV221(int currVersion, int newVersion)
1887{
1888 static TCHAR batch[] =
1889 _T("ALTER TABLE object_properties ADD image varchar(36)\n")
1890 _T("UPDATE object_properties SET image='00000000-0000-0000-0000-000000000000'\n")
1891 _T("<END>");
1892
1893 CHK_EXEC(SQLBatch(batch));
1894
1895 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='222' WHERE var_name='SchemaVersion'")));
1896 return TRUE;
1897}
1898
1899
1900//
1901// Upgrade from V220 to V221
465b3f2d
VK
1902//
1903
1904static BOOL H_UpgradeFromV220(int currVersion, int newVersion)
1905{
1906 static TCHAR batch[] =
1907 _T("ALTER TABLE network_maps DROP COLUMN background\n")
1908 _T("ALTER TABLE network_maps ADD background varchar(36)\n")
1909 _T("<END>");
1910
1911 CHK_EXEC(SQLBatch(batch));
1912
1913 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='221' WHERE var_name='SchemaVersion'")));
1914 return TRUE;
1915}
1916
1917
d74c80ea
VK
1918//
1919// Upgrade from V219 to V220
1920//
1921
1922static BOOL H_UpgradeFromV219(int currVersion, int newVersion)
1923{
1924 static TCHAR batch[] =
1925 _T("ALTER TABLE interfaces ADD bridge_port integer\n")
1926 _T("ALTER TABLE interfaces ADD phy_slot integer\n")
1927 _T("ALTER TABLE interfaces ADD phy_port integer\n")
1928 _T("ALTER TABLE interfaces ADD peer_node_id integer\n")
1929 _T("ALTER TABLE interfaces ADD peer_if_id integer\n")
1930 _T("UPDATE interfaces SET bridge_port=0,phy_slot=0,phy_port=0,peer_node_id=0,peer_if_id=0\n")
1931 _T("ALTER TABLE nodes ADD snmp_sys_name varchar(127)\n")
1932 _T("UPDATE nodes SET snmp_sys_name=''\n")
1933 _T("<END>");
1934
1935 CHK_EXEC(SQLBatch(batch));
1936
1937 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='220' WHERE var_name='SchemaVersion'")));
1938 return TRUE;
1939}
1940
1941
e6b9439a
AK
1942//
1943// Upgrade from V218 to V219
1944//
1945
1946static BOOL H_UpgradeFromV218(int currVersion, int newVersion)
1947{
1948 CHK_EXEC(CreateTable(_T("CREATE TABLE images (")
9cd5201a
AK
1949 _T(" guid varchar(36) not null,")
1950 _T(" mimetype varchar(64) not null,")
1951 _T(" name varchar(255) not null,")
e6b9439a
AK
1952 _T(" category varchar(255) not null,")
1953 _T(" protected integer default 0,")
1954 _T(" ")
9cd5201a
AK
1955 _T(" PRIMARY KEY(guid),")
1956 _T(" UNIQUE(name, category))")));
1957
1958 static TCHAR batch[] =
1959 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1960 _T("('1ddb76a3-a05f-4a42-acda-22021768feaf', 'image/png', 'ATM', 'Network Objects', 1)\n")
9cd5201a 1961 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1962 _T("('b314cf44-b2aa-478e-b23a-73bc5bb9a624', 'image/png', 'HSM', 'Network Objects', 1)\n")
9cd5201a 1963 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1964 _T("('904e7291-ee3f-41b7-8132-2bd29288ecc8', 'image/png', 'Node', 'Network Objects', 1)\n")
9cd5201a 1965 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1966 _T("('f5214d16-1ab1-4577-bb21-063cfd45d7af', 'image/png', 'Printer', 'Network Objects', 1)\n")
9cd5201a 1967 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1968 _T("('bacde727-b183-4e6c-8dca-ab024c88b999', 'image/png', 'Router', 'Network Objects', 1)\n")
9cd5201a 1969 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1970 _T("('ba6ab507-f62d-4b8f-824c-ca9d46f22375', 'image/png', 'Server', 'Network Objects', 1)\n")
9cd5201a 1971 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1972 _T("('092e4b35-4e7c-42df-b9b7-d5805bfac64e', 'image/png', 'Service', 'Network Objects', 1)\n")
9cd5201a 1973 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1974 _T("('f9105c54-8dcf-483a-b387-b4587dfd3cba', 'image/png', 'Switch', 'Network Objects', 1)\n")
9cd5201a 1975 _T("INSERT INTO images (guid, mimetype, name, category, protected) VALUES ")
a32a5bc3 1976 _T("('7cd999e9-fbe0-45c3-a695-f84523b3a50c', 'image/png', 'Unknown', 'Network Objects', 1)\n")
9cd5201a
AK
1977 _T("<END>");
1978
1979 CHK_EXEC(SQLBatch(batch));
e6b9439a
AK
1980
1981 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='219' WHERE var_name='SchemaVersion'")));
1982 return TRUE;
1983}
1984
480e036b
VK
1985/**
1986 * Upgrade from V217 to V218
1987 */
4fe87cdc
VK
1988static BOOL H_UpgradeFromV217(int currVersion, int newVersion)
1989{
480e036b 1990 CHK_EXEC(SetColumnNullable(_T("snmp_communities"), _T("community")));
4fe87cdc
VK
1991 CHK_EXEC(ConvertStrings(_T("snmp_communities"), _T("id"), _T("community")));
1992
1993 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='218' WHERE var_name='SchemaVersion'")));
1994 return TRUE;
1995}
1996
480e036b
VK
1997/**
1998 * Upgrade from V216 to V217
1999 */
65e2005b
VK
2000static BOOL H_UpgradeFromV216(int currVersion, int newVersion)
2001{
2002 static TCHAR batch[] =
2003 _T("ALTER TABLE nodes ADD snmp_port integer\n")
8cad4ebb 2004 _T("UPDATE nodes SET snmp_port=161\n")
65e2005b
VK
2005 _T("ALTER TABLE items ADD snmp_port integer\n")
2006 _T("UPDATE items SET snmp_port=0\n")
2007 _T("<END>");
2008
2009 CHK_EXEC(SQLBatch(batch));
2010
480e036b 2011 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("community")));
65e2005b
VK
2012 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("community")));
2013
480e036b 2014 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("usm_auth_password")));
65e2005b
VK
2015 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("usm_auth_password")));
2016
480e036b 2017 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("usm_priv_password")));
65e2005b
VK
2018 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("usm_priv_password")));
2019
480e036b 2020 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("snmp_oid")));
65e2005b
VK
2021 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("snmp_oid")));
2022
480e036b 2023 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("secret")));
65e2005b
VK
2024 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("secret")));
2025
480e036b 2026 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("agent_version")));
65e2005b
VK
2027 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("agent_version")));
2028
480e036b 2029 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("platform_name")));
65e2005b
VK
2030 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("platform_name")));
2031
480e036b 2032 CHK_EXEC(SetColumnNullable(_T("nodes"), _T("uname")));
65e2005b
VK
2033 CHK_EXEC(ConvertStrings(_T("nodes"), _T("id"), _T("uname")));
2034
480e036b 2035 CHK_EXEC(SetColumnNullable(_T("items"), _T("name")));
65e2005b
VK
2036 CHK_EXEC(ConvertStrings(_T("items"), _T("item_id"), _T("name")));
2037
480e036b 2038 CHK_EXEC(SetColumnNullable(_T("items"), _T("description")));
65e2005b
VK
2039 CHK_EXEC(ConvertStrings(_T("items"), _T("item_id"), _T("description")));
2040
480e036b 2041 CHK_EXEC(SetColumnNullable(_T("items"), _T("transformation")));
65e2005b
VK
2042 CHK_EXEC(ConvertStrings(_T("items"), _T("item_id"), _T("transformation")));
2043
480e036b 2044 CHK_EXEC(SetColumnNullable(_T("items"), _T("instance")));
65e2005b
VK
2045 CHK_EXEC(ConvertStrings(_T("items"), _T("item_id"), _T("instance")));
2046
2047 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='217' WHERE var_name='SchemaVersion'")));
2048 return TRUE;
2049}
2050
480e036b
VK
2051/**
2052 * Upgrade from V215 to V216
2053 */
3f7c0fe4
VK
2054static BOOL H_UpgradeFromV215(int currVersion, int newVersion)
2055{
480e036b 2056 CHK_EXEC(SetColumnNullable(_T("ap_common"), _T("description")));
3f7c0fe4
VK
2057 CHK_EXEC(ConvertStrings(_T("ap_common"), _T("id"), _T("description")));
2058
a6c1f07f
VK
2059 if (g_iSyntax != DB_SYNTAX_SQLITE)
2060 CHK_EXEC(SQLQuery(_T("ALTER TABLE ap_config_files DROP COLUMN file_name")));
2061
480e036b 2062 CHK_EXEC(SetColumnNullable(_T("ap_config_files"), _T("file_content")));
a30fa363
VK
2063 CHK_EXEC(ConvertStrings(_T("ap_config_files"), _T("policy_id"), _T("file_content")));
2064
3f7c0fe4
VK
2065 CHK_EXEC(SQLQuery(_T("ALTER TABLE object_properties ADD guid varchar(36)")));
2066
2067 // Generate GUIDs for all objects
2068 DB_RESULT hResult = SQLSelect(_T("SELECT object_id FROM object_properties"));
2069 if (hResult != NULL)
2070 {
2071 int count = DBGetNumRows(hResult);
2072 for(int i = 0; i < count; i++)
2073 {
2074 uuid_t guid;
2075 TCHAR query[256], buffer[64];
2076
2077 uuid_generate(guid);
2078 _sntprintf(query, 256, _T("UPDATE object_properties SET guid='%s' WHERE object_id=%d"),
2079 uuid_to_string(guid, buffer), DBGetFieldULong(hResult, i, 0));
2080 CHK_EXEC(SQLQuery(query));
2081 }
2082 DBFreeResult(hResult);
2083 }
2084
2085 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='216' WHERE var_name='SchemaVersion'")));
2086 return TRUE;
2087}
2088
2089
021dcda7
VK
2090//
2091// Upgrade from V214 to V215
2092//
2093
2094static BOOL H_UpgradeFromV214(int currVersion, int newVersion)
2095{
2096 CHK_EXEC(CreateTable(_T("CREATE TABLE network_maps (")
2097 _T("id integer not null,")
2098 _T("map_type integer not null,")
2099 _T("layout integer not null,")
2100 _T("seed integer not null,")
2101 _T("background integer not null,")
2102 _T("PRIMARY KEY(id))")));
2103
2104 CHK_EXEC(CreateTable(_T("CREATE TABLE network_map_elements (")
2105 _T("map_id integer not null,")
2106 _T("element_id integer not null,")
2107 _T("element_type integer not null,")
2108 _T("element_data $SQL:TEXT not null,")
2109 _T("PRIMARY KEY(map_id,element_id))")));
2110
cce8f214
VK
2111 CHK_EXEC(CreateTable(_T("CREATE TABLE network_map_links (")
2112 _T("map_id integer not null,")
2113 _T("element1 integer not null,")
2114 _T("element2 integer not null,")
2115 _T("link_type integer not null,")
2116 _T("link_name varchar(255) null,")
2117 _T("connector_name1 varchar(255) null,")
2118 _T("connector_name2 varchar(255) null,")
2119 _T("PRIMARY KEY(map_id,element1,element2))")));
2120
021dcda7
VK
2121 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='215' WHERE var_name='SchemaVersion'")));
2122 return TRUE;
2123}
2124
480e036b
VK
2125/**
2126 * Upgrade from V213 to V214
2127 */
50da2d20
VK
2128static BOOL H_UpgradeFromV213(int currVersion, int newVersion)
2129{
480e036b 2130 CHK_EXEC(SetColumnNullable(_T("script_library"), _T("script_code")));
50da2d20
VK
2131 CHK_EXEC(ConvertStrings(_T("script_library"), _T("script_id"), _T("script_code")));
2132
480e036b 2133 CHK_EXEC(SetColumnNullable(_T("raw_dci_values"), _T("raw_value")));
50da2d20
VK
2134 CHK_EXEC(ConvertStrings(_T("raw_dci_values"), _T("item_id"), _T("raw_value")));
2135
2136 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='CREATE TABLE idata_%d (item_id integer not null,idata_timestamp integer not null,idata_value varchar(255) null)' WHERE var_name='IDataTableCreationCommand'")));
2137
2138 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM nodes"));
2139 if (hResult != NULL)
2140 {
2141 int count = DBGetNumRows(hResult);
2142 for(int i = 0; i < count; i++)
2143 {
2144 TCHAR table[32];
2145
2146 DWORD nodeId = DBGetFieldULong(hResult, i, 0);
2147 _sntprintf(table, 32, _T("idata_%d"), nodeId);
480e036b 2148 CHK_EXEC(SetColumnNullable(table, _T("idata_value")));
50da2d20
VK
2149 }
2150 DBFreeResult(hResult);
2151 }
2152
2153 // Convert values for string DCIs from # encoded to normal form
2154 hResult = SQLSelect(_T("SELECT node_id,item_id FROM items WHERE datatype=4"));
2155 if (hResult != NULL)
2156 {
2157 int count = DBGetNumRows(hResult);
2158 for(int i = 0; i < count; i++)
2159 {
2160 TCHAR query[512];
2161
2162 DWORD nodeId = DBGetFieldULong(hResult, i, 0);
2163 DWORD dciId = DBGetFieldULong(hResult, i, 1);
2164
035a4d73 2165 if (IsNodeExist(nodeId))
50da2d20 2166 {
3f7c0fe4 2167 _sntprintf(query, 512, _T("SELECT idata_timestamp,idata_value FROM idata_%d WHERE item_id=%d AND idata_value LIKE '%%#%%'"), nodeId, dciId);
035a4d73
VK
2168 DB_RESULT hData = SQLSelect(query);
2169 if (hData != NULL)
50da2d20 2170 {
035a4d73
VK
2171 int valueCount = DBGetNumRows(hData);
2172 for(int j = 0; j < valueCount; j++)
2173 {
2174 TCHAR buffer[MAX_DB_STRING];
2175
2176 LONG ts = DBGetFieldLong(hData, j, 0);
2177 DBGetField(hData, j, 1, buffer, MAX_DB_STRING);
3f7c0fe4
VK
2178 DecodeSQLString(buffer);
2179
2180 _sntprintf(query, 512, _T("UPDATE idata_%d SET idata_value=%s WHERE item_id=%d AND idata_timestamp=%ld"),
2181 nodeId, (const TCHAR *)DBPrepareString(g_hCoreDB, buffer), dciId, (long)ts);
2182 CHK_EXEC(SQLQuery(query));
035a4d73
VK
2183 }
2184 DBFreeResult(hData);
50da2d20 2185 }
50da2d20
VK
2186 }
2187 }
2188 DBFreeResult(hResult);
2189 }
2190
2191 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='214' WHERE var_name='SchemaVersion'")));
50da2d20
VK
2192 return TRUE;
2193}
2194
480e036b
VK
2195/**
2196 * Upgrade from V212 to V213
2197 */
f57209fd
VK
2198static BOOL H_UpgradeFromV212(int currVersion, int newVersion)
2199{
480e036b
VK
2200 CHK_EXEC(SetColumnNullable(_T("items"), _T("custom_units_name")));
2201 CHK_EXEC(SetColumnNullable(_T("items"), _T("perftab_settings")));
f57209fd
VK
2202
2203 CHK_EXEC(ConvertStrings(_T("items"), _T("item_id"), _T("custom_units_name")));
2204 CHK_EXEC(ConvertStrings(_T("items"), _T("item_id"), _T("perftab_settings")));
2205
2206 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='213' WHERE var_name='SchemaVersion'")));
2207
2208 return TRUE;
2209}
2210
480e036b
VK
2211/**
2212 * Upgrade from V211 to V212
2213 */
1024e962
VK
2214static BOOL H_UpgradeFromV211(int currVersion, int newVersion)
2215{
480e036b
VK
2216 CHK_EXEC(SetColumnNullable(_T("snmp_trap_cfg"), _T("snmp_oid")));
2217 CHK_EXEC(SetColumnNullable(_T("snmp_trap_cfg"), _T("user_tag")));
2218 CHK_EXEC(SetColumnNullable(_T("snmp_trap_cfg"), _T("description")));
1024e962
VK
2219
2220 CHK_EXEC(ConvertStrings(_T("snmp_trap_cfg"), _T("trap_id"), _T("user_tag")));
2221 CHK_EXEC(ConvertStrings(_T("snmp_trap_cfg"), _T("trap_id"), _T("description")));
2222
2223 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='212' WHERE var_name='SchemaVersion'")));
2224
2225 return TRUE;
2226}
2227
480e036b
VK
2228/**
2229 * Upgrade from V210 to V211
2230 */
a8f60046
VK
2231static BOOL H_UpgradeFromV210(int currVersion, int newVersion)
2232{
2233 static TCHAR batch[] =
2234 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
2235 _T(" (53,'SYS_DCI_UNSUPPORTED',2,1,'Status of DCI %1 (%5: %2) changed to UNSUPPORTED',")
2236 _T("'Generated when DCI status changed to UNSUPPORTED.#0D#0AParameters:#0D#0A")
2237 _T(" 1) DCI ID#0D#0A 2) DCI Name#0D#0A 3) DCI Description#0D#0A 4) DCI Origin code#0D#0A 5) DCI Origin name')\n")
2238 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
2239 _T(" (54,'SYS_DCI_DISABLED',1,1,'Status of DCI %1 (%5: %2) changed to DISABLED',")
2240 _T("'Generated when DCI status changed to DISABLED.#0D#0AParameters:#0D#0A")
2241 _T(" 1) DCI ID#0D#0A 2) DCI Name#0D#0A 3) DCI Description#0D#0A 4) DCI Origin code#0D#0A 5) DCI Origin name')\n")
2242 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
2243 _T(" (55,'SYS_DCI_ACTIVE',0,1,'Status of DCI %1 (%5: %2) changed to ACTIVE',")
2244 _T("'Generated when DCI status changed to ACTIVE.#0D#0AParameters:#0D#0A")
2245 _T(" 1) DCI ID#0D#0A 2) DCI Name#0D#0A 3) DCI Description#0D#0A 4) DCI Origin code#0D#0A 5) DCI Origin name')\n")
2246 _T("<END>");
2247
1024e962
VK
2248 CHK_EXEC(SQLBatch(batch));
2249 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='211' WHERE var_name='SchemaVersion'")));
a8f60046
VK
2250
2251 return TRUE;
2252}
2253
2254
43c1be79
VK
2255//
2256// Upgrade from V209 to V210
2257//
2258
2259static BOOL H_UpgradeFromV209(int currVersion, int newVersion)
2260{
2261 if (!SQLQuery(_T("DELETE FROM metadata WHERE var_name like 'IDataIndexCreationCommand_%'")))
2262 if (!g_bIgnoreErrors)
2263 return FALSE;
2264
2265 const TCHAR *query;
2266 switch(g_iSyntax)
2267 {
2268 case DB_SYNTAX_PGSQL:
2269 query = _T("INSERT INTO metadata (var_name,var_value) VALUES ('IDataIndexCreationCommand_0','CREATE INDEX idx_idata_%d_timestamp_id ON idata_%d(idata_timestamp,item_id)')");
2270 break;
2271 case DB_SYNTAX_MSSQL:
2272 query = _T("INSERT INTO metadata (var_name,var_value) VALUES ('IDataIndexCreationCommand_0','CREATE CLUSTERED INDEX idx_idata_%d_id_timestamp ON idata_%d(item_id,idata_timestamp)')");
2273 break;
2274 default:
2275 query = _T("INSERT INTO metadata (var_name,var_value) VALUES ('IDataIndexCreationCommand_0','CREATE INDEX idx_idata_%d_id_timestamp ON idata_%d(item_id,idata_timestamp)')");
2276 break;
2277 }
2278
2279 if (!SQLQuery(query))
2280 if (!g_bIgnoreErrors)
2281 return FALSE;
2282
2283 ReindexIData();
2284
2285 if (!SQLQuery(_T("UPDATE metadata SET var_value='210' WHERE var_name='SchemaVersion'")))
2286 if (!g_bIgnoreErrors)
2287 return FALSE;
2288
2289 return TRUE;
2290}
2291
2292
ab185583
VK
2293//
2294// Upgrade from V208 to V209
2295//
2296
2297static BOOL H_UpgradeFromV208(int currVersion, int newVersion)
2298{
2299 static TCHAR batch[] =
2300 _T("ALTER TABLE users ADD auth_failures integer\n")
2301 _T("ALTER TABLE users ADD last_passwd_change integer\n")
2302 _T("ALTER TABLE users ADD min_passwd_length integer\n")
2303 _T("ALTER TABLE users ADD disabled_until integer\n")
2304 _T("ALTER TABLE users ADD last_login integer\n")
2305 _T("ALTER TABLE users ADD password_history $SQL:TEXT\n")
2306 _T("UPDATE users SET auth_failures=0,last_passwd_change=0,min_passwd_length=-1,disabled_until=0,last_login=0\n")
2307 _T("<END>");
2308
2309 if (!SQLBatch(batch))
2310 if (!g_bIgnoreErrors)
2311 return FALSE;
2312
2313 if (!CreateConfigParam(_T("PasswordHistoryLength"), _T("0"), 1, 0))
2314 if (!g_bIgnoreErrors)
2315 return FALSE;
2316
0f1ddb74 2317 if (!CreateConfigParam(_T("IntruderLockoutThreshold"), _T("0"), 1, 0))
ab185583
VK
2318 if (!g_bIgnoreErrors)
2319 return FALSE;
2320
2321 if (!CreateConfigParam(_T("IntruderLockoutTime"), _T("30"), 1, 0))
2322 if (!g_bIgnoreErrors)
2323 return FALSE;
2324
2325 if (!CreateConfigParam(_T("MinPasswordLength"), _T("0"), 1, 0))
2326 if (!g_bIgnoreErrors)
2327 return FALSE;
2328
2329 if (!CreateConfigParam(_T("PasswordComplexity"), _T("0"), 1, 0))
2330 if (!g_bIgnoreErrors)
2331 return FALSE;
2332
2333 if (!CreateConfigParam(_T("PasswordExpiration"), _T("0"), 1, 0))
2334 if (!g_bIgnoreErrors)
2335 return FALSE;
2336
2337 if (!CreateConfigParam(_T("BlockInactiveUserAccounts"), _T("0"), 1, 0))
2338 if (!g_bIgnoreErrors)
2339 return FALSE;
2340
2341 if (!SQLQuery(_T("UPDATE metadata SET var_value='209' WHERE var_name='SchemaVersion'")))
2342 if (!g_bIgnoreErrors)
2343 return FALSE;
2344
2345 return TRUE;
2346}
2347
2348
80d3565a
VK
2349//
2350// Upgrade from V207 to V208
2351//
2352
2353static BOOL H_UpgradeFromV207(int currVersion, int newVersion)
2354{
2355 if (!SQLQuery(_T("ALTER TABLE items ADD system_tag varchar(255)")))
2356 if (!g_bIgnoreErrors)
2357 return FALSE;
2358
2359 if (!SQLQuery(_T("UPDATE metadata SET var_value='208' WHERE var_name='SchemaVersion'")))
2360 if (!g_bIgnoreErrors)
2361 return FALSE;
2362
2363 return TRUE;
2364}
2365
2366
5ab2f1ab
VK
2367//
2368// Upgrade from V206 to V207
2369//
2370
2371static BOOL H_UpgradeFromV206(int currVersion, int newVersion)
2372{
2373 if (!CreateConfigParam(_T("RADIUSSecondaryServer"), _T("none"), 1, 0))
2374 if (!g_bIgnoreErrors)
2375 return FALSE;
2376
2377 if (!CreateConfigParam(_T("RADIUSSecondarySecret"), _T("netxms"), 1, 0))
2378 if (!g_bIgnoreErrors)
2379 return FALSE;
2380
2381 if (!CreateConfigParam(_T("RADIUSSecondaryPort"), _T("1645"), 1, 0))
2382 if (!g_bIgnoreErrors)
2383 return FALSE;
2384
2385 if (!CreateConfigParam(_T("ExternalAuditServer"), _T("none"), 1, 1))
2386 if (!g_bIgnoreErrors)
2387 return FALSE;
2388
2389 if (!CreateConfigParam(_T("ExternalAuditPort"), _T("514"), 1, 1))
2390 if (!g_bIgnoreErrors)
2391 return FALSE;
2392
2393 if (!CreateConfigParam(_T("ExternalAuditFacility"), _T("13"), 1, 1))
2394 if (!g_bIgnoreErrors)
2395 return FALSE;
2396
2397 if (!CreateConfigParam(_T("ExternalAuditSeverity"), _T("5"), 1, 1))
2398 if (!g_bIgnoreErrors)
2399 return FALSE;
2400
2401 if (!CreateConfigParam(_T("ExternalAuditTag"), _T("netxmsd-audit"), 1, 1))
2402 if (!g_bIgnoreErrors)
2403 return FALSE;
2404
2405 if (!SQLQuery(_T("UPDATE metadata SET var_value='207' WHERE var_name='SchemaVersion'")))
2406 if (!g_bIgnoreErrors)
2407 return FALSE;
2408
2409 return TRUE;
2410}
2411
2412
ce7565e7
VK
2413//
2414// Upgrade from V205 to V206
2415//
2416
2417static BOOL H_UpgradeFromV205(int currVersion, int newVersion)
2418{
a4743a0f
VK
2419 if (g_iSyntax == DB_SYNTAX_ORACLE)
2420 {
2421 static TCHAR oraBatch[] =
2422 _T("ALTER TABLE audit_log MODIFY message null\n")
2423 _T("ALTER TABLE event_log MODIFY event_message null\n")
2424 _T("ALTER TABLE event_log MODIFY user_tag null\n")
2425 _T("ALTER TABLE syslog MODIFY hostname null\n")
2426 _T("ALTER TABLE syslog MODIFY msg_tag null\n")
2427 _T("ALTER TABLE syslog MODIFY msg_text null\n")
2428 _T("ALTER TABLE snmp_trap_log MODIFY trap_varlist null\n")
2429 _T("<END>");
2430
2431 if (!SQLBatch(oraBatch))
2432 if (!g_bIgnoreErrors)
2433 return FALSE;
2434 }
ce7565e7 2435
a4743a0f
VK
2436 bool clearLogs = GetYesNo(_T("This database upgrade requires log conversion. This can take significant amount of time ")
2437 _T("(up to few hours for large databases). If preserving all log records is not very important, it is ")
2438 _T("recommended to clear logs befor conversion. Clear logs?"));
ce7565e7 2439
a4743a0f
VK
2440 if (clearLogs)
2441 {
2442 if (!SQLQuery(_T("DELETE FROM audit_log")))
2443 if (!g_bIgnoreErrors)
2444 return FALSE;
ce7565e7 2445
a4743a0f
VK
2446 if (!SQLQuery(_T("DELETE FROM event_log")))
2447 if (!g_bIgnoreErrors)
2448 return FALSE;
2449
2450 if (!SQLQuery(_T("DELETE FROM syslog")))
2451 if (!g_bIgnoreErrors)
2452 return FALSE;
2453
2454 if (!SQLQuery(_T("DELETE FROM snmp_trap_log")))
2455 if (!g_bIgnoreErrors)
2456 return FALSE;
2457 }
2458 else
2459 {
2460 // Convert event log
08b214c6 2461 if (!ConvertStrings(_T("event_log"), _T("event_id"), _T("event_message")))
a4743a0f
VK
2462 if (!g_bIgnoreErrors)
2463 return FALSE;
08b214c6 2464 if (!ConvertStrings(_T("event_log"), _T("event_id"), _T("user_tag")))
a4743a0f
VK
2465 if (!g_bIgnoreErrors)
2466 return FALSE;
2467
2468 // Convert audit log
08b214c6 2469 if (!ConvertStrings(_T("audit_log"), _T("record_id"), _T("message")))
a4743a0f
VK
2470 if (!g_bIgnoreErrors)
2471 return FALSE;
2472
2473 // Convert syslog
08b214c6 2474 if (!ConvertStrings(_T("syslog"), _T("msg_id"), _T("msg_text")))
a4743a0f
VK
2475 if (!g_bIgnoreErrors)
2476 return FALSE;
2477
2478 // Convert SNMP trap log
08b214c6 2479 if (!ConvertStrings(_T("snmp_trap_log"), _T("trap_id"), _T("trap_varlist")))
a4743a0f
VK
2480 if (!g_bIgnoreErrors)
2481 return FALSE;
2482 }
ce7565e7
VK
2483
2484 if (!SQLQuery(_T("UPDATE metadata SET var_value='206' WHERE var_name='SchemaVersion'")))
2485 if (!g_bIgnoreErrors)
2486 return FALSE;
2487
2488 return TRUE;
2489}
2490
2491
df8a4ca2
VK
2492//
2493// Upgrade from V204 to V205
2494//
2495
2496static BOOL H_UpgradeFromV204(int currVersion, int newVersion)
2497{
2498 if (!CreateTable(_T("CREATE TABLE usm_credentials (")
2499 _T("id integer not null,")
2500 _T("user_name varchar(255) not null,")
2501 _T("auth_method integer not null,")
2502 _T("priv_method integer not null,")
2503 _T("auth_password varchar(255),")
2504 _T("priv_password varchar(255),")
2505 _T("PRIMARY KEY(id))")))
2506 if (!g_bIgnoreErrors)
2507 return FALSE;
2508
2509 if (!SQLQuery(_T("UPDATE metadata SET var_value='205' WHERE var_name='SchemaVersion'")))
2510 if (!g_bIgnoreErrors)
2511 return FALSE;
2512
2513 return TRUE;
2514}
2515
2516
e2babedf
VK
2517//
2518// Upgrade from V203 to V204
2519//
2520
2521static BOOL H_UpgradeFromV203(int currVersion, int newVersion)
2522{
2523 static TCHAR batch[] =
2524 _T("ALTER TABLE object_properties ADD location_type integer\n")
2525 _T("ALTER TABLE object_properties ADD latitude varchar(20)\n")
2526 _T("ALTER TABLE object_properties ADD longitude varchar(20)\n")
2527 _T("UPDATE object_properties SET location_type=0\n")
2528 _T("ALTER TABLE object_properties DROP COLUMN image_id\n")
2529 _T("<END>");
2530
2531 if (!SQLBatch(batch))
2532 if (!g_bIgnoreErrors)
2533 return FALSE;
2534
2535 if (!CreateConfigParam(_T("ConnectionPoolBaseSize"), _T("5"), 1, 1))
2536 if (!g_bIgnoreErrors)
2537 return FALSE;
2538
2539 if (!CreateConfigParam(_T("ConnectionPoolMaxSize"), _T("20"), 1, 1))
2540 if (!g_bIgnoreErrors)
2541 return FALSE;
2542
2543 if (!CreateConfigParam(_T("ConnectionPoolCooldownTime"), _T("300"), 1, 1))
2544 if (!g_bIgnoreErrors)
2545 return FALSE;
2546
a4743a0f
VK
2547 if (g_iSyntax == DB_SYNTAX_ORACLE)
2548 {
2549 if (!SQLQuery(_T("ALTER TABLE object_properties MODIFY comments null\n")))
2550 if (!g_bIgnoreErrors)
2551 return FALSE;
2552 }
2553
08b214c6 2554 if (!ConvertStrings(_T("object_properties"), _T("object_id"), _T("comments")))
e2babedf
VK
2555 if (!g_bIgnoreErrors)
2556 return FALSE;
2557
2558 if (!SQLQuery(_T("UPDATE metadata SET var_value='204' WHERE var_name='SchemaVersion'")))
2559 if (!g_bIgnoreErrors)
2560 return FALSE;
2561
2562 return TRUE;
2563}
2564
2565
7cda0d53
VK
2566//
2567// Upgrade from V202 to V203
2568//
2569
2570static BOOL H_UpgradeFromV202(int currVersion, int newVersion)
2571{
2572 static TCHAR batch[] =
2573 _T("INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description,confirmation_text)")
2574 _T(" VALUES (20,'&Info->Topology table (LLDP)',2,'Topology Table',1,' ','Show topology table (LLDP)','#00')\n")
2575 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr)")
2576 _T(" VALUES (20,0,'Chassis ID','.1.0.8802.1.1.2.1.4.1.1.5',0,0)\n")
2577 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr)")
2578 _T(" VALUES (20,1,'Local port','.1.0.8802.1.1.2.1.4.1.1.2',5,0)\n")
2579 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr)")
2580 _T(" VALUES (20,2,'System name','.1.0.8802.1.1.2.1.4.1.1.9',0,0)\n")
2581 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr)")
2582 _T(" VALUES (20,3,'System description','.1.0.8802.1.1.2.1.4.1.1.10',0,0)\n")
2583 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr)")
2584 _T(" VALUES (20,4,'Remote port ID','.1.0.8802.1.1.2.1.4.1.1.7',4,0)\n")
2585 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr)")
2586 _T(" VALUES (20,5,'Remote port description','.1.0.8802.1.1.2.1.4.1.1.8',0,0)\n")
2587 _T("INSERT INTO object_tools_acl (tool_id,user_id) VALUES (20,-2147483648)\n")
2588 _T("<END>");
2589
2590 if (!SQLBatch(batch))
2591 if (!g_bIgnoreErrors)
2592 return FALSE;
2593
2594 if (!SQLQuery(_T("UPDATE metadata SET var_value='203' WHERE var_name='SchemaVersion'")))
2595 if (!g_bIgnoreErrors)
2596 return FALSE;
2597
2598 return TRUE;
2599}
2600
2601
643c9dcb
VK
2602//
2603// Upgrade from V201 to V202
2604//
2605
2606static BOOL H_UpgradeFromV201(int currVersion, int newVersion)
2607{
a4743a0f
VK
2608 if (g_iSyntax == DB_SYNTAX_ORACLE)
2609 {
2610 static TCHAR oraBatch[] =
2611 _T("ALTER TABLE alarms MODIFY message null\n")
2612 _T("ALTER TABLE alarms MODIFY alarm_key null\n")
2613 _T("ALTER TABLE alarms MODIFY hd_ref null\n")
2614 _T("<END>");
2615
2616 if (!SQLBatch(oraBatch))
2617 if (!g_bIgnoreErrors)
2618 return FALSE;
2619 }
2620
08b214c6 2621 if (!ConvertStrings(_T("alarms"), _T("alarm_id"), _T("message")))
643c9dcb
VK
2622 if (!g_bIgnoreErrors)
2623 return FALSE;
08b214c6 2624 if (!ConvertStrings(_T("alarms"), _T("alarm_id"), _T("alarm_key")))
643c9dcb
VK
2625 if (!g_bIgnoreErrors)
2626 return FALSE;
08b214c6 2627 if (!ConvertStrings(_T("alarms"), _T("alarm_id"), _T("hd_ref")))
643c9dcb
VK
2628 if (!g_bIgnoreErrors)
2629 return FALSE;
2630
2631 if (!SQLQuery(_T("UPDATE metadata SET var_value='202' WHERE var_name='SchemaVersion'")))
2632 if (!g_bIgnoreErrors)
2633 return FALSE;
2634
2635 return TRUE;
2636}
2637
2638
31cc1924
VK
2639//
2640// Upgrade from V200 to V201
2641//
2642
2643static BOOL H_UpgradeFromV200(int currVersion, int newVersion)
2644{
2645 static TCHAR batch[] =
2646 _T("ALTER TABLE nodes ADD usm_auth_password varchar(127)\n")
2647 _T("ALTER TABLE nodes ADD usm_priv_password varchar(127)\n")
2648 _T("ALTER TABLE nodes ADD usm_methods integer\n")
e8daf6d5 2649 _T("UPDATE nodes SET usm_auth_password='#00',usm_priv_password='#00',usm_methods=0\n")
31cc1924
VK
2650 _T("<END>");
2651
2652 if (!SQLBatch(batch))
2653 if (!g_bIgnoreErrors)
2654 return FALSE;
2655
2656 if (!SQLQuery(_T("UPDATE metadata SET var_value='201' WHERE var_name='SchemaVersion'")))
2657 if (!g_bIgnoreErrors)
2658 return FALSE;
2659
2660 return TRUE;
2661}
2662
2663
45d84f8a
VK
2664//
2665// Upgrade from V92 to V200
31cc1924 2666// or from V93 to V201
e2babedf
VK
2667// or from V94 to V202
2668// or from V95 to V203
ce7565e7
VK
2669// or from V96 to V204
2670// or from V97 to V205
2671// or from V98 to V206
43c1be79 2672// or from V99 to V207
45d84f8a
VK
2673//
2674
e2babedf 2675static BOOL H_UpgradeFromV9x(int currVersion, int newVersion)
45d84f8a
VK
2676{
2677 if (!CreateTable(_T("CREATE TABLE ap_common (")
2678 _T("id integer not null,")
2679 _T("policy_type integer not null,")
2680 _T("version integer not null,")
2681 _T("description $SQL:TEXT not null,")
2682 _T("PRIMARY KEY(id))")))
2683 if (!g_bIgnoreErrors)
2684 return FALSE;
2685
2686 if (!CreateTable(_T("CREATE TABLE ap_bindings (")
2687 _T("policy_id integer not null,")
2688 _T("node_id integer not null,")
2689 _T("PRIMARY KEY(policy_id,node_id))")))
2690 if (!g_bIgnoreErrors)
2691 return FALSE;
2692
2693 if (!CreateTable(_T("CREATE TABLE ap_config_files (")
2694 _T("policy_id integer not null,")
2695 _T("file_name varchar(63) not null,")
2696 _T("file_content $SQL:TEXT not null,")
2697 _T("PRIMARY KEY(policy_id))")))
2698 if (!g_bIgnoreErrors)
2699 return FALSE;
2700
31cc1924
VK
2701 TCHAR query[256];
2702 _sntprintf(query, 256, _T("UPDATE metadata SET var_value='%d' WHERE var_name='SchemaVersion'"), newVersion);
2703 if (!SQLQuery(query))
45d84f8a
VK
2704 if (!g_bIgnoreErrors)
2705 return FALSE;
2706
2707 return TRUE;
2708}
2709
2710
43c1be79 2711//
50da2d20
VK
2712// Upgrade from V100 to V214
2713// or from V101 to V214
2714// or from V102 to V214
2715// or from V103 to V214
2716// or from V104 to V214
43c1be79
VK
2717//
2718
a8f60046 2719static BOOL H_UpgradeFromV10x(int currVersion, int newVersion)
43c1be79 2720{
50da2d20 2721 if (!H_UpgradeFromV9x(currVersion, 207))
43c1be79
VK
2722 return FALSE;
2723
2724 // Now database at V207 level
50da2d20
VK
2725 // V100 already has changes V209 -> V210, but missing V207 -> V209 and V210 -> V214 changes
2726 // V101 already has changes V209 -> V211, but missing V207 -> V209 and V211 -> V214 changes
2727 // V102 already has changes V209 -> V212, but missing V207 -> V209 and V212 -> V214 changes
2728 // V103 already has changes V209 -> V213, but missing V207 -> V209 and V213 -> V214 changes
2729 // V104 already has changes V209 -> V214, but missing V207 -> V209 changes
43c1be79
VK
2730
2731 if (!H_UpgradeFromV207(207, 208))
2732 return FALSE;
2733
a6c1f07f 2734 if (!H_UpgradeFromV208(208, 209))
43c1be79
VK
2735 return FALSE;
2736
a8f60046 2737 if (currVersion == 100)
50da2d20
VK
2738 if (!H_UpgradeFromV210(210, 211))
2739 return FALSE;
a8f60046 2740
1024e962 2741 if (currVersion < 102)
50da2d20
VK
2742 if (!H_UpgradeFromV211(211, 212))
2743 return FALSE;
1024e962 2744
f57209fd 2745 if (currVersion < 103)
50da2d20
VK
2746 if (!H_UpgradeFromV212(212, 213))
2747 return FALSE;
2748
2749 if (currVersion < 104)
2750 if (!H_UpgradeFromV213(213, 214))
2751 return FALSE;
f57209fd 2752
65e2005b
VK
2753 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='214' WHERE var_name='SchemaVersion'")));
2754 return TRUE;
2755}
2756
2757
2758//
2759// Upgrade from V105 to V217
2760//
2761
2762static BOOL H_UpgradeFromV105(int currVersion, int newVersion)
2763{
2764 if (!H_UpgradeFromV10x(currVersion, 214))
2765 return FALSE;
2766
2767 // V105 already have V216 -> V217 changes, but missing V207 -> V209 and V214 -> V216 changes
2768 if (!H_UpgradeFromV214(214, 215))
2769 return FALSE;
2770
2771 if (!H_UpgradeFromV215(215, 216))
2772 return FALSE;
43c1be79 2773
65e2005b 2774 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='217' WHERE var_name='SchemaVersion'")));
43c1be79
VK
2775 return TRUE;
2776}
2777
2778
6e53f004
VK
2779//
2780// Upgrade from V91 to V92
2781//
2782
31cc1924 2783static BOOL H_UpgradeFromV91(int currVersion, int newVersion)
6e53f004
VK
2784{
2785 static TCHAR batch[] =
2786 _T("DROP TABLE images\n")
2787 _T("DROP TABLE default_images\n")
2788 _T("<END>");
2789
2790 if (!SQLBatch(batch))
2791 if (!g_bIgnoreErrors)
2792 return FALSE;
2793
2794 if (!SQLQuery(_T("UPDATE metadata SET var_value='92' WHERE var_name='SchemaVersion'")))
2795 if (!g_bIgnoreErrors)
2796 return FALSE;
2797
2798 return TRUE;
2799}
2800
2801
c45e0213
VK
2802//
2803// Upgrade from V90 to V91
2804//
2805
31cc1924 2806static BOOL H_UpgradeFromV90(int currVersion, int newVersion)
c45e0213
VK
2807{
2808 if (!CreateTable(_T("CREATE TABLE userdb_custom_attributes (")
2809 _T("object_id integer not null,")
2810 _T("attr_name varchar(255) not null,")
2811 _T("attr_value $SQL:TEXT not null,")
2812 _T("PRIMARY KEY(object_id,attr_name))")))
2813 if (!g_bIgnoreErrors)
2814 return FALSE;
2815
2816 if (!SQLQuery(_T("UPDATE metadata SET var_value='91' WHERE var_name='SchemaVersion'")))
2817 if (!g_bIgnoreErrors)
2818 return FALSE;
2819
2820 return TRUE;
2821}
2822
2823
4262c0dc
VK
2824//
2825// Upgrade from V89 to V90
2826//
2827
31cc1924 2828static BOOL H_UpgradeFromV89(int currVersion, int newVersion)
4262c0dc
VK
2829{
2830 static TCHAR m_szBatch[] =
2831 _T("ALTER TABLE items ADD base_units integer\n")
2832 _T("ALTER TABLE items ADD unit_multiplier integer\n")
2833 _T("ALTER TABLE items ADD custom_units_name varchar(63)\n")
2834 _T("ALTER TABLE items ADD perftab_settings $SQL:TEXT\n")
2835 _T("UPDATE items SET base_units=0,unit_multiplier=1,custom_units_name='#00',perftab_settings='#00'\n")
2836 _T("<END>");
2837
2838 if (!SQLBatch(m_szBatch))
2839 if (!g_bIgnoreErrors)
2840 return FALSE;
2841
2842 if (!SQLQuery(_T("UPDATE metadata SET var_value='90' WHERE var_name='SchemaVersion'")))
2843 if (!g_bIgnoreErrors)
2844 return FALSE;
2845
2846 return TRUE;
2847}
2848
2849
2850//
2851// Upgrade from V88 to V89
2852//
2853
31cc1924 2854static BOOL H_UpgradeFromV88(int currVersion, int newVersion)
4262c0dc
VK
2855{
2856 static TCHAR m_szBatch[] =
2857 _T("ALTER TABLE containers ADD enable_auto_bind integer\n")
2858 _T("ALTER TABLE containers ADD auto_bind_filter $SQL:TEXT\n")
2859 _T("UPDATE containers SET enable_auto_bind=0,auto_bind_filter='#00'\n")
2860 _T("ALTER TABLE cluster_resources ADD current_owner integer\n")
2861 _T("UPDATE cluster_resources SET current_owner=0\n")
2862 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (")
2863 _T("52,'SYS_DB_QUERY_FAILED',4,1,'Database query failed (Query: %1; Error: %2)',")
2864 _T("'Generated when SQL query to backend database failed.#0D#0A")
2865 _T("Parameters:#0D#0A 1) Query#0D#0A 2) Error message')\n")
2866 _T("<END>");
2867
2868 if (!SQLBatch(m_szBatch))
2869 if (!g_bIgnoreErrors)
2870 return FALSE;
2871
2872 if (!SQLQuery(_T("UPDATE metadata SET var_value='89' WHERE var_name='SchemaVersion'")))
2873 if (!g_bIgnoreErrors)
2874 return FALSE;
2875
2876 return TRUE;
2877}
2878
2879
2880//
2881// Upgrade from V87 to V88
2882//
2883
31cc1924 2884static BOOL H_UpgradeFromV87(int currVersion, int newVersion)
4262c0dc
VK
2885{
2886 static TCHAR m_szBatch[] =
2887 _T("ALTER TABLE templates ADD enable_auto_apply integer\n")
2888 _T("ALTER TABLE templates ADD apply_filter $SQL:TEXT\n")
2889 _T("UPDATE templates SET enable_auto_apply=0,apply_filter='#00'\n")
2890 _T("<END>");
2891
2892 if (!SQLBatch(m_szBatch))
2893 if (!g_bIgnoreErrors)
2894 return FALSE;
2895
2896 if (!SQLQuery(_T("UPDATE metadata SET var_value='88' WHERE var_name='SchemaVersion'")))
2897 if (!g_bIgnoreErrors)
2898 return FALSE;
2899
2900 return TRUE;
2901}
2902
2903
28f5b9a4
VK
2904//
2905// Upgrade from V86 to V87
2906//
2907
2908static BOOL MoveConfigToMetadata(const TCHAR *cfgVar, const TCHAR *mdVar)
2909{
2910 TCHAR query[1024], buffer[256];
2911 DB_RESULT hResult;
2912 BOOL success;
2913
2914 _sntprintf(query, 1024, _T("SELECT var_value FROM config WHERE var_name='%s'"), cfgVar);
2915 hResult = SQLSelect(query);
2916 if (hResult != NULL)
2917 {
2918 if (DBGetNumRows(hResult) > 0)
2919 {
2920 DBGetField(hResult, 0, 0, buffer, 256);
2921 DecodeSQLString(buffer);
2922 _sntprintf(query, 1024, _T("INSERT INTO metadata (var_name,var_value) VALUES ('%s','%s')"),
2923 mdVar, buffer);
2924 DBFreeResult(hResult);
2925 success = SQLQuery(query);
2926 if (success)
2927 {
2928 _sntprintf(query, 1024, _T("DELETE FROM config WHERE var_name='%s'"), cfgVar);
2929 success = SQLQuery(query);
2930 }
2931 }
2932 else
2933 {
2934 success = TRUE; // Variable missing in 'config' table, nothing to move
2935 }
2936 }
2937 else
2938 {
2939 success = FALSE;
2940 }
2941 return success;
2942}
2943
31cc1924 2944static BOOL H_UpgradeFromV86(int currVersion, int newVersion)
28f5b9a4
VK
2945{
2946 if (!CreateTable(_T("CREATE TABLE metadata (")
2947 _T("var_name varchar(63) not null,")
2948 _T("var_value varchar(255) not null,")
2949 _T("PRIMARY KEY(var_name))")))
2950 if (!g_bIgnoreErrors)
2951 return FALSE;
2952
2953 if (!MoveConfigToMetadata(_T("DBFormatVersion"), _T("SchemaVersion")))
2954 if (!g_bIgnoreErrors)
2955 return FALSE;
2956
2957 if (!MoveConfigToMetadata(_T("DBSyntax"), _T("Syntax")))
2958 if (!g_bIgnoreErrors)
2959 return FALSE;
2960
2961 if (!MoveConfigToMetadata(_T("IDataTableCreationCommand"), _T("IDataTableCreationCommand")))
2962 if (!g_bIgnoreErrors)
2963 return FALSE;
2964
2965 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_0"), _T("IDataIndexCreationCommand_0")))
2966 if (!g_bIgnoreErrors)
2967 return FALSE;
2968
2969 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_1"), _T("IDataIndexCreationCommand_1")))
2970 if (!g_bIgnoreErrors)
2971 return FALSE;
2972
2973 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_2"), _T("IDataIndexCreationCommand_2")))
2974 if (!g_bIgnoreErrors)
2975 return FALSE;
2976
2977 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_3"), _T("IDataIndexCreationCommand_3")))
2978 if (!g_bIgnoreErrors)
2979 return FALSE;
2980
2981 if (!SQLQuery(_T("UPDATE metadata SET var_value='87' WHERE var_name='SchemaVersion'")))
2982 if (!g_bIgnoreErrors)
2983 return FALSE;
2984
2985 return TRUE;
2986}
2987
2988
4c4c9b03
VK
2989//
2990// Upgrade from V85 to V86
2991//
2992
31cc1924 2993static BOOL H_UpgradeFromV85(int currVersion, int newVersion)
4c4c9b03
VK
2994{
2995 static TCHAR m_szBatch[] =
2996 _T("DROP TABLE alarm_grops\n")
2997 _T("DROP TABLE alarm_group_map\n")
47912c44
VK
2998 _T("DROP TABLE alarm_change_log\n")
2999 _T("DROP TABLE lpp\n")
3000 _T("DROP TABLE lpp_associations\n")
3001 _T("DROP TABLE lpp_rulesets\n")
3002 _T("DROP TABLE lpp_rules\n")
3003 _T("DROP TABLE lpp_groups\n")
4c4c9b03
VK
3004 _T("<END>");
3005
3006 if (!SQLBatch(m_szBatch))
3007 if (!g_bIgnoreErrors)
3008 return FALSE;
3009
3010 if (!SQLQuery(_T("UPDATE config SET var_value='86' WHERE var_name='DBFormatVersion'")))
3011 if (!g_bIgnoreErrors)
3012 return FALSE;
3013
3014 return TRUE;
3015}
3016
3017
5039dede
AK
3018//
3019// Upgrade from V84 to V85
3020//
3021
31cc1924 3022static BOOL H_UpgradeFromV84(int currVersion, int newVersion)
5039dede
AK
3023{
3024 static TCHAR m_szBatch[] =
3025 _T("ALTER TABLE nodes ADD use_ifxtable integer\n")
3026 _T("UPDATE nodes SET use_ifxtable=0\n")
3027 _T("<END>");
3028
3029 if (!SQLBatch(m_szBatch))
3030 if (!g_bIgnoreErrors)
3031 return FALSE;
3032
3033 if (!CreateConfigParam(_T("UseIfXTable"), _T("1"), 1, 0))
3034 if (!g_bIgnoreErrors)
3035 return FALSE;
3036
3037 if (!CreateConfigParam(_T("SMTPRetryCount"), _T("1"), 1, 0))
3038 if (!g_bIgnoreErrors)
3039 return FALSE;
3040
3041 if (!SQLQuery(_T("UPDATE config SET var_value='85' WHERE var_name='DBFormatVersion'")))
3042 if (!g_bIgnoreErrors)
3043 return FALSE;
3044
3045 return TRUE;
3046}
3047
3048
3049//
3050// Upgrade from V83 to V84
3051//
3052
31cc1924 3053static BOOL H_UpgradeFromV83(int currVersion, int newVersion)
5039dede
AK
3054{
3055 if (!CreateConfigParam(_T("EnableAgentRegistration"), _T("1"), 1, 0))
3056 if (!g_bIgnoreErrors)
3057 return FALSE;
3058
3059 if (!CreateConfigParam(_T("AnonymousFileAccess"), _T("0"), 1, 0))
3060 if (!g_bIgnoreErrors)
3061 return FALSE;
3062
3063 if (!CreateConfigParam(_T("EnableISCListener"), _T("0"), 1, 1))
3064 if (!g_bIgnoreErrors)
3065 return FALSE;
3066
3067 if (!CreateConfigParam(_T("ReceiveForwardedEvents"), _T("0"), 1, 0))
3068 if (!g_bIgnoreErrors)
3069 return FALSE;
3070
3071 if (!SQLQuery(_T("UPDATE config SET var_value='84' WHERE var_name='DBFormatVersion'")))
3072 if (!g_bIgnoreErrors)
3073 return FALSE;
3074
3075 return TRUE;
3076}
3077
3078
3079//
3080// Upgrade from V82 to V83
3081//
3082
31cc1924 3083static BOOL H_UpgradeFromV82(int currVersion, int newVersion)
5039dede
AK
3084{
3085 // Fix incorrect alarm timeouts
3086 if (!SQLQuery(_T("UPDATE alarms SET timeout=0,timeout_event=43")))
3087 if (!g_bIgnoreErrors)
3088 return FALSE;
3089
3090 if (!SQLQuery(_T("UPDATE config SET var_value='83' WHERE var_name='DBFormatVersion'")))
3091 if (!g_bIgnoreErrors)
3092 return FALSE;
3093
3094 return TRUE;
3095}
3096
3097
3098//
3099// Upgrade from V81 to V82
3100//
3101
31cc1924 3102static BOOL H_UpgradeFromV81(int currVersion, int newVersion)
5039dede
AK
3103{
3104 if (!CreateTable(_T("CREATE TABLE config_clob (")
3105 _T("var_name varchar(63) not null,")
3106 _T("var_value $SQL:TEXT not null,")
3107 _T("PRIMARY KEY(var_name))")))
3108 if (!g_bIgnoreErrors)
3109 return FALSE;
3110
3111 if (!SQLQuery(_T("UPDATE config SET var_value='82' WHERE var_name='DBFormatVersion'")))
3112 if (!g_bIgnoreErrors)
3113 return FALSE;
3114
3115 return TRUE;
3116}
3117
3118
3119//
3120// Upgrade from V80 to V81
3121//
3122
31cc1924 3123static BOOL H_UpgradeFromV80(int currVersion, int newVersion)
5039dede
AK
3124{
3125 DB_RESULT hResult;
3126 TCHAR query[1024], buffer[1024];
3127 int i;
3128
3129 // Update dci_schedules table
3130 hResult = SQLSelect(_T("SELECT item_id,schedule FROM dci_schedules"));
3131 if (hResult != NULL)
3132 {
3133 if (!SQLQuery(_T("DROP TABLE dci_schedules")))
3134 if (!g_bIgnoreErrors)
3135 return FALSE;
3136
3137 if (!CreateTable(_T("CREATE TABLE dci_schedules (")
3138 _T("schedule_id integer not null,")
3139 _T("item_id integer not null,")
3140 _T("schedule varchar(255) not null,")
3141 _T("PRIMARY KEY(item_id,schedule_id))")))
3142 if (!g_bIgnoreErrors)
3143 return FALSE;
3144
3145 for(i = 0; i < DBGetNumRows(hResult); i++)
3146 {
3147 _sntprintf(query, 1024, _T("INSERT INTO dci_schedules (item_id,schedule_id,schedule) VALUES(%d,%d,'%s')"),
3148 DBGetFieldULong(hResult, i, 0), i + 1, DBGetField(hResult, i, 1, buffer, 1024));
3149 if (!SQLQuery(query))
3150 if (!g_bIgnoreErrors)
3151 return FALSE;
3152 }
3153 DBFreeResult(hResult);
3154 }
3155 else
3156 {
3157 if (!g_bIgnoreErrors)
3158 return FALSE;
3159 }
3160
3161 // Update address_lists table
3162 hResult = SQLSelect(_T("SELECT list_type,community_id,addr_type,addr1,addr2 FROM address_lists"));
3163 if (hResult != NULL)
3164 {
3165 if (!SQLQuery(_T("DROP TABLE address_lists")))
3166 if (!g_bIgnoreErrors)
3167 return FALSE;
3168
3169 if (!CreateTable(_T("CREATE TABLE address_lists (")
3170 _T("list_type integer not null,")
3171 _T("community_id integer not null,")
3172 _T("addr_type integer not null,")
3173 _T("addr1 varchar(15) not null,")
3174 _T("addr2 varchar(15) not null,")
3175 _T("PRIMARY KEY(list_type,community_id,addr_type,addr1,addr2))")))
3176 if (!g_bIgnoreErrors)
3177 return FALSE;
3178
3179 for(i = 0; i < DBGetNumRows(hResult); i++)
3180 {
3181 _sntprintf(query, 1024, _T("INSERT INTO address_lists (list_type,community_id,addr_type,addr1,addr2) VALUES(%d,%d,%d,'%s','%s')"),
3182 DBGetFieldULong(hResult, i, 0), DBGetFieldULong(hResult, i, 1),
3183 DBGetFieldULong(hResult, i, 2), DBGetField(hResult, i, 3, buffer, 64),
3184 DBGetField(hResult, i, 4, &buffer[128], 64));
3185 if (!SQLQuery(query))
3186 if (!g_bIgnoreErrors)
3187 return FALSE;
3188 }
3189
3190 DBFreeResult(hResult);
3191 }
3192 else
3193 {
3194 if (!g_bIgnoreErrors)
3195 return FALSE;
3196 }
3197
3198 // Create new tables
3199 if (!CreateTable(_T("CREATE TABLE object_custom_attributes (")
3200 _T("object_id integer not null,")
3201 _T("attr_name varchar(127) not null,")
3202 _T("attr_value $SQL:TEXT not null,")
3203 _T("PRIMARY KEY(object_id,attr_name))")))
3204 if (!g_bIgnoreErrors)
3205 return FALSE;
3206
3207 if (!CreateTable(_T("CREATE TABLE web_maps (")
3208 _T("id integer not null,")
3209 _T("title varchar(63) not null,")
3210 _T("properties $SQL:TEXT not null,")
3211 _T("data $SQL:TEXT not null,")
3212 _T("PRIMARY KEY(id))")))
3213 if (!g_bIgnoreErrors)
3214 return FALSE;
3215
3216 if (!SQLQuery(_T("UPDATE config SET var_value='81' WHERE var_name='DBFormatVersion'")))
3217 if (!g_bIgnoreErrors)
3218 return FALSE;
3219
3220 return TRUE;
3221}
3222
3223
3224//
3225// Upgrade from V79 to V80
3226//
3227
31cc1924 3228static BOOL H_UpgradeFromV79(int currVersion, int newVersion)
5039dede
AK
3229{
3230 static TCHAR m_szBatch[] =
3231 _T("ALTER TABLE nodes ADD uname varchar(255)\n")
3232 _T("UPDATE nodes SET uname='#00'\n")
3233 _T("<END>");
3234
3235 if (!SQLBatch(m_szBatch))
3236 if (!g_bIgnoreErrors)
3237 return FALSE;
3238
3239 if (!SQLQuery(_T("UPDATE config SET var_value='80' WHERE var_name='DBFormatVersion'")))
3240 if (!g_bIgnoreErrors)
3241 return FALSE;
3242
3243 return TRUE;
3244}
3245
3246
3247//
3248// Upgrade from V78 to V79
3249//
3250
31cc1924 3251static BOOL H_UpgradeFromV78(int currVersion, int newVersion)
5039dede
AK
3252{
3253 static TCHAR m_szBatch[] =
3254 _T("DELETE FROM config WHERE var_name='RetainCustomInterfaceNames'\n")
3255 _T("DROP TABLE modules\n")
3256 _T("<END>");
3257 static TCHAR m_szMySQLBatch[] =
3258 _T("ALTER TABLE users MODIFY COLUMN cert_mapping_data text not null\n")
3259 _T("ALTER TABLE user_profiles MODIFY COLUMN var_value text not null\n")
3260 _T("ALTER TABLE object_properties MODIFY COLUMN comments text not null\n")
3261 _T("ALTER TABLE network_services MODIFY COLUMN check_request text not null\n")
3262 _T("ALTER TABLE network_services MODIFY COLUMN check_responce text not null\n")
3263 _T("ALTER TABLE conditions MODIFY COLUMN script text not null\n")
3264 _T("ALTER TABLE container_categories MODIFY COLUMN description text not null\n")
3265 _T("ALTER TABLE items MODIFY COLUMN transformation text not null\n")
3266 _T("ALTER TABLE event_cfg MODIFY COLUMN description text not null\n")
3267 _T("ALTER TABLE actions MODIFY COLUMN action_data text not null\n")
3268 _T("ALTER TABLE event_policy MODIFY COLUMN comments text not null\n")
3269 _T("ALTER TABLE event_policy MODIFY COLUMN script text not null\n")
3270 _T("ALTER TABLE alarm_change_log MODIFY COLUMN info_text text not null\n")
3271 _T("ALTER TABLE alarm_notes MODIFY COLUMN note_text text not null\n")
3272 _T("ALTER TABLE object_tools MODIFY COLUMN tool_data text not null\n")
3273 _T("ALTER TABLE syslog MODIFY COLUMN msg_text text not null\n")
3274 _T("ALTER TABLE script_library MODIFY COLUMN script_code text not null\n")
3275 _T("ALTER TABLE snmp_trap_log MODIFY COLUMN trap_varlist text not null\n")
3276 _T("ALTER TABLE maps MODIFY COLUMN description text not null\n")
3277 _T("ALTER TABLE agent_configs MODIFY COLUMN config_file text not null\n")
3278 _T("ALTER TABLE agent_configs MODIFY COLUMN config_filter text not null\n")
3279 _T("ALTER TABLE graphs MODIFY COLUMN config text not null\n")
3280 _T("ALTER TABLE certificates MODIFY COLUMN cert_data text not null\n")
3281 _T("ALTER TABLE certificates MODIFY COLUMN subject text not null\n")
3282 _T("ALTER TABLE certificates MODIFY COLUMN comments text not null\n")
3283 _T("ALTER TABLE audit_log MODIFY COLUMN message text not null\n")
3284 _T("ALTER TABLE situations MODIFY COLUMN comments text not null\n")
3285 _T("<END>");
3286
3287 if (!SQLBatch(m_szBatch))
3288 if (!g_bIgnoreErrors)
3289 return FALSE;
3290
3291 if (g_iSyntax == DB_SYNTAX_MYSQL)
3292 {
3293 if (!SQLBatch(m_szMySQLBatch))
3294 if (!g_bIgnoreErrors)
3295 return FALSE;
3296 }
3297
3298 if (!SQLQuery(_T("UPDATE config SET var_value='79' WHERE var_name='DBFormatVersion'")))
3299 if (!g_bIgnoreErrors)
3300 return FALSE;
3301
3302 return TRUE;
3303}
3304
3305
3306//
3307// Upgrade from V77 to V78
3308//
3309
31cc1924 3310static BOOL H_UpgradeFromV77(int currVersion, int newVersion)
5039dede
AK
3311{
3312 if (!CreateTable(_T("CREATE TABLE trusted_nodes (")
3313 _T("source_object_id integer not null,")
3314 _T("target_node_id integer not null,")
3315 _T("PRIMARY KEY(source_object_id,target_node_id))")))
3316 if (!g_bIgnoreErrors)
3317 return FALSE;
3318
3319 if (!CreateConfigParam(_T("CheckTrustedNodes"), _T("1"), 1, 1))
3320 if (!g_bIgnoreErrors)
3321 return FALSE;
3322
3323 if (!SQLQuery(_T("UPDATE config SET var_value='78' WHERE var_name='DBFormatVersion'")))
3324 if (!g_bIgnoreErrors)
3325 return FALSE;
3326
3327 return TRUE;
3328}
3329
3330
3331//
3332// Upgrade from V76 to V77
3333//
3334
31cc1924 3335static BOOL H_UpgradeFromV76(int currVersion, int newVersion)
5039dede
AK
3336{
3337 DB_RESULT hResult;
3338 int i, count, seq;
3339 DWORD id, lastId;
3340 TCHAR query[1024];
3341
3342 hResult = SQLSelect(_T("SELECT condition_id,dci_id,node_id,dci_func,num_polls FROM cond_dci_map ORDER BY condition_id"));
3343 if (hResult == NULL)
3344 if (!g_bIgnoreErrors)
3345 return FALSE;
3346
3347 if (!SQLQuery(_T("DROP TABLE cond_dci_map")))
3348 if (!g_bIgnoreErrors)
3349 goto error;
3350
3351 if (!CreateTable(_T("CREATE TABLE cond_dci_map (")
3352 _T("condition_id integer not null,")
3353 _T("sequence_number integer not null,")
3354 _T("dci_id integer not null,")
3355 _T("node_id integer not null,")
3356 _T("dci_func integer not null,")
3357 _T("num_polls integer not null,")
3358 _T("PRIMARY KEY(condition_id,sequence_number))")))
3359 if (!g_bIgnoreErrors)
3360 goto error;
3361
3362 count = DBGetNumRows(hResult);
3363 for(i = 0, seq = 0, lastId = 0; i < count; i++, seq++)
3364 {
3365 id = DBGetFieldULong(hResult, i, 0);
3366 if (id != lastId)
3367 {
3368 seq = 0;
3369 lastId = id;
3370 }
3371 _sntprintf(query, 1024, _T("INSERT INTO cond_dci_map (condition_id,sequence_number,dci_id,node_id,dci_func,num_polls) VALUES (%d,%d,%d,%d,%d,%d)"),
3372 id, seq, DBGetFieldULong(hResult, i, 1), DBGetFieldULong(hResult, i, 2),
3373 DBGetFieldULong(hResult, i, 3), DBGetFieldULong(hResult, i, 4));
3374 if (!SQLQuery(query))
3375 if (!g_bIgnoreErrors)
3376 goto error;
3377 }
3378
3379 DBFreeResult(hResult);
3380
3381 if (!SQLQuery(_T("UPDATE config SET var_value='77' WHERE var_name='DBFormatVersion'")))
3382 if (!g_bIgnoreErrors)
3383 return FALSE;
3384
3385 return TRUE;
3386
3387error:
3388 DBFreeResult(hResult);
3389 return FALSE;
3390}
3391
3392
3393//
3394// Upgrade from V75 to V76
3395//
3396
31cc1924 3397static BOOL H_UpgradeFromV75(int currVersion, int newVersion)
5039dede
AK
3398{
3399 static TCHAR m_szBatch[] =
3400 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (")
3401 _T("50,'SYS_NETWORK_CONN_LOST',4,1,'NetXMS server network connectivity lost',")
3402 _T("'Generated when system detects loss of network connectivity based on beacon ")
3403 _T("probing.#0D#0AParameters:#0D#0A 1) Number of beacons')\n")
3404 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (")
3405 _T("51,'SYS_NETWORK_CONN_RESTORED',0,1,'NetXMS server network connectivity restored',")
3406 _T("'Generated when system detects restoration of network connectivity based on ")
3407 _T("beacon probing.#0D#0AParameters:#0D#0A 1) Number of beacons')\n")
3408 _T("<END>");
3409
3410 if (!SQLBatch(m_szBatch))
3411 if (!g_bIgnoreErrors)
3412 return FALSE;
3413
3414 if (!CreateConfigParam(_T("AgentCommandTimeout"), _T("2000"), 1, 1))
3415 if (!g_bIgnoreErrors)
3416 return FALSE;
3417
3418 if (!CreateConfigParam(_T("BeaconHosts"), _T(""), 1, 1))
3419 if (!g_bIgnoreErrors)
3420 return FALSE;
3421
3422 if (!CreateConfigParam(_T("BeaconTimeout"), _T("1000"), 1, 1))
3423 if (!g_bIgnoreErrors)
3424 return FALSE;
3425
3426 if (!CreateConfigParam(_T("BeaconPollingInterval"), _T("1000"), 1, 1))
3427 if (!g_bIgnoreErrors)
3428 return FALSE;
3429
3430 if (!SQLQuery(_T("UPDATE config SET var_value='76' WHERE var_name='DBFormatVersion'")))
3431 if (!g_bIgnoreErrors)
3432 return FALSE;
3433
3434 return TRUE;
3435}
3436
3437
3438//
3439// Upgrade from V74 to V75
3440//
3441
31cc1924 3442static BOOL H_UpgradeFromV74(int currVersion, int newVersion)
5039dede
AK
3443{
3444 static TCHAR m_szBatch[] =
3445 _T("ALTER TABLE address_lists ADD community_id integer\n")
3446 _T("UPDATE address_lists SET community_id=0\n")
3447 _T("<END>");
3448
3449 if (!SQLBatch(m_szBatch))
3450 if (!g_bIgnoreErrors)
3451 return FALSE;
3452
3453 if (!CreateTable(_T("CREATE TABLE snmp_communities (")
3454 _T("id integer not null,")
3455 _T("community varchar(255) not null,")
3456 _T("PRIMARY KEY(id))")))
3457 if (!g_bIgnoreErrors)
3458 return FALSE;
3459
3460 if (!CreateConfigParam(_T("UseInterfaceAliases"), _T("0"), 1, 0))
3461 if (!g_bIgnoreErrors)
3462 return FALSE;
3463
3464 if (!CreateConfigParam(_T("SyncNodeNamesWithDNS"), _T("0"), 1, 0))
3465 if (!g_bIgnoreErrors)
3466 return FALSE;
3467
3468 if (!SQLQuery(_T("UPDATE config SET var_value='75' WHERE var_name='DBFormatVersion'")))
3469 if (!g_bIgnoreErrors)
3470 return FALSE;
3471
3472 return TRUE;
3473}
3474
3475
3476//
3477// Upgrade from V73 to V74
3478//
3479
31cc1924 3480static BOOL H_UpgradeFromV73(int currVersion, int newVersion)
5039dede
AK
3481{
3482 static TCHAR m_szBatch[] =
3483 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
3484 _T("VALUES (48,'SYS_EVENT_STORM_DETECTED',3,1,'Event storm detected (Events per second: %1)',")
3485 _T("'Generated when system detects an event storm.#0D#0AParameters:#0D#0A")
3486 _T(" 1) Events per second#0D#0A 2) Duration#0D#0A 3) Threshold')\n")
3487 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
3488 _T("VALUES (49,'SYS_EVENT_STORM_ENDED',0,1,'Event storm ended',")
3489 _T("'Generated when system clears event storm condition.#0D#0AParameters:#0D#0A")
3490 _T(" 1) Events per second#0D#0A 2) Duration#0D#0A 3) Threshold')\n")
3491 _T("DELETE FROM config WHERE var_name='NumberOfEventProcessors'\n")
3492 _T("DELETE FROM config WHERE var_name='EventStormThreshold'\n")
3493 _T("<END>");
3494
3495 if (!SQLBatch(m_szBatch))
3496 if (!g_bIgnoreErrors)
3497 return FALSE;
3498
3499 if (!CreateConfigParam(_T("EnableEventStormDetection"), _T("0"), 1, 1))
3500 if (!g_bIgnoreErrors)
3501 return FALSE;
3502
3503 if (!CreateConfigParam(_T("EventStormEventsPerSecond"), _T("100"), 1, 1))
3504 if (!g_bIgnoreErrors)
3505 return FALSE;
3506
3507 if (!CreateConfigParam(_T("EventStormDuration"), _T("15"), 1, 1))
3508 if (!g_bIgnoreErrors)
3509 return FALSE;
3510
3511 if (!SQLQuery(_T("UPDATE config SET var_value='74' WHERE var_name='DBFormatVersion'")))
3512 if (!g_bIgnoreErrors)
3513 return FALSE;
3514
3515 return TRUE;
3516}
3517
3518
3519//
3520// Upgrade from V72 to V73
3521//
3522
31cc1924 3523static BOOL H_UpgradeFromV72(int currVersion, int newVersion)
5039dede
AK
3524{
3525 static TCHAR m_szBatch[] =
3526 _T("ALTER TABLE event_policy ADD situation_id integer\n")
3527 _T("ALTER TABLE event_policy ADD situation_instance varchar(255)\n")
3528 _T("UPDATE event_policy SET situation_id=0,situation_instance='#00'\n")
3529 _T("<END>");
3530
3531 if (!SQLBatch(m_szBatch))
3532 if (!g_bIgnoreErrors)
3533 return FALSE;
3534
3535 if (!CreateTable(_T("CREATE TABLE policy_situation_attr_list (")
3536 _T("rule_id integer not null,")
3537 _T("situation_id integer not null,")
3538 _T("attr_name varchar(255) not null,")
3539 _T("attr_value varchar(255) not null,")
3540 _T("PRIMARY KEY(rule_id,situation_id,attr_name))")))
3541 if (!g_bIgnoreErrors)
3542 return FALSE;
3543
3544 if (!CreateTable(_T("CREATE TABLE situations (")
3545 _T("id integer not null,")
3546 _T("name varchar(127) not null,")
3547 _T("comments $SQL:TEXT not null,")
3548 _T("PRIMARY KEY(id))")))
3549 if (!g_bIgnoreErrors)
3550 return FALSE;
3551
3552 if (!CreateConfigParam(_T("RetainCustomInterfaceNames"), _T("0"), 1, 0))
3553 if (!g_bIgnoreErrors)
3554 return FALSE;
3555
3556 if (!CreateConfigParam(_T("AllowDirectSMS"), _T("0"), 1, 0))
3557 if (!g_bIgnoreErrors)
3558 return FALSE;
3559
3560 if (!CreateConfigParam(_T("EventStormThreshold"), _T("0"), 1, 1))
3561 if (!g_bIgnoreErrors)
3562 return FALSE;
3563
3564 if (!SQLQuery(_T("UPDATE config SET var_value='73' WHERE var_name='DBFormatVersion'")))
3565 if (!g_bIgnoreErrors)
3566 return FALSE;
3567
3568 return TRUE;
3569}
3570
3571
3572//
3573// Upgrade from V71 to V72
3574//
3575
31cc1924 3576static BOOL H_UpgradeFromV71(int currVersion, int newVersion)
5039dede
AK
3577{
3578 static TCHAR m_szBatch[] =
3579 _T("ALTER TABLE items ADD proxy_node integer\n")
3580 _T("UPDATE items SET proxy_node=0\n")
3581 _T("<END>");
3582
3583 if (!SQLBatch(m_szBatch))
3584 if (!g_bIgnoreErrors)
3585 return FALSE;
3586
3587 if (!SQLQuery(_T("UPDATE config SET var_value='72' WHERE var_name='DBFormatVersion'")))
3588 if (!g_bIgnoreErrors)
3589 return FALSE;
3590
3591 return TRUE;
3592}
3593
3594
3595//
3596// Upgrade from V70 to V71
3597//
3598
31cc1924 3599static BOOL H_UpgradeFromV70(int currVersion, int newVersion)
5039dede
AK
3600{
3601 static TCHAR m_szBatch[] =
3602 _T("ALTER TABLE nodes ADD required_polls integer\n")
3603 _T("UPDATE nodes SET required_polls=0\n")
3604 _T("ALTER TABLE interfaces ADD required_polls integer\n")
3605 _T("UPDATE interfaces SET required_polls=0\n")
3606 _T("ALTER TABLE network_services ADD required_polls integer\n")
3607 _T("UPDATE network_services SET required_polls=0\n")
3608 _T("<END>");
3609
3610 if (!SQLBatch(m_szBatch))
3611 if (!g_bIgnoreErrors)
3612 return FALSE;
3613
3614 if (!CreateConfigParam(_T("PollCountForStatusChange"), _T("1"), 1, 1))