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