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