Line data Source code
1 : /**
2 : * @file db_migrate_from_1_to_2.c
3 : * @brief
4 : */
5 :
6 : #include "precizer.h"
7 :
8 : /**
9 : * @brief Migrates database from version 1 to version 2
10 : * @param db_file_path Path to the SQLite database file
11 : * @return Return status code
12 : */
13 10 : Return db_migrate_from_1_to_2(const char *db_file_path)
14 : {
15 10 : Return status = SUCCESS;
16 10 : sqlite3 *db = NULL;
17 10 : char *err_msg = NULL;
18 :
19 10 : if(config->dry_run == true)
20 : {
21 0 : slog(TRACE,"Dry Run mode is enabled. Database migration is not required\n");
22 0 : provide(status);
23 : }
24 :
25 : /* Open database in safe mode */
26 10 : int rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READWRITE|SQLITE_OPEN_FULLMUTEX,NULL);
27 :
28 10 : if(SQLITE_OK != rc)
29 : {
30 0 : log_sqlite_error(db,rc,NULL,"Failed to open database");
31 0 : status = FAILURE;
32 : }
33 :
34 10 : if(SUCCESS == status)
35 : {
36 : /* Set safety pragmas */
37 10 : const char *pragmas =
38 : "PRAGMA journal_mode=DELETE;"
39 : "PRAGMA strict=ON;"
40 : "PRAGMA fsync=ON;"
41 : "PRAGMA synchronous=EXTRA;"
42 : "PRAGMA locking_mode=EXCLUSIVE;";
43 :
44 10 : rc = sqlite3_exec(db,pragmas,NULL,NULL,&err_msg);
45 :
46 10 : if(SQLITE_OK != rc)
47 : {
48 0 : log_sqlite_error(db,rc,err_msg,"Failed to set pragmas");
49 0 : status = FAILURE;
50 : }
51 : }
52 :
53 10 : if(SUCCESS == status)
54 : {
55 : /**
56 : *
57 : * If the database being updated is the primary one, adjust the global
58 : * flag indicating that the main database file has been
59 : * modified (this will consequently update the file's ctime, mtime, and size)
60 : */
61 10 : if(strcmp(db_file_path,config->db_primary_file_path) == 0)
62 : {
63 : /* Changes have been made to the database. Update
64 : this in the global variable value. */
65 6 : config->db_primary_file_modified = true;
66 : }
67 : }
68 :
69 : /* Cleanup */
70 10 : call(db_close(db,&config->db_primary_file_modified));
71 :
72 10 : provide(status);
73 : }
|