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