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