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