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