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