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