Branch data Line data Source code
1 : : /**
2 : : * @file db_upgrade.c
3 : : * @brief Orchestrates upgrades of legacy database versions to the current one.
4 : : */
5 : :
6 : : #include "precizer.h"
7 : :
8 : : /**
9 : : * @brief Upgrade database to CURRENT_DB_VERSION.
10 : : *
11 : : * The upgrade path is sequential: first migrate to version 1 (for legacy v0),
12 : : * then migrate to version 4 from the currently reached version.
13 : : *
14 : : * @param[in,out] db_version Current database version from metadata; updated
15 : : * to each successfully stored target version.
16 : : * @param[in] db_file_path Path to the SQLite database file.
17 : : * @param[in] db_file_name Database file name used for logging.
18 : : * @return Return status code.
19 : : */
20 : 28 : Return db_upgrade(
21 : : int *db_version,
22 : : const char *db_file_path,
23 : : const char *db_file_name)
24 : : {
25 : : /* Status returned by this function through provide()
26 : : Default value assumes successful completion */
27 : 28 : Return status = SUCCESS;
28 : :
29 : 28 : slog(EVERY,"The database %s file has an outdated version %d and requires updating to the version %d\n",db_file_name,*db_version,CURRENT_DB_VERSION);
30 : 28 : slog(EVERY,"Warning! The update will be performed in transaction mode for database safety\n");
31 : 28 : slog(EVERY,"Caution! After the update, the database file will not work correctly with old versions of %s. "
32 : : "Please update all copies of the application that will use the new database version!\n",APP_NAME);
33 : :
34 [ + + ]: 28 : if(config->update == false)
35 : : {
36 : 4 : slog(ERROR,"Program execution cannot continue. Database update required. Use the " BOLD "--update" RESET " flag to perform this action\n");
37 : 4 : provide(WARNING);
38 : : }
39 : :
40 : : /* Sequentially upgrade through versions */
41 : :
42 : : /* This legacy can be removed in 2034 (10-year Long-Term Support) */
43 [ + + ]: 24 : if(*db_version < 1)
44 : : {
45 : 8 : const int from_version = *db_version;
46 : :
47 : 8 : slog(TRACE,"Migration from version %d to version 1 started\n",*db_version);
48 : 8 : status = db_migrate_from_0_to_1(db_file_path);
49 : :
50 [ + - ]: 8 : if(SUCCESS == status)
51 : : {
52 : 8 : slog(TRACE,"Store the database version 1 in the metadata table\n");
53 : 8 : status = db_specify_version(db_file_path,1);
54 : :
55 [ + - ]: 8 : if(SUCCESS == status)
56 : : {
57 : 8 : *db_version = 1;
58 : : }
59 : : }
60 : :
61 [ + - ]: 8 : if(SUCCESS == status)
62 : : {
63 : 8 : slog(TRACE,"Migration from version %d to version 1 completed\n",from_version);
64 : : }
65 : : }
66 : :
67 : : /* This legacy can be removed in 2036 (10-year Long-Term Support) */
68 [ + - + - ]: 24 : if(SUCCESS == status && *db_version < 4)
69 : : {
70 : 24 : const int from_version = *db_version;
71 : :
72 : 24 : slog(TRACE,"Migration from version %d to version 4 started\n",*db_version);
73 : 24 : status = db_migrate_to_version_4(db_file_path);
74 : :
75 [ + + ]: 24 : if(SUCCESS == status)
76 : : {
77 : 22 : slog(TRACE,"Store the database version 4 in the metadata table\n");
78 : 22 : status = db_specify_version(db_file_path,4);
79 : :
80 [ + - ]: 22 : if(SUCCESS == status)
81 : : {
82 : 22 : *db_version = 4;
83 : : }
84 : : }
85 : :
86 [ + + ]: 24 : if(SUCCESS == status)
87 : : {
88 : 22 : slog(TRACE,"Migration from version %d to version 4 completed\n",from_version);
89 : : }
90 : : }
91 : :
92 [ + + ]: 24 : if(SUCCESS == status)
93 : : {
94 : 22 : slog(EVERY,"The database has been successfully upgraded\n");
95 : : }
96 : :
97 : 24 : provide(status);
98 : : }
|