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