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