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