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