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