added function AppAgentGetParameterArg in application agent
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade.cpp
CommitLineData
6f33021d 1/*
5039dede 2** nxdbmgr - NetXMS database manager
805171de 3** Copyright (C) 2004-2015 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"
bf6fb6c3 24#include <nxevent.h>
5039dede 25
4f50e45c
VK
26/**
27 * Externals
28 */
7a41a06e
VK
29BOOL MigrateMaps();
30
271e3971
VK
31/**
32 * Generate GUIDs
33 */
34static bool GenerateGUID(const TCHAR *table, const TCHAR *idColumn, const TCHAR *guidColumn)
35{
36 TCHAR query[256];
37 _sntprintf(query, 256, _T("SELECT %s FROM %s"), idColumn, table);
38 DB_RESULT hResult = SQLSelect(query);
39 if (hResult == NULL)
40 return false;
41
42 int count = DBGetNumRows(hResult);
43 for(int i = 0; i < count; i++)
44 {
45 uuid_t guid;
46 TCHAR buffer[64];
47
48 uuid_generate(guid);
49 _sntprintf(query, 256, _T("UPDATE %s SET %s='%s' WHERE %s=%d"),
50 table, guidColumn, uuid_to_string(guid, buffer), idColumn, DBGetFieldULong(hResult, i, 0));
51 if (!SQLQuery(query))
52 {
53 DBFreeResult(hResult);
54 return false;
55 }
56 }
57 DBFreeResult(hResult);
58 return true;
59}
60
4f50e45c
VK
61/**
62 * Create table
63 */
5039dede
AK
64static BOOL CreateTable(const TCHAR *pszQuery)
65{
5039dede 66 BOOL bResult;
7618e362 67 String query(pszQuery);
5039dede 68
2a964810
VK
69 query.replace(_T("$SQL:TEXT"), g_pszSqlType[g_dbSyntax][SQL_TYPE_TEXT]);
70 query.replace(_T("$SQL:TXT4K"), g_pszSqlType[g_dbSyntax][SQL_TYPE_TEXT4K]);
71 query.replace(_T("$SQL:INT64"), g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64]);
72 if (g_dbSyntax == DB_SYNTAX_MYSQL)
7618e362
VK
73 query += g_pszTableSuffix;
74 bResult = SQLQuery(query);
5039dede
AK
75 return bResult;
76}
77
4f50e45c
VK
78/**
79 * Create configuration parameter if it doesn't exist (unless bForceUpdate set to true)
80 */
b3eb058b 81BOOL CreateConfigParam(const TCHAR *pszName, const TCHAR *pszValue, int iVisible, int iNeedRestart, BOOL bForceUpdate)
5039dede 82{
42475f3e 83 TCHAR szQuery[1024];
5039dede
AK
84 DB_RESULT hResult;
85 BOOL bVarExist = FALSE, bResult = TRUE;
86
87 // Check for variable existence
08b214c6 88 _sntprintf(szQuery, 1024, _T("SELECT var_value FROM config WHERE var_name='%s'"), pszName);
5039dede
AK
89 hResult = DBSelect(g_hCoreDB, szQuery);
90 if (hResult != 0)
91 {
92 if (DBGetNumRows(hResult) > 0)
93 bVarExist = TRUE;
94 DBFreeResult(hResult);
95 }
96
97 if (!bVarExist)
98 {
08b214c6 99 _sntprintf(szQuery, 1024, _T("INSERT INTO config (var_name,var_value,is_visible,")
6f33021d 100 _T("need_server_restart) VALUES (%s,%s,%d,%d)"),
101 (const TCHAR *)DBPrepareString(g_hCoreDB, pszName, 63),
42475f3e 102 (const TCHAR *)DBPrepareString(g_hCoreDB, pszValue, 255), iVisible, iNeedRestart);
5039dede
AK
103 bResult = SQLQuery(szQuery);
104 }
105 else if (bForceUpdate)
106 {
42475f3e
VK
107 _sntprintf(szQuery, 1024, _T("UPDATE config SET var_value=%s WHERE var_name=%s"),
108 (const TCHAR *)DBPrepareString(g_hCoreDB, pszValue, 255), (const TCHAR *)DBPrepareString(g_hCoreDB, pszName, 63));
5039dede
AK
109 bResult = SQLQuery(szQuery);
110 }
111 return bResult;
112}
113
4f50e45c
VK
114/**
115 * Set primary key constraint
116 */
5039dede
AK
117static BOOL SetPrimaryKey(const TCHAR *table, const TCHAR *key)
118{
119 TCHAR query[4096];
120
2a964810 121 if (g_dbSyntax == DB_SYNTAX_SQLITE)
5039dede 122 return TRUE; // SQLite does not support adding constraints
6f33021d 123
5039dede
AK
124 _sntprintf(query, 4096, _T("ALTER TABLE %s ADD PRIMARY KEY (%s)"), table, key);
125 return SQLQuery(query);
126}
127
4f50e45c
VK
128/**
129 * Drop primary key from table
130 */
9bfc9a6b
VK
131static BOOL DropPrimaryKey(const TCHAR *table)
132{
133 TCHAR query[1024];
134 DB_RESULT hResult;
135 BOOL success;
136
2a964810 137 switch(g_dbSyntax)
9bfc9a6b
VK
138 {
139 case DB_SYNTAX_ORACLE:
140 case DB_SYNTAX_MYSQL:
141 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP PRIMARY KEY"), table);
142 success = SQLQuery(query);
143 break;
144 case DB_SYNTAX_PGSQL:
145 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s_pkey"), table, table);
146 success = SQLQuery(query);
147 break;
148 case DB_SYNTAX_MSSQL:
149 success = FALSE;
150 _sntprintf(query, 1024, _T("SELECT name FROM sysobjects WHERE xtype='PK' AND parent_obj=OBJECT_ID('%s')"), table);
151 hResult = SQLSelect(query);
152 if (hResult != NULL)
153 {
154 if (DBGetNumRows(hResult) > 0)
155 {
156 TCHAR objName[512];
157
158 DBGetField(hResult, 0, 0, objName, 512);
159 _sntprintf(query, 1024, _T("ALTER TABLE %s DROP CONSTRAINT %s"), table, objName);
160 success = SQLQuery(query);
161 }
162 DBFreeResult(hResult);
163 }
164 break;
165 default: // Unsupported DB engine
166 success = FALSE;
167 break;
168 }
169 return success;
170}
171
4f50e45c
VK
172/**
173 * Convert strings from # encoded form to normal form
174 */
d6bf58f9 175static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *idColumn2, const TCHAR *column, bool isStringId)
643c9dcb
VK
176{
177 DB_RESULT hResult;
178 TCHAR *query;
4e0e77e6 179 size_t queryLen = 512;
643c9dcb
VK
180 BOOL success = FALSE;
181
c29fb885 182 query = (TCHAR *)malloc(queryLen * sizeof(TCHAR));
fe12a1ea 183
2a964810 184 switch(g_dbSyntax)
a4743a0f
VK
185 {
186 case DB_SYNTAX_MSSQL:
9f6712bc 187 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE CAST(%s AS nvarchar(4000))=N'#00'"), table, column, column);
a4743a0f
VK
188 break;
189 case DB_SYNTAX_ORACLE:
190 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE to_char(%s)='#00'"), table, column, column);
191 break;
192 default:
193 _sntprintf(query, queryLen, _T("UPDATE %s SET %s='' WHERE %s='#00'"), table, column, column);
194 break;
195 }
fe12a1ea
VK
196 if (!SQLQuery(query))
197 {
198 free(query);
199 return FALSE;
200 }
201
6f33021d 202 _sntprintf(query, queryLen, _T("SELECT %s,%s%s%s FROM %s WHERE %s LIKE '%%#%%'"),
98cd01bb 203 idColumn, column, (idColumn2 != NULL) ? _T(",") : _T(""), (idColumn2 != NULL) ? idColumn2 : _T(""), table, column);
643c9dcb
VK
204 hResult = SQLSelect(query);
205 if (hResult == NULL)
206 {
207 free(query);
208 return FALSE;
209 }
210
211 int count = DBGetNumRows(hResult);
212 for(int i = 0; i < count; i++)
213 {
643c9dcb 214 TCHAR *value = DBGetField(hResult, i, 1, NULL, 0);
035a4d73 215 if (_tcschr(value, _T('#')) != NULL)
643c9dcb 216 {
035a4d73
VK
217 DecodeSQLString(value);
218 String newValue = DBPrepareString(g_hCoreDB, value);
4e0e77e6 219 if (newValue.length() + 256 > queryLen)
035a4d73 220 {
4e0e77e6 221 queryLen = newValue.length() + 256;
c29fb885 222 query = (TCHAR *)realloc(query, queryLen * sizeof(TCHAR));
035a4d73 223 }
d6bf58f9 224 if (isStringId)
98cd01bb 225 {
d6bf58f9
VK
226 TCHAR *id = DBGetField(hResult, i, 0, NULL, 0);
227 if (idColumn2 != NULL)
228 {
229 TCHAR *id2 = DBGetField(hResult, i, 2, NULL, 0);
230 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s AND %s=%s"),
231 table, column, (const TCHAR *)newValue,
6f33021d 232 idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id),
d6bf58f9
VK
233 idColumn2, (const TCHAR *)DBPrepareString(g_hCoreDB, id2));
234 }
235 else
236 {
237 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=%s"), table, column,
238 (const TCHAR *)newValue, idColumn, (const TCHAR *)DBPrepareString(g_hCoreDB, id));
239 }
240 free(id);
98cd01bb
VK
241 }
242 else
243 {
d6bf58f9
VK
244 INT64 id = DBGetFieldInt64(hResult, i, 0);
245 if (idColumn2 != NULL)
246 {
247 INT64 id2 = DBGetFieldInt64(hResult, i, 2);
6f33021d 248 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT _T(" AND %s=") INT64_FMT,
d6bf58f9
VK
249 table, column, (const TCHAR *)newValue, idColumn, id, idColumn2, id2);
250 }
251 else
252 {
253 _sntprintf(query, queryLen, _T("UPDATE %s SET %s=%s WHERE %s=") INT64_FMT, table, column,
254 (const TCHAR *)newValue, idColumn, id);
255 }
98cd01bb 256 }
035a4d73
VK
257 if (!SQLQuery(query))
258 goto cleanup;
643c9dcb 259 }
643c9dcb
VK
260 }
261 success = TRUE;
262
263cleanup:
264 DBFreeResult(hResult);
a4743a0f 265 free(query);
643c9dcb
VK
266 return success;
267}
268
98cd01bb
VK
269static BOOL ConvertStrings(const TCHAR *table, const TCHAR *idColumn, const TCHAR *column)
270{
d6bf58f9 271 return ConvertStrings(table, idColumn, NULL, column, false);
98cd01bb
VK
272}
273
4f50e45c 274/**
70ffb771 275 * Set column nullable (currently only Oracle and PostgreSQL)
4f50e45c 276 */
480e036b 277static BOOL SetColumnNullable(const TCHAR *table, const TCHAR *column)
1024e962
VK
278{
279 TCHAR query[1024] = _T("");
280
2a964810 281 switch(g_dbSyntax)
1024e962
VK
282 {
283 case DB_SYNTAX_ORACLE:
284 _sntprintf(query, 1024, _T("DECLARE already_null EXCEPTION; ")
285 _T("PRAGMA EXCEPTION_INIT(already_null, -1451); ")
f57209fd
VK
286 _T("BEGIN EXECUTE IMMEDIATE 'ALTER TABLE %s MODIFY %s null'; ")
287 _T("EXCEPTION WHEN already_null THEN null; END;"), table, column);
1024e962 288 break;
480e036b
VK
289 case DB_SYNTAX_PGSQL:
290 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL"), table, column);
291 break;
1024e962
VK
292 default:
293 break;
294 }
295
296 return (query[0] != 0) ? SQLQuery(query) : TRUE;
297}
298
480e036b
VK
299/**
300 * Resize varchar column
301 */
b69710b9 302static BOOL ResizeColumn(const TCHAR *table, const TCHAR *column, int newSize, bool nullable)
480e036b
VK
303{
304 TCHAR query[1024];
305
2a964810 306 switch(g_dbSyntax)
480e036b
VK
307 {
308 case DB_SYNTAX_DB2:
309 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE varchar(%d)"), table, column, newSize);
310 break;
311 case DB_SYNTAX_MSSQL:
b69710b9 312 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s varchar(%d) %s NULL"), table, column, newSize, nullable ? _T("") : _T("NOT"));
480e036b
VK
313 break;
314 case DB_SYNTAX_PGSQL:
315 _sntprintf(query, 1024, _T("ALTER TABLE %s ALTER COLUMN %s TYPE varchar(%d)"), table, column, newSize);
316 break;
317 case DB_SYNTAX_SQLITE:
318 /* TODO: add SQLite support */
319 query[0] = 0;
320 break;
321 default:
322 _sntprintf(query, 1024, _T("ALTER TABLE %s MODIFY %s varchar(%d)"), table, column, newSize);
323 break;
324 }
325
326 return (query[0] != 0) ? SQLQuery(query) : TRUE;
327}
328
4f50e45c
VK
329/**
330 * Create new event template
331 */
aa16f82b
VK
332static BOOL CreateEventTemplate(int code, const TCHAR *name, int severity, int flags, const TCHAR *message, const TCHAR *description)
333{
480e036b 334 TCHAR query[4096];
aa16f82b 335
480e036b
VK
336 _sntprintf(query, 4096, _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (%d,'%s',%d,%d,%s,%s)"),
337 code, name, severity, flags, (const TCHAR *)DBPrepareString(g_hCoreDB, message),
338 (const TCHAR *)DBPrepareString(g_hCoreDB, description));
aa16f82b
VK
339 return SQLQuery(query);
340}
341
22aaa779
VK
342/**
343 * Re-create TDATA tables
344 */
2f1bc68b 345static BOOL RecreateTData(const TCHAR *className, bool multipleTables, bool indexFix)
22aaa779 346{
1d4f7890 347 TCHAR query[1024];
22aaa779
VK
348 _sntprintf(query, 256, _T("SELECT id FROM %s"), className);
349 DB_RESULT hResult = SQLSelect(query);
350 if (hResult != NULL)
351 {
352 int count = DBGetNumRows(hResult);
353 for(int i = 0; i < count; i++)
354 {
2f1bc68b 355 bool recreateTables = true;
22aaa779
VK
356 DWORD id = DBGetFieldULong(hResult, i, 0);
357
2f1bc68b 358 if (indexFix)
1d4f7890 359 {
2f1bc68b
VK
360 _sntprintf(query, 256, _T("SELECT count(*) FROM dc_tables WHERE node_id=%d"), id);
361 DB_RESULT hResultCount = SQLSelect(query);
362 if (hResultCount != NULL)
22aaa779 363 {
7d4f8ace 364 recreateTables = (DBGetFieldLong(hResultCount, 0, 0) == 0);
2f1bc68b
VK
365 DBFreeResult(hResultCount);
366 }
367
368 if (!recreateTables)
369 {
a9f5aa55 370 _sntprintf(query, 256, _T("CREATE INDEX idx_tdata_rec_%d_id ON tdata_records_%d(record_id)"), id, id);
2f1bc68b
VK
371 if (!SQLQuery(query))
372 {
373 if (!g_bIgnoreErrors)
374 {
375 DBFreeResult(hResult);
376 return FALSE;
377 }
378 }
22aaa779
VK
379 }
380 }
381
2f1bc68b 382 if (recreateTables)
22aaa779 383 {
2f1bc68b 384 if (multipleTables)
22aaa779 385 {
2f1bc68b
VK
386 _sntprintf(query, 1024, _T("DROP TABLE tdata_rows_%d\nDROP TABLE tdata_records_%d\nDROP TABLE tdata_%d\n<END>"), id, id, id);
387 }
388 else
389 {
390 _sntprintf(query, 256, _T("DROP TABLE tdata_%d\n<END>"), id);
391 }
392 if (!SQLBatch(query))
393 {
394 if (!g_bIgnoreErrors)
395 {
396 DBFreeResult(hResult);
397 return FALSE;
398 }
399 }
400
401 if (!CreateTDataTables(id))
402 {
403 if (!g_bIgnoreErrors)
404 {
405 DBFreeResult(hResult);
406 return FALSE;
407 }
22aaa779
VK
408 }
409 }
410 }
411 DBFreeResult(hResult);
412 }
413 else
414 {
415 if (!g_bIgnoreErrors)
416 return FALSE;
417 }
385b1f20 418 return TRUE;
419}
420
c75e9ee4
VK
421/**
422 * Convert network masks from dotted decimal format to number of bits
423 */
424static BOOL ConvertNetMasks(const TCHAR *table, const TCHAR *column, const TCHAR *idColumn, const TCHAR *idColumn2 = NULL, const TCHAR *condition = NULL)
425{
426 TCHAR query[256];
fcf91b2e 427
c75e9ee4
VK
428 if (idColumn2 != NULL)
429 _sntprintf(query, 256, _T("SELECT %s,%s,%s FROM %s"), idColumn, column, idColumn2, table);
430 else
431 _sntprintf(query, 256, _T("SELECT %s,%s FROM %s"), idColumn, column, table);
432 DB_RESULT hResult = SQLSelect(query);
433 if (hResult == NULL)
434 return FALSE;
435
a19fa589 436 BOOL success = SQLDropColumn(table, column);
fcf91b2e 437
c75e9ee4
VK
438 if (success)
439 {
440 _sntprintf(query, 256, _T("ALTER TABLE %s ADD %s integer"), table, column);
441 success = SQLQuery(query);
442 }
443
444 if (success)
445 {
446 int count = DBGetNumRows(hResult);
447 for(int i = 0; (i < count) && success; i++)
448 {
449 if (idColumn2 != NULL)
450 {
451 TCHAR id2[256];
fcf91b2e 452 _sntprintf(query, 256, _T("UPDATE %s SET %s=%d WHERE %s=%d AND %s='%s'"),
c75e9ee4
VK
453 table, column, BitsInMask(DBGetFieldIPAddr(hResult, i, 1)), idColumn, DBGetFieldLong(hResult, i, 0),
454 idColumn2, DBGetField(hResult, i, 2, id2, 256));
455 }
456 else
457 {
fcf91b2e 458 _sntprintf(query, 256, _T("UPDATE %s SET %s=%d WHERE %s=%d"),
c75e9ee4
VK
459 table, column, BitsInMask(DBGetFieldIPAddr(hResult, i, 1)), idColumn, DBGetFieldLong(hResult, i, 0));
460 }
461 success = SQLQuery(query);
462 }
463 }
464
465 DBFreeResult(hResult);
466 return success;
467}
468
e1d2050f
VK
469/**
470 * Upgrade from V355 to V356
471 */
472static BOOL H_UpgradeFromV355(int currVersion, int newVersion)
473{
474 static TCHAR batch[] =
475 _T("DELETE FROM config WHERE var_name='NumberOfBusinessServicePollers'\n")
476 _T("DELETE FROM config WHERE var_name='NumberOfConditionPollers'\n")
477 _T("DELETE FROM config WHERE var_name='NumberOfConfigurationPollers'\n")
478 _T("DELETE FROM config WHERE var_name='NumberOfDiscoveryPollers'\n")
479 _T("DELETE FROM config WHERE var_name='NumberOfInstancePollers'\n")
480 _T("DELETE FROM config WHERE var_name='NumberOfRoutingTablePollers'\n")
481 _T("DELETE FROM config WHERE var_name='NumberOfStatusPollers'\n")
482 _T("DELETE FROM config WHERE var_name='NumberOfTopologyTablePollers'\n")
483 _T("<END>");
484 CHK_EXEC(SQLBatch(batch));
485
486 CHK_EXEC(CreateConfigParam(_T("PollerThreadPoolBaseSize"), _T("10"), 1, 1));
487 CHK_EXEC(CreateConfigParam(_T("PollerThreadPoolMaxSize"), _T("250"), 1, 1));
488
489 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='356' WHERE var_name='SchemaVersion'")));
490 return TRUE;
491}
492
9708eff4
VK
493/**
494 * Upgrade from V354 to V355
495 */
496static BOOL H_UpgradeFromV354(int currVersion, int newVersion)
497{
498 static TCHAR batch[] =
499 _T("ALTER TABLE nodes ADD agent_cache_mode char(1)\n")
55d81770 500 _T("UPDATE nodes SET agent_cache_mode='0'\n")
e9902466 501 _T("DELETE FROM config WHERE var_name='ServerID'\n")
9708eff4
VK
502 _T("<END>");
503 CHK_EXEC(SQLBatch(batch));
504
dec46d8a 505 CHK_EXEC(CreateConfigParam(_T("DefaultAgentCacheMode"), _T("2"), 1, 1));
9708eff4
VK
506
507 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='355' WHERE var_name='SchemaVersion'")));
508 return TRUE;
509}
510
981d246a
VK
511/**
512 * Upgrade from V353 to V354
513 */
514static BOOL H_UpgradeFromV353(int currVersion, int newVersion)
515{
516 CHK_EXEC(ResizeColumn(_T("users"), _T("password"), 127, false));
517 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='354' WHERE var_name='SchemaVersion'")));
518 return TRUE;
519}
520
2096c8f0
VK
521/**
522 * Upgrade from V352 to V353
523 */
524static BOOL H_UpgradeFromV352(int currVersion, int newVersion)
525{
526 CHK_EXEC(SQLQuery(_T("ALTER TABLE dci_summary_tables ADD guid varchar(36)")));
527 CHK_EXEC(GenerateGUID(_T("dci_summary_tables"), _T("id"), _T("guid")));
528 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='353' WHERE var_name='SchemaVersion'")));
529 return TRUE;
530}
531
271e3971
VK
532/**
533 * Upgrade from V351 to V352
534 */
535static BOOL H_UpgradeFromV351(int currVersion, int newVersion)
536{
537 CHK_EXEC(SQLQuery(_T("ALTER TABLE object_tools ADD guid varchar(36)")));
538 CHK_EXEC(GenerateGUID(_T("object_tools"), _T("tool_id"), _T("guid")));
539 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='352' WHERE var_name='SchemaVersion'")));
540 return TRUE;
541}
542
ac58ffe9
VK
543/**
544 * Upgrade from V350 to V351
545 */
546static BOOL H_UpgradeFromV350(int currVersion, int newVersion)
547{
548 static TCHAR batch[] =
549 _T("ALTER TABLE access_points ADD ap_index integer\n")
550 _T("UPDATE access_points SET ap_index=0\n")
551 _T("<END>");
552 CHK_EXEC(SQLBatch(batch));
553
554 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='351' WHERE var_name='SchemaVersion'")));
555 return TRUE;
556}
557
fcf91b2e 558/**
559 * Upgrade from V349 to V350
560 */
561static BOOL H_UpgradeFromV349(int currVersion, int newVersion)
562{
563 switch(g_dbSyntax)
564 {
565 case DB_SYNTAX_ORACLE:
a8a0793d
VK
566 CHK_EXEC(SQLQuery(_T("UPDATE object_properties SET comments = comments || chr(13) || chr(10) || (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id) WHERE EXISTS (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id AND description IS NOT NULL)")));
567 break;
fcf91b2e 568 case DB_SYNTAX_DB2:
a8a0793d
VK
569 CHK_EXEC(SQLQuery(_T("UPDATE object_properties SET comments = comments || chr(13) || chr(10) || (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id) WHERE EXISTS (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id AND description IS NOT NULL AND description <> '')")));
570 break;
96e03924 571 case DB_SYNTAX_MSSQL:
b69710b9 572 CHK_EXEC(SQLQuery(_T("UPDATE object_properties SET comments = CAST(comments AS varchar(4000)) + char(13) + char(10) + CAST((SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id) AS varchar(4000)) WHERE EXISTS (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id AND description IS NOT NULL AND datalength(description) <> 0)")));
fcf91b2e 573 break;
574 case DB_SYNTAX_PGSQL:
a8a0793d 575 CHK_EXEC(SQLQuery(_T("UPDATE object_properties SET comments = comments || '\\015\\012' || (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id) WHERE EXISTS (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id AND description IS NOT NULL AND description <> '')")));
fcf91b2e 576 break;
577 case DB_SYNTAX_SQLITE:
a8a0793d 578 CHK_EXEC(SQLQuery(_T("UPDATE object_properties SET comments = comments || char(13,10) || (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id) WHERE EXISTS (SELECT description FROM ap_common WHERE ap_common.id = object_properties.object_id AND description IS NOT NULL AND description <> '')")));
fcf91b2e 579 break;
580 case DB_SYNTAX_MYSQL:
a8a0793d 581 CHK_EXEC(SQLQuery(_T("UPDATE object_properties, ap_common SET object_properties.comments=CONCAT(object_properties.comments, '\\r\\n', ap_common.description) WHERE object_properties.object_id=ap_common.id AND (ap_common.description!='' AND ap_common.description IS NOT NULL)")));
fcf91b2e 582 break;
583 default:
96e03924 584 break;
fcf91b2e 585 }
586
3f543e56 587 CHK_EXEC(SQLDropColumn(_T("ap_common"), _T("description")));
fcf91b2e 588 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='350' WHERE var_name='SchemaVersion'")));
589 return TRUE;
590}
591
b06ae508
VK
592/**
593 * Upgrade from V348 to V349
594 */
595static BOOL H_UpgradeFromV348(int currVersion, int newVersion)
596{
597 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='HouseKeepingInterval'")));
598 CHK_EXEC(CreateConfigParam(_T("HousekeeperStartTime"), _T("02:00"), 1, 1));
599 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='349' WHERE var_name='SchemaVersion'")));
600 return TRUE;
601}
602
c30c0c0f
VK
603/**
604 * Upgrade from V347 to V348
605 */
606static BOOL H_UpgradeFromV347(int currVersion, int newVersion)
607{
608 CHK_EXEC(CreateEventTemplate(EVENT_IF_IPADDR_ADDED, _T("SYS_IF_IPADDR_ADDED"), SEVERITY_NORMAL, EF_LOG,
609 _T("IP address %3/%4 added to interface \"%2\""),
610 _T("Generated when IP address added to interface.\r\n")
611 _T("Parameters:\r\n")
612 _T(" 1) Interface object ID\r\n")
613 _T(" 2) Interface name\r\n")
614 _T(" 3) IP address\r\n")
615 _T(" 4) Network mask\r\n")
616 _T(" 5) Interface index")));
617
618 CHK_EXEC(CreateEventTemplate(EVENT_IF_IPADDR_DELETED, _T("SYS_IF_IPADDR_DELETED"), SEVERITY_NORMAL, EF_LOG,
619 _T("IP address %3/%4 deleted from interface \"%2\""),
620 _T("Generated when IP address deleted from interface.\r\n")
621 _T("Parameters:\r\n")
622 _T(" 1) Interface object ID\r\n")
623 _T(" 2) Interface name\r\n")
624 _T(" 3) IP address\r\n")
625 _T(" 4) Network mask\r\n")
626 _T(" 5) Interface index")));
627
628 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='348' WHERE var_name='SchemaVersion'")));
629 return TRUE;
630}
631
632/**
633 * Upgrade from V346 to V347
634 */
635static BOOL H_UpgradeFromV346(int currVersion, int newVersion)
636{
637 CHK_EXEC(CreateTable(
638 _T("CREATE TABLE interface_address_list (")
639 _T(" iface_id integer not null,")
640 _T(" ip_addr varchar(48) not null,")
641 _T(" ip_netmask integer not null,")
642 _T(" PRIMARY KEY(iface_id,ip_addr))")));
643
644 DB_RESULT hResult = SQLSelect(_T("SELECT id,ip_addr,ip_netmask FROM interfaces WHERE ip_addr<>'0.0.0.0'"));
645 if (hResult != NULL)
646 {
647 int count = DBGetNumRows(hResult);
648 for(int i = 0; i < count; i++)
649 {
650 TCHAR query[256], addr[64];
651 _sntprintf(query, 256, _T("INSERT INTO interface_address_list (iface_id,ip_addr,ip_netmask) VALUES (%d,'%s',%d)"),
652 DBGetFieldLong(hResult, i, 0), DBGetField(hResult, i, 1, addr, 64), DBGetFieldLong(hResult, i, 2));
653 CHK_EXEC(SQLQuery(query));
654 }
655 DBFreeResult(hResult);
656 }
657 else
658 {
659 if (!g_bIgnoreErrors)
660 return FALSE;
661 }
662
663 static TCHAR batch[] =
664 _T("ALTER TABLE interfaces DROP COLUMN ip_addr\n")
665 _T("ALTER TABLE interfaces DROP COLUMN ip_netmask\n")
666 _T("<END>");
667 CHK_EXEC(SQLBatch(batch));
668
669 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='347' WHERE var_name='SchemaVersion'")));
670 return TRUE;
671}
672
c75e9ee4
VK
673/**
674 * Upgrade from V345 to V346
675 */
676static BOOL H_UpgradeFromV345(int currVersion, int newVersion)
677{
b69710b9
VK
678 if (g_dbSyntax == DB_SYNTAX_MSSQL)
679 {
680 CHK_EXEC(DropPrimaryKey(_T("cluster_sync_subnets")));
681 CHK_EXEC(DropPrimaryKey(_T("address_lists")));
682 CHK_EXEC(DropPrimaryKey(_T("vpn_connector_networks")));
683 }
684
685 CHK_EXEC(ResizeColumn(_T("cluster_sync_subnets"), _T("subnet_addr"), 48, false));
686 CHK_EXEC(ResizeColumn(_T("cluster_resources"), _T("ip_addr"), 48, false));
687 CHK_EXEC(ResizeColumn(_T("subnets"), _T("ip_addr"), 48, false));
688 CHK_EXEC(ResizeColumn(_T("interfaces"), _T("ip_addr"), 48, false));
689 CHK_EXEC(ResizeColumn(_T("network_services"), _T("ip_bind_addr"), 48, false));
690 CHK_EXEC(ResizeColumn(_T("vpn_connector_networks"), _T("ip_addr"), 48, false));
691 CHK_EXEC(ResizeColumn(_T("snmp_trap_log"), _T("ip_addr"), 48, false));
692 CHK_EXEC(ResizeColumn(_T("address_lists"), _T("addr1"), 48, false));
693 CHK_EXEC(ResizeColumn(_T("address_lists"), _T("addr2"), 48, false));
694 CHK_EXEC(ResizeColumn(_T("nodes"), _T("primary_ip"), 48, false));
c75e9ee4
VK
695
696 CHK_EXEC(ConvertNetMasks(_T("cluster_sync_subnets"), _T("subnet_mask"), _T("cluster_id")));
697 CHK_EXEC(ConvertNetMasks(_T("subnets"), _T("ip_netmask"), _T("id")));
698 CHK_EXEC(ConvertNetMasks(_T("interfaces"), _T("ip_netmask"), _T("id")));
699 CHK_EXEC(ConvertNetMasks(_T("vpn_connector_networks"), _T("ip_netmask"), _T("vpn_id"), _T("ip_addr")));
700
701 DB_RESULT hResult = SQLSelect(_T("SELECT community_id,addr_type,addr1,addr2 FROM address_lists WHERE list_type=0"));
702 if (hResult != NULL)
703 {
704 int count = DBGetNumRows(hResult);
705 if (count > 0)
706 {
707 CHK_EXEC(SQLQuery(_T("DELETE FROM address_lists WHERE list_type=0")));
708
709 for(int i = 0; i < count; i++)
710 {
711 TCHAR query[256], addr[64];
712 _sntprintf(query, 256, _T("INSERT INTO address_lists (list_type,community_id,addr_type,addr1,addr2) VALUES (0,%d,%d,'%s','%d')"),
713 DBGetFieldLong(hResult, i, 0), DBGetFieldLong(hResult, i, 1), DBGetField(hResult, i, 2, addr, 64),
714 BitsInMask(DBGetFieldIPAddr(hResult, i, 3)));
715 CHK_EXEC(SQLQuery(query));
716 }
717 }
718 DBFreeResult(hResult);
719 }
720 else
721 {
722 if (!g_bIgnoreErrors)
723 return FALSE;
724 }
725
b69710b9
VK
726 if (g_dbSyntax == DB_SYNTAX_MSSQL)
727 {
728 CHK_EXEC(SQLQuery(_T("ALTER TABLE cluster_sync_subnets ADD CONSTRAINT pk_cluster_sync_subnets PRIMARY KEY (cluster_id,subnet_addr)")));
729 CHK_EXEC(SQLQuery(_T("ALTER TABLE address_lists ADD CONSTRAINT pk_address_lists PRIMARY KEY (list_type,community_id,addr_type,addr1,addr2)")));
730 CHK_EXEC(SQLQuery(_T("ALTER TABLE vpn_connector_networks ADD CONSTRAINT pk_vpn_connector_networks PRIMARY KEY (vpn_id,ip_addr)")));
731 }
732
c75e9ee4
VK
733 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='346' WHERE var_name='SchemaVersion'")));
734 return TRUE;
735}
736
805171de
VK
737/**
738 * Upgrade from V344 to V345
739 */
740static BOOL H_UpgradeFromV344(int currVersion, int newVersion)
741{
742 CHK_EXEC(CreateConfigParam(_T("NumberOfInstancePollers"), _T("10"), 1, 1));
743 CHK_EXEC(CreateConfigParam(_T("InstancePollingInterval"), _T("600"), 1, 1));
744 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='345' WHERE var_name='SchemaVersion'")));
745 return TRUE;
746}
747
e95680e5
VK
748/**
749 * Upgrade from V343 to V344
750 */
751static BOOL H_UpgradeFromV343(int currVersion, int newVersion)
752{
c30c0c0f 753 static TCHAR batch[] =
e95680e5
VK
754 _T("ALTER TABLE interfaces ADD mtu integer\n")
755 _T("ALTER TABLE interfaces ADD alias varchar(255)\n")
756 _T("UPDATE interfaces SET mtu=0\n")
757 _T("<END>");
758 CHK_EXEC(SQLBatch(batch));
759 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='344' WHERE var_name='SchemaVersion'")));
760 return TRUE;
761}
762
d140955e
VK
763/**
764 * Upgrade from V342 to V343
765 */
766static BOOL H_UpgradeFromV342(int currVersion, int newVersion)
767{
768 if (g_dbSyntax != DB_SYNTAX_MSSQL)
769 {
770 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='CREATE INDEX idx_idata_%d_id_timestamp ON idata_%d(item_id,idata_timestamp DESC)' WHERE var_name='IDataIndexCreationCommand_0'")));
5096f5a5 771 CHK_EXEC(DBCommit(g_hCoreDB)); // do reindexing outside current transaction
d140955e 772 ReindexIData();
5096f5a5 773 CHK_EXEC(DBBegin(g_hCoreDB));
d140955e
VK
774 }
775 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='343' WHERE var_name='SchemaVersion'")));
776 return TRUE;
777}
778
4b70cb26 779/**
780 * Upgrade from V341 to V342
781 */
782static BOOL H_UpgradeFromV341(int currVersion, int newVersion)
783{
d140955e 784 CHK_EXEC(SQLQuery(_T("ALTER TABLE object_tools ADD tool_filter $SQL:TEXT")));
4b70cb26 785 DB_RESULT hResult = SQLSelect(_T("SELECT tool_id, matching_oid FROM object_tools"));
786 if (hResult != NULL)
787 {
788 int count = DBGetNumRows(hResult);
789 for(int i = 0; i < count; i++)
790 {
791 TCHAR *oid = DBGetField(hResult, i, 1, NULL, 0);
792 if (oid == NULL || !_tcscmp(oid, _T(" ")))
793 {
794 oid = _tcsdup(_T(""));
795 }
796 else
797 {
798 TCHAR *newConfig = (TCHAR *)malloc((_tcslen(oid) + 512) * sizeof(TCHAR));
799 _tcscpy(newConfig, _T("<objectToolFilter>"));
800 _tcscat(newConfig, _T("<snmpOid>"));
801 _tcscat(newConfig, oid);
802 _tcscat(newConfig, _T("</snmpOid>"));
803 _tcscat(newConfig, _T("</objectToolFilter>"));
804
805 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE object_tools SET tool_filter=? WHERE tool_id=?"));
806 if (statment != NULL)
807 {
808 DBBind(statment, 1, DB_SQLTYPE_TEXT, newConfig, DB_BIND_STATIC);
809 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
810 CHK_EXEC(DBExecute(statment));
811 DBFreeStatement(statment);
812 }
813 else
814 {
815 if (!g_bIgnoreErrors)
816 return FALSE;
817 }
818 }
819 }
820 }
a19fa589 821 CHK_EXEC(SQLDropColumn(_T("object_tools"), _T("matching_oid"))); //delete old column
4b70cb26 822 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='342' WHERE var_name='SchemaVersion'")));
823 return TRUE;
824}
825
56fa1092
VK
826/**
827 * Upgrade from V340 to V341
828 */
829static BOOL H_UpgradeFromV340(int currVersion, int newVersion)
830{
831 static TCHAR batch[] =
832 _T("ALTER TABLE object_properties ADD country varchar(63)\n")
833 _T("ALTER TABLE object_properties ADD city varchar(63)\n")
834 _T("ALTER TABLE object_properties ADD street_address varchar(255)\n")
835 _T("ALTER TABLE object_properties ADD postcode varchar(31)\n")
836 _T("<END>");
837 CHK_EXEC(SQLBatch(batch));
838 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='341' WHERE var_name='SchemaVersion'")));
839 return TRUE;
840}
841
b00236dd 842/**
843 * Upgrade from V339 to V340
844 */
845static BOOL H_UpgradeFromV339(int currVersion, int newVersion)
846{
847 CHK_EXEC(CreateConfigParam(_T("LdapPageSize"), _T("1000"), 1, 0));
848 CHK_EXEC(SQLQuery(_T("UPDATE config SET var_value='1' WHERE var_name='LdapUserDeleteAction'")));
849 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='340' WHERE var_name='SchemaVersion'")));
850 return TRUE;
851}
852
815638fe
VK
853/**
854 * Upgrade from V338 to V339
855 */
856static BOOL H_UpgradeFromV338(int currVersion, int newVersion)
857{
858 CHK_EXEC(CreateConfigParam(_T("EscapeLocalCommands"), _T("0"), 1, 0));
859 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='339' WHERE var_name='SchemaVersion'")));
860 return TRUE;
861}
862
9208c84b
VK
863/**
864 * Upgrade from V337 to V338
865 */
866static BOOL H_UpgradeFromV337(int currVersion, int newVersion)
867{
868 static TCHAR batch[] =
869 _T("ALTER TABLE nodes ADD icmp_proxy integer\n")
870 _T("UPDATE nodes SET icmp_proxy=0\n")
871 _T("<END>");
872 CHK_EXEC(SQLBatch(batch));
873 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='338' WHERE var_name='SchemaVersion'")));
874 return TRUE;
875}
876
d368074d
VK
877/**
878 * Upgrade from V336 to V337
879 */
880static BOOL H_UpgradeFromV336(int currVersion, int newVersion)
881{
882 CHK_EXEC(CreateConfigParam(_T("SyslogNodeMatchingPolicy"), _T("0"), 1, 1));
883 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='337' WHERE var_name='SchemaVersion'")));
884 return TRUE;
885}
886
2b01f47c 887/**
888 * Upgrade from V335 to V336
889 */
890static BOOL H_UpgradeFromV335(int currVersion, int newVersion)
891{
b69710b9
VK
892 CHK_EXEC(ResizeColumn(_T("network_map_links"), _T("connector_name1"), 255, true));
893 CHK_EXEC(ResizeColumn(_T("network_map_links"), _T("connector_name2"), 255, true));
2b01f47c 894 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='336' WHERE var_name='SchemaVersion'")));
895 return TRUE;
896}
897
b30bb1bc 898/**
899 * Upgrade from V334 to V335
900 */
901static BOOL H_UpgradeFromV334(int currVersion, int newVersion)
902{
17b15353
VK
903 CHK_EXEC(CreateEventTemplate(EVENT_IF_MASK_CHANGED, _T("SYS_IF_MASK_CHANGED"), SEVERITY_NORMAL, EF_LOG,
904 _T("Interface \"%2\" changed mask from %6 to %4 (IP Addr: %3/%4, IfIndex: %5)"),
905 _T("Generated when when network mask on interface is changed.\r\n")
906 _T("Parameters:\r\n")
907 _T(" 1) Interface object ID\r\n")
908 _T(" 2) Interface name\r\n")
909 _T(" 3) IP address\r\n")
910 _T(" 4) New network mask\r\n")
911 _T(" 5) Interface index\r\n")
912 _T(" 6) Old network mask")));
b30bb1bc 913 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='335' WHERE var_name='SchemaVersion'")));
914 return TRUE;
915}
916
39e60274
AK
917/**
918 * Upgrade from V333 to V334
919 */
920static BOOL H_UpgradeFromV333(int currVersion, int newVersion)
921{
922 CHK_EXEC(SetColumnNullable(_T("user_groups"), _T("description")));
923 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='334' WHERE var_name='SchemaVersion'")));
924 return TRUE;
925}
926
4899db4d 927/**
928 * Upgrade from V332 to V333
929 */
930static BOOL H_UpgradeFromV332(int currVersion, int newVersion)
931{
932 static TCHAR batch[] =
933 _T("INSERT INTO metadata (var_name,var_value)")
934 _T(" VALUES ('LocationHistory','CREATE TABLE gps_history_%d (latitude varchar(20), longitude varchar(20), accuracy integer not null, start_timestamp integer not null, end_timestamp integer not null, PRIMARY KEY(start_timestamp))')\n")
935 _T("<END>");
936 CHK_EXEC(SQLBatch(batch));
937 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='333' WHERE var_name='SchemaVersion'")));
938 return TRUE;
939}
940
a6312bd6
VK
941/**
942 * Upgrade from V331 to V332
943 */
944static BOOL H_UpgradeFromV331(int currVersion, int newVersion)
945{
946 CHK_EXEC(SQLQuery(_T("UPDATE items SET instd_data=instance WHERE node_id=template_id AND instd_method=0")));
947 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='332' WHERE var_name='SchemaVersion'")));
948 return TRUE;
949}
950
2a964810
VK
951/**
952 * Upgrade from V330 to V331
953 */
954static BOOL H_UpgradeFromV330(int currVersion, int newVersion)
955{
956 if (g_dbSyntax == DB_SYNTAX_ORACLE)
957 {
958 CHK_EXEC(SQLQuery(_T("ALTER TABLE audit_log ADD session_id integer DEFAULT 0 NOT NULL")));
959 }
960 else
961 {
962 CHK_EXEC(SQLQuery(_T("ALTER TABLE audit_log ADD session_id integer NOT NULL DEFAULT 0")));
963 }
964 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='331' WHERE var_name='SchemaVersion'")));
965 return TRUE;
966}
967
b66098ac
VK
968/**
969 * Upgrade from V329 to V330
970 */
971static BOOL H_UpgradeFromV329(int currVersion, int newVersion)
972{
973 CHK_EXEC(CreateConfigParam(_T("AlarmListDisplayLimit"), _T("4096"), 1, 0));
974 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='330' WHERE var_name='SchemaVersion'")));
975 return TRUE;
976}
977
4016c0df 978/**
979 * Upgrade from V328 to V329
980 */
981static BOOL H_UpgradeFromV328(int currVersion, int newVersion)
982{
b6005694
VK
983 CHK_EXEC(SQLQuery(_T("ALTER TABLE items ADD comments $SQL:TEXT")));
984 CHK_EXEC(SQLQuery(_T("ALTER TABLE dc_tables ADD comments $SQL:TEXT")));
4016c0df 985 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='329' WHERE var_name='SchemaVersion'")));
986 return TRUE;
987}
988
385b1f20 989/**
990 * Upgrade from V327 to V328
991 */
992static BOOL H_UpgradeFromV327(int currVersion, int newVersion)
993{
d368074d 994 CHK_EXEC(CreateConfigParam(_T("ResolveDNSToIPOnStatusPoll"), _T("0"), 1, 1));
385b1f20 995 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='328' WHERE var_name='SchemaVersion'")));
996 return TRUE;
997}
173c189d
VK
998
999/**
1000 * Upgrade from V326 to V327
1001 */
1002static BOOL H_UpgradeFromV326(int currVersion, int newVersion)
1003{
3fa5e14a
VK
1004 CHK_EXEC(DropPrimaryKey(_T("network_map_links")));
1005 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_network_map_links_map_id ON network_map_links(map_id)")));
173c189d 1006 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='327' WHERE var_name='SchemaVersion'")));
22aaa779
VK
1007 return TRUE;
1008}
1009
ade20a86 1010/**
1011 * Upgrade from V325 to V326
1012 */
1013static BOOL H_UpgradeFromV325(int currVersion, int newVersion)
1014{
ade20a86 1015 static TCHAR batch[] =
1016 _T("ALTER TABLE network_map_links DROP COLUMN color\n")
1017 _T("ALTER TABLE network_map_links DROP COLUMN status_object\n")
1018 _T("ALTER TABLE network_map_links DROP COLUMN routing\n")
1019 _T("ALTER TABLE network_map_links DROP COLUMN bend_points\n")
1020 _T("<END>");
1021 CHK_EXEC(SQLBatch(batch));
1022
1023 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='326' WHERE var_name='SchemaVersion'")));
1024 return TRUE;
1025}
1026
1027/**
1028 * Upgrade from V324 to V325
1029 */
1030static BOOL H_UpgradeFromV324(int currVersion, int newVersion)
1031{
1032 //move map link configuration to xml
1033
1034 DB_RESULT hResult = SQLSelect(_T("SELECT map_id, element1, element2, element_data, color, status_object, routing, bend_points FROM network_map_links"));
1035 if (hResult != NULL)
1036 {
1037 int count = DBGetNumRows(hResult);
1038 for(int i = 0; i < count; i++)
1039 {
c55ab1c2
VK
1040 TCHAR *config = DBGetField(hResult, i, 3, NULL, 0);
1041 if (config == NULL)
1042 config = _tcsdup(_T(""));
ade20a86 1043 UINT32 color = DBGetFieldULong(hResult, i, 4);
1044 UINT32 statusObject = DBGetFieldULong(hResult, i, 5);
1045 UINT32 routing = DBGetFieldULong(hResult, i, 6);
1046 TCHAR bendPoints[1024];
1047 DBGetField(hResult, i, 7, bendPoints, 1024);
1048
c55ab1c2 1049 TCHAR *newConfig = (TCHAR *)malloc((_tcslen(config) + 4096) * sizeof(TCHAR));
ade20a86 1050 _tcscpy(newConfig, _T("<config>"));
1051 TCHAR* c1 = _tcsstr(config, _T("<dciList"));
1052 TCHAR* c2 = _tcsstr(config, _T("</dciList>"));
1053 if(c1 != NULL && c2!= NULL)
1054 {
1055 *c2 = 0;
1056 _tcscat(newConfig, c1);
1057 _tcscat(newConfig, _T("</dciList>"));
1058 }
1059
1060 TCHAR tmp[2048];
1061 _sntprintf(tmp, 2048, _T("<color>%d</color>"), color),
1062 _tcscat(newConfig, tmp);
1063
c55ab1c2 1064 if (statusObject != 0)
ade20a86 1065 {
1066 _sntprintf(tmp, 2048, _T("<objectStatusList length=\"1\"><long>%d</long></objectStatusList>"), statusObject);
1067 _tcscat(newConfig, tmp);
1068 }
1069
1070 _sntprintf(tmp, 2048, _T("<routing>%d</routing>"), routing);
1071 _tcscat(newConfig, tmp);
1072
c55ab1c2 1073 if (routing == 3 && bendPoints[0] != 0)
ade20a86 1074 {
1075 count = 1;
30b86580 1076 for(size_t j = 0; j < _tcslen(bendPoints); j++)
ade20a86 1077 {
c55ab1c2 1078 if (bendPoints[j] == _T(','))
ade20a86 1079 count++;
1080 }
1081 _sntprintf(tmp, 2048, _T("<bendPoints length=\"%d\">%s</bendPoints>"), count, bendPoints);
1082 _tcscat(newConfig, tmp);
1083 }
1084 _tcscat(newConfig, _T("</config>"));
1085
1086 safe_free(config);
1087 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE network_map_links SET element_data=? WHERE map_id=? AND element1=? AND element2=?"));
e196e5d8
VK
1088 if (statment != NULL)
1089 {
1090 DBBind(statment, 1, DB_SQLTYPE_TEXT, newConfig, DB_BIND_STATIC);
1091 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
1092 DBBind(statment, 3, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 1));
1093 DBBind(statment, 4, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 2));
1094 CHK_EXEC(DBExecute(statment));
1095 DBFreeStatement(statment);
1096 }
1097 else
1098 {
1099 if (!g_bIgnoreErrors)
1100 return FALSE;
1101 }
081bba09 1102 safe_free(newConfig);
ade20a86 1103 }
1104 DBFreeResult(hResult);
1105 }
1106
1107 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='325' WHERE var_name='SchemaVersion'")));
1108 return TRUE;
1109}
1110
2f1bc68b
VK
1111/**
1112 * Upgrade from V323 to V324
1113 */
1114static BOOL H_UpgradeFromV323(int currVersion, int newVersion)
1115{
a9f5aa55 1116 if (!MetaDataReadInt(_T("ValidTDataIndex"), 0)) // check if schema is already correct
2f1bc68b
VK
1117 {
1118 TCHAR query[1024];
ade20a86 1119 _sntprintf(query, 1024,
66717659 1120 _T("UPDATE metadata SET var_value='CREATE TABLE tdata_records_%%d (record_id %s not null,row_id %s not null,instance varchar(255) null,PRIMARY KEY(row_id),FOREIGN KEY (record_id) REFERENCES tdata_%%d(record_id) ON DELETE CASCADE)' WHERE var_name='TDataTableCreationCommand_1'"),
2a964810 1121 g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64], g_pszSqlType[g_dbSyntax][SQL_TYPE_INT64]);
2f1bc68b
VK
1122 CHK_EXEC(SQLQuery(query));
1123
1124 RecreateTData(_T("nodes"), true, true);
1125 RecreateTData(_T("clusters"), true, true);
1126 RecreateTData(_T("mobile_devices"), true, true);
1127 }
1128
a9f5aa55 1129 CHK_EXEC(SQLQuery(_T("DELETE FROM metadata WHERE var_name='ValidTDataIndex'")));
2f1bc68b
VK
1130 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='324' WHERE var_name='SchemaVersion'")));
1131 return TRUE;
1132}
1133
c8076b19
VK
1134/**
1135 * Upgrade from V322 to V323
1136 */
1137static BOOL H_UpgradeFromV322(int currVersion, int newVersion)
1138{
1139 CHK_EXEC(CreateConfigParam(_T("ProcessTrapsFromUnmanagedNodes"), _T("0"), 1, 1));
1140 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='323' WHERE var_name='SchemaVersion'")));
1141 return TRUE;
1142}
1143
f4d8fe18 1144/**
1145 * Upgrade from V321 to V322
1146 */
1147static BOOL H_UpgradeFromV321(int currVersion, int newVersion)
1148{
2a964810 1149 switch(g_dbSyntax)
f4d8fe18 1150 {
1151 case DB_SYNTAX_DB2:
87104c6a
VK
1152 CHK_EXEC(SQLBatch(
1153 _T("ALTER TABLE users ALTER COLUMN system_access SET DATA TYPE $SQL:INT64\n")
1154 _T("ALTER TABLE user_groups ALTER COLUMN system_access SET DATA TYPE $SQL:INT64\n")
1155 _T("<END>")));
f4d8fe18 1156 break;
1157 case DB_SYNTAX_MSSQL:
87104c6a
VK
1158 CHK_EXEC(SQLBatch(
1159 _T("ALTER TABLE users ALTER COLUMN system_access $SQL:INT64\n")
1160 _T("ALTER TABLE user_groups ALTER COLUMN system_access $SQL:INT64\n")
1161 _T("<END>")));
f4d8fe18 1162 break;
1163 case DB_SYNTAX_PGSQL:
87104c6a 1164 CHK_EXEC(SQLBatch(
d27fb10d
VK
1165 _T("ALTER TABLE users ALTER COLUMN system_access TYPE $SQL:INT64\n")
1166 _T("ALTER TABLE user_groups ALTER COLUMN system_access TYPE $SQL:INT64\n")
87104c6a 1167 _T("<END>")));
f4d8fe18 1168 break;
1169 case DB_SYNTAX_SQLITE:
87104c6a
VK
1170 CHK_EXEC(SQLBatch(
1171 _T("CREATE TABLE temp_users AS SELECT * FROM users\n")
1172 _T("DROP TABLE users\n")
1173 _T("CREATE TABLE users (id integer not null, guid varchar(36) not null, name varchar(63) not null, password varchar(48) not null, system_access $SQL:INT64 not null, flags integer not null,")
1174 _T(" full_name varchar(127) null, description varchar(255) null, grace_logins integer not null, auth_method integer not null, cert_mapping_method integer not null, cert_mapping_data $SQL:TEXT null,")
1175 _T(" auth_failures integer not null, last_passwd_change integer not null, min_passwd_length integer not null, disabled_until integer not null, last_login integer not null, password_history $SQL:TEXT null,")
1176 _T(" xmpp_id varchar(127) null, ldap_dn $SQL:TEXT null, PRIMARY KEY(id))\n")
1177 _T("INSERT INTO users SELECT * FROM temp_users\n")
1178 _T("DROP TABLE temp_users\n")
1179 _T("CREATE TABLE temp_user_groups AS SELECT * FROM user_groups\n")
1180 _T("DROP TABLE user_groups\n")
1181 _T("CREATE TABLE user_groups (id integer not null, guid varchar(36) not null, name varchar(63) not null, system_access $SQL:INT64 not null, flags integer not null,")
1182 _T(" description varchar(255) not null, ldap_dn $SQL:TEXT null, PRIMARY KEY(id))\n")
1183 _T("INSERT INTO user_groups SELECT * FROM temp_user_groups\n")
1184 _T("DROP TABLE temp_user_groups\n")
1185 _T("<END>")));
f4d8fe18 1186 break;
52162395
VK
1187 case DB_SYNTAX_ORACLE:
1188 // no changes needed
1189 break;
f4d8fe18 1190 default:
87104c6a
VK
1191 CHK_EXEC(SQLBatch(
1192 _T("ALTER TABLE users MODIFY system_access $SQL:INT64\n")
1193 _T("ALTER TABLE user_groups MODIFY system_access $SQL:INT64\n")
1194 _T("<END>")));
f4d8fe18 1195 break;
1196 }
1197
1198 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='322' WHERE var_name='SchemaVersion'")));
1199 return TRUE;
1200}
1201
1202/**
d4ce93a2
VK
1203 * Upgrade from V320 to V321
1204 */
1205static BOOL H_UpgradeFromV320(int currVersion, int newVersion)
1206{
1207 static TCHAR batch[] =
1208 _T("ALTER TABLE object_tools ADD command_short_name varchar(31)\n")
1209 _T("UPDATE object_tools SET command_short_name='Shutdown' WHERE tool_id=1\n")
1210 _T("UPDATE object_tools SET command_short_name='Restart' WHERE tool_id=2\n")
1211 _T("UPDATE object_tools SET command_short_name='Wakeup' WHERE tool_id=3\n")
1212 _T("<END>");
1213 CHK_EXEC(SQLBatch(batch));
1214 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='321' WHERE var_name='SchemaVersion'")));
1215 return TRUE;
1216}
1217
ac14e3e6 1218/**
d4ce93a2 1219 * Upgrade from V319 to V320
d9177dd1 1220 */
1221static BOOL H_UpgradeFromV319(int currVersion, int newVersion)
1222{
6c352bb5 1223 CHK_EXEC(CreateConfigParam(_T("LdapConnectionString"), _T("ldap://localhost:389"), 1, 0));
d9177dd1 1224 CHK_EXEC(CreateConfigParam(_T("LdapSyncUser"), _T(""), 1, 0));
1225 CHK_EXEC(CreateConfigParam(_T("LdapSyncUserPassword"), _T(""), 1, 0));
1226 CHK_EXEC(CreateConfigParam(_T("LdapSearchBase"), _T(""), 1, 0));
1227 CHK_EXEC(CreateConfigParam(_T("LdapSearchFilter"), _T(""), 1, 0));
1228 CHK_EXEC(CreateConfigParam(_T("LdapUserDeleteAction"), _T("1"), 1, 0));
f70a8b3c 1229 CHK_EXEC(CreateConfigParam(_T("LdapMappingName"), _T(""), 1, 0));
d9177dd1 1230 CHK_EXEC(CreateConfigParam(_T("LdapMappingFullName"), _T("displayName"), 1, 0));
2575f149 1231 CHK_EXEC(CreateConfigParam(_T("LdapMappingDescription"), _T(""), 1, 0));
d9177dd1 1232 CHK_EXEC(CreateConfigParam(_T("LdapGroupClass"), _T(""), 1, 0));
1233 CHK_EXEC(CreateConfigParam(_T("LdapUserClass"), _T(""), 1, 0));
1234 CHK_EXEC(CreateConfigParam(_T("LdapSyncInterval"), _T("0"), 1, 0));
1235
1236 static TCHAR batch[] =
1237 _T("ALTER TABLE users ADD ldap_dn $SQL:TEXT\n")
1238 _T("ALTER TABLE user_groups ADD ldap_dn $SQL:TEXT\n")
1239 _T("<END>");
1240 CHK_EXEC(SQLBatch(batch));
1241
1242 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='320' WHERE var_name='SchemaVersion'")));
1243 return TRUE;
1244}
d4ce93a2 1245
d9177dd1 1246/*
ac14e3e6
VK
1247 * Upgrade from V318 to V319
1248 */
1249static BOOL H_UpgradeFromV318(int currVersion, int newVersion)
1250{
1251 static TCHAR batch[] =
1252 _T("ALTER TABLE object_tools ADD icon $SQL:TEXT\n")
1253 _T("ALTER TABLE object_tools ADD command_name varchar(255)\n")
1254 _T("UPDATE object_tools SET flags=74,command_name='Shutdown system',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c086488000002bf49444154388d95933f689c7518c73fbf3f7779efaede995c137369d54123ba4843070b1204c10ed24db182939b90c1c1e2eee05871e8d0d2b98a8ba0c52ee2d0cb622acd99222dd1084d9a18cd3597f7bd6bdebbdffbfbe7701d82e8e0071ebec303dfe7e1e1fb883fea7542aba54c080dac153c261ed1a30540b51a8f359bd9c9e565afedd4943ab6b4f4d56873f38d5014ff6af04f95950aaad5fa7e7d7dfdbcee1d1c3cd95d59395b5a5c7c82ffc1fd4ee7acc9f349fd683090b1db15499ae2013b3f8f1c0e296f6f539c380140796707333747a856296d6ca081d1e1a138cc73a95d8cc28f468834459f3ecd7367cee0b38ccd7bf7787e711180dfaf5ee599850544a3c1760898d5556c51e06314d2c5288be150186b995d58404bc9eef5ebb87e86140229257690b17be33b4a4a3173ea14236b71d60a17a3901684b59652b34952ab31dcda6470f76794c9b0b6c0160665320eefae317ab04552ad529e9ec6c78003292dc861bf2f4408e369fb7b948a8cb2cd7085c115868998936887eb75514a617a3db66eb68505211d30f86b97dde536420844a341b17e8bf8db0a21ed12d23ddcda0ff46f7e4dac24482939b8b386b3060f4207206a457afb16be9f519f7f91f22baf52f9e91bfca7ef00829a4fb1af9fa3fed2cbf8419f6c75054a0a0fc800a025f151cafdcb17514af3ecc79f939fbf40d69c259d9ca1ffd687cc7d7411a5145b573e230e52d0120f68ffd8400ad8b97685c9934f31f9ee07b4de5e227ff37d8c311c4f12aad50afb5f5c62e7da65a400519204408f37108408de471e5cfa04fbe3b74c9d7b8ff2d32f1042805f7e25bdf1257fdeee103c8408528d53afa356c85a42b107d6812920bdd3c16f7448cae3d81a0b837cdc2b1c380f724203445d8ff161767cb66df1afe5380a0d3d05ca8d0f148110c02bb035b013109b1a17747b06baa20d3c84897dc93420feeb0b8f22203603dd19307f037f0665861328b32e0000000049454e44ae426082' WHERE tool_id=1\n")
1255 _T("UPDATE object_tools SET flags=74,command_name='Restart system',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c0864880000029849444154388d85934d8b1c5514869f73eeadaaae6ea77bda388999882241fc5c0e09ae4484c4857b4177fa0b5474256edcfa075cbacbc23f6074a12332a0a2200a2189e8c468a23d93e99e9eaeaaae7beb1e1733f9902c7ce06c0ebc2f2fe7f0ca5fe311fdd71e77d93332922e08b749dcc5fe3bc9f72d5d1fcf861f7dd6f9f295353778ebdd0b71ff977374ad60f7888e1003bbb3379ceb930f4e7fbe7be5a7573da7f65697db17cf2ff2e757fe6e06a00e1141040aab59ebb5dc47076efbdb739cacc63edc9aabee4f64796248efb10dbcf738e750556268e97eff14b937ce6d1607126e55eae33c4956d5f4f72f21ae40d6cfe0bc4755c9f39cd4ccc0d27d7a695ae23c896fe7a645d5482c26340f0d19e639beb9811463c85788610a29c1d11d4284cc416a82848589861a49754bab390fac3f4ba69174f963ba7040d745249f4136033f63efd859769f78933a792c244265ea436d9a9a99e86895bc28b0e90fc03632bd88e463acb787580696f3e0fa299ade09e275a5feed2b09b5898f4ba35bdc40bb6b0034f5357cda4277bec354400d0b0d5d75406a5e42caa751d90596c425e22d00aa48771933a3c99e6230a8d1b241dcd1eb03a4a2c4563600f07615bc622da80510149aefa1b982ef3dc24d7d071b7afc71f0c781d58c83d107e48347d1f62a1a7f44f4d0c0130c4c4196d89fefa1273f215f7d9d4b379fa4dfdf22cb32bc7f99b5f533c45893edbc4fe75a8c0116c05b008b408434fd9a327f031b7d08c73670ee2c65595296259afe20fbe76de2fc9ba39e1cd6c6a7e4b0ae87d5200a4cbea4acce3318bd8865cfe1a283dd9fe9a65f901615a982d400c96360be9eda4ebbfdf0a6ec752f741ac11f1a89db02d9ba5bc8d483ae877587e2f0abdfac2b26b209488fa218b07627d7ff636dc524d52cff0513e53f37235ac3190000000049454e44ae426082' WHERE tool_id=2\n")
1256 _T("UPDATE object_tools SET flags=64,command_name='Wakeup node using Wake-On-LAN',icon='89504e470d0a1a0a0000000d49484452000000100000001008060000001ff3ff610000000473424954080808087c086488000000097048597300000dd700000dd70142289b780000001974455874536f667477617265007777772e696e6b73636170652e6f72679bee3c1a0000023649444154388d8d924f48545114c67ff7bd37ff7cf9071bc70a4d47271ca15c848d448185b40a89204890362d8568eb2270d7a2762d5cb7711504d1ae10c195218895a488528e4e06a653d338de7bdfbcf75ac84ce38c901f9ccdb9e7fbce39f77c627ce6872df6dd71f01f781e1d9c00866003215efaf99de7d6763afb1078721262053a800908ed5a5aa9b1e3bb0802a600c0717d3cdf3fae6cccd24a25abb302a80b990c265a009859d941299763249296d6b2a6732468d25a1f24156f00e0cbd62e9b5a71a0dd9a490cad14a570b4266c780cf546797cab1b1317139747435ddcec69266c78385a53c9b1b45265b548d022d51563f45a9c778b69ce35850058de928c0cb4933fd04c7ffece812e9639e5158480865098ebc9181fbfeef07a6e9dc68805c0af8243f45480ab174e33bb9426e7484a9b942710020c3b40e24c236f3facb1bd9b634d3a00d8e100ab992cb7af7421bc225aa9b280a195a414524972054d5f679488e5a394442949d8f4b8d4d14caea09115f55a490cad155a2b9452ecfdcef37e619ddef6287706ba89c76ce2319be1fe4e926d51663e6d90cdeda3d42147ebaa4fcc161da6a61739df52cfe88d8b0ca712f8be871d0e31bb94666a7a916c2e8feb7aff3cd33ef2f4c8612dd3a0a5d1a6bfa78d544f1bbeef33bf9a617e65939fb902c50a328068bd3bb10c1c71a3210401cb24143cbc82d2459c62ad8980154b2b3909bca87e91c09fea642d26ad67f7fb32afe6bebd5958dd1c2c48ddf45f8a10d87591bdcb89b3b3f7063a337f01f30f1c1c580292640000000049454e44ae426082' WHERE tool_id=3\n")
1257 _T("<END>");
1258 CHK_EXEC(SQLBatch(batch));
1259 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='319' WHERE var_name='SchemaVersion'")));
1260 return TRUE;
1261}
1262
56d5289b
VK
1263/**
1264 * Upgrade from V317 to V318
1265 */
1266static BOOL H_UpgradeFromV317(int currVersion, int newVersion)
1267{
1268 CHK_EXEC(CreateEventTemplate(EVENT_AP_DOWN, _T("SYS_AP_DOWN"), SEVERITY_CRITICAL, EF_LOG,
1269 _T("Access point %2 changed state to DOWN"),
1270 _T("Generated when access point state changes to DOWN.\r\n")
1271 _T("Parameters:\r\n")
1272 _T(" 1) Access point object ID\r\n")
1273 _T(" 2) Access point name\r\n")
1274 _T(" 3) Access point MAC address\r\n")
1275 _T(" 4) Access point IP address\r\n")
1276 _T(" 5) Access point vendor name\r\n")
1277 _T(" 6) Access point model\r\n")
1278 _T(" 7) Access point serial number")));
1279
1280 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='318' WHERE var_name='SchemaVersion'")));
1281 return TRUE;
1282}
1283
8deadd79
VK
1284/**
1285 * Upgrade from V316 to V317
1286 */
1287static BOOL H_UpgradeFromV316(int currVersion, int newVersion)
1288{
1289 CHK_EXEC(CreateConfigParam(_T("MinViewRefreshInterval"), _T("1000"), 1, 0));
1290 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='317' WHERE var_name='SchemaVersion'")));
1291 return TRUE;
1292}
1293
23ed00c4
VK
1294/**
1295 * Upgrade from V315 to V316
1296 */
1297static BOOL H_UpgradeFromV315(int currVersion, int newVersion)
1298{
1299 static TCHAR batch[] =
1300 _T("ALTER TABLE access_points ADD ap_state integer\n")
1301 _T("UPDATE access_points SET ap_state=0\n")
1302 _T("<END>");
1303 CHK_EXEC(SQLBatch(batch));
1304
1305 CHK_EXEC(CreateEventTemplate(EVENT_AP_ADOPTED, _T("SYS_AP_ADOPTED"), SEVERITY_NORMAL, EF_LOG,
1306 _T("Access point %2 changed state to ADOPTED"),
1307 _T("Generated when access point state changes to ADOPTED.\r\n")
1308 _T("Parameters:\r\n")
1309 _T(" 1) Access point object ID\r\n")
1310 _T(" 2) Access point name\r\n")
1311 _T(" 3) Access point MAC address\r\n")
1312 _T(" 4) Access point IP address\r\n")
1313 _T(" 5) Access point vendor name\r\n")
1314 _T(" 6) Access point model\r\n")
1315 _T(" 7) Access point serial number")));
1316
1317 CHK_EXEC(CreateEventTemplate(EVENT_AP_UNADOPTED, _T("SYS_AP_UNADOPTED"), SEVERITY_MAJOR, EF_LOG,
1318 _T("Access point %2 changed state to UNADOPTED"),
1319 _T("Generated when access point state changes to UNADOPTED.\r\n")
1320 _T("Parameters:\r\n")
1321 _T(" 1) Access point object ID\r\n")
1322 _T(" 2) Access point name\r\n")
1323 _T(" 3) Access point MAC address\r\n")
1324 _T(" 4) Access point IP address\r\n")
1325 _T(" 5) Access point vendor name\r\n")
1326 _T(" 6) Access point model\r\n")
1327 _T(" 7) Access point serial number")));
1328
1329 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='316' WHERE var_name='SchemaVersion'")));
1330 return TRUE;
1331}
1332
b2042b58
VK
1333/**
1334 * Upgrade from V314 to V315
1335 */
1336static BOOL H_UpgradeFromV314(int currVersion, int newVersion)
1337{
1338 static TCHAR batch[] =
1339 _T("ALTER TABLE thresholds ADD match_count integer\n")
1340 _T("UPDATE thresholds SET match_count=0 WHERE current_state=0\n")
1341 _T("UPDATE thresholds SET match_count=1 WHERE current_state<>0\n")
1342 _T("<END>");
1343 CHK_EXEC(SQLBatch(batch));
1344 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='315' WHERE var_name='SchemaVersion'")));
1345 return TRUE;
1346}
1347
2d7ee2e0 1348/**
1349 * Upgrade from V313 to V314
1350 */
1351static BOOL H_UpgradeFromV313(int currVersion, int newVersion)
1352{
b2042b58 1353 // Replace double backslash with single backslash in all "file download" (code 7) object tools
2d7ee2e0 1354 DB_RESULT hResult = SQLSelect(_T("SELECT tool_id, tool_data FROM object_tools WHERE tool_type=7"));
1355 if (hResult != NULL)
1356 {
1357 int count = DBGetNumRows(hResult);
1358 for(int i = 0; i < count; i++)
1359 {
1360 TCHAR* toolData = DBGetField(hResult, i, 1, NULL, 0);
1361 TranslateStr(toolData, _T("\\\\"), _T("\\"));
1362
1363 DB_STATEMENT statment = DBPrepare(g_hCoreDB, _T("UPDATE object_tools SET tool_data=? WHERE tool_id=?"));
1364 if (statment == NULL)
1365 return FALSE;
1366 DBBind(statment, 1, DB_SQLTYPE_TEXT, toolData, DB_BIND_DYNAMIC);
1367 DBBind(statment, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
1368 if(!DBExecute(statment))
1369 return FALSE;
1370 DBFreeStatement(statment);
1371 }
1372 DBFreeResult(hResult);
1373 }
1374
1375 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='314' WHERE var_name='SchemaVersion'")));
1376 return TRUE;
1377}
1378
1379/**
1380 * Upgrade from V312 to V313
1381 */
1382static BOOL H_UpgradeFromV312(int currVersion, int newVersion)
1383{
4dfa78b6
VK
1384 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("tool_name")));
1385 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("tool_data")));
1386 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("description")));
1387 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("confirmation_text")));
1388 CHK_EXEC(SetColumnNullable(_T("object_tools"), _T("matching_oid")));
1389 CHK_EXEC(SetColumnNullable(_T("object_tools_table_columns"), _T("col_name")));
2d7ee2e0 1390 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("tool_name")));
1391 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("tool_data")));
1392 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("description")));
1393 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("confirmation_text")));
1394 CHK_EXEC(ConvertStrings(_T("object_tools"), _T("tool_id"), _T("matching_oid")));
1395 CHK_EXEC(ConvertStrings(_T("object_tools_table_columns"), _T("tool_id"), _T("col_number"), _T("col_name"), false));
1396 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='313' WHERE var_name='SchemaVersion'")));
1397 return TRUE;
1398}
1399
6498e05a
VK
1400/**
1401 * Upgrade from V311 to V312
1402 */
1403static BOOL H_UpgradeFromV311(int currVersion, int newVersion)
1404{
1405 CHK_EXEC(CreateConfigParam(_T("EnableReportingServer"), _T("0"), 1, 1));
1406 CHK_EXEC(CreateConfigParam(_T("ReportingServerHostname"), _T("localhost"), 1, 1));
1407 CHK_EXEC(CreateConfigParam(_T("ReportingServerPort"), _T("4710"), 1, 1));
1408 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='312' WHERE var_name='SchemaVersion'")));
1409 return TRUE;
1410}
1411
9262b092
VK
1412/**
1413 * Upgrade from V310 to V311
1414 */
1415static BOOL H_UpgradeFromV310(int currVersion, int newVersion)
1416{
1417 IntegerArray<UINT32> deleteList;
1418
1419 // reports
1420 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM reports"));
1421 if (hResult != NULL)
1422 {
1423 int count = DBGetNumRows(hResult);
1424 for(int i = 0; i < count; i++)
1425 deleteList.add(DBGetFieldULong(hResult, i, 0));
1426 DBFreeResult(hResult);
1427 }
1428
1429 // report groups
1430 hResult = SQLSelect(_T("SELECT id FROM containers WHERE object_class=25"));
1431 if (hResult != NULL)
1432 {
1433 int count = DBGetNumRows(hResult);
1434 for(int i = 0; i < count; i++)
1435 deleteList.add(DBGetFieldULong(hResult, i, 0));
1436 DBFreeResult(hResult);
1437 }
1438
1439 for(int i = 0; i < deleteList.size(); i++)
1440 {
1441 TCHAR query[256];
1442
1443 _sntprintf(query, 256, _T("DELETE FROM object_properties WHERE object_id=%d"), deleteList.get(i));
1444 CHK_EXEC(SQLQuery(query));
1445
1446 _sntprintf(query, 256, _T("DELETE FROM object_custom_attributes WHERE object_id=%d"), deleteList.get(i));
1447 CHK_EXEC(SQLQuery(query));
1448
1449 _sntprintf(query, 256, _T("DELETE FROM acl WHERE object_id=%d"), deleteList.get(i));
1450 CHK_EXEC(SQLQuery(query));
1451
1452 _sntprintf(query, 256, _T("DELETE FROM container_members WHERE object_id=%d OR container_id=%d"), deleteList.get(i), deleteList.get(i));
1453 CHK_EXEC(SQLQuery(query));
1454 }
1455
1456 static TCHAR batch[] =
1457 _T("DROP TABLE reports\n")
1458 _T("DROP TABLE report_results\n")
1459 _T("DELETE FROM containers WHERE object_class=25\n")
1460 _T("DELETE FROM object_properties WHERE object_id=8\n")
1461 _T("<END>");
1462 CHK_EXEC(SQLBatch(batch));
1463 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='311' WHERE var_name='SchemaVersion'")));
1464 return TRUE;
1465}
1466
32745683
VK
1467/**
1468 * Upgrade from V309 to V310
1469 */
1470static BOOL H_UpgradeFromV309(int currVersion, int newVersion)
1471{
1472 static TCHAR batch[] =
1473 _T("ALTER TABLE interfaces ADD peer_proto integer\n")
1474 _T("UPDATE interfaces SET peer_proto=0\n")
1475 _T("<END>");
1476 CHK_EXEC(SQLBatch(batch));
1477 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='310' WHERE var_name='SchemaVersion'")));
1478 return TRUE;
1479}
1480
34a320ad
VK
1481/**
1482 * Upgrade from V308 to V309
1483 */
1484static BOOL H_UpgradeFromV308(int currVersion, int newVersion)
1485{
1486 CHK_EXEC(CreateConfigParam(_T("HelpDeskLink"), _T("none"), 1, 1));
1487 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='309' WHERE var_name='SchemaVersion'")));
1488 return TRUE;
1489}
1490
2ab9314f 1491/**
1492 * Upgrade from V307 to V308
1493 */
1494static BOOL H_UpgradeFromV307(int currVersion, int newVersion)
1495{
1496 static TCHAR batch[] =
32745683
VK
1497 _T("ALTER TABLE network_map_elements ADD flags integer\n")
1498 _T("UPDATE network_map_elements SET flags=0\n") //set all elements like manually generated
1499 _T("ALTER TABLE network_map_links ADD element_data $SQL:TEXT\n")
1500 _T("ALTER TABLE network_map_links ADD flags integer\n")
1501 _T("UPDATE network_map_links SET flags=0\n") //set all elements like manually generated
1502 _T("<END>");
2ab9314f 1503 CHK_EXEC(SQLBatch(batch));
1504
32745683
VK
1505 // it is assumed that now all autogenerated maps contain only autogenerated objects and links
1506 // get elements from autogenerated maps and set their flags to AUTO_GENERATED for map elements and map links
2ab9314f 1507 TCHAR query[256];
1508 _sntprintf(query, 256, _T("SELECT id FROM network_maps WHERE map_type=%d OR map_type=%d"),
1509 MAP_TYPE_LAYER2_TOPOLOGY, MAP_TYPE_IP_TOPOLOGY);
1510 DB_RESULT hResult = SQLSelect(query);
1511 if (hResult != NULL)
1512 {
1513 int count = DBGetNumRows(hResult);
1514 for(int i = 0; i < count; i++)
1515 {
1516 _sntprintf(query, 256, _T("UPDATE network_map_elements SET flags='1' WHERE map_id=%d"),
1517 DBGetFieldULong(hResult, i, 0));
1518 CHK_EXEC(SQLQuery(query));
1519 _sntprintf(query, 256, _T("UPDATE network_map_links SET flags='1' WHERE map_id=%d"),
1520 DBGetFieldULong(hResult, i, 0));
1521 CHK_EXEC(SQLQuery(query));
1522 }
1523 DBFreeResult(hResult);
1524 }
1525
1526 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='308' WHERE var_name='SchemaVersion'")));
1527 return TRUE;
1528}
1529
b3eb058b
VK
1530/**
1531 * Upgrade from V306 to V307
1532 */
1533static BOOL H_UpgradeFromV306(int currVersion, int newVersion)
1534{
1535 CHK_EXEC(SetColumnNullable(_T("config_clob"), _T("var_value")));
1536 CHK_EXEC(ConvertStrings(_T("config_clob"), _T("var_name"), NULL, _T("var_value"), true));
1537 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='307' WHERE var_name='SchemaVersion'")));
1538 return TRUE;
1539}
1540
d1730ccf
VK
1541/**
1542 * Upgrade from V305 to V306
1543 */
1544static BOOL H_UpgradeFromV305(int currVersion, int newVersion)
1545{
1546 CHK_EXEC(CreateConfigParam(_T("ExtendedLogQueryAccessControl"), _T("0"), 1, 0));
1547 CHK_EXEC(CreateConfigParam(_T("EnableTimedAlarmAck"), _T("1"), 1, 1));
9a1f7c45 1548 CHK_EXEC(CreateConfigParam(_T("EnableCheckPointSNMP"), _T("0"), 1, 0));
d1730ccf
VK
1549 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='306' WHERE var_name='SchemaVersion'")));
1550 return TRUE;
1551}
1552
035745fc
VK
1553/**
1554 * Upgrade from V304 to V305
1555 */
1556static BOOL H_UpgradeFromV304(int currVersion, int newVersion)
1557{
2ab9314f 1558 CHK_EXEC(CreateEventTemplate(EVENT_IF_PEER_CHANGED, _T("SYS_IF_PEER_CHANGED"), SEVERITY_NORMAL, EF_LOG,
035745fc
VK
1559 _T("New peer for interface %3 is %7 interface %10 (%12)"),
1560 _T("Generated when peer information for interface changes.\r\n")
1561 _T("Parameters:\r\n")
1562 _T(" 1) Local interface object ID\r\n")
1563 _T(" 2) Local interface index\r\n")
1564 _T(" 3) Local interface name\r\n")
1565 _T(" 4) Local interface IP address\r\n")
1566 _T(" 5) Local interface MAC address\r\n")
1567 _T(" 6) Peer node object ID\r\n")
1568 _T(" 7) Peer node name\r\n")
1569 _T(" 8) Peer interface object ID\r\n")
1570 _T(" 9) Peer interface index\r\n")
1571 _T(" 10) Peer interface name\r\n")
1572 _T(" 11) Peer interface IP address\r\n")
1573 _T(" 12) Peer interface MAC address")));
1574 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='305' WHERE var_name='SchemaVersion'")));
1575 return TRUE;
1576}
1577
fce4295c 1578/**
1579 * Upgrade from V303 to V304
1580 */
1581static BOOL H_UpgradeFromV303(int currVersion, int newVersion)
1582{
d184506f 1583 CHK_EXEC(CreateConfigParam(_T("StrictAlarmStatusFlow"), _T("0"), 1, 0));
fce4295c 1584 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='304' WHERE var_name='SchemaVersion'")));
1585 return TRUE;
1586}
1587
6f33021d 1588/**
1589 * Upgrade from V302 to V303
1590 */
1591static BOOL H_UpgradeFromV302(int currVersion, int newVersion)
1592{
1593 static TCHAR batch[] =
1594 _T("ALTER TABLE alarms ADD ack_timeout integer\n")
1595 _T("UPDATE alarms SET ack_timeout='0'\n")
1596 _T("<END>");
1597 CHK_EXEC(SQLBatch(batch));
1598
1599 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='303' WHERE var_name='SchemaVersion'")));
1600 return TRUE;
1601}
1602
27088c41
AK
1603/**
1604 * Upgrade from V301 to V302
1605 */
1606static BOOL H_UpgradeFromV301(int currVersion, int newVersion)
1607{
1608 static TCHAR batch[] =
1609 _T("DELETE FROM config WHERE var_name='DisableVacuum'\n")
1610 _T("<END>");
1611 CHK_EXEC(SQLBatch(batch));
1612
1613 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='302' WHERE var_name='SchemaVersion'")));
1614 return TRUE;
1615}
1616
1d34c533
VK
1617/**
1618 * Upgrade from V300 to V301
1619 */
1620static BOOL H_UpgradeFromV300(int currVersion, int newVersion)
1621{
1622 static TCHAR batch[] =
1623 _T("ALTER TABLE thresholds ADD script $SQL:TEXT\n")
1624 _T("ALTER TABLE thresholds ADD sample_count integer\n")
1625 _T("UPDATE thresholds SET sample_count=parameter_1\n")
1626 _T("ALTER TABLE thresholds DROP COLUMN parameter_1\n")
1627 _T("ALTER TABLE thresholds DROP COLUMN parameter_2\n")
1628 _T("<END>");
1629 CHK_EXEC(SQLBatch(batch));
1630
1631 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='301' WHERE var_name='SchemaVersion'")));
1632 return TRUE;
1633}
1634
244c65ef
VK
1635/**
1636 * Upgrade from V299 to V300
1637 */
1638static BOOL H_UpgradeFromV299(int currVersion, int newVersion)
1639{
1640 CHK_EXEC(CreateConfigParam(_T("EnableXMPPConnector"), _T("0"), 1, 1));
1641 CHK_EXEC(CreateConfigParam(_T("XMPPLogin"), _T("netxms@localhost"), 1, 1));
1642 CHK_EXEC(CreateConfigParam(_T("XMPPPassword"), _T("netxms"), 1, 1));
1643 CHK_EXEC(CreateConfigParam(_T("XMPPServer"), _T("localhost"), 1, 1));
1644 CHK_EXEC(CreateConfigParam(_T("XMPPPort"), _T("5222"), 1, 1));
1645
1646 SetColumnNullable(_T("users"), _T("full_name"));
1647 SetColumnNullable(_T("users"), _T("description"));
1648 SetColumnNullable(_T("users"), _T("cert_mapping_data"));
1649 SetColumnNullable(_T("user_groups"), _T("description"));
1650 SetColumnNullable(_T("userdb_custom_attributes"), _T("attr_value"));
1651
1652 ConvertStrings(_T("users"), _T("id"), _T("full_name"));
1653 ConvertStrings(_T("users"), _T("id"), _T("description"));
1654 ConvertStrings(_T("users"), _T("id"), _T("cert_mapping_data"));
1655 ConvertStrings(_T("user_groups"), _T("id"), _T("description"));
1656 ConvertStrings(_T("userdb_custom_attributes"), _T("object_id"), _T("attr_name"), _T("attr_name"), true);
1657 ConvertStrings(_T("userdb_custom_attributes"), _T("object_id"), _T("attr_name"), _T("attr_value"), true);
1658
1659 CHK_EXEC(SQLQuery(_T("ALTER TABLE users ADD xmpp_id varchar(127)")));
1660
1661 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='300' WHERE var_name='SchemaVersion'")));
1662 return TRUE;
1663}
1664
ce47611c
VK
1665/**
1666 * Upgrade from V298 to V299
1667 */
1668static BOOL H_UpgradeFromV298(int currVersion, int newVersion)
1669{
1670 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET message='Subnet %2 added',description='")
1671 _T("Generated when subnet object added to the database.\r\n")
1672 _T("Parameters:\r\n")
1673 _T(" 1) Subnet object ID\r\n")
1674 _T(" 2) Subnet name\r\n")
1675 _T(" 3) IP address\r\n")
1676 _T(" 4) Network mask")
1677 _T("' WHERE event_code=2")));
1678 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET message='Subnet %2 deleted',description='")
1679 _T("Generated when subnet object deleted from the database.\r\n")
1680 _T("Parameters:\r\n")
1681 _T(" 1) Subnet object ID\r\n")
1682 _T(" 2) Subnet name\r\n")
1683 _T(" 3) IP address\r\n")
1684 _T(" 4) Network mask")
1685 _T("' WHERE event_code=19")));
1686 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='299' WHERE var_name='SchemaVersion'")));
1687 return TRUE;
1688}
1689
af21affe
VK
1690/**
1691 * Upgrade from V297 to V298
1692 */
1693static BOOL H_UpgradeFromV297(int currVersion, int newVersion)
1694{
1695 CHK_EXEC(CreateConfigParam(_T("AgentDefaultSharedSecret"), _T("netxms"), 1, 0));
1696 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='298' WHERE var_name='SchemaVersion'")));
1697 return TRUE;
1698}
1699
e02953a4
VK
1700/**
1701 * Upgrade from V296 to V297
1702 */
1703static BOOL H_UpgradeFromV296(int currVersion, int newVersion)
1704{
1705 CHK_EXEC(CreateConfigParam(_T("UseSNMPTrapsForDiscovery"), _T("0"), 1, 1));
1706 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='297' WHERE var_name='SchemaVersion'")));
1707 return TRUE;
1708}
1709
71e4ed3a
VK
1710/**
1711 * Upgrade from V295 to V296
1712 */
1713static BOOL H_UpgradeFromV295(int currVersion, int newVersion)
1714{
1715 static TCHAR batch[] =
1716 _T("ALTER TABLE nodes ADD boot_time integer\n")
1717 _T("UPDATE nodes SET boot_time=0\n")
1718 _T("<END>");
1719 CHK_EXEC(SQLBatch(batch));
1720
1721 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='296' WHERE var_name='SchemaVersion'")));
1722 return TRUE;
1723}
1724
c59466d2
VK
1725/**
1726 * Upgrade from V294 to V295
1727 */
1728static BOOL H_UpgradeFromV294(int currVersion, int newVersion)
1729{
1730 CHK_EXEC(CreateConfigParam(_T("IcmpPingTimeout"), _T("1500"), 1, 1));
1731 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='295' WHERE var_name='SchemaVersion'")));
1732 return TRUE;
1733}
1734
1d4f7890
VK
1735/**
1736 * Upgrade from V293 to V294
1737 */
1738static BOOL H_UpgradeFromV293(int currVersion, int newVersion)
1739{
1740 static TCHAR batch[] =
1741 _T("DELETE FROM metadata WHERE var_name LIKE 'TDataTableCreationCommand_%'\n")
1742 _T("INSERT INTO metadata (var_name,var_value)")
1743 _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")
1744 _T("INSERT INTO metadata (var_name,var_value)")
a9f5aa55 1745 _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")
1d4f7890
VK
1746 _T("INSERT INTO metadata (var_name,var_value)")
1747 _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")
a9f5aa55
VK
1748 _T("INSERT INTO metadata (var_name,var_value)")
1749 _T(" VALUES ('TDataIndexCreationCommand_2','CREATE INDEX idx_tdata_rec_%d_id ON tdata_records_%d(record_id)')\n")
1d4f7890
VK
1750 _T("<END>");
1751 CHK_EXEC(SQLBatch(batch));
1752
2f1bc68b
VK
1753 RecreateTData(_T("nodes"), true, false);
1754 RecreateTData(_T("clusters"), true, false);
1755 RecreateTData(_T("mobile_devices"), true, false);
1d4f7890 1756
a9f5aa55 1757 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('ValidTDataIndex','1')")));
1d4f7890
VK
1758 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='294' WHERE var_name='SchemaVersion'")));
1759 return TRUE;
1760}
1761
bc969eaf
VK
1762/**
1763 * Upgrade from V292 to V293
1764 */
1765static BOOL H_UpgradeFromV292(int currVersion, int newVersion)
1766{
1767 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleShortTimeFormat"), _T("HH:mm"), 1, 0));
1768 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='293' WHERE var_name='SchemaVersion'")));
1769 return TRUE;
1770}
1771
badf9a95
VK
1772/**
1773 * Upgrade from V291 to V292
1774 */
1775static BOOL H_UpgradeFromV291(int currVersion, int newVersion)
1776{
1777 CHK_EXEC(SQLQuery(_T("ALTER TABLE event_policy ADD rule_guid varchar(36)")));
271e3971 1778 CHK_EXEC(GenerateGUID(_T("event_policy"), _T("rule_id"), _T("rule_guid")));
badf9a95
VK
1779 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='292' WHERE var_name='SchemaVersion'")));
1780 return TRUE;
1781}
1782
48d50116
VK
1783/**
1784 * Upgrade from V290 to V291
1785 */
1786static BOOL H_UpgradeFromV290(int currVersion, int newVersion)
1787{
1788 CHK_EXEC(SQLQuery(_T("UPDATE network_services SET service_type=7 WHERE service_type=6")));
1789 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='291' WHERE var_name='SchemaVersion'")));
1790 return TRUE;
1791}
1792
701726bc
VK
1793/**
1794 * Upgrade from V289 to V290
1795 */
1796static BOOL H_UpgradeFromV289(int currVersion, int newVersion)
1797{
1798 CHK_EXEC(SQLQuery(_T("ALTER TABLE network_maps ADD filter $SQL:TEXT")));
1799 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='290' WHERE var_name='SchemaVersion'")));
1800 return TRUE;
1801}
1802
90297892
VK
1803/**
1804 * Upgrade from V288 to V289
1805 */
1806static BOOL H_UpgradeFromV288(int currVersion, int newVersion)
1807{
d95c3dad
A
1808 static TCHAR batch[] =
1809 _T("ALTER TABLE dct_thresholds DROP COLUMN current_state\n")
1810 _T("<END>");
1811 CHK_EXEC(SQLBatch(batch));
90297892
VK
1812 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='289' WHERE var_name='SchemaVersion'")));
1813 return TRUE;
1814}
1815
a0dc14f9
VK
1816/**
1817 * Upgrade from V287 to V288
1818 */
1819static BOOL H_UpgradeFromV287(int currVersion, int newVersion)
1820{
1821 CHK_EXEC(CreateEventTemplate(EVENT_TABLE_THRESHOLD_ACTIVATED, _T("SYS_TABLE_THRESHOLD_ACTIVATED"), EVENT_SEVERITY_MINOR, EF_LOG,
1822 _T("Threshold activated on table \"%2\" row %4 (%5)"),
1823 _T("Generated when table threshold is activated.\r\n")
1824 _T("Parameters:\r\n")
1825 _T(" 1) Table DCI name\r\n")
1826 _T(" 2) Table DCI description\r\n")
1827 _T(" 3) Table DCI ID\r\n")
1828 _T(" 4) Table row\r\n")
1829 _T(" 5) Instance")));
1830
1831 CHK_EXEC(CreateEventTemplate(EVENT_TABLE_THRESHOLD_DEACTIVATED, _T("SYS_TABLE_THRESHOLD_DEACTIVATED"), EVENT_SEVERITY_NORMAL, EF_LOG,
1832 _T("Threshold deactivated on table \"%2\" row %4 (%5)"),
1833 _T("Generated when table threshold is deactivated.\r\n")
1834 _T("Parameters:\r\n")
1835 _T(" 1) Table DCI name\r\n")
1836 _T(" 2) Table DCI description\r\n")
1837 _T(" 3) Table DCI ID\r\n")
1838 _T(" 4) Table row\r\n")
1839 _T(" 5) Instance")));
1840
1841 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='288' WHERE var_name='SchemaVersion'")));
1842 return TRUE;
1843}
1844
9387bc59
VK
1845/**
1846 * Upgrade from V286 to V287
1847 */
1848static BOOL H_UpgradeFromV286(int currVersion, int newVersion)
1849{
1850 static TCHAR batch[] =
1851 _T("ALTER TABLE dc_table_columns ADD sequence_number integer\n")
1852 _T("UPDATE dc_table_columns SET sequence_number=0\n")
1853 _T("<END>");
1854 CHK_EXEC(SQLBatch(batch));
1855
1856 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='287' WHERE var_name='SchemaVersion'")));
1857 return TRUE;
1858}
1859
9098ad59
VK
1860/**
1861 * Upgrade from V285 to V286
1862 */
1863static BOOL H_UpgradeFromV285(int currVersion, int newVersion)
1864{
1865 CHK_EXEC(CreateTable(
1866 _T("CREATE TABLE dct_thresholds (")
1867 _T("id integer not null,")
1868 _T("table_id integer not null,")
1869 _T("sequence_number integer not null,")
1870 _T("current_state char(1) not null,")
1871 _T("activation_event integer not null,")
1872 _T("deactivation_event integer not null,")
1873 _T("PRIMARY KEY(id))")));
1874
1875 CHK_EXEC(CreateTable(
1876 _T("CREATE TABLE dct_threshold_conditions (")
1877 _T("threshold_id integer not null,")
1878 _T("group_id integer not null,")
1879 _T("sequence_number integer not null,")
1880 _T("column_name varchar(63) null,")
1881 _T("check_operation integer not null,")
1882 _T("check_value varchar(255) null,")
1883 _T("PRIMARY KEY(threshold_id,group_id,sequence_number))")));
1884
1885 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='286' WHERE var_name='SchemaVersion'")));
1886 return TRUE;
1887}
1888
17017844
VK
1889/**
1890 * Upgrade from V284 to V285
1891 */
1892static BOOL H_UpgradeFromV284(int currVersion, int newVersion)
1893{
1894 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_items_node_id ON items(node_id)")));
1895 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_dc_tables_node_id ON dc_tables(node_id)")));
1896
1897 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='285' WHERE var_name='SchemaVersion'")));
1898 return TRUE;
1899}
1900
eb4822b6
VK
1901/**
1902 * Upgrade from V283 to V284
1903 */
1904static BOOL H_UpgradeFromV283(int currVersion, int newVersion)
1905{
1906 CHK_EXEC(CreateConfigParam(_T("SNMPTrapPort"), _T("162"), 1, 1));
1907
1908 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='284' WHERE var_name='SchemaVersion'")));
1909 return TRUE;
1910}
1911
0b77b73e
VK
1912/**
1913 * Upgrade from V282 to V283
1914 */
1915static BOOL H_UpgradeFromV282(int currVersion, int newVersion)
1916{
1917 static TCHAR batch[] =
1918 _T("ALTER TABLE dc_table_columns ADD display_name varchar(255)\n")
1919 _T("UPDATE dc_table_columns SET display_name=column_name\n")
1920 _T("<END>");
1921 CHK_EXEC(SQLBatch(batch));
1922
1923 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='283' WHERE var_name='SchemaVersion'")));
1924 return TRUE;
1925}
1926
549f48b3
VK
1927/**
1928 * Upgrade from V281 to V282
1929 */
1930static BOOL H_UpgradeFromV281(int currVersion, int newVersion)
1931{
1932 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='WindowsConsoleUpgradeURL'")));
1933 CHK_EXEC(CreateConfigParam(_T("EnableObjectTransactions"), _T("0"), 1, 1));
1934
1935 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='282' WHERE var_name='SchemaVersion'")));
1936 return TRUE;
1937}
1938
22aaa779
VK
1939/**
1940 * Upgrade from V280 to V281
1941 */
1942static BOOL H_UpgradeFromV280(int currVersion, int newVersion)
1943{
1944 static TCHAR batch[] =
1945 _T("DELETE FROM metadata WHERE var_name='TDataTableCreationCommand'\n")
1946 _T("INSERT INTO metadata (var_name,var_value)")
1947 _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")
1948 _T("INSERT INTO metadata (var_name,var_value)")
1949 _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")
1950 _T("INSERT INTO metadata (var_name,var_value)")
1951 _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")
1952 _T("INSERT INTO metadata (var_name,var_value)")
1953 _T(" VALUES ('TDataIndexCreationCommand_1','CREATE INDEX idx_tdata_rec_%d_instance ON tdata_records_%d(instance)')\n")
1954 _T("<END>");
1955 CHK_EXEC(SQLBatch(batch));
1956
2f1bc68b
VK
1957 RecreateTData(_T("nodes"), false, false);
1958 RecreateTData(_T("clusters"), false, false);
1959 RecreateTData(_T("mobile_devices"), false, false);
22aaa779
VK
1960
1961 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='281' WHERE var_name='SchemaVersion'")));
1962 return TRUE;
1963}
1964
1965/**
1966 * Upgrade from V279 to V280
1967 */
1968static BOOL H_UpgradeFromV279(int currVersion, int newVersion)
1969{
1970 static TCHAR batch[] =
1971 _T("ALTER TABLE dc_table_columns ADD flags integer\n")
1972 _T("UPDATE dc_table_columns SET flags=data_type\n")
1973 _T("ALTER TABLE dc_table_columns DROP COLUMN data_type\n")
1974 _T("<END>");
1975 CHK_EXEC(SQLBatch(batch));
1976
1977 DB_RESULT hResult = SQLSelect(_T("SELECT item_id,instance_column FROM dc_tables"));
1978 if (hResult != NULL)
1979 {
1980 int count = DBGetNumRows(hResult);
1981 for(int i = 0; i < count; i++)
1982 {
1983 TCHAR columnName[MAX_COLUMN_NAME] = _T("");
1984 DBGetField(hResult, i, 1, columnName, MAX_COLUMN_NAME);
1985 if (columnName[0] != 0)
1986 {
1987 TCHAR query[256];
1988 _sntprintf(query, 256, _T("UPDATE dc_table_columns SET flags=flags+256 WHERE table_id=%d AND column_name=%s"),
1989 DBGetFieldLong(hResult, i, 0), (const TCHAR *)DBPrepareString(g_hCoreDB, columnName));
1990 CHK_EXEC(SQLQuery(query));
1991 }
1992 }
1993 DBFreeResult(hResult);
1994 }
1995 else
1996 {
1997 if (!g_bIgnoreErrors)
1998 return FALSE;
1999 }
2000
2001 CHK_EXEC(SQLQuery(_T("ALTER TABLE dc_tables DROP COLUMN instance_column")));
2002
2003 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='280' WHERE var_name='SchemaVersion'")));
2004 return TRUE;
2005}
2006
22ee6d97
VK
2007/**
2008 * Upgrade from V278 to V279
2009 */
2010static BOOL H_UpgradeFromV278(int currVersion, int newVersion)
2011{
2012 CHK_EXEC(CreateConfigParam(_T("DeleteEventsOfDeletedObject"), _T("1"), 1, 0));
2013 CHK_EXEC(CreateConfigParam(_T("DeleteAlarmsOfDeletedObject"), _T("1"), 1, 0));
2014
2015 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='279' WHERE var_name='SchemaVersion'")));
2016 return TRUE;
2017}
2018
85ae39bc
VK
2019/**
2020 * Upgrade from V277 to V278
2021 */
2022static BOOL H_UpgradeFromV277(int currVersion, int newVersion)
2023{
2024 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM clusters"));
2025 if (hResult != NULL)
2026 {
2027 int count = DBGetNumRows(hResult);
2028 for(int i = 0; i < count; i++)
2029 {
2030 DWORD id = DBGetFieldULong(hResult, i, 0);
2031 if (!CreateIDataTable(id))
2032 {
2033 if (!g_bIgnoreErrors)
2034 {
2035 DBFreeResult(hResult);
2036 return FALSE;
2037 }
2038 }
22aaa779 2039 if (!CreateTDataTable_preV281(id))
85ae39bc
VK
2040 {
2041 if (!g_bIgnoreErrors)
2042 {
2043 DBFreeResult(hResult);
2044 return FALSE;
2045 }
2046 }
2047 }
2048 DBFreeResult(hResult);
2049 }
2050 else
2051 {
2052 if (!g_bIgnoreErrors)
2053 return FALSE;
2054 }
2055
2056 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='278' WHERE var_name='SchemaVersion'")));
2057 return TRUE;
2058}
2059
62f41164
VK
2060/**
2061 * Upgrade from V276 to V277
2062 */
2063static BOOL H_UpgradeFromV276(int currVersion, int newVersion)
2064{
b4c2a628
VK
2065 CHK_EXEC(CreateTable(_T("CREATE TABLE dci_summary_tables (")
2066 _T("id integer not null,")
2067 _T("menu_path varchar(255) not null,")
2068 _T("title varchar(127) null,")
2069 _T("node_filter $SQL:TEXT null,")
2070 _T("flags integer not null,")
2071 _T("columns $SQL:TEXT null,")
2072 _T("PRIMARY KEY(id))")));
2073
62f41164
VK
2074 CHK_EXEC(CreateConfigParam(_T("DefaultMapBackgroundColor"), _T("0xffffff"), 1, 0));
2075
2076 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='277' WHERE var_name='SchemaVersion'")));
2077 return TRUE;
2078}
2079
55bdca5a
VK
2080/**
2081 * Upgrade from V275 to V276
2082 */
2083static BOOL H_UpgradeFromV275(int currVersion, int newVersion)
2084{
2085 static TCHAR batch[] =
2086 _T("ALTER TABLE dc_table_columns DROP COLUMN transformation_script\n")
2087 _T("ALTER TABLE dc_tables ADD transformation_script $SQL:TEXT\n")
2088 _T("<END>");
2089 CHK_EXEC(SQLBatch(batch));
2090
2091 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='276' WHERE var_name='SchemaVersion'")));
2092 return TRUE;
2093}
2094
8715a84c
VK
2095/**
2096 * Upgrade from V274 to V275
2097 */
2098static BOOL H_UpgradeFromV274(int currVersion, int newVersion)
2099{
2100 static TCHAR batch[] =
2101 _T("ALTER TABLE nodes ADD rack_image varchar(36)\n")
2102 _T("ALTER TABLE nodes ADD rack_position integer\n")
2103 _T("ALTER TABLE nodes ADD rack_id integer\n")
2104 _T("UPDATE nodes SET rack_image='00000000-0000-0000-0000-000000000000',rack_position=0,rack_id=0\n")
2105 _T("<END>");
2106 CHK_EXEC(SQLBatch(batch));
2107
2108 CHK_EXEC(CreateTable(_T("CREATE TABLE access_points (")
2109 _T("id integer not null,")
2110 _T("node_id integer not null,")
f1989a3a 2111 _T("mac_address varchar(12) null,")
8715a84c
VK
2112 _T("vendor varchar(64) null,")
2113 _T("model varchar(128) null,")
2114 _T("serial_number varchar(64) null,")
2115 _T("PRIMARY KEY(id))")));
2116
2117 CHK_EXEC(CreateTable(_T("CREATE TABLE racks (")
2118 _T("id integer not null,")
2119 _T("height integer not null,")
2120 _T("PRIMARY KEY(id))")));
2121
2122 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='275' WHERE var_name='SchemaVersion'")));
2123 return TRUE;
2124}
2125
d02f6b92
VK
2126/**
2127 * Upgrade from V273 to V274
2128 */
2129static BOOL H_UpgradeFromV273(int currVersion, int newVersion)
2130{
2131 static TCHAR batch[] =
2132 _T("ALTER TABLE items ADD samples integer\n")
2133 _T("UPDATE items SET samples=0\n")
2134 _T("<END>");
2135 CHK_EXEC(SQLBatch(batch));
2136
2137 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='274' WHERE var_name='SchemaVersion'")));
2138 return TRUE;
2139}
2140
f05a8a45
VK
2141/**
2142 * Upgrade from V272 to V273
2143 */
2144static BOOL H_UpgradeFromV272(int currVersion, int newVersion)
2145{
2146 CHK_EXEC(CreateConfigParam(_T("DefaultDCIRetentionTime"), _T("30"), 1, 0));
2147 CHK_EXEC(CreateConfigParam(_T("DefaultDCIPollingInterval"), _T("60"), 1, 0));
2148 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='273' WHERE var_name='SchemaVersion'")));
2149 return TRUE;
2150}
2151
0f75f8fc
VK
2152/**
2153 * Upgrade from V271 to V272
2154 */
2155static BOOL H_UpgradeFromV271(int currVersion, int newVersion)
2156{
2157 CHK_EXEC(CreateConfigParam(_T("SNMPTrapLogRetentionTime"), _T("90"), 1, 0));
28e1575f 2158 CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD driver_name varchar(32)\n")));
0f75f8fc
VK
2159 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='272' WHERE var_name='SchemaVersion'")));
2160 return TRUE;
2161}
2162
86634c2c
VK
2163/**
2164 * Upgrade from V270 to V271
2165 */
2166static BOOL H_UpgradeFromV270(int currVersion, int newVersion)
2167{
2168 static TCHAR batch[] =
2169 _T("ALTER TABLE object_properties ADD location_accuracy integer\n")
f2d5b2c4
VK
2170 _T("ALTER TABLE object_properties ADD location_timestamp integer\n")
2171 _T("UPDATE object_properties SET location_accuracy=0,location_timestamp=0\n")
86634c2c
VK
2172 _T("<END>");
2173 CHK_EXEC(SQLBatch(batch));
2174
2175 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='271' WHERE var_name='SchemaVersion'")));
2176 return TRUE;
2177}
2178
afbe5388
VK
2179/**
2180 * Upgrade from V269 to V270
2181 */
2182static BOOL H_UpgradeFromV269(int currVersion, int newVersion)
2183{
6f33021d 2184 static TCHAR batch[] =
afbe5388
VK
2185 _T("ALTER TABLE items ADD instd_method integer\n")
2186 _T("ALTER TABLE items ADD instd_data varchar(255)\n")
2187 _T("ALTER TABLE items ADD instd_filter $SQL:TEXT\n")
2188 _T("UPDATE items SET instd_method=0\n")
2189 _T("<END>");
2190 CHK_EXEC(SQLBatch(batch));
2191
2192 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='270' WHERE var_name='SchemaVersion'")));
2193 return TRUE;
2194}
2195
480e036b
VK
2196/**
2197 * Upgrade from V268 to V269
2198 */
2199static BOOL H_UpgradeFromV268(int currVersion, int newVersion)
2200{
b69710b9
VK
2201 CHK_EXEC(ResizeColumn(_T("alarms"), _T("message"), 2000, true));
2202 CHK_EXEC(ResizeColumn(_T("alarm_events"), _T("message"), 2000, true));
2203 CHK_EXEC(ResizeColumn(_T("event_log"), _T("event_message"), 2000, true));
2204 CHK_EXEC(ResizeColumn(_T("event_cfg"), _T("message"), 2000, true));
2205 CHK_EXEC(ResizeColumn(_T("event_policy"), _T("alarm_message"), 2000, true));
2206 CHK_EXEC(ResizeColumn(_T("items"), _T("name"), 1024, true));
2207 CHK_EXEC(ResizeColumn(_T("dc_tables"), _T("name"), 1024, true));
480e036b
VK
2208
2209 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_key")));
2210 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("alarm_message")));
2211 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("comments")));
2212 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("situation_instance")));
2213 CHK_EXEC(SetColumnNullable(_T("event_policy"), _T("script")));
2214 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_key")));
2215 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("alarm_message")));
2216 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("comments")));
2217 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("situation_instance")));
2218 CHK_EXEC(ConvertStrings(_T("event_policy"), _T("rule_id"), _T("script")));
2219
2220 CHK_EXEC(SetColumnNullable(_T("policy_situation_attr_list"), _T("attr_value")));
2221 // convert strings in policy_situation_attr_list
2222 DB_RESULT hResult = SQLSelect(_T("SELECT rule_id,situation_id,attr_name,attr_value FROM policy_situation_attr_list"));
2223 if (hResult != NULL)
2224 {
2225 if (SQLQuery(_T("DELETE FROM policy_situation_attr_list")))
2226 {
2227 TCHAR name[MAX_DB_STRING], value[MAX_DB_STRING], query[1024];
2228 int count = DBGetNumRows(hResult);
2229 for(int i = 0; i < count; i++)
2230 {
2231 LONG ruleId = DBGetFieldLong(hResult, i, 0);
2232 LONG situationId = DBGetFieldLong(hResult, i, 1);
2233 DBGetField(hResult, i, 2, name, MAX_DB_STRING);
2234 DBGetField(hResult, i, 3, value, MAX_DB_STRING);
2235
2236 DecodeSQLString(name);
2237 DecodeSQLString(value);
2238
2239 if (name[0] == 0)
2240 _tcscpy(name, _T("noname"));
2241
2242 _sntprintf(query, 1024, _T("INSERT INTO policy_situation_attr_list (rule_id,situation_id,attr_name,attr_value) VALUES (%d,%d,%s,%s)"),
2243 ruleId, situationId, (const TCHAR *)DBPrepareString(g_hCoreDB, name), (const TCHAR *)DBPrepareString(g_hCoreDB, value));
2244 if (!SQLQuery(query))
2245 {
2246 if (!g_bIgnoreErrors)
2247 {
2248 DBFreeResult(hResult);
2249 return FALSE;
2250 }
2251 }
2252 }
2253 }
2254 else
2255 {
2256 if (!g_bIgnoreErrors)
2257 {
2258 DBFreeResult(hResult);
2259 return FALSE;
2260 }
2261 }
2262 DBFreeResult(hResult);
2263 }
2264 else
2265 {
2266 if (!g_bIgnoreErrors)
2267 return FALSE;
2268 }
2269
2270 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("description")));
2271 CHK_EXEC(SetColumnNullable(_T("event_cfg"), _T("message")));
2272 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("description")));
2273 CHK_EXEC(ConvertStrings(_T("event_cfg"), _T("event_code"), _T("message")));
2274
2275 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='269' WHERE var_name='SchemaVersion'")));
2276 return TRUE;
2277}
2278
d6bf58f9
VK
2279/**
2280 * Upgrade from V267 to V268
2281 */
2282static BOOL H_UpgradeFromV267(int currVersion, int newVersion)
2283{
480e036b
VK
2284 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_request")));
2285 CHK_EXEC(SetColumnNullable(_T("network_services"), _T("check_responce")));
d6bf58f9
VK
2286 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_request")));
2287 CHK_EXEC(ConvertStrings(_T("network_services"), _T("id"), _T("check_responce")));
2288
480e036b 2289 CHK_EXEC(SetColumnNullable(_T("config"), _T("var_value")));
d6bf58f9
VK
2290 CHK_EXEC(ConvertStrings(_T("config"), _T("var_name"), NULL, _T("var_value"), true));
2291
480e036b 2292 CHK_EXEC(SetColumnNullable(_T("dci_schedules"), _T("schedule")));
d6bf58f9
VK
2293 CHK_EXEC(ConvertStrings(_T("dci_schedules"), _T("schedule_id"), _T("item_id"), _T("schedule"), false));
2294
2295 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='268' WHERE var_name='SchemaVersion'")));
2296 return TRUE;
2297}
2298
c1482463
VK
2299/**
2300 * Upgrade from V266 to V267
2301 */
2302static BOOL H_UpgradeFromV266(int currVersion, int newVersion)
2303{
2304 CHK_EXEC(CreateEventTemplate(EVENT_NODE_UNREACHABLE, _T("SYS_NODE_UNREACHABLE"), EVENT_SEVERITY_CRITICAL,
2305 EF_LOG, _T("Node unreachable because of network failure"),
2306 _T("Generated when node is unreachable by management server because of network failure.\r\nParameters:\r\n No event-specific parameters")));
2307 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='267' WHERE var_name='SchemaVersion'")));
2308 return TRUE;
2309}
2310
69bb7f47
VK
2311/**
2312 * Upgrade from V265 to V266
2313 */
2314static BOOL H_UpgradeFromV265(int currVersion, int newVersion)
2315{
2316 // create index on root event ID in event log
2a964810 2317 switch(g_dbSyntax)
69bb7f47
VK
2318 {
2319 case DB_SYNTAX_MSSQL:
2320 case DB_SYNTAX_PGSQL:
2321 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id) WHERE root_event_id > 0")));
2322 break;
2323 case DB_SYNTAX_ORACLE:
2324 CHK_EXEC(SQLQuery(_T("CREATE OR REPLACE FUNCTION zero_to_null(id NUMBER) ")
2325 _T("RETURN NUMBER ")
2326 _T("DETERMINISTIC ")
2327 _T("AS BEGIN")
2328 _T(" IF id > 0 THEN")
2329 _T(" RETURN id;")
2330 _T(" ELSE")
2331 _T(" RETURN NULL;")
2332 _T(" END IF;")
2333 _T("END;")));
2334 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(zero_to_null(root_event_id))")));
2335 break;
2336 default:
2337 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_event_log_root_id ON event_log(root_event_id)")));
2338 break;
2339 }
2340
2341 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_tables (")
2342 _T("id integer not null,")
2343 _T("name varchar(63) not null,")
2344 _T("flags integer not null,")
2345 _T("description $SQL:TXT4K null,")
2346 _T("PRIMARY KEY(id))")));
2347
2348 CHK_EXEC(CreateTable(_T("CREATE TABLE mapping_data (")
2349 _T("table_id integer not null,")
2350 _T("md_key varchar(63) not null,")
2351 _T("md_value varchar(255) null,")
f41af2c4 2352 _T("description $SQL:TXT4K null,")
69bb7f47
VK
2353 _T("PRIMARY KEY(table_id,md_key))")));
2354
2355 CHK_EXEC(SQLQuery(_T("DROP TABLE deleted_objects")));
2356
2357 CHK_EXEC(CreateConfigParam(_T("FirstFreeObjectId"), _T("100"), 0, 1, FALSE));
2358
2359 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='266' WHERE var_name='SchemaVersion'")));
2360 return TRUE;
2361}
2362
b1e9b6b3
VK
2363/**
2364 * Upgrade from V264 to V265
2365 */
2366static BOOL H_UpgradeFromV264(int currVersion, int newVersion)
2367{
2368 CHK_EXEC(CreateTable(_T("CREATE TABLE alarm_events (")
2369 _T("alarm_id integer not null,")
2370 _T("event_id $SQL:INT64 not null,")
2371 _T("event_code integer not null,")
2372 _T("event_name varchar(63) null,")
2373 _T("severity integer not null,")
2374 _T("source_object_id integer not null,")
2375 _T("event_timestamp integer not null,")
2376 _T("message varchar(255) null,")
2377 _T("PRIMARY KEY(alarm_id,event_id))")));
2378 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_alarm_events_alarm_id ON alarm_events(alarm_id)")));
2379 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='265' WHERE var_name='SchemaVersion'")));
2380 return TRUE;
2381}
2382
9aa67910
VK
2383/**
2384 * Upgrade from V263 to V264
2385 */
2386static BOOL H_UpgradeFromV263(int currVersion, int newVersion)
2387{
2388 CHK_EXEC(CreateTable(_T("CREATE TABLE mobile_devices (")
2389 _T("id integer not null,")
2390 _T("device_id varchar(64) not null,")
2391 _T("vendor varchar(64) null,")
2392 _T("model varchar(128) null,")
2393 _T("serial_number varchar(64) null,")
2394 _T("os_name varchar(32) null,")
2395 _T("os_version varchar(64) null,")
2396 _T("user_id varchar(64) null,")
171c2fd6 2397 _T("battery_level integer not null,")
9aa67910 2398 _T("PRIMARY KEY(id))")));
534e1b83 2399 CHK_EXEC(CreateConfigParam(_T("MobileDeviceListenerPort"), _T("4747"), 1, 1));
9aa67910
VK
2400 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='264' WHERE var_name='SchemaVersion'")));
2401 return TRUE;
2402}
2403
84880c89
VK
2404/**
2405 * Upgrade from V262 to V263
2406 */
2407static BOOL H_UpgradeFromV262(int currVersion, int newVersion)
2408{
2409 CHK_EXEC(SQLQuery(_T("ALTER TABLE network_maps ADD radius integer")));
2410 CHK_EXEC(SQLQuery(_T("UPDATE network_maps SET radius=-1")));
2411 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='263' WHERE var_name='SchemaVersion'")));
2412 return TRUE;
2413}
2414
dc4a1aec
AK
2415/**
2416 * Upgrade from V261 to V262
2417 */
2418static BOOL H_UpgradeFromV261(int currVersion, int newVersion)
2419{
534e1b83 2420 CHK_EXEC(CreateConfigParam(_T("ApplyDCIFromTemplateToDisabledDCI"), _T("0"), 1, 1));
dc4a1aec
AK
2421 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='262' WHERE var_name='SchemaVersion'")));
2422 return TRUE;
2423}
2424
4f50e45c
VK
2425/**
2426 * Upgrade from V260 to V261
2427 */
2428static BOOL H_UpgradeFromV260(int currVersion, int newVersion)
2429{
534e1b83 2430 CHK_EXEC(CreateConfigParam(_T("NumberOfBusinessServicePollers"), _T("10"), 1, 1));
4f50e45c
VK
2431 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfEventProcessors'")));
2432 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='261' WHERE var_name='SchemaVersion'")));
2433 return TRUE;
2434}
a203ea53 2435
4f50e45c
VK
2436/**
2437 * Upgrade from V259 to V260
2438 */
a203ea53
AK
2439static BOOL H_UpgradeFromV259(int currVersion, int newVersion)
2440{
534e1b83 2441 CHK_EXEC(CreateConfigParam(_T("UseFQDNForNodeNames"), _T("1"), 1, 1));
a203ea53
AK
2442 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='260' WHERE var_name='SchemaVersion'")));
2443 return TRUE;
2444}
2445
4f50e45c
VK
2446/**
2447 * Upgrade from V258 to V259
2448 */
5da6ca46
VK
2449static BOOL H_UpgradeFromV258(int currVersion, int newVersion)
2450{
2451 // have to made these columns nullable again because
2452 // because they was forgotten as NOT NULL in schema.in
2453 // and so some databases can still have them as NOT NULL
480e036b
VK
2454 CHK_EXEC(SetColumnNullable(_T("templates"), _T("apply_filter")));
2455 CHK_EXEC(SetColumnNullable(_T("containers"), _T("auto_bind_filter")));
5da6ca46
VK
2456
2457 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='259' WHERE var_name='SchemaVersion'")));
2458 return TRUE;
2459}
2460
4f50e45c
VK
2461/**
2462 * Upgrade from V257 to V258
2463 */
dd42ad0a
AK
2464static BOOL H_UpgradeFromV257(int currVersion, int newVersion)
2465{
6f33021d 2466 static TCHAR batch[] =
dd42ad0a
AK
2467 _T("ALTER TABLE nodes ADD down_since integer\n")
2468 _T("UPDATE nodes SET down_since=0\n")
2469 _T("<END>");
2470
2471 CHK_EXEC(SQLBatch(batch));
2472
534e1b83 2473 CHK_EXEC(CreateConfigParam(_T("DeleteUnreachableNodesPeriod"), _T("0"), 1, 1));
dd42ad0a
AK
2474
2475 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='258' WHERE var_name='SchemaVersion'")));
2476 return TRUE;
2477}
aa16f82b 2478
534e1b83
VK
2479/**
2480 * Upgrade from V256 to V257
2481 */
e4acd770
VK
2482static BOOL H_UpgradeFromV256(int currVersion, int newVersion)
2483{
6f33021d 2484 static TCHAR batch[] =
e4acd770
VK
2485 _T("ALTER TABLE network_maps ADD bg_color integer\n")
2486 _T("ALTER TABLE network_maps ADD link_routing integer\n")
2487 _T("UPDATE network_maps SET bg_color=16777215,link_routing=1\n")
2488 _T("ALTER TABLE network_map_links ADD routing integer\n")
2489 _T("ALTER TABLE network_map_links ADD bend_points $SQL:TXT4K\n")
2490 _T("UPDATE network_map_links SET routing=0\n")
2491 _T("<END>");
2492
2493 CHK_EXEC(SQLBatch(batch));
6f33021d 2494
e4acd770
VK
2495 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='257' WHERE var_name='SchemaVersion'")));
2496 return TRUE;
2497}
2498
534e1b83
VK
2499/**
2500 * Upgrade from V255 to V256
2501 */
aabe5b72
VK
2502static BOOL H_UpgradeFromV255(int currVersion, int newVersion)
2503{
534e1b83
VK
2504 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleDateFormat"), _T("dd.MM.yyyy"), 1, 0));
2505 CHK_EXEC(CreateConfigParam(_T("DefaultConsoleTimeFormat"), _T("HH:mm:ss"), 1, 0));
aabe5b72
VK
2506
2507 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='256' WHERE var_name='SchemaVersion'")));
2508 return TRUE;
2509}
2510
480e036b
VK
2511/**
2512 * Upgrade from V254 to V255
2513 */
5f6bc78c
VK
2514static BOOL H_UpgradeFromV254(int currVersion, int newVersion)
2515{
6f33021d 2516 static TCHAR batch[] =
5f6bc78c
VK
2517 _T("ALTER TABLE alarms ADD resolved_by integer\n")
2518 _T("UPDATE alarms SET resolved_by=0\n")
2519 _T("UPDATE alarms SET alarm_state=3 WHERE alarm_state=2\n")
2520 _T("<END>");
2521
2522 CHK_EXEC(SQLBatch(batch));
2523
3e9e0cc5 2524 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='255' WHERE var_name='SchemaVersion'")));
5f6bc78c
VK
2525 return TRUE;
2526}
40647141 2527
badf9a95
VK
2528/**
2529 * Upgrade from V253 to V254
2530 */
40647141
VK
2531static BOOL H_UpgradeFromV253(int currVersion, int newVersion)
2532{
6f33021d 2533 static TCHAR batch[] =
40647141
VK
2534 _T("ALTER TABLE network_maps ADD flags integer\n")
2535 _T("ALTER TABLE network_maps ADD link_color integer\n")
2536 _T("UPDATE network_maps SET flags=1,link_color=-1\n")
2537 _T("ALTER TABLE network_map_links ADD color integer\n")
2538 _T("ALTER TABLE network_map_links ADD status_object integer\n")
2539 _T("UPDATE network_map_links SET color=-1,status_object=0\n")
2540 _T("<END>");
2541
2542 CHK_EXEC(SQLBatch(batch));
2543
2544 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='254' WHERE var_name='SchemaVersion'")));
2545 return TRUE;
2546}
2547
d6bf58f9
VK
2548/**
2549 * Upgrade from V252 to V253
2550 */
feab3324
VK
2551static BOOL H_UpgradeFromV252(int currVersion, int newVersion)
2552{
480e036b 2553 CHK_EXEC(SetColumnNullable(_T("templates"), _T("apply_filter")));
242e9c7f
VK
2554 CHK_EXEC(ConvertStrings(_T("templates"), _T("id"), _T("apply_filter")));
2555
480e036b 2556 CHK_EXEC(SetColumnNullable(_T("containers"), _T("auto_bind_filter")));
242e9c7f
VK
2557 CHK_EXEC(ConvertStrings(_T("containers"), _T("id"), _T("auto_bind_filter")));
2558
6f33021d 2559 static TCHAR batch[] =
feab3324 2560 _T("ALTER TABLE templates ADD flags integer\n")
2984ac95
VK
2561 _T("UPDATE templates SET flags=0 WHERE enable_auto_apply=0\n")
2562 _T("UPDATE templates SET flags=3 WHERE enable_auto_apply<>0\n")
feab3324
VK
2563 _T("ALTER TABLE templates DROP COLUMN enable_auto_apply\n")
2564 _T("ALTER TABLE containers ADD flags integer\n")
2984ac95
VK
2565 _T("UPDATE containers SET flags=0 WHERE enable_auto_bind=0\n")
2566 _T("UPDATE containers SET flags=3 WHERE enable_auto_bind<>0\n")
feab3324
VK
2567 _T("ALTER TABLE containers DROP COLUMN enable_auto_bind\n")
2568 _T("<END>");
feab3324
VK
2569 CHK_EXEC(SQLBatch(batch));
2570
2571 CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOBIND, _T("SYS_CONTAINER_AUTOBIND"), EVENT_SEVERITY_NORMAL, 1,
6f33021d 2572 _T("Node %2 automatically bound to container %4"),
feab3324
VK
2573 _T("Generated when node bound to container object by autobind rule.\r\n")
2574 _T("Parameters:#\r\n")
2575 _T(" 1) Node ID\r\n")
2576 _T(" 2) Node name\r\n")
2577 _T(" 3) Container ID\r\n")
2578 _T(" 4) Container name")
2579 ));
2580
2581 CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOUNBIND, _T("SYS_CONTAINER_AUTOUNBIND"), EVENT_SEVERITY_NORMAL, 1,
6f33021d 2582 _T("Node %2 automatically unbound from container %4"),
feab3324
VK
2583 _T("Generated when node unbound from container object by autobind rule.\r\n")
2584 _T("Parameters:#\r\n")
2585 _T(" 1) Node ID\r\n")
2586 _T(" 2) Node name\r\n")
2587 _T(" 3) Container ID\r\n")
2588 _T(" 4) Container name")
2589 ));
2590
2591 CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOAPPLY, _T("SYS_TEMPLATE_AUTOAPPLY"), EVENT_SEVERITY_NORMAL, 1,
6f33021d 2592 _T("Template %4 automatically applied to node %2"),
feab3324
VK
2593 _T("Generated when template applied to node by autoapply rule.\r\n")
2594 _T("Parameters:#\r\n")
2595 _T(" 1) Node ID\r\n")
2596 _T(" 2) Node name\r\n")
2597 _T(" 3) Template ID\r\n")
2598 _T(" 4) Template name")
2599 ));
2600
2601 CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOREMOVE, _T("SYS_TEMPLATE_AUTOREMOVE"), EVENT_SEVERITY_NORMAL, 1,
6f33021d 2602 _T("Template %4 automatically removed from node %2"),
feab3324
VK
2603 _T("Generated when template removed from node by autoapply rule.\r\n")
2604 _T("Parameters:#\r\n")
2605 _T(" 1) Node ID\r\n")
2606 _T(" 2) Node name\r\n")
2607 _T(" 3) Template ID\r\n")
2608 _T(" 4) Template name")
2609 ));
2610
65790062
VK
2611 TCHAR buffer[64];
2612 _sntprintf(buffer, 64, _T("%d"), ConfigReadInt(_T("AllowedCiphers"), 15) + 16);
2613 CreateConfigParam(_T("AllowedCiphers"), buffer, 1, 1, TRUE);
2614
feab3324
VK
2615 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='253' WHERE var_name='SchemaVersion'")));
2616 return TRUE;
2617}
2618
badf9a95
VK
2619/**
2620 * Upgrade from V251 to V252
2621 */
1e6b68a6
VK
2622static BOOL H_UpgradeFromV251(int currVersion, int newVersion)
2623{
6f33021d 2624 static TCHAR batch[] =
1e6b68a6
VK
2625 _T("ALTER TABLE interfaces ADD admin_state integer\n")
2626 _T("ALTER TABLE interfaces ADD oper_state integer\n")
2627 _T("UPDATE interfaces SET admin_state=0,oper_state=0\n")
2628 _T("<END>");
2629
2630 CHK_EXEC(SQLBatch(batch));
2631
2632 CHK_EXEC(CreateEventTemplate(EVENT_INTERFACE_UNEXPECTED_UP, _T("SYS_IF_UNEXPECTED_UP"), EVENT_SEVERITY_MAJOR, 1,
6f33021d 2633 _T("Interface \"%2\" unexpectedly changed state to UP (IP Addr: %3/%4, IfIndex: %5)"),
1e6b68a6
VK
2634 _T("Generated when interface goes up but it's expected state set to DOWN.\r\n")
2635 _T("Please note that source of event is node, not an interface itself.\r\n")
2636 _T("Parameters:#\r\n")
2637 _T(" 1) Interface object ID\r\n")
2638 _T(" 2) Interface name\r\n")
2639 _T(" 3) Interface IP address\r\n")
2640 _T(" 4) Interface netmask\r\n")
2641 _T(" 5) Interface index")
2642 ));
2643
2644 CHK_EXEC(CreateEventTemplate(EVENT_INTERFACE_EXPECTED_DOWN, _T("SYS_IF_EXPECTED_DOWN"), EVENT_SEVERITY_NORMAL, 1,
6f33021d 2645 _T("Interface \"%2\" with expected state DOWN changed state to DOWN (IP Addr: %3/%4, IfIndex: %5)"),
1e6b68a6
VK
2646 _T("Generated when interface goes down and it's expected state is DOWN.\r\n")
2647 _T("Please note that source of event is node, not an interface itself.\r\n")
2648 _T("Parameters:#\r\n")
2649 _T(" 1) Interface object ID\r\n")
2650 _T(" 2) Interface name\r\n")
2651 _T(" 3) Interface IP address\r\n")
2652 _T(" 4) Interface netmask\r\n")
2653 _T(" 5) Interface index")
2654 ));
2655
2656 // Create rule pair in event processing policy
2657 int ruleId = 0;
2658 DB_RESULT hResult = SQLSelect(_T("SELECT max(rule_id) FROM event_policy"));
2659 if (hResult != NULL)
2660 {
2661 ruleId = DBGetFieldLong(hResult, 0, 0) + 1;
2662 DBFreeResult(hResult);
2663 }
2664
2665 TCHAR query[1024];
6f33021d 2666 _sntprintf(query, 1024,
1e6b68a6
VK
2667 _T("INSERT INTO event_policy (rule_id,flags,comments,alarm_message,alarm_severity,alarm_key,")
2668 _T("script,alarm_timeout,alarm_timeout_event,situation_id,situation_instance) VALUES ")
2669 _T("(%d,7944,'Show alarm when interface is unexpectedly up','%%m',5,'IF_UNEXP_UP_%%i_%%1',")
2670 _T("'#00',0,%d,0,'#00')"), ruleId, EVENT_ALARM_TIMEOUT);
2671 CHK_EXEC(SQLQuery(query));
2672 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_INTERFACE_UNEXPECTED_UP);
2673 CHK_EXEC(SQLQuery(query));
2674 ruleId++;
2675
6f33021d 2676 _sntprintf(query, 1024,
1e6b68a6
VK
2677 _T("INSERT INTO event_policy (rule_id,flags,comments,alarm_message,alarm_severity,alarm_key,")
2678 _T("script,alarm_timeout,alarm_timeout_event,situation_id,situation_instance) VALUES ")
2679 _T("(%d,7944,'Acknowlege interface unexpectedly up alarms when interface goes down','%%m',")
2680 _T("6,'IF_UNEXP_UP_%%i_%%1','#00',0,%d,0,'#00')"), ruleId, EVENT_ALARM_TIMEOUT);
2681 CHK_EXEC(SQLQuery(query));
2682 _sntprintf(query, 1024, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"), ruleId, EVENT_INTERFACE_EXPECTED_DOWN);
2683 CHK_EXEC(SQLQuery(query));
2684
2685 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='252' WHERE var_name='SchemaVersion'")));
2686 return TRUE;
2687}
2688
9aa67910
VK
2689/**
2690 * Upgrade from V250 to V251
2691 */
711e5e9a
VK
2692static BOOL H_UpgradeFromV250(int currVersion, int newVersion)
2693{
6f33021d 2694 static TCHAR batch[] =
711e5e9a
VK
2695 _T("ALTER TABLE thresholds ADD current_severity integer\n")
2696 _T("ALTER TABLE thresholds ADD last_event_timestamp integer\n")
2697 _T("UPDATE thresholds SET current_severity=0,last_event_timestamp=0\n")
2698 _T("<END>");
2699
2700 CHK_EXEC(SQLBatch(batch));
2701
480e036b
VK
2702 CHK_EXEC(SetColumnNullable(_T("thresholds"), _T("fire_value")));
2703 CHK_EXEC(SetColumnNullable(_T("thresholds"), _T("rearm_value")));
711e5e9a
VK
2704 CHK_EXEC(ConvertStrings(_T("thresholds"), _T("threshold_id"), _T("fire_value")));
2705 CHK_EXEC(ConvertStrings(_T("thresholds"), _T("threshold_id"), _T("rearm_value")));
2706
2707 CHK_EXEC(CreateConfigParam(_T("EnableNXSLContainerFunctions"), _T("1"), 1, 1));
2708 CHK_EXEC(CreateConfigParam(_T("UseDNSNameForDiscoveredNodes"), _T("0"), 1, 0));
2709 CHK_EXEC(CreateConfigParam(_T("AllowTrapVarbindsConversion"), _T("1"), 1, 1));
2710
2711 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='251' WHERE var_name='SchemaVersion'")));
2712 return TRUE;
2713}
2714
9aa67910
VK
2715/**
2716 * Upgrade from V249 to V250
2717 */
ae2a3458
VK
2718static BOOL H_UpgradeFromV249(int currVersion, int newVersion)
2719{
2720 CHK_EXEC(CreateTable(_T("CREATE TABLE licenses (")
2721 _T("id integer not null,")
2722 _T("content $SQL:TEXT null,")
2723 _T("PRIMARY KEY(id))")));
2724
2725 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='250' WHERE var_name='SchemaVersion'")));
2726 return TRUE;
2727}
2728
9aa67910
VK
2729/**
2730 * Upgrade from V248 to V249
2731 */
d6124fa0
VK
2732#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)")
2733#define TDATA_INDEX_MSSQL _T("CREATE CLUSTERED INDEX idx_tdata_%d_id_timestamp ON tdata_%d(item_id,tdata_timestamp)")
2734#define TDATA_INDEX_PGSQL _T("CREATE INDEX idx_tdata_%d_timestamp_id ON tdata_%d(tdata_timestamp,item_id)")
2735#define TDATA_INDEX_DEFAULT _T("CREATE INDEX idx_tdata_%d_id_timestamp ON tdata_%d(item_id,tdata_timestamp)")
2736
2737static BOOL CreateTData(DWORD nodeId)
2738{
2739 TCHAR query[256];
2740
2741 _sntprintf(query, 256, TDATA_CREATE_QUERY, (int)nodeId);
2742 CHK_EXEC(SQLQuery(query));
2743
2a964810 2744 switch(g_dbSyntax)
d6124fa0
VK
2745 {
2746 case DB_SYNTAX_MSSQL:
2747 _sntprintf(query, 256, TDATA_INDEX_MSSQL, (int)nodeId, (int)nodeId);
2748 break;
2749 case DB_SYNTAX_PGSQL:
2750 _sntprintf(query, 256, TDATA_INDEX_PGSQL, (int)nodeId, (int)nodeId);
2751 break;
2752 default:
2753 _sntprintf(query, 256, TDATA_INDEX_DEFAULT, (int)nodeId, (int)nodeId);
2754 break;
2755 }
2756 CHK_EXEC(SQLQuery(query));
2757
2758 return TRUE;
2759}
2760
2761static BOOL H_UpgradeFromV248(int currVersion, int newVersion)
2762{
2763 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataTableCreationCommand','") TDATA_CREATE_QUERY _T("')")));
2764
2a964810 2765 switch(g_dbSyntax)
d6124fa0
VK
2766 {
2767 case DB_SYNTAX_MSSQL:
2768 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_MSSQL _T("')")));
2769 break;
2770 case DB_SYNTAX_PGSQL:
2771 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_PGSQL _T("')")));
2772 break;
2773 default:
2774 CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('TDataIndexCreationCommand_0','") TDATA_INDEX_DEFAULT _T("')")));
2775 break;
2776 }
2777
2778 CHK_EXEC(CreateTable(_T("CREATE TABLE dct_column_names (")
2779 _T("column_id integer not null,")
2780 _T("column_name varchar(63) not null,")
2781 _T("PRIMARY KEY(column_id))")));
2782
2783 DB_RESULT hResult = SQLSelect(_T("SELECT id FROM nodes"));
2784 if (hResult != NULL)
2785 {
2786 int count = DBGetNumRows(hResult);
2787 for(int i = 0 ; i < count; i++)
2788 {
2789 if (!CreateTData(DBGetFieldULong(hResult, i, 0)))
2790 {
2791 if (!g_bIgnoreErrors)
2792 {
2793 DBFreeResult(hResult);
2794 return FALSE;
2795 }
2796 }
2797 }
2798 DBFreeResult(hResult);
2799 }
2800 else
2801 {
2802 if (!g_bIgnoreErrors)
2803 return FALSE;
2804 }
2805
2806 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='249' WHERE var_name='SchemaVersion'")));
2807 return TRUE;
2808}
2809
480e036b
VK
2810/**
2811 * Upgrade from V247 to V248
2812 */
cc8ce218
VK
2813static BOOL H_UpgradeFromV247(int currVersion, int newVersion)
2814{
2815 CHK_EXEC(CreateTable(_T("CREATE TABLE dc_tables (")
2816 _T("item_id integer not null,")
2817 _T("node_id integer not null,")
2818 _T("template_id integer not null,")
2819 _T("template_item_id integer not null,")
2820 _T("name varchar(255) null,")
2821 _T("instance_column varchar(63) null,")
2822 _T("description varchar(255) null,")
2823 _T("flags integer not null,")
2824 _T("source integer not null,")
2825 _T("snmp_port integer not null,")
2826 _T("polling_interval integer not null,")
2827 _T("retention_time integer not null,")
2828 _T("status integer not null,")
2829 _T("system_tag varchar(255) null,")
2830 _T("resource_id integer not null,")
2831 _T("proxy_node integer not null,")
2832 _T("perftab_settings $SQL:TEXT null,")
2833 _T("PRIMARY KEY(item_id))")));
2834
2835 CHK_EXEC(CreateTable(_T("CREATE TABLE dc_table_columns (")
2836 _T("table_id integer not null,")
2837 _T("column_name varchar(63) not null,")
2838 _T("snmp_oid varchar(1023) null,")
2839 _T("data_type integer not null,")
2840 _T("transformation_script $SQL:TEXT null,")
2841 _T("PRIMARY KEY(table_id,column_name))")));
2842
2843 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='248' WHERE var_name='SchemaVersion'")));
2844 return TRUE;
2845}
2846
480e036b
VK
2847/**
2848 * Upgrade from V246 to V247
2849 */
ff392ecf
VK
2850static BOOL H_UpgradeFromV246(int currVersion, int newVersion)
2851{
2852 static TCHAR insertQuery[] = _T("INSERT INTO object_custom_attributes (object_id,attr_name,attr_value) VALUES (?,?,?)");
2853
480e036b 2854 CHK_EXEC(SetColumnNullable(_T("object_custom_attributes"), _T("attr_value")));
ff392ecf
VK
2855
2856 // Convert strings in object_custom_attributes table
2857 DB_RESULT hResult = SQLSelect(_T("SELECT object_id,attr_name,attr_value FROM object_custom_attributes"));
2858 if (hResult != NULL)
2859 {
2860 if (SQLQuery(_T("DELETE FROM object_custom_attributes")))
2861 {
2862 TCHAR errorText[DBDRV_MAX_ERROR_TEXT];
2863 DB_STATEMENT hStmt = DBPrepareEx(g_hCoreDB, insertQuery, errorText);
2864 if (hStmt != NULL)
2865 {
2866 TCHAR name[128], *value;
2867 int count = DBGetNumRows(hResult);
2868 for(int i = 0; i < count; i++)
2869 {
967893bb 2870 UINT32 id = DBGetFieldULong(hResult, i, 0);
ff392ecf
VK
2871 DBGetField(hResult, i, 1, name, 128);
2872 DecodeSQLString(name);
2873 value = DBGetField(hResult, i, 2, NULL, 0);
2874 DecodeSQLString(value);
2875
2876 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, id);
2877 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, name, DB_BIND_STATIC);
2878 DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, value, DB_BIND_DYNAMIC);
2879 if (g_bTrace)
2880 ShowQuery(insertQuery);
2881 if (!DBExecuteEx(hStmt, errorText))
2882 {
2883 WriteToTerminalEx(_T("SQL query failed (%s):\n\x1b[33;1m%s\x1b[0m\n"), errorText, insertQuery);
2884 if (!g_bIgnoreErrors)
2885 {
2886 DBFreeStatement(hStmt);
2887 DBFreeResult(hResult);
2888 return FALSE;
2889 }
2890 }
2891 }
2892 DBFreeStatement(hStmt);
2893 }
2894 else
2895 {
2896 WriteToTerminalEx(_T("SQL query failed (%s):\n\x1b[33;1m%s\x1b[0m\n"), errorText, insertQuery);
2897 if (!g_bIgnoreErrors)
2898 {
2899 DBFreeResult(hResult);
2900 return FALSE;
2901 }
2902 }
2903 }
2904 else
2905 {
2906 if (!g_bIgnoreErrors)
2907 {
2908 DBFreeResult(hResult);
2909 return FALSE;
2910 }
2911 }
2912
2913 DBFreeResult(hResult);
2914 }
2915 else
2916 {
2917 if (!g_bIgnoreErrors)
2918 return FALSE;
2919 }
2920
2921 CHK_EXEC(SQLQuery(_T("CREATE INDEX idx_ocattr_oid ON object_custom_attributes(object_id)")));
2922 CHK_EXEC(CreateConfigParam(_T("AlarmHistoryRetentionTime"), _T("180"), 1, 0));
2923
2924 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='247' WHERE var_name='SchemaVersion'")));
2925 return TRUE;
2926}
2927
d6bf58f9
VK
2928/**
2929 * Upgrade from V245 to V246
2930 */
98cd01bb
VK
2931static BOOL H_UpgradeFromV245(int currVersion, int newVersion)
2932{
6f33021d 2933 static TCHAR batch[] =
98cd01bb
VK
2934 _T("ALTER TABLE snmp_trap_pmap ADD flags integer\n")
2935 _T("UPDATE snmp_trap_pmap SET flags=0\n")
2936 _T("<END>");
2937
2938 CHK_EXEC(SQLBatch(batch));
2939
480e036b 2940 CHK_EXEC(SetColumnNullable(_T("snmp_trap_pmap"), _T("description")));
d6bf58f9 2941 CHK_EXEC(ConvertStrings(_T("snmp_trap_pmap"), _T("trap_id"), _T("parameter"), _T("description"), false));
6f33021d 2942
480e036b 2943 CHK_EXEC(SetColumnNullable(_T("cluster_resources"), _T("resource_name")));
d6bf58f9 2944 CHK_EXEC(ConvertStrings(_T("cluster_resources"), _T("cluster_id"), _T("resource_id"), _T("resource_name"), false));
6f33021d 2945
98cd01bb
VK
2946 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='246' WHERE var_name='SchemaVersion'")));
2947 return TRUE;
2948}
2949
d6bf58f9
VK
2950/**
2951 * Upgrade from V244 to V245
2952 */
4866d57b
VK
2953static BOOL H_UpgradeFromV244(int currVersion, int newVersion)
2954{
6f33021d 2955 static TCHAR batch[] =
4866d57b
VK
2956 _T("ALTER TABLE nodes ADD runtime_flags integer\n")
2957 _T("UPDATE nodes SET runtime_flags=0\n")
2958 _T("<END>");
2959
2960 CHK_EXEC(SQLBatch(batch));
2961
480e036b
VK
2962 CHK_EXEC(SetColumnNullable(_T("actions"), _T("rcpt_addr")));
2963 CHK_EXEC(SetColumnNullable(_T("actions"), _T("email_subject")));
2964 CHK_EXEC(SetColumnNullable(_T("actions"), _T("action_data")));
4866d57b
VK
2965
2966 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("rcpt_addr")));
2967 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("email_subject")));
2968 CHK_EXEC(ConvertStrings(_T("actions"), _T("action_id"), _T("action_data")));
6f33021d 2969
4866d57b
VK
2970 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='245' WHERE var_name='SchemaVersion'")));
2971 return TRUE;
2972}
2973
2974
aa16f82b
VK
2975//
2976// Upgrade from V243 to V244
2977//
2978
2979static BOOL H_UpgradeFromV243(int currVersion, int newVersion)
2980{
6f33021d 2981 static TCHAR batch[] =
aa16f82b
VK
2982 _T("ALTER TABLE interfaces ADD dot1x_pae_state integer\n")
2983 _T("ALTER TABLE interfaces ADD dot1x_backend_state integer\n")
2984 _T("UPDATE interfaces SET dot1x_pae_state=0,dot1x_backend_state=0\n")
2985 _T("<END>");
2986
2987 CHK_EXEC(SQLBatch(batch));
2988
2989 CHK_EXEC(CreateEventTemplate(EVENT_8021X_PAE_STATE_CHANGED, _T("SYS_8021X_PAE_STATE_CHANGED"),
6f33021d 2990 EVENT_SEVERITY_NORMAL, 1, _T("Port %6 PAE state changed from %4 to %2"),
aa16f82b
VK
2991 _T("Generated when switch port PAE state changed.\r\nParameters:\r\n")
2992 _T(" 1) New PAE state code\r\n")
2993 _T(" 2) New PAE state as text\r\n")
2994 _T(" 3) Old PAE state code\r\n")
2995 _T(" 4) Old PAE state as text\r\n")
2996 _T(" 5) Interface index\r\n")
2997 _T(" 6) Interface name")));
2998
2999 CHK_EXEC(CreateEventTemplate(EVENT_8021X_BACKEND_STATE_CHANGED, _T("SYS_8021X_BACKEND_STATE_CHANGED"),
6f33021d 3000 EVENT_SEVERITY_NORMAL, 1, _T("Port %6 backend authentication state changed from %4 to %2"),
aa16f82b
VK
3001 _T("Generated when switch port backend authentication state changed.\r\nParameters:\r\n")
3002 _T(" 1) New backend state code\r\n")
3003 _T(" 2) New backend state as text\r\n")
3004 _T(" 3) Old backend state code\r\n")
3005 _T(" 4) Old backend state as text\r\n")
3006 _T(" 5) Interface index\r\n")
3007 _T(" 6) Interface name")));
3008
3009 CHK_EXEC(CreateEventTemplate(EVENT_8021X_PAE_FORCE_UNAUTH, _T("SYS_8021X_PAE_FORCE_UNAUTH"),
6f33021d 3010 EVENT_SEVERITY_MAJOR, 1, _T("Port %2 switched to force unauthorize state"),
aa16f82b
VK
3011 _T("Generated when switch port PAE state changed to FORCE UNAUTHORIZE.\r\nParameters:\r\n")
3012 _T(" 1) Interface index\r\n")
3013 _T(" 2) Interface name")));
3014
3015 CHK_EXEC(CreateEventTemplate(EVENT_8021X_AUTH_FAILED, _T("SYS_8021X_AUTH_FAILED"),
6f33021d 3016 EVENT_SEVERITY_MAJOR, 1, _T("802.1x authentication failed on port %2"),
aa16f82b
VK
3017 _T("Generated when switch port backend authentication state changed to FAIL.\r\nParameters:\r\n")
3018 _T(" 1) Interface index\r\n")
3019 _T(" 2) Interface name")));
3020
3021 CHK_EXEC(CreateEventTemplate(EVENT_8021X_AUTH_TIMEOUT, _T("SYS_8021X_AUTH_TIMEOUT"),
6f33021d 3022 EVENT_SEVERITY_MAJOR, 1, _T("802.1x authentication time out on port %2"),
aa16f82b
VK
3023 _T("Generated when switch port backend authentication state changed to TIMEOUT.\r\nParameters:\r\n")
3024 _T(" 1) Interface index\r\n")
3025 _T(" 2) Interface name")));
3026
3027 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='244' WHERE var_name='SchemaVersion'")));
3028 return TRUE;
3029}
3030
3031
e320f8ce
VK
3032//
3033// Upgrade from V242 to V243
3034//
3035
3036static BOOL H_UpgradeFromV242(int currVersion, int newVersion)
3037{
6f33021d 3038 static TCHAR batch[] =
242e9c7f
VK
3039 _T("ALTER TABLE items ADD snmp_raw_value_type integer\n")
3040 _T("UPDATE items SET snmp_raw_value_type=0\n")
e320f8ce
VK
3041 _T("ALTER TABLE items ADD flags integer\n")
3042 _T("UPDATE items SET flags=adv_schedule+(all_thresholds*2)\n")
3043 _T("ALTER TABLE items DROP COLUMN adv_schedule\n")
3044 _T("ALTER TABLE items DROP COLUMN all_thresholds\n")
e320f8ce
VK
3045 _T("<END>");
3046
3047 CHK_EXEC(SQLBatch(batch));
3048 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='243' WHERE var_name='SchemaVersion'")));
3049 return TRUE;
3050}
3051
3052
f5e80875
VK
3053//
3054// Upgrade from V241 to V242
3055//
3056
3057static BOOL H_UpgradeFromV241(int currVersion, int newVersion)
3058{
6f33021d 3059 static TCHAR batch[] =
f5e80875
VK
3060 _T("DROP TABLE business_service_templates\n")
3061 _T("ALTER TABLE dashboards ADD options integer\n")
3062 _T("UPDATE dashboards SET options=0\n")
3063 _T("<END>");
3064
3065 CHK_EXEC(SQLBatch(batch));
3066 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='242' WHERE var_name='SchemaVersion'")));
3067 return TRUE;
3068}
3069
3070
6ff21d27
VK
3071//
3072// Upgrade from V240 to V241
3073//
3074
3075static BOOL H_UpgradeFromV240(int currVersion, int newVersion)
3076{
6f33021d 3077 static TCHAR batch[] =
6ff21d27
VK
3078 _T("ALTER TABLE slm_checks ADD template_id integer\n")
3079 _T("ALTER TABLE slm_checks ADD current_ticket integer\n")
3080 _T("UPDATE slm_checks SET template_id=0,current_ticket=0\n")
3081 _T("<END>");
3082
3083 CHK_EXEC(SQLBatch(batch));
3084 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='241' WHERE var_name='SchemaVersion'")));
3085 return TRUE;
3086}
3087
3088
5fe19f9e
VK
3089//
3090// Upgrade from V239 to V240
3091//
3092
3093static BOOL H_UpgradeFromV239(int currVersion, int newVersion)
3094{
3095 CHK_EXEC(SQLQuery(_T("ALTER TABLE raw_dci_values ADD transformed_value varchar(255)")));
3096 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='240' WHERE var_name='SchemaVersion'")));
3097 return TRUE;
3098}
3099
3100
5a7d6a10
VK
3101//
3102// Upgrade from V238 to V239
3103//
3104
3105static BOOL H_UpgradeFromV238(int currVersion, int newVersion)
3106{
3107 CHK_EXEC(SQLQuery(
3108 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES ")
3109 _T("(56,'SYS_IP_ADDRESS_CHANGED',1,1,'Primary IP address changed from %2 to %1',")
3110 _T("'Generated when primary IP address changed (usually because of primary name change or DNS change).#0D#0A")
3111 _T("Parameters:#0D#0A 1) New IP address#0D#0A 2) Old IP address#0D#0A 3) Primary host name')")));
3112
3113 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='239' WHERE var_name='SchemaVersion'")));
3114 return TRUE;
3115}
3116
3117
34ccf87f
VK
3118//
3119// Upgrade from V232 to V238
3120//
3121
3122static BOOL H_UpgradeFromV232toV238(int currVersion, int newVersion)
3123{
3124 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
3125 _T("id integer not null,")
3126 _T("type integer not null,")
3127 _T("content $SQL:TEXT null,")
3128 _T("threshold_id integer not null,")
3129 _T("reason varchar(255) null,")
3130 _T("is_template integer not null,")
3131 _T("PRIMARY KEY(id))")));
3132
3133 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
3134 _T("ticket_id integer not null,")
3135 _T("service_id integer not null,")
3136 _T("check_id integer not null,")
3137 _T("create_timestamp integer not null,")
3138 _T("close_timestamp integer not null,")
3139 _T("reason varchar(255) null,")
3140 _T("PRIMARY KEY(ticket_id))")));
3141
3142 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_service_history (")
3143 _T("record_id integer not null,")
3144 _T("service_id integer not null,")
3145 _T("change_timestamp integer not null,")
3146 _T("new_status integer not null,")
3147 _T("PRIMARY KEY(record_id))")));
3148
3149 CHK_EXEC(CreateTable(_T("CREATE TABLE report_results (")
3150 _T("report_id integer not null,")
3151 _T("generated integer not null,")
3152 _T("job_id integer not null,")
3153 _T("PRIMARY KEY(report_id,job_id))")));
3154
3155 CHK_EXEC(CreateTable(_T("CREATE TABLE reports (")
3156 _T("id integer not null,")
3157 _T("definition $SQL:TEXT null,")
3158 _T("PRIMARY KEY(id))")));
3159
3160 CHK_EXEC(CreateTable(_T("CREATE TABLE job_history (")
3161 _T("id integer not null,")
3162 _T("time_created integer not null,")
3163 _T("time_started integer not null,")
3164 _T("time_finished integer not null,")
3165 _T("job_type varchar(127) null,")
3166 _T("description varchar(255) null,")
3167 _T("additional_info varchar(255) null,")
3168 _T("node_id integer not null,")
3169 _T("user_id integer not null,")
3170 _T("status integer not null,")
3171 _T("failure_message varchar(255) null,")
3172 _T("PRIMARY KEY(id))")));
3173
3174 CHK_EXEC(CreateTable(_T("CREATE TABLE business_services (")
3175 _T("service_id integer not null,")
3176 _T("PRIMARY KEY(service_id))")));
3177
3178 CHK_EXEC(CreateTable(_T("CREATE TABLE business_service_templates (")
3179 _T("service_id integer not null,")
3180 _T("template_id integer not null,")
3181 _T("PRIMARY KEY(service_id,template_id))")));
3182
3183 CHK_EXEC(CreateTable(_T("CREATE TABLE node_links (")
3184 _T("nodelink_id integer not null,")
3185 _T("node_id integer not null,")
3186 _T("PRIMARY KEY(nodelink_id))")));
3187
3188 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_agreements (")
3189 _T("agreement_id integer not null,")
3190 _T("service_id integer not null,")
3191 _T("org_id integer not null,")
3192 _T("uptime varchar(63) not null,")
3193 _T("period integer not null,")
3194 _T("start_date integer not null,")
3195 _T("notes varchar(255),")
3196 _T("PRIMARY KEY(agreement_id))")));
3197
3198 CHK_EXEC(CreateTable(_T("CREATE TABLE organizations (")
3199 _T("id integer not null,")
3200 _T("parent_id integer not null,")
3201 _T("org_type integer not null,")
3202 _T("name varchar(63) not null,")
3203 _T("description varchar(255),")
3204 _T("manager integer not null,")
3205 _T("PRIMARY KEY(id))")));
3206
3207 CHK_EXEC(CreateTable(_T("CREATE TABLE persons (")
3208 _T("id integer not null,")
3209 _T("org_id integer not null,")
3210 _T("first_name varchar(63),")
3211 _T("last_name varchar(63),")
3212 _T("title varchar(255),")
3213 _T("status integer not null,")
3214 _T("PRIMARY KEY(id))")));
3215
3216 CHK_EXEC(CreateConfigParam(_T("JobHistoryRetentionTime"), _T("90"), 1, 0));
3217
3218 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description=")
3219 _T("'Generated when threshold check is rearmed for specific data collection item.#0D#0A")
3220 _T("Parameters:#0D#0A")
3221 _T(" 1) Parameter name#0D#0A")
3222 _T(" 2) Item description#0D#0A")
3223 _T(" 3) Data collection item ID#0D#0A")
3224 _T(" 4) Instance#0D#0A")
3225 _T(" 5) Threshold value#0D#0A")
3226 _T(" 6) Actual value' WHERE event_code=18")));
3227
3228 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='238' WHERE var_name='SchemaVersion'")));
3229 return TRUE;
3230}
3231
3232
9c529287
VK
3233//
3234// Upgrade from V237 to V238
3235//
3236
3237static BOOL H_UpgradeFromV237(int currVersion, int newVersion)
3238{
6f33021d 3239 static TCHAR batch[] =
9c529287
VK
3240 _T("DROP TABLE slm_check_templates\n")
3241 _T("DROP TABLE node_link_checks\n")
3242 _T("DROP TABLE slm_checks\n")
3243 _T("DROP TABLE slm_tickets\n")
3244 _T("<END>");
3245
3246 CHK_EXEC(SQLBatch(batch));
3247
3248 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
3249 _T("id integer not null,")
3250 _T("type integer not null,")
3251 _T("content $SQL:TEXT null,")
3252 _T("threshold_id integer not null,")
3253 _T("reason varchar(255) null,")
3254 _T("is_template integer not null,")
3255 _T("PRIMARY KEY(id))")));
3256
3257 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
3258 _T("ticket_id integer not null,")
3259 _T("service_id integer not null,")
3260 _T("check_id integer not null,")
3261 _T("create_timestamp integer not null,")
3262 _T("close_timestamp integer not null,")
3263 _T("reason varchar(255) null,")
3264 _T("PRIMARY KEY(ticket_id))")));
3265
3266 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_service_history (")
3267 _T("record_id integer not null,")
3268 _T("service_id integer not null,")
3269 _T("change_timestamp integer not null,")
3270 _T("new_status integer not null,")
3271 _T("PRIMARY KEY(record_id))")));
3272
3273 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='238' WHERE var_name='SchemaVersion'")));
3274 return TRUE;
3275}
3276
3277
f5c9fdc5
VK
3278//
3279// Upgrade from V236 to V237
3280//
3281
3282static BOOL H_UpgradeFromV236(int currVersion, int newVersion)
3283{
6f33021d 3284 static TCHAR batch[] =
f5c9fdc5
VK
3285 _T("ALTER TABLE business_services DROP COLUMN name\n")
3286 _T("ALTER TABLE business_services DROP COLUMN parent_id\n")
3287 _T("ALTER TABLE business_services DROP COLUMN status\n")
3288 _T("ALTER TABLE slm_checks DROP COLUMN name\n")
3289 _T("<END>");
3290
3291 CHK_EXEC(SQLBatch(batch));
3292
3293 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='237' WHERE var_name='SchemaVersion'")));
3294 return TRUE;
3295}
3296
3297
5418326b
AK
3298//
3299// Upgrade from V235 to V236
3300//
3301
3302static BOOL H_UpgradeFromV235(int currVersion, int newVersion)
3303{
3304 CHK_EXEC(CreateTable(_T("CREATE TABLE report_results (")
3305 _T("report_id integer not null,")
3306 _T("generated integer not null,")
3307 _T("job_id integer not null,")
3308 _T("PRIMARY KEY(report_id,job_id))")));
3309
3310 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='236' WHERE var_name='SchemaVersion'")));
3311 return TRUE;
3312}
3313
3314
845b8121
VK
3315//
3316// Upgrade from V234 to V235
3317//
3318
3319static BOOL H_UpgradeFromV234(int currVersion, int newVersion)
3320{
3321 CHK_EXEC(CreateTable(_T("CREATE TABLE reports (")
3322 _T("id integer not null,")
3323 _T("definition $SQL:TEXT null,")
3324 _T("PRIMARY KEY(id))")));
3325
845b8121
VK
3326 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='235' WHERE var_name='SchemaVersion'")));
3327 return TRUE;
3328}
3329
3330
878b4261
VK
3331//
3332// Upgrade from V233 to V234
3333//
3334
3335static BOOL H_UpgradeFromV233(int currVersion, int newVersion)
3336{
3337 CHK_EXEC(CreateTable(_T("CREATE TABLE job_history (")
3338 _T("id integer not null,")
3339 _T("time_created integer not null,")
3340 _T("time_started integer not null,")
3341 _T("time_finished integer not null,")
3342 _T("job_type varchar(127) null,")
3343 _T("description varchar(255) null,")
24dc5346 3344 _T("additional_info varchar(255) null,")
878b4261
VK
3345 _T("node_id integer not null,")
3346 _T("user_id integer not null,")
3347 _T("status integer not null,")
3348 _T("failure_message varchar(255) null,")
3349 _T("PRIMARY KEY(id))")));
3350
3351 CHK_EXEC(CreateConfigParam(_T("JobHistoryRetentionTime"), _T("90"), 1, 0));
3352
3353 CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description=")
3354 _T("'Generated when threshold check is rearmed for specific data collection item.#0D#0A")
3355 _T("Parameters:#0D#0A")
3356 _T(" 1) Parameter name#0D#0A")
3357 _T(" 2) Item description#0D#0A")
3358 _T(" 3) Data collection item ID#0D#0A")
3359 _T(" 4) Instance#0D#0A")
3360 _T(" 5) Threshold value#0D#0A")
3361 _T(" 6) Actual value' WHERE event_code=18")));
3362
3363 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='234' WHERE var_name='SchemaVersion'")));
3364 return TRUE;
3365}
3366
3367
128681c8
VK
3368//
3369// Upgrade from V232 to V233
3370//
3371
3372static BOOL H_UpgradeFromV232(int currVersion, int newVersion)
3373{
3374 CHK_EXEC(CreateTable(_T("CREATE TABLE business_services (")
3375 _T("service_id integer not null,")
3376 _T("name varchar(63) not null,")
2498594e
VK
3377 _T("parent_id integer not null,")
3378 _T("status integer not null,")
128681c8
VK
3379 _T("PRIMARY KEY(service_id))")));
3380
128681c8
VK
3381 CHK_EXEC(CreateTable(_T("CREATE TABLE business_service_templates (")
3382 _T("service_id integer not null,")
3383 _T("template_id integer not null,")
3384 _T("PRIMARY KEY(service_id,template_id))")));
3385
3386 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_checks (")
3387 _T("check_id integer not null,")
3388 _T("name varchar(63) not null,")
2498594e 3389 _T("type integer not null,")
128681c8 3390 _T("content $SQL:TEXT,")
2498594e 3391 _T("threshold_id integer not null,")
128681c8
VK
3392 _T("state integer not null,")
3393 _T("reason varchar(255) not null,")
3394 _T("PRIMARY KEY(check_id))")));
3395
3396 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_check_templates (")
3397 _T("id integer not null,")
3398 _T("name varchar(63) not null,")
2498594e 3399 _T("type integer not null,")
128681c8 3400 _T("content $SQL:TEXT,")
2498594e 3401 _T("threshold_id integer not null,")
128681c8
VK
3402 _T("PRIMARY KEY(id))")));
3403
3404 CHK_EXEC(CreateTable(_T("CREATE TABLE node_links (")
3405 _T("nodelink_id integer not null,")
3406 _T("node_id integer not null,")
3407 _T("PRIMARY KEY(nodelink_id))")));
3408
3409 CHK_EXEC(CreateTable(_T("CREATE TABLE node_link_checks (")
3410 _T("nodelink_id integer not null,")
3411 _T("check_id integer not null,")
3412 _T("PRIMARY KEY(nodelink_id,check_id))")));
3413
3414 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_agreements (")
3415 _T("agreement_id integer not null,")
3416 _T("service_id integer not null,")
3417 _T("org_id integer not null,")
3418 _T("uptime varchar(63) not null,")
3419 _T("period integer not null,")
3420 _T("start_date integer not null,")
3421 _T("notes varchar(255),")
3422 _T("PRIMARY KEY(agreement_id))")));
3423
3424 CHK_EXEC(CreateTable(_T("CREATE TABLE slm_tickets (")
3425 _T("ticket_id integer not null,")
3426 _T("service_id integer not null,")
3427 _T("create_timestamp integer not null,")
3428 _T("close_timestamp integer not null,")
3429 _T("reason varchar(255) not null,")
3430 _T("PRIMARY KEY(ticket_id))")));
3431
3432 CHK_EXEC(CreateTable(_T("CREATE TABLE organizations (")
3433 _T("id integer not null,")
3434 _T("parent_id integer not null,")
3435 _T("org_type integer not null,")
3436 _T("name varchar(63) not null,")
3437 _T("description varchar(255),")
3438 _T("manager integer not null,")
3439 _T("PRIMARY KEY(id))")));
3440
3441 CHK_EXEC(CreateTable(_T("CREATE TABLE persons (")
3442 _T("id integer not null,")
3443 _T("org_id integer not null,")
3444 _T("first_name varchar(63),")
3445 _T("last_name varchar(63),")
3446 _T("title varchar(255),")
3447 _T("status integer not null,")
3448 _T("PRIMARY KEY(id))")));
3449
3450 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='233' WHERE var_name='SchemaVersion'")));
3451 return TRUE;
3452}
3453
3454
8f245200
VK
3455//
3456// Upgrade from V231 to V232
3457//
3458
3459static BOOL H_UpgradeFromV231(int currVersion, int newVersion)
3460{
6f33021d 3461 static TCHAR batch[] =
8f245200
VK
3462 _T("ALTER TABLE object_properties ADD submap_id integer\n")
3463 _T("UPDATE object_properties SET submap_id=0\n")
3464 _T("DROP TABLE maps\n")
3465 _T("DROP TABLE map_access_lists\n")
3466 _T("DROP TABLE submaps\n")
3467 _T("DROP TABLE submap_object_positions\n")
3468 _T("DROP TABLE submap_links\n")
3469 _T("<END>");
3470
3471 CHK_EXEC(SQLBatch(batch));
3472
3473 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='232' WHERE var_name='SchemaVersion'")));
3474 return TRUE;
3475}
3476
3477
3eab63f2
VK
3478//
3479// Upgrade from V230 to V231
3480//
3481
3482static BOOL H_UpgradeFromV230(int currVersion, int newVersion)
3483{
6f33021d 3484 static TCHAR batch[] =
3eab63f2
VK
3485 _T("ALTER TABLE nodes ADD bridge_base_addr varchar(15)\n")
3486 _T("UPDATE nodes SET bridge_base_addr='000000000000'\n")
3487 _T("<END>");
3488
3489 CHK_EXEC(SQLBatch(batch));
3490
3491 CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='231' WHERE var_name='SchemaVersion'")));
3492 return TRUE;
3493}