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