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