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