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