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