Branch data Line data Source code
1 : : /**
2 : : * @file db_check_version.c
3 : : * @brief Database-version validation and upgrade entry point
4 : : */
5 : :
6 : : #include "precizer.h"
7 : :
8 : : /**
9 : : * @brief Check a database version and run required upgrades
10 : : *
11 : : * Retrieves the version stored in the database metadata, upgrades older
12 : : * databases to `CURRENT_DB_VERSION`, and rejects databases created for a newer
13 : : * application version. When a non-primary database is upgraded, the function
14 : : * vacuums it after the upgrade. The primary database is left for the regular
15 : : * session-end vacuum path
16 : : *
17 : : * @param[in] db_file_path Path to the SQLite database file being checked
18 : : * @param[in] db_file_name Display name used in diagnostic messages
19 : : * @return SUCCESS when the database is compatible or was upgraded, WARNING
20 : : * when the database requires a newer application, or FAILURE when
21 : : * version retrieval, upgrade, or post-upgrade vacuum fails
22 : : */
23 : 397 : Return db_check_version(
24 : : const char *db_file_path,
25 : : const char *db_file_name)
26 : : {
27 : : /* Status returned by this function through provide()
28 : : Default value assumes successful completion */
29 : 397 : Return status = SUCCESS;
30 : :
31 : : /* Interrupt the function smoothly */
32 : : /* Interrupt when Ctrl+C */
33 [ - + ]: 397 : if(global_interrupt_flag == true)
34 : : {
35 : 0 : provide(status);
36 : : }
37 : :
38 : : /* Has the database been updated or not? */
39 : 397 : bool db_has_been_upgraded = false;
40 : :
41 : : /// Database Version Control. Each database file maintains
42 : : /// a version number. This is essential for proper database upgrades
43 : : /// and ensures full compatibility between newer application versions and
44 : : /// legacy versions of DB.
45 : : /// Zerro by default
46 : 397 : int db_version = 0;
47 : :
48 : 397 : status = db_retrieve_version(&db_version,db_file_path);
49 : :
50 [ + - ]: 397 : if(SUCCESS == status)
51 : : {
52 : 397 : slog(TRACE,"The %s database file is version %d\n",db_file_name,db_version);
53 : : } else {
54 : :
55 : 0 : slog(ERROR,"Failed to retrieve database version\n");
56 : : }
57 : :
58 [ + - ]: 397 : if(SUCCESS == status)
59 : : {
60 [ + + ]: 397 : if(db_version < CURRENT_DB_VERSION)
61 : : {
62 : 28 : status = db_upgrade(&db_version,db_file_path,db_file_name);
63 : :
64 [ + + ]: 28 : if(SUCCESS == status)
65 : : {
66 : 22 : db_has_been_upgraded = true;
67 : : } else {
68 : 6 : slog(ERROR,"Database %s upgrade failed\n",db_file_name);
69 : : }
70 : :
71 [ + + ]: 369 : } else if(db_version > CURRENT_DB_VERSION){
72 : 4 : slog(ERROR,"The database %s is designed to work with a newer version "
73 : : "of the application and cannot be used with the old one. "
74 : : "Please update %s application to the last version\n",db_file_name,APP_NAME);
75 : 4 : status = WARNING;
76 : : } else {
77 : 365 : slog(TRACE,"The database %s is on version %d and does not require any upgrades\n",db_file_name,db_version);
78 : : }
79 : : }
80 : :
81 [ + + ]: 397 : if(SUCCESS == status)
82 : : {
83 [ + + ]: 387 : if(db_has_been_upgraded == true)
84 : : {
85 : : /* Check if the database being vacuumed is not the primary database.
86 : : The primary database doesn't need to be vacuumed during updates.
87 : : It will be vacuumed automatically before the Precizer
88 : : application session ends */
89 [ + + ]: 22 : if(strcmp(db_file_path,confstr(db_primary_file_path)) != 0)
90 : : {
91 : : /* Vacuum the database */
92 : 12 : status = db_vacuum(db_file_path);
93 : : } else {
94 : 10 : slog(TRACE,"The primary database doesn't need to be vacuumed during updates\n");
95 : : }
96 : : }
97 : : }
98 : :
99 : 397 : provide(status);
100 : : }
|