Raw DCI values saved in history along with corresponding transformed values
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade_v30.cpp
1 /*
2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004-2017 Victor Kirhenshtein
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_v30.cpp
20 **
21 **/
22
23 #include "nxdbmgr.h"
24
25 /**
26 * Upgrade from 30.10 to 30.11
27 */
28 static bool H_UpgradeFromV10()
29 {
30 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,raw_value varchar(255) null)' WHERE var_name='IDataTableCreationCommand'")));
31
32 IntegerArray<UINT32> *targets = GetDataCollectionTargets();
33 for(int i = 0; i < targets->size(); i++)
34 {
35 TCHAR query[256];
36 _sntprintf(query, 256, _T("ALTER TABLE idata_%d ADD raw_value varchar(255)"), targets->get(i));
37 CHK_EXEC(SQLQuery(query));
38 }
39 delete targets;
40
41 CHK_EXEC(SetMinorSchemaVersion(11));
42 return true;
43 }
44
45 /**
46 * Upgrade from 30.9 to 30.10
47 */
48 static bool H_UpgradeFromV9()
49 {
50 static const TCHAR *batch =
51 _T("ALTER TABLE snmp_communities ADD zone integer null\n")
52 _T("ALTER TABLE usm_credentials ADD zone integer null\n")
53 _T("ALTER TABLE zones ADD snmp_ports varchar(512) null\n")
54 _T("UPDATE snmp_communities SET zone=-1\n")
55 _T("UPDATE usm_credentials SET zone=-1\n")
56 _T("UPDATE zones SET snmp_ports=''\n")
57 _T("<END>");
58 CHK_EXEC(SQLBatch(batch));
59
60 DBSetNotNullConstraint(g_hCoreDB, _T("snmp_communities"), _T("zone"));
61 DBSetNotNullConstraint(g_hCoreDB, _T("usm_credentials"), _T("zone"));
62
63 CHK_EXEC(SetMinorSchemaVersion(10));
64 return true;
65 }
66
67 /**
68 * Upgrade from 30.8 to 30.9 (changes also included into 22.2)
69 */
70 static bool H_UpgradeFromV8()
71 {
72 if (GetSchemaLevelForMajorVersion(22) < 2)
73 {
74 CHK_EXEC(CreateConfigParam(_T("DBWriter.MaxRecordsPerTransaction"), _T("1000"), _T("Maximum number of records per one transaction for delayed database writes."), 'I', true, true, false, false));
75 CHK_EXEC(SetSchemaLevelForMajorVersion(22, 2));
76 }
77 CHK_EXEC(SetMinorSchemaVersion(9));
78 return true;
79 }
80
81 /**
82 * Upgrade from 30.7 to 30.8 (changes also included into 22.1)
83 */
84 static bool H_UpgradeFromV7()
85 {
86 if (GetSchemaLevelForMajorVersion(22) < 1)
87 {
88 int count = ConfigReadInt(_T("NumberOfDataCollectors"), 250);
89 TCHAR value[64];
90 _sntprintf(value, 64,_T("%d"), std::max(250, count));
91 CHK_EXEC(CreateConfigParam(_T("DataCollector.ThreadPool.BaseSize"), _T("10"), _T("Base size for data collector thread pool."), 'I', true, true, false, false));
92 CHK_EXEC(CreateConfigParam(_T("DataCollector.ThreadPool.MaxSize"), value, _T("Maximum size for data collector thread pool."), 'I', true, true, false, false));
93 CHK_EXEC(SQLQuery(_T("UPDATE config SET default_value='250' WHERE var_name='DataCollector.ThreadPool.MaxSize'")));
94 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfDataCollectors'")));
95 CHK_EXEC(SetSchemaLevelForMajorVersion(22, 1));
96 }
97 CHK_EXEC(SetMinorSchemaVersion(8));
98 return true;
99 }
100
101 /**
102 * Upgrade from 30.6 to 30.7 (changes also included into 21.4)
103 */
104 static bool H_UpgradeFromV6()
105 {
106 if (GetSchemaLevelForMajorVersion(21) < 4)
107 {
108 DB_RESULT hResult = DBSelect(g_hCoreDB, _T("SELECT access_rights,object_id FROM acl WHERE user_id=-2147483647")); // Get group Admins object acl
109 if (hResult != NULL)
110 {
111 DB_STATEMENT hStmt = DBPrepare(g_hCoreDB, _T("UPDATE acl SET access_rights=? WHERE user_id=-2147483647 AND object_id=? "));
112 if (hStmt != NULL)
113 {
114 int nRows = DBGetNumRows(hResult);
115 UINT32 rights;
116 for(int i = 0; i < nRows; i++)
117 {
118 rights = DBGetFieldULong(hResult, i, 0);
119 if (rights & OBJECT_ACCESS_READ)
120 {
121 rights |= (OBJECT_ACCESS_READ_AGENT | OBJECT_ACCESS_READ_SNMP | OBJECT_ACCESS_SCREENSHOT);
122 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, rights);
123 DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 1));
124
125 if (!SQLExecute(hStmt))
126 {
127 if (!g_bIgnoreErrors)
128 {
129 DBFreeStatement(hStmt);
130 DBFreeResult(hResult);
131 return FALSE;
132 }
133 }
134 }
135 }
136
137 DBFreeStatement(hStmt);
138 }
139 else if (!g_bIgnoreErrors)
140 return FALSE;
141 DBFreeResult(hResult);
142 }
143 else if (!g_bIgnoreErrors)
144 return false;
145 CHK_EXEC(SetSchemaLevelForMajorVersion(21, 4));
146 }
147 CHK_EXEC(SetMinorSchemaVersion(7));
148 return true;
149 }
150
151 /**
152 * Upgrade from 30.5 to 30.6 (changes also included into 21.3)
153 */
154 static bool H_UpgradeFromV5()
155 {
156 if (GetSchemaLevelForMajorVersion(21) < 3)
157 {
158 static const TCHAR *batch =
159 _T("UPDATE nodes SET fail_time_snmp=0 WHERE fail_time_snmp IS NULL\n")
160 _T("UPDATE nodes SET fail_time_agent=0 WHERE fail_time_agent IS NULL\n")
161 _T("<END>");
162 CHK_EXEC(SQLBatch(batch));
163
164 CHK_EXEC(DBSetNotNullConstraint(g_hCoreDB, _T("nodes"), _T("fail_time_snmp")));
165 CHK_EXEC(DBSetNotNullConstraint(g_hCoreDB, _T("nodes"), _T("fail_time_agent")));
166 CHK_EXEC(SetSchemaLevelForMajorVersion(21, 3));
167 }
168 CHK_EXEC(SetMinorSchemaVersion(6));
169 return true;
170 }
171
172 /**
173 * Upgrade from 30.4 to 30.5 (changes also included into 21.2)
174 */
175 static bool H_UpgradeFromV4()
176 {
177 if (GetSchemaLevelForMajorVersion(21) < 2)
178 {
179 static const TCHAR *batch =
180 _T("ALTER TABLE nodes ADD fail_time_snmp integer\n")
181 _T("ALTER TABLE nodes ADD fail_time_agent integer\n")
182 _T("<END>");
183 CHK_EXEC(SQLBatch(batch));
184 CHK_EXEC(SetSchemaLevelForMajorVersion(21, 2));
185 }
186 CHK_EXEC(SetMinorSchemaVersion(5));
187 return true;
188 }
189
190 /**
191 * Move object flags from old to new tables
192 */
193 static BOOL MoveFlagsFromOldTables(const TCHAR *tableName)
194 {
195 TCHAR query[256];
196 _sntprintf(query, 256, _T("SELECT id,flags FROM %s"), tableName);
197 DB_RESULT hResult = DBSelect(g_hCoreDB, query);
198 DB_STATEMENT hStmt = DBPrepare(g_hCoreDB, _T("UPDATE object_properties SET flags=? WHERE object_id=?"));
199 if (hResult != NULL)
200 {
201 if (hStmt != NULL)
202 {
203 int nRows = DBGetNumRows(hResult);
204 for(int i = 0; i < nRows; i++)
205 {
206 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 1));
207 DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
208
209 if (!SQLExecute(hStmt))
210 {
211 if (!g_bIgnoreErrors)
212 {
213 DBFreeStatement(hStmt);
214 DBFreeResult(hResult);
215 return FALSE;
216 }
217 }
218 }
219 DBFreeStatement(hStmt);
220 }
221 else if (!g_bIgnoreErrors)
222 {
223 return FALSE;
224 }
225 DBFreeResult(hResult);
226 }
227 else if (!g_bIgnoreErrors)
228 {
229 return FALSE;
230 }
231
232 CHK_EXEC(DBDropColumn(g_hCoreDB, tableName, _T("flags")));
233 return TRUE;
234 }
235
236 /**
237 * Move single flag
238 */
239 inline void MoveFlag(UINT32 oldVar, UINT32 *newVar, UINT32 oldFlag, UINT32 newFlag)
240 {
241 *newVar |= ((oldVar & oldFlag) != 0) ? newFlag : 0;
242 }
243
244 /**
245 * Move node flags
246 */
247 static void MoveNodeFlags(UINT32 oldFlag, UINT32 *flags)
248 {
249 MoveFlag(oldFlag, flags, 0x10000000, DCF_DISABLE_STATUS_POLL);
250 MoveFlag(oldFlag, flags, 0x20000000, DCF_DISABLE_CONF_POLL);
251 MoveFlag(oldFlag, flags, 0x80000000, DCF_DISABLE_DATA_COLLECT);
252 MoveFlag(oldFlag, flags, 0x00000080, NF_REMOTE_AGENT);
253 MoveFlag(oldFlag, flags, 0x00400000, NF_DISABLE_DISCOVERY_POLL);
254 MoveFlag(oldFlag, flags, 0x00800000, NF_DISABLE_TOPOLOGY_POLL);
255 MoveFlag(oldFlag, flags, 0x01000000, NF_DISABLE_SNMP);
256 MoveFlag(oldFlag, flags, 0x02000000, NF_DISABLE_NXCP);
257 MoveFlag(oldFlag, flags, 0x04000000, NF_DISABLE_ICMP);
258 MoveFlag(oldFlag, flags, 0x08000000, NF_FORCE_ENCRYPTION);
259 MoveFlag(oldFlag, flags, 0x40000000, NF_DISABLE_ROUTE_POLL);
260 }
261
262 /**
263 * Move node capabilities flags
264 */
265 static void MoveNodeCapabilities(UINT32 oldFlag, UINT32 *capabilities)
266 {
267 MoveFlag(oldFlag, capabilities, 0x00000001, NC_IS_SNMP);
268 MoveFlag(oldFlag, capabilities, 0x00000002, NC_IS_NATIVE_AGENT);
269 MoveFlag(oldFlag, capabilities, 0x00000004, NC_IS_BRIDGE);
270 MoveFlag(oldFlag, capabilities, 0x00000008, NC_IS_ROUTER);
271 MoveFlag(oldFlag, capabilities, 0x00000010, NC_IS_LOCAL_MGMT);
272 MoveFlag(oldFlag, capabilities, 0x00000020, NC_IS_PRINTER);
273 MoveFlag(oldFlag, capabilities, 0x00000040, NC_IS_OSPF);
274 MoveFlag(oldFlag, capabilities, 0x00000100, NC_IS_CPSNMP);
275 MoveFlag(oldFlag, capabilities, 0x00000200, NC_IS_CDP);
276 MoveFlag(oldFlag, capabilities, 0x00000400, NC_IS_NDP);
277 MoveFlag(oldFlag, capabilities, 0x00000800, NC_IS_LLDP);
278 MoveFlag(oldFlag, capabilities, 0x00001000, NC_IS_VRRP);
279 MoveFlag(oldFlag, capabilities, 0x00002000, NC_HAS_VLANS);
280 MoveFlag(oldFlag, capabilities, 0x00004000, NC_IS_8021X);
281 MoveFlag(oldFlag, capabilities, 0x00008000, NC_IS_STP);
282 MoveFlag(oldFlag, capabilities, 0x00010000, NC_HAS_ENTITY_MIB);
283 MoveFlag(oldFlag, capabilities, 0x00020000, NC_HAS_IFXTABLE);
284 MoveFlag(oldFlag, capabilities, 0x00040000, NC_HAS_AGENT_IFXCOUNTERS);
285 MoveFlag(oldFlag, capabilities, 0x00080000, NC_HAS_WINPDH);
286 MoveFlag(oldFlag, capabilities, 0x00100000, NC_IS_WIFI_CONTROLLER);
287 MoveFlag(oldFlag, capabilities, 0x00200000, NC_IS_SMCLP);
288 }
289
290 /**
291 * Move node state flags
292 */
293 static void MoveNodeState(UINT32 oldRuntime, UINT32 *state)
294 {
295 MoveFlag(oldRuntime, state, 0x000004, DCSF_UNREACHABLE);
296 MoveFlag(oldRuntime, state, 0x000008, NSF_AGENT_UNREACHABLE);
297 MoveFlag(oldRuntime, state, 0x000010, NSF_SNMP_UNREACHABLE);
298 MoveFlag(oldRuntime, state, 0x000200, NSF_CPSNMP_UNREACHABLE);
299 MoveFlag(oldRuntime, state, 0x008000, DCSF_NETWORK_PATH_PROBLEM);
300 MoveFlag(oldRuntime, state, 0x020000, NSF_CACHE_MODE_NOT_SUPPORTED);
301 }
302
303 /**
304 * Move sensor state flags
305 */
306 static void MoveSensorState(UINT32 oldFlag, UINT32 oldRuntime, UINT32 *status)
307 {
308 MoveFlag(oldFlag, status, 0x00000001, SSF_PROVISIONED);
309 MoveFlag(oldFlag, status, 0x00000002, SSF_REGISTERED);
310 MoveFlag(oldFlag, status, 0x00000004, SSF_ACTIVE);
311 MoveFlag(oldFlag, status, 0x00000008, SSF_CONF_UPDATE_PENDING);
312 MoveFlag(oldRuntime, status, 0x000004, DCSF_UNREACHABLE);
313 }
314
315 /**
316 * Upgrade from 30.3 to 30.4
317 */
318 static bool H_UpgradeFromV3()
319 {
320 static const TCHAR *batch =
321 _T("ALTER TABLE object_properties ADD flags integer null\n")
322 _T("ALTER TABLE object_properties ADD state integer null\n")
323 _T("ALTER TABLE nodes ADD capabilities integer null\n")
324 _T("UPDATE object_properties set flags=0,state=0\n")
325 _T("<END>");
326 CHK_EXEC(SQLBatch(batch));
327
328 //move flags from old tables to the new one
329 CHK_EXEC(MoveFlagsFromOldTables(_T("interfaces")));
330 CHK_EXEC(MoveFlagsFromOldTables(_T("templates")));
331 CHK_EXEC(MoveFlagsFromOldTables(_T("chassis")));
332 CHK_EXEC(MoveFlagsFromOldTables(_T("object_containers")));
333 CHK_EXEC(MoveFlagsFromOldTables(_T("network_maps")));
334 //create special behavior for node and sensor, cluster
335 //node
336 DB_RESULT hResult = DBSelect(g_hCoreDB, _T("SELECT id,runtime_flags FROM nodes"));
337 DB_STATEMENT stmtNetObj = DBPrepare(g_hCoreDB, _T("UPDATE object_properties SET flags=?, state=? WHERE object_id=?"));
338 DB_STATEMENT stmtNode = DBPrepare(g_hCoreDB, _T("UPDATE nodes SET capabilities=? WHERE id=?"));
339 if (hResult != NULL)
340 {
341 if (stmtNetObj != NULL && stmtNode != NULL)
342 {
343 int nRows = DBGetNumRows(hResult);
344 for(int i = 0; i < nRows; i++)
345 {
346 UINT32 id = DBGetFieldULong(hResult, i, 0);
347 UINT32 oldFlags = 0;
348 UINT32 oldRuntime = DBGetFieldULong(hResult, i, 1);
349 UINT32 flags = 0;
350 UINT32 state = 0;
351 UINT32 capabilities = 0;
352 TCHAR query[256];
353 _sntprintf(query, 256, _T("SELECT node_flags FROM nodes WHERE id=%d"), id);
354 DB_RESULT flagResult = DBSelect(g_hCoreDB, query);
355 if(DBGetNumRows(flagResult) >= 1)
356 {
357 oldFlags = DBGetFieldULong(flagResult, 0, 0);
358 }
359 else
360 {
361 if(!g_bIgnoreErrors)
362 {
363 DBFreeStatement(stmtNetObj);
364 DBFreeStatement(stmtNode);
365 DBFreeResult(hResult);
366 return FALSE;
367 }
368 }
369 MoveNodeFlags(oldFlags, &flags);
370 MoveNodeCapabilities(oldFlags, &capabilities);
371 MoveNodeState(oldRuntime, &state);
372
373 DBBind(stmtNetObj, 1, DB_SQLTYPE_INTEGER, flags);
374 DBBind(stmtNetObj, 2, DB_SQLTYPE_INTEGER, state);
375 DBBind(stmtNetObj, 3, DB_SQLTYPE_INTEGER, id);
376
377 DBBind(stmtNode, 1, DB_SQLTYPE_INTEGER, capabilities);
378 DBBind(stmtNode, 2, DB_SQLTYPE_INTEGER, id);
379
380 if (!(SQLExecute(stmtNetObj)))
381 {
382 if (!g_bIgnoreErrors)
383 {
384 DBFreeStatement(stmtNetObj);
385 DBFreeStatement(stmtNode);
386 DBFreeResult(hResult);
387 return FALSE;
388 }
389 }
390
391 if (!SQLExecute(stmtNode))
392 {
393 if (!g_bIgnoreErrors)
394 {
395 DBFreeStatement(stmtNetObj);
396 DBFreeStatement(stmtNode);
397 DBFreeResult(hResult);
398 return FALSE;
399 }
400 }
401 }
402 DBFreeStatement(stmtNetObj);
403 DBFreeStatement(stmtNode);
404 }
405 else
406 {
407 if(stmtNetObj != NULL)
408 DBFreeStatement(stmtNetObj);
409
410 if(stmtNode != NULL)
411 DBFreeStatement(stmtNode);
412 if (!g_bIgnoreErrors)
413 {
414 return FALSE;
415 }
416 }
417 DBFreeResult(hResult);
418 }
419 CHK_EXEC(DBDropColumn(g_hCoreDB, _T("nodes"), _T("runtime_flags")));
420 CHK_EXEC(DBDropColumn(g_hCoreDB, _T("nodes"), _T("node_flags")));
421
422 //sensor
423 hResult = DBSelect(g_hCoreDB, _T("SELECT id,runtime_flags,flags FROM sensors"));
424 DB_STATEMENT stmt = DBPrepare(g_hCoreDB, _T("UPDATE object_properties SET status=? WHERE object_id=?"));
425 if (hResult != NULL)
426 {
427 if (stmt != NULL)
428 {
429 int nRows = DBGetNumRows(hResult);
430 for(int i = 0; i < nRows; i++)
431 {
432 UINT32 status = 0;
433 MoveSensorState(DBGetFieldULong(hResult, i, 2), DBGetFieldULong(hResult, i, 1), &status);
434
435 DBBind(stmt, 1, DB_SQLTYPE_INTEGER, status);
436 DBBind(stmt, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
437
438 if (!(SQLExecute(stmt)))
439 {
440 if (!g_bIgnoreErrors)
441 {
442 DBFreeStatement(stmt);
443 DBFreeResult(hResult);
444 return FALSE;
445 }
446 }
447 }
448 DBFreeStatement(stmt);
449 }
450 else if (!g_bIgnoreErrors)
451 {
452 return FALSE;
453 }
454 DBFreeResult(hResult);
455 }
456 CHK_EXEC(DBDropColumn(g_hCoreDB, _T("sensors"), _T("runtime_flags")));
457 CHK_EXEC(DBDropColumn(g_hCoreDB, _T("sensors"), _T("flags")));
458
459 CHK_EXEC(SetMinorSchemaVersion(4));
460 return true;
461 }
462
463 /**
464 * Upgrade from 30.2 to 30.3
465 */
466 static bool H_UpgradeFromV2()
467 {
468 CHK_EXEC(DBDropColumn(g_hCoreDB, _T("event_groups"), _T("range_start")));
469 CHK_EXEC(DBDropColumn(g_hCoreDB, _T("event_groups"), _T("range_end")));
470
471 static const TCHAR *batch =
472 _T("ALTER TABLE event_groups ADD guid varchar(36) null\n")
473 _T("UPDATE event_groups SET guid='04b326c0-5cc0-411f-8587-2836cb87c920' WHERE id=-2147483647\n")
474 _T("UPDATE event_groups SET guid='b61859c6-1768-4a61-a0cf-eed07d688f66' WHERE id=-2147483646\n")
475 _T("<END>");
476 CHK_EXEC(SQLBatch(batch));
477 DBSetNotNullConstraint(g_hCoreDB, _T("event_groups"), _T("guid"));
478
479 CHK_EXEC(SetMinorSchemaVersion(3));
480 return true;
481 }
482
483 /**
484 * Upgrade from 30.1 to 30.2
485 */
486 static bool H_UpgradeFromV1()
487 {
488 static const TCHAR *batch =
489 _T("ALTER TABLE users ADD created integer null\n")
490 _T("ALTER TABLE user_groups ADD created integer null\n")
491 _T("UPDATE users SET created=0\n")
492 _T("UPDATE user_groups SET created=0\n")
493 _T("<END>");
494 CHK_EXEC(SQLBatch(batch));
495 CHK_EXEC(DBSetNotNullConstraint(g_hCoreDB, _T("users"), _T("created")));
496 CHK_EXEC(DBSetNotNullConstraint(g_hCoreDB, _T("user_groups"), _T("created")));
497
498 CHK_EXEC(SetMinorSchemaVersion(2));
499 return true;
500 }
501
502 /**
503 * Upgrade from 30.0 to 30.1
504 */
505 static bool H_UpgradeFromV0()
506 {
507 CHK_EXEC(CreateTable(
508 _T("CREATE TABLE sensors (")
509 _T(" id integer not null,")
510 _T(" proxy_node integer not null,")
511 _T(" flags integer not null,")
512 _T(" mac_address varchar(16) null,")
513 _T(" device_class integer not null,")
514 _T(" vendor varchar(128) null,")
515 _T(" communication_protocol integer not null,")
516 _T(" xml_config varchar(4000) null,")
517 _T(" xml_reg_config varchar(4000) null,")
518 _T(" serial_number varchar(256) null,")
519 _T(" device_address varchar(256) null,")
520 _T(" meta_type varchar(256) null,")
521 _T(" description varchar(512) null,")
522 _T(" last_connection_time integer not null,")
523 _T(" frame_count integer not null,")
524 _T(" signal_strenght integer not null,")
525 _T(" signal_noise integer not null,")
526 _T(" frequency integer not null,")
527 _T(" runtime_flags integer null,")
528 _T("PRIMARY KEY(id))")));
529
530 CHK_EXEC(SetMinorSchemaVersion(1));
531 return true;
532 }
533
534 /**
535 * Upgrade map
536 */
537 static struct
538 {
539 int version;
540 int nextMajor;
541 int nextMinor;
542 bool (* upgradeProc)();
543 } s_dbUpgradeMap[] =
544 {
545 { 10, 30, 11, H_UpgradeFromV10 },
546 { 9, 30, 10, H_UpgradeFromV9 },
547 { 8, 30, 9, H_UpgradeFromV8 },
548 { 7, 30, 8, H_UpgradeFromV7 },
549 { 6, 30, 7, H_UpgradeFromV6 },
550 { 5, 30, 6, H_UpgradeFromV5 },
551 { 4, 30, 5, H_UpgradeFromV4 },
552 { 3, 30, 4, H_UpgradeFromV3 },
553 { 2, 30, 3, H_UpgradeFromV2 },
554 { 1, 30, 2, H_UpgradeFromV1 },
555 { 0, 30, 1, H_UpgradeFromV0 },
556 { 0, 0, 0, NULL }
557 };
558
559 /**
560 * Upgrade database to new version
561 */
562 bool MajorSchemaUpgrade_V30()
563 {
564 INT32 major, minor;
565 if (!DBGetSchemaVersion(g_hCoreDB, &major, &minor))
566 return false;
567
568 while((major == 30) && (minor < DB_SCHEMA_VERSION_V30_MINOR))
569 {
570 // Find upgrade procedure
571 int i;
572 for(i = 0; s_dbUpgradeMap[i].upgradeProc != NULL; i++)
573 if (s_dbUpgradeMap[i].version == minor)
574 break;
575 if (s_dbUpgradeMap[i].upgradeProc == NULL)
576 {
577 _tprintf(_T("Unable to find upgrade procedure for version 30.%d\n"), minor);
578 return false;
579 }
580 _tprintf(_T("Upgrading from version 30.%d to %d.%d\n"), minor, s_dbUpgradeMap[i].nextMajor, s_dbUpgradeMap[i].nextMinor);
581 DBBegin(g_hCoreDB);
582 if (s_dbUpgradeMap[i].upgradeProc())
583 {
584 DBCommit(g_hCoreDB);
585 if (!DBGetSchemaVersion(g_hCoreDB, &major, &minor))
586 return false;
587 }
588 else
589 {
590 _tprintf(_T("Rolling back last stage due to upgrade errors...\n"));
591 DBRollback(g_hCoreDB);
592 return false;
593 }
594 }
595 return true;
596 }