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