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