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