Commit 051e674f828aa629d975e44a088e6edf0a591fcc

Authored by David RENÉ
1 parent 8dac565c

[+] Add backup_database for SQLite database

[+] backup database on every starting of the application
CXM_message_constants.anubis
... ... @@ -3,7 +3,7 @@
3 3 * User: David RENÉ
4 4 * Date: 07/04/2007
5 5 * Time: 16:06
6   - * © Calexium
  6 + * © David RENÉ
7 7 *
8 8 */
9 9  
... ...
database/migration_sqlite3.anubis
... ... @@ -12,7 +12,9 @@ read system/logger.anubis
12 12 read system/string.anubis
13 13 read data_base/sqlite.anubis
14 14 read calexium_lib/database/alter_table.anubis
  15 +read calexium_lib/database/db_utils.anubis
15 16 read calexium_lib/net_services_protocols/logger_service.anubis
  17 +read calexium_lib/types/version.anubis
16 18  
17 19 read migration_common_sqlite3.anubis
18 20  
... ... @@ -24,7 +26,7 @@ public type Migration:
24 26 ).
25 27  
26 28  
27   -define String dir_save_database = "/db_backup/".
  29 +define String dir_save_database = "db_backup/".
28 30  
29 31  
30 32 public define Bool
... ... @@ -36,41 +38,6 @@ public define Bool
36 38 db_version_less(a.version, b.version)
37 39 .
38 40  
39   -
40   -/**
41   - * Compare two version string. Returns -1 if first is lower than second, 1 if first is greater than second, and 0 if equals.
42   - */
43   -//public define Int
44   -// compare_db_version
45   -// (
46   -// String version1,
47   -// String version2
48   -// ) =
49   -// with compare = (List(String) v1, List(String) v2) |-compare-> (Int)
50   -// if v1 is
51   -// {
52   -// [] then
53   -// if v2 is
54   -// {
55   -// [] then 0,
56   -// [h2 . t2] then -1
57   -// },
58   -// [h1 . t1] then
59   -// if v2 is
60   -// {
61   -// [] then 1,
62   -// [h2 . t2] then
63   -// if h1 = h2 then compare(t1, t2)
64   -// else
65   -// with v1i = force_Type(decimal_scan(h1), 0),
66   -// v2i = force_Type(decimal_scan(h2), 0),
67   -// if v1i < v2i then -1
68   -// else if v1i > v2i then 1
69   -// else 0
70   -// },
71   -// },
72   -// compare(split(version1, '.'), split(version2, '.')).
73   -
74 41 public define Maybe(One)
75 42 backup_database
76 43 (
... ... @@ -95,8 +62,8 @@ public define Maybe(One)
95 62 with dir_save = db_path + dir_save_database,
96 63 if (Maybe(String))make_directory(dir_save) is
97 64 {
98   - failure then logError(logger, "Can't create directory '"+db_path + dir_save_database+"'"); failure,
99   - success(_) then
  65 + failure then logError(logger, "Can't create directory '"+db_path + dir_save_database+"'"); failure,
  66 + success(_) then
100 67 with copy_with_log = (String src, String dst) |->
101 68 if file_exists(src) then
102 69 if copy_file(src, dst) is
... ... @@ -124,6 +91,51 @@ public define Maybe(One)
124 91 }
125 92 //else success(unique)
126 93 .
  94 +
  95 +public define Maybe(One)
  96 + backup_database
  97 + (
  98 + Logger logger,
  99 + SQLite3DataBase db,
  100 + String db_path,
  101 + String main_db_name
  102 + )=
  103 + //we use SQLite 3.27.0 feature to backup the database. So we check the running SQLite engine version before
  104 + //to proceed
  105 + with v3_27 = version(3,27,0,0),
  106 + with sqlite_version = if sql_query_timeout(db, "SELECT sqlite_version();", "backup_database get version") is
  107 + {
  108 + error(_) then logError(logger, "Can't get sqlite version for backup");failure,
  109 + ok(_, cursor, _) then
  110 + with sqlite_version_str = db_get_String(cursor),
  111 + to_Version(sqlite_version_str)
  112 + },
  113 +
  114 + if sqlite_version is
  115 + {
  116 + failure then logError(logger, "Can't get sqlite version for backup ");failure,
  117 + success(sqlite_v) then
  118 + //if the SQLite engine version is < 3.27 we use old method for backup
  119 + if sqlite_v.numeric +< v3_27.numeric then
  120 + logWarning(logger, "The current SQLite engine v"+sqlite_v+" doesn't support vaccum into. Fallback to previous method");
  121 + backup_database(logger, db_path, main_db_name, [])
  122 + else
  123 + //construct the target folder for backup
  124 + with dir_save = db_path + dir_save_database,
  125 + if (Maybe(String))make_directory(dir_save) is
  126 + {
  127 + failure then logError(logger, "Can't create directory '"+db_path + dir_save_database+"'"); failure,
  128 + success(_) then
  129 + with filename = db_path + dir_save_database+get_current_datetime_condensed+"-"+main_db_name,
  130 + if sql_query(db, "VACUUM INTO '"+filename+"';") is
  131 + {
  132 + error(err) then logError(logger,db_error(err, "Can't backup database into "+filename));failure,
  133 + ok(_,) then logInfo(logger, "database backuped into "+filename);success(unique)
  134 + }
  135 + }
  136 + }
  137 +.
  138 +
127 139 //
128 140 //define Maybe(One)
129 141 // do_migration_loop
... ...