Added is_ospf field to nodes table
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade.cpp
1 /*
2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004 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 ** $module: upgrade.cpp
20 **
21 **/
22
23 #include "nxdbmgr.h"
24
25
26 //
27 // Create table
28 //
29
30 static BOOL CreateTable(TCHAR *pszQuery)
31 {
32 TCHAR *pszBuffer;
33 BOOL bResult;
34
35 pszBuffer = (TCHAR *)malloc(_tcslen(pszQuery) * sizeof(TCHAR) + 256);
36 _tcscpy(pszBuffer, pszQuery);
37 TranslateStr(pszBuffer, _T("$SQL:TEXT"), g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
38 if (g_iSyntax == DB_SYNTAX_MYSQL)
39 _tcscat(pszBuffer, _T(" type=InnoDB"));
40 bResult = SQLQuery(pszBuffer);
41 free(pszBuffer);
42 return bResult;
43 }
44
45
46 //
47 // Create configuration parameter if it doesn't exist
48 //
49
50 static BOOL CreateConfigParam(TCHAR *pszName, TCHAR *pszValue, int iVisible, int iNeedRestart)
51 {
52 TCHAR szQuery[1024];
53 DB_RESULT hResult;
54 BOOL bVarExist = FALSE, bResult = TRUE;
55
56 // Check for variable existence
57 _stprintf(szQuery, _T("SELECT var_value FROM config WHERE var_name='%s'"), pszName);
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 {
68 _stprintf(szQuery, _T("INSERT INTO config (var_name,var_value,is_visible,"
69 "need_server_restart) VALUES ('%s','%s',%d,%d)"),
70 pszName, pszValue, iVisible, iNeedRestart);
71 bResult = SQLQuery(szQuery);
72 }
73 return bResult;
74 }
75
76
77 //
78 // Upgrade from V19 to V20
79 //
80
81 static BOOL H_UpgradeFromV19(void)
82 {
83 static TCHAR m_szBatch[] =
84 "ALTER TABLE nodes ADD poller_node_id integer\n"
85 "ALTER TABLE nodes ADD is_ospf integer\n"
86 "UPDATE nodes SET poller_node_id=0,is_ospf=0\n"
87 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
88 " VALUES (28,'SYS_NODE_DOWN',4,1,'Node down',"
89 "'Generated when node is not responding to management server.#0D#0A"
90 "Parameters:#0D#0A No event-specific parameters')\n"
91 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
92 " VALUES (29,'SYS_NODE_UP',0,1,'Node up',"
93 "'Generated when communication with the node re-established.#0D#0A"
94 "Parameters:#0D#0A No event-specific parameters')\n"
95 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
96 " VALUES (25,'SYS_SERVICE_DOWN',3,1,'Network service #22%1#22 is not responding',"
97 "'Generated when network service is not responding to management server as "
98 "expected.#0D#0AParameters:#0D#0A 1) Service name#0D0A"
99 " 2) Service object ID#0D0A 3) Service type')\n"
100 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
101 " VALUES (26,'SYS_SERVICE_UP',0,1,"
102 "'Network service #22%1#22 returned to operational state',"
103 "'Generated when network service responds as expected after failure.#0D#0A"
104 "Parameters:#0D#0A 1) Service name#0D0A"
105 " 2) Service object ID#0D0A 3) Service type')\n"
106 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
107 " VALUES (27,'SYS_SERVICE_UNKNOWN',1,1,"
108 "'Status of network service #22%1#22 is unknown',"
109 "'Generated when management server is unable to determine state of the network "
110 "service due to agent or server-to-agent communication failure.#0D#0A"
111 "Parameters:#0D#0A 1) Service name#0D0A"
112 " 2) Service object ID#0D0A 3) Service type')\n"
113 "INSERT INTO images (image_id,name,file_name_png,file_hash_png,file_name_ico,"
114 "file_hash_ico) VALUES (12,'Obj.NetworkService','network_service.png',"
115 "'<invalid_hash>','network_service.ico','<invalid_hash>')\n"
116 "INSERT INTO default_images (object_class,image_id) VALUES (11,12)\n"
117 "<END>";
118
119 if (!SQLBatch(m_szBatch))
120 if (!g_bIgnoreErrors)
121 return FALSE;
122
123 if (!CreateTable(_T("CREATE TABLE network_services ("
124 "id integer not null,"
125 "name varchar(63),"
126 "status integer,"
127 "is_deleted integer,"
128 "node_id integer not null,"
129 "service_type integer,"
130 "ip_bind_addr varchar(15),"
131 "ip_proto integer,"
132 "ip_port integer,"
133 "check_request $SQL:TEXT,"
134 "check_responce $SQL:TEXT,"
135 "poller_node_id integer not null,"
136 "image_id integer not null,"
137 "PRIMARY KEY(id))")))
138 if (!g_bIgnoreErrors)
139 return FALSE;
140
141 if (!SQLQuery(_T("UPDATE config SET var_value='20' WHERE var_name='DBFormatVersion'")))
142 if (!g_bIgnoreErrors)
143 return FALSE;
144
145 return TRUE;
146 }
147
148
149 //
150 // Upgrade from V18 to V19
151 //
152
153 static BOOL H_UpgradeFromV18(void)
154 {
155 static TCHAR m_szBatch[] =
156 "ALTER TABLE nodes ADD platform_name varchar(63)\n"
157 "UPDATE nodes SET platform_name=''\n"
158 "<END>";
159
160 if (!SQLBatch(m_szBatch))
161 if (!g_bIgnoreErrors)
162 return FALSE;
163
164 if (!CreateTable(_T("CREATE TABLE agent_pkg ("
165 "pkg_id integer not null,"
166 "pkg_name varchar(63),"
167 "version varchar(31),"
168 "platform varchar(63),"
169 "pkg_file varchar(255),"
170 "description varchar(255),"
171 "PRIMARY KEY(pkg_id))")))
172 if (!g_bIgnoreErrors)
173 return FALSE;
174
175 if (!SQLQuery(_T("UPDATE config SET var_value='19' WHERE var_name='DBFormatVersion'")))
176 if (!g_bIgnoreErrors)
177 return FALSE;
178
179 return TRUE;
180 }
181
182
183 //
184 // Upgrade from V17 to V18
185 //
186
187 static BOOL H_UpgradeFromV17(void)
188 {
189 static TCHAR m_szBatch[] =
190 "ALTER TABLE nodes DROP COLUMN inherit_access_rights\n"
191 "ALTER TABLE nodes ADD agent_version varchar(63)\n"
192 "UPDATE nodes SET agent_version='' WHERE is_agent=0\n"
193 "UPDATE nodes SET agent_version='<unknown>' WHERE is_agent=1\n"
194 "INSERT INTO event_cfg (event_code,event_name,severity,flags,"
195 "message,description) SELECT event_id,name,severity,flags,"
196 "message,description FROM events\n"
197 "DROP TABLE events\n"
198 "DROP TABLE event_group_members\n"
199 "CREATE TABLE event_group_members (group_id integer not null,"
200 "event_code integer not null, PRIMARY KEY(group_id,event_code))\n"
201 "ALTER TABLE alarms ADD source_event_code integer\n"
202 "UPDATE alarms SET source_event_code=source_event_id\n"
203 "ALTER TABLE alarms DROP COLUMN source_event_id\n"
204 "ALTER TABLE alarms ADD source_event_id bigint\n"
205 "UPDATE alarms SET source_event_id=0\n"
206 "ALTER TABLE snmp_trap_cfg ADD event_code integer not null default 0\n"
207 "UPDATE snmp_trap_cfg SET event_code=event_id\n"
208 "ALTER TABLE snmp_trap_cfg DROP COLUMN event_id\n"
209 "DROP TABLE event_log\n"
210 "CREATE TABLE event_log (event_id bigint not null,event_code integer,"
211 "event_timestamp integer,event_source integer,event_severity integer,"
212 "event_message varchar(255),root_event_id bigint default 0,"
213 "PRIMARY KEY(event_id))\n"
214 "<END>";
215 TCHAR szQuery[4096];
216 DB_RESULT hResult;
217
218 hResult = SQLSelect(_T("SELECT rule_id,event_id FROM policy_event_list"));
219 if (hResult != NULL)
220 {
221 DWORD i, dwNumRows;
222
223 if (!SQLQuery(_T("DROP TABLE policy_event_list")))
224 {
225 if (!g_bIgnoreErrors)
226 {
227 DBFreeResult(hResult);
228 return FALSE;
229 }
230 }
231
232 if (!SQLQuery(_T("CREATE TABLE policy_event_list ("
233 "rule_id integer not null,"
234 "event_code integer not null,"
235 "PRIMARY KEY(rule_id,event_code))")))
236 {
237 if (!g_bIgnoreErrors)
238 {
239 DBFreeResult(hResult);
240 return FALSE;
241 }
242 }
243
244 dwNumRows = DBGetNumRows(hResult);
245 for(i = 0; i < dwNumRows; i++)
246 {
247 _sntprintf(szQuery, 4096, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%ld,%ld)"),
248 DBGetFieldULong(hResult, i, 0), DBGetFieldULong(hResult, i, 1));
249 if (!SQLQuery(szQuery))
250 if (!g_bIgnoreErrors)
251 {
252 DBFreeResult(hResult);
253 return FALSE;
254 }
255 }
256
257 DBFreeResult(hResult);
258 }
259 else
260 {
261 if (!g_bIgnoreErrors)
262 return FALSE;
263 }
264
265 _sntprintf(szQuery, 4096,
266 _T("CREATE TABLE event_cfg (event_code integer not null,"
267 "event_name varchar(63) not null,severity integer,flags integer,"
268 "message varchar(255),description %s,PRIMARY KEY(event_code))"),
269 g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
270 if (!SQLQuery(szQuery))
271 if (!g_bIgnoreErrors)
272 return FALSE;
273
274 if (!SQLBatch(m_szBatch))
275 if (!g_bIgnoreErrors)
276 return FALSE;
277
278 _sntprintf(szQuery, 4096,
279 _T("CREATE TABLE modules (module_id integer not null,"
280 "module_name varchar(63),exec_name varchar(255),"
281 "module_flags integer not null default 0,description %s,"
282 "license_key varchar(255),PRIMARY KEY(module_id))"),
283 g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
284 if (!SQLQuery(szQuery))
285 if (!g_bIgnoreErrors)
286 return FALSE;
287
288 if (!SQLQuery(_T("UPDATE config SET var_value='18' WHERE var_name='DBFormatVersion'")))
289 if (!g_bIgnoreErrors)
290 return FALSE;
291
292 return TRUE;
293 }
294
295
296 //
297 // Upgrade from V16 to V17
298 //
299
300 static BOOL H_UpgradeFromV16(void)
301 {
302 static TCHAR m_szBatch[] =
303 "DROP TABLE locks\n"
304 "CREATE TABLE snmp_trap_cfg (trap_id integer not null,snmp_oid varchar(255) not null,"
305 "event_id integer not null,description varchar(255),PRIMARY KEY(trap_id))\n"
306 "CREATE TABLE snmp_trap_pmap (trap_id integer not null,parameter integer not null,"
307 "snmp_oid varchar(255),description varchar(255),PRIMARY KEY(trap_id,parameter))\n"
308 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
309 "(500, 'SNMP_UNMATCHED_TRAP', 0, 1, 'SNMP trap received: %1 (Parameters: %2)',"
310 "'Generated when system receives an SNMP trap without match in trap "
311 "configuration table#0D#0AParameters:#0D#0A 1) SNMP trap OID#0D#0A"
312 " 2) Trap parameters')\n"
313 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
314 "(501, 'SNMP_COLD_START', 0, 1, 'System was cold-started',"
315 "'Generated when system receives a coldStart SNMP trap#0D#0AParameters:#0D#0A"
316 " 1) SNMP trap OID')\n"
317 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
318 "(502, 'SNMP_WARM_START', 0, 1, 'System was warm-started',"
319 "'Generated when system receives a warmStart SNMP trap#0D#0A"
320 "Parameters:#0D#0A 1) SNMP trap OID')\n"
321 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
322 "(503, 'SNMP_LINK_DOWN', 3, 1, 'Link is down',"
323 "'Generated when system receives a linkDown SNMP trap#0D#0A"
324 "Parameters:#0D#0A 1) SNMP trap OID#0D#0A 2) Interface index')\n"
325 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
326 "(504, 'SNMP_LINK_UP', 0, 1, 'Link is up',"
327 "'Generated when system receives a linkUp SNMP trap#0D#0AParameters:#0D#0A"
328 " 1) SNMP trap OID#0D#0A 2) Interface index')\n"
329 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
330 "(505, 'SNMP_AUTH_FAILURE', 1, 1, 'SNMP authentication failure',"
331 "'Generated when system receives an authenticationFailure SNMP trap#0D#0A"
332 "Parameters:#0D#0A 1) SNMP trap OID')\n"
333 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
334 "(506, 'SNMP_EGP_NEIGHBOR_LOSS', 1, 1, 'EGP neighbor loss',"
335 "'Generated when system receives an egpNeighborLoss SNMP trap#0D#0A"
336 "Parameters:#0D#0A 1) SNMP trap OID')\n"
337 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
338 "VALUES (1,'.1.3.6.1.6.3.1.1.5.1',501,'Generic coldStart trap')\n"
339 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
340 "VALUES (2,'.1.3.6.1.6.3.1.1.5.2',502,'Generic warmStart trap')\n"
341 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
342 "VALUES (3,'.1.3.6.1.6.3.1.1.5.3',503,'Generic linkDown trap')\n"
343 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
344 "VALUES (4,'.1.3.6.1.6.3.1.1.5.4',504,'Generic linkUp trap')\n"
345 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
346 "VALUES (5,'.1.3.6.1.6.3.1.1.5.5',505,'Generic authenticationFailure trap')\n"
347 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
348 "VALUES (6,'.1.3.6.1.6.3.1.1.5.6',506,'Generic egpNeighborLoss trap')\n"
349 "INSERT INTO snmp_trap_pmap (trap_id,parameter,snmp_oid,description) "
350 "VALUES (3,1,'.1.3.6.1.2.1.2.2.1.1','Interface index')\n"
351 "INSERT INTO snmp_trap_pmap (trap_id,parameter,snmp_oid,description) "
352 "VALUES (4,1,'.1.3.6.1.2.1.2.2.1.1','Interface index')\n"
353 "<END>";
354
355 if (!SQLBatch(m_szBatch))
356 if (!g_bIgnoreErrors)
357 return FALSE;
358
359 if (!CreateConfigParam(_T("DBLockStatus"), _T("UNLOCKED"), 0, 1))
360 if (!g_bIgnoreErrors)
361 return FALSE;
362
363 if (!CreateConfigParam(_T("DBLockInfo"), _T(""), 0, 0))
364 if (!g_bIgnoreErrors)
365 return FALSE;
366
367 if (!CreateConfigParam(_T("EnableSNMPTraps"), _T("1"), 1, 1))
368 if (!g_bIgnoreErrors)
369 return FALSE;
370
371 if (!CreateConfigParam(_T("SMSDriver"), _T("<none>"), 1, 1))
372 if (!g_bIgnoreErrors)
373 return FALSE;
374
375 if (!CreateConfigParam(_T("SMTPServer"), _T("localhost"), 1, 0))
376 if (!g_bIgnoreErrors)
377 return FALSE;
378
379 if (!CreateConfigParam(_T("SMTPFromAddr"), _T("netxms@localhost"), 1, 0))
380 if (!g_bIgnoreErrors)
381 return FALSE;
382
383 if (!SQLQuery(_T("UPDATE config SET var_value='17' WHERE var_name='DBFormatVersion'")))
384 if (!g_bIgnoreErrors)
385 return FALSE;
386
387 return TRUE;
388 }
389
390
391 //
392 // Upgrade from V15 to V16
393 //
394
395 static BOOL H_UpgradeFromV15(void)
396 {
397 static TCHAR m_szBatch[] =
398 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
399 "(4005, 'DC_MAILBOX_TOO_LARGE', 1, 1,"
400 "'Mailbox #22%6#22 exceeds size limit (allowed size: %3; actual size: %4)',"
401 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
402 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
403 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
404 " 5) Data collection item ID#0D#0A 6) Instance')\n"
405 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
406 "(4006, 'DC_AGENT_VERSION_CHANGE', 0, 1,"
407 "'NetXMS agent version was changed from %3 to %4',"
408 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
409 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
410 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
411 " 5) Data collection item ID#0D#0A 6) Instance')\n"
412 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
413 "(4007, 'DC_HOSTNAME_CHANGE', 1, 1,"
414 "'Host name was changed from %3 to %4',"
415 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
416 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
417 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
418 " 5) Data collection item ID#0D#0A 6) Instance')\n"
419 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
420 "(4008, 'DC_FILE_CHANGE', 1, 1,"
421 "'File #22%6#22 was changed',"
422 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
423 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
424 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
425 " 5) Data collection item ID#0D#0A 6) Instance')\n"
426 "<END>";
427
428 if (!SQLBatch(m_szBatch))
429 if (!g_bIgnoreErrors)
430 return FALSE;
431 if (!SQLQuery(_T("UPDATE config SET var_value='16' WHERE var_name='DBFormatVersion'")))
432 if (!g_bIgnoreErrors)
433 return FALSE;
434 return TRUE;
435 }
436
437
438 //
439 // Upgrade from V14 to V15
440 //
441
442 static BOOL H_UpgradeFromV14(void)
443 {
444 static TCHAR m_szBatch[] =
445 "ALTER TABLE items ADD instance varchar(255)\n"
446 "UPDATE items SET instance=''\n"
447 "INSERT INTO config (var_name,var_value,is_visible,need_server_restart) VALUES "
448 "('SMTPServer','localhost',1,0)\n"
449 "INSERT INTO config (var_name,var_value,is_visible,need_server_restart) VALUES "
450 "('SMTPFromAddr','netxms@localhost',1,0)\n"
451 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
452 "(4003, 'DC_AGENT_RESTARTED', 0, 1,"
453 "'NetXMS agent was restarted within last 5 minutes',"
454 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
455 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
456 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
457 " 5) Data collection item ID#0D#0A 6) Instance')\n"
458 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
459 "(4004, 'DC_SERVICE_NOT_RUNNING', 3, 1,"
460 "'Service #22%6#22 is not running',"
461 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
462 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
463 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
464 " 5) Data collection item ID#0D#0A 6) Instance')\n"
465 "UPDATE events SET "
466 "description='Generated when threshold value reached "
467 "for specific data collection item.#0D#0A"
468 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
469 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
470 " 5) Data collection item ID#0D#0A 6) Instance' WHERE "
471 "event_id=17 OR (event_id>=4000 AND event_id<5000)\n"
472 "UPDATE events SET "
473 "description='Generated when threshold check is rearmed "
474 "for specific data collection item.#0D#0A"
475 "Parameters:#0D#0A 1) Parameter name#0D#0A"
476 " 2) Item description#0D#0A 3) Data collection item ID' "
477 "WHERE event_id=18\n"
478 "<END>";
479
480 if (!SQLBatch(m_szBatch))
481 if (!g_bIgnoreErrors)
482 return FALSE;
483 if (!SQLQuery(_T("UPDATE config SET var_value='15' WHERE var_name='DBFormatVersion'")))
484 if (!g_bIgnoreErrors)
485 return FALSE;
486 return TRUE;
487 }
488
489
490 //
491 // Upgrade map
492 //
493
494 static struct
495 {
496 int iVersion;
497 BOOL (* fpProc)(void);
498 } m_dbUpgradeMap[] =
499 {
500 { 14, H_UpgradeFromV14 },
501 { 15, H_UpgradeFromV15 },
502 { 16, H_UpgradeFromV16 },
503 { 17, H_UpgradeFromV17 },
504 { 18, H_UpgradeFromV18 },
505 { 19, H_UpgradeFromV19 },
506 { 0, NULL }
507 };
508
509
510 //
511 // Upgrade database to new version
512 //
513
514 void UpgradeDatabase(void)
515 {
516 DB_RESULT hResult;
517 long i, iVersion = 0;
518 BOOL bLocked = FALSE;
519
520 _tprintf(_T("Upgrading database...\n"));
521
522 // Get database format version
523 hResult = DBSelect(g_hCoreDB, _T("SELECT var_value FROM config WHERE var_name='DBFormatVersion'"));
524 if (hResult != NULL)
525 {
526 if (DBGetNumRows(hResult) > 0)
527 iVersion = DBGetFieldLong(hResult, 0, 0);
528 DBFreeResult(hResult);
529 }
530 if (iVersion == DB_FORMAT_VERSION)
531 {
532 _tprintf(_T("Your database format is up to date\n"));
533 }
534 else if (iVersion > DB_FORMAT_VERSION)
535 {
536 _tprintf(_T("Your database has format version %d, this tool is compiled for version %d.\n"
537 "You need to upgrade your server before using this database.\n"),
538 iVersion, DB_FORMAT_VERSION);
539 }
540 else
541 {
542 // Check if database is locked
543 hResult = DBSelect(g_hCoreDB, _T("SELECT var_value FROM config WHERE var_name='DBLockStatus'"));
544 if (hResult != NULL)
545 {
546 if (DBGetNumRows(hResult) > 0)
547 bLocked = _tcscmp(DBGetField(hResult, 0, 0), _T("UNLOCKED"));
548 DBFreeResult(hResult);
549 }
550 if (!bLocked)
551 {
552 // Upgrade database
553 while(iVersion < DB_FORMAT_VERSION)
554 {
555 // Find upgrade procedure
556 for(i = 0; m_dbUpgradeMap[i].fpProc != NULL; i++)
557 if (m_dbUpgradeMap[i].iVersion == iVersion)
558 break;
559 if (m_dbUpgradeMap[i].fpProc == NULL)
560 {
561 _tprintf(_T("Unable to find upgrade procedure for version %d\n"), iVersion);
562 break;
563 }
564 printf("Upgrading from version %d to %d\n", iVersion, iVersion + 1);
565 if (m_dbUpgradeMap[i].fpProc())
566 iVersion++;
567 else
568 break;
569 }
570
571 _tprintf(_T("Database upgrade %s\n"), (iVersion == DB_FORMAT_VERSION) ? _T("succeeded") : _T("failed"));
572 }
573 else
574 {
575 _tprintf(_T("Database is locked\n"));
576 }
577 }
578 }