Branch data Line data Source code
1 : : /**
2 : : * @file db_check_version.c
3 : : * @brief
4 : : */
5 : :
6 : : #include "precizer.h"
7 : :
8 : : /**
9 : : * @brief Checks database version and initiates upgrade if needed
10 : : *
11 : : */
12 : 264 : Return db_check_version(
13 : : const char *db_file_path,
14 : : const char *db_file_name)
15 : : {
16 : : /* Status returned by this function through provide()
17 : : Default value assumes successful completion */
18 : 264 : Return status = SUCCESS;
19 : :
20 : : /* Interrupt the function smoothly */
21 : : /* Interrupt when Ctrl+C */
22 [ - + ]: 264 : if(global_interrupt_flag == true)
23 : : {
24 : 0 : provide(status);
25 : : }
26 : :
27 : : /* Has the database been updated or not? */
28 : 264 : bool db_has_been_upgraded = false;
29 : :
30 : : /// Database Version Control. Each database file maintains
31 : : /// a version number. This is essential for proper database upgrades
32 : : /// and ensures full compatibility between newer application versions and
33 : : /// legacy versions of DB.
34 : : /// Zerro by default
35 : 264 : int db_version = 0;
36 : :
37 : 264 : status = db_get_version(&db_version,db_file_path);
38 : :
39 [ + - ]: 264 : if(SUCCESS == status)
40 : : {
41 : 264 : slog(TRACE,"The %s database file is version %d\n",db_file_name,db_version);
42 : : } else {
43 : :
44 : 0 : slog(ERROR,"Failed to get database version\n");
45 : : }
46 : :
47 [ + - ]: 264 : if(SUCCESS == status)
48 : : {
49 [ + + ]: 264 : if(db_version < CURRENT_DB_VERSION)
50 : : {
51 : 28 : status = db_upgrade(&db_version,db_file_path,db_file_name);
52 : :
53 [ + + ]: 28 : if(SUCCESS == status)
54 : : {
55 : 22 : db_has_been_upgraded = true;
56 : : } else {
57 : 6 : slog(ERROR,"Database %s upgrade failed\n",db_file_name);
58 : : }
59 : :
60 [ + + ]: 236 : } else if(db_version > CURRENT_DB_VERSION){
61 : 4 : slog(ERROR,"The database %s is designed to work with a newer version "
62 : : "of the application and cannot be used with the old one. "
63 : : "Please update %s application to the last version\n",db_file_name,APP_NAME);
64 : 4 : status = WARNING;
65 : : } else {
66 : 232 : slog(TRACE,"The database %s is on version %d and does not require any upgrades\n",db_file_name,db_version);
67 : : }
68 : : }
69 : :
70 [ + + ]: 264 : if(SUCCESS == status)
71 : : {
72 [ + + ]: 254 : if(db_has_been_upgraded == true)
73 : : {
74 : : /* Check if the database being vacuumed is not the primary database.
75 : : The primary database doesn't need to be vacuumed during updates.
76 : : It will be vacuumed automatically before the Precizer
77 : : application session ends */
78 [ + + ]: 22 : if(strcmp(db_file_path,confstr(db_primary_file_path)) != 0)
79 : : {
80 : : /* Vacuum the database */
81 : 12 : status = db_vacuum(db_file_path);
82 : : } else {
83 : 10 : slog(TRACE,"The primary database doesn't need to be vacuumed during updates\n");
84 : : }
85 : : }
86 : : }
87 : :
88 : 264 : provide(status);
89 : : }
|