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