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