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