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