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