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