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