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