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