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