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