Line data Source code
1 : /**
2 : * @file db_upgrade.c
3 : * @brief
4 : */
5 :
6 : #include "precizer.h"
7 :
8 : /**
9 : * @brief Upgrades database to the current version
10 : * @param[in] db_file_path Path to the SQLite database file
11 : * @param[out] db_version Pointer to store the retrieved version number
12 : * @return Return status code
13 : */
14 18 : Return db_upgrade(
15 : const int *db_version,
16 : const char *db_file_path,
17 : const char *db_file_name)
18 : {
19 18 : Return status = SUCCESS;
20 :
21 18 : 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);
22 18 : slog(EVERY,"Caution! The update will be performed in transaction mode for database safety\n");
23 18 : slog(EVERY,"Caution! After the update, the database file will not work correctly with old versions of %s. "
24 : "Please update all copies of the application that will use the new database version!\n",APP_NAME);
25 :
26 18 : if(config->update == false)
27 : {
28 4 : slog(ERROR,"Program execution cannot continue. Database update required. Use the " BOLD "--update" RESET " flag to perform this action\n");
29 4 : provide(WARNING);
30 : }
31 :
32 : /* Sequentially upgrade through versions */
33 14 : if(*db_version < 1)
34 : {
35 6 : slog(TRACE,"Migration from version 0 to version 1 started\n");
36 6 : status = db_migrate_from_0_to_1(db_file_path);
37 :
38 6 : if(SUCCESS == status)
39 : {
40 6 : slog(TRACE,"Store the database version 1 in the metadata table\n");
41 6 : status = db_specify_version(db_file_path,1);
42 : }
43 :
44 6 : if(SUCCESS == status)
45 : {
46 6 : slog(TRACE,"Migration from version 0 to version 1 completed\n");
47 : }
48 : }
49 :
50 14 : if(SUCCESS == status && *db_version < 2)
51 : {
52 10 : slog(TRACE,"Migration from version 1 to version 2 started\n");
53 10 : status = db_migrate_from_1_to_2(db_file_path);
54 :
55 10 : if(SUCCESS == status)
56 : {
57 10 : slog(TRACE,"Store the database version 2 in the metadata table\n");
58 10 : status = db_specify_version(db_file_path,2);
59 : }
60 :
61 10 : if(SUCCESS == status)
62 : {
63 10 : slog(TRACE,"Migration from version 1 to version 2 completed\n");
64 : }
65 : }
66 :
67 14 : if(SUCCESS == status && *db_version < 3)
68 : {
69 14 : slog(TRACE,"Migration from version 2 to version 3 started\n");
70 14 : status = db_migrate_from_2_to_3(db_file_path);
71 :
72 14 : if(SUCCESS == status)
73 : {
74 14 : slog(TRACE,"Store the database version 3 in the metadata table\n");
75 14 : status = db_specify_version(db_file_path,3);
76 : }
77 :
78 14 : if(SUCCESS == status)
79 : {
80 14 : slog(TRACE,"Migration from version 2 to version 3 completed\n");
81 : }
82 : }
83 :
84 14 : if(SUCCESS == status)
85 : {
86 14 : slog(EVERY,"The database has been successfully upgraded\n");
87 : }
88 :
89 14 : provide(status);
90 : }
|