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