migration_sqlite3.anubis
11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Created by PyramIDE.
* User: David René
* Date: 31/08/2011
* Time: 2:09
*
*/
read tools/basis.anubis
read system/files.anubis
read system/logger.anubis
read system/string.anubis
read data_base/sqlite.anubis
read calexium_lib/database/alter_table.anubis
read calexium_lib/net_services_protocols/logger_service.anubis
read migration_common_sqlite3.anubis
public type Migration:
migration(DB_Version version,
(SQLite3DataBase, Logger) -> Maybe(One) migrate_to,
(SQLite3DataBase, Logger) -> Maybe(One) create_tables,
(SQLite3DataBase, Logger) -> Maybe(One) create_indexes,
).
define String dir_save_database = "/db_backup/".
public define Bool
migration_less
(
Migration a,
Migration b
)=
db_version_less(a.version, b.version)
.
/**
* Compare two version string. Returns -1 if first is lower than second, 1 if first is greater than second, and 0 if equals.
*/
//public define Int
// compare_db_version
// (
// String version1,
// String version2
// ) =
// with compare = (List(String) v1, List(String) v2) |-compare-> (Int)
// if v1 is
// {
// [] then
// if v2 is
// {
// [] then 0,
// [h2 . t2] then -1
// },
// [h1 . t1] then
// if v2 is
// {
// [] then 1,
// [h2 . t2] then
// if h1 = h2 then compare(t1, t2)
// else
// with v1i = force_Type(decimal_scan(h1), 0),
// v2i = force_Type(decimal_scan(h2), 0),
// if v1i < v2i then -1
// else if v1i > v2i then 1
// else 0
// },
// },
// compare(split(version1, '.'), split(version2, '.')).
public define Maybe(One)
backup_database
(
Logger logger,
// String current_version,
String db_path,
String main_db_name,
List(String) other_db_names,
)
=
// with should_copy = if file_exists(db_path + main_db_name) then
// if sqlite3_open(db_path + main_db_name) is
// {
// error(sql_error) then false, // maybe not created yet
// ok(Database db) then
// with ver = get_version_settings(db),
// ver != current_version
// }
// else false,
//if should_copy then
with dir_save = db_path + dir_save_database,
if (Maybe(String))make_directory(dir_save) is
{
failure then logError(logger, "Can't create directory '"+db_path + dir_save_database+"'"); failure,
success(_) then
with copy_with_log = (String src, String dst) |->
if file_exists(src) then
if copy_file(src, dst) is
{
cant_read_file then logError(logger, "Can't read source file '" + src + "'."); failure,
cant_create_file then logError(logger, "Can't create destination file '" + dst + "'."); failure,
copy_error then logError(logger, "Can't copy file '" + src + "' to file '" + dst + "'."); failure,
copy_file_mode_error then logError(logger, "Can't get file mode for source file '" + src + "'."); failure,
copy_file_times_error then logError(logger, "Can't set file times into file '" + dst + "'."); failure,
copy_ok(_) then logInfo(logger, "File '" + dst + "' saved"); success(unique)
}
else success(unique),
with copy_db = (List(String) names) |-copy_db->
if names is
{
[] then success(unique),
[h . t] then
if copy_with_log(db_path + h, dir_save + h) is
{
failure then failure,
success(_) then copy_db(t)
}
},
copy_db([main_db_name . other_db_names])
}
//else success(unique)
.
//
//define Maybe(One)
// do_migration_loop
// (
// SQLite3DataBase db,
// Logger logger,
// String current_version, //current version of app
// String db_ver, //version found in db settings
// List(Migration) migrations,
// Maybe(Migration) need_to_create_table, // success in case of new DB or when no migration have been done
// (SQLite3DataBase, String) -> One update_db_version,
// ) =
// if migrations is
// {
// [] then
// if need_to_create_table is
// {
// failure then
// update_db_version(db, current_version);
// success(unique),
// success(last) then
// if last is migration(_, _, create_tables, create_indexes) then
// if create_tables(db, logger) is success(_) then
// if create_indexes(db, logger) is success(_) then
// update_db_version(db, current_version);
// success(unique)
// else
// failure
// else
// failure
// },
// [h . t] then
// since h is migration(version, migrate_to, _, _),
// if compare_db_version(db_ver, version) < 0 then
// logInfo(logger, "Migrating Database from version " + db_ver + " to version " + version);
// if migrate_to(db, logger) is failure then
// failure
// else
// do_migration_loop(db, logger, current_version, version, t, failure, update_db_version)
// else
// do_migration_loop(db, logger, current_version, db_ver, t, success(h), update_db_version),
// }.
//
///* SQLite 3 version */
//public define Maybe(One)
// do_migration
// (
// SQLite3DataBase db,
// Logger logger,
// String current_version,
// (SQLite3DataBase) -> String get_db_version,
// (SQLite3DataBase, String) -> One update_db_version,
// List(Migration) all_migrations,
// )=
// if is_empty_database(db) then // new empty database
// logInfo(logger, "No database found. Creating a new one...");
// do_migration_loop(db, logger, current_version, "", [], last(all_migrations), update_db_version)
// else
// (
// with db_version = get_db_version(db),
// //if app required db version is same as db_version, there is nothing to migrate
// if compare_db_version(db_version, current_version) = 0 then
// logInfo(logger, "Database version "+db_version+". No database migration found.");
// success(unique)
// else
// do_migration_loop(db, logger, current_version, db_version, all_migrations, failure, update_db_version)
// ).
/* SQLite 3 version hk_settings with DB_Version*/
define Maybe(One)
do_table_indexes
(
SQLite3DataBase db,
Logger logger,
Migration _migration,
(DB_Version) -> One update_db_version
)=
since _migration is migration(mig_version, _, create_tables, create_indexes),
if create_tables(db, logger) is success(_) then
if create_indexes(db, logger) is success(_) then
update_db_version(mig_version);
success(unique)
else
failure
else
failure
.
define Maybe(One)
do_migration_loop
(
SQLite3DataBase db,
Logger logger,
// String current_version, //current version of app
DB_Version db_ver, //version found in db settings
List(Migration) migrations,
(DB_Version) -> One update_db_version,
) =
if migrations is
{
[] then success(unique),
[h . t] then
since h is migration(migration_version, migrate_to, create_tables, create_indexes),
if compare_db_version(db_ver, migration_version) is
{
less then
logInfo(logger, "[less] Migrating Database from version " + db_ver + " to version " + migration_version);
if migrate_to(db, logger) is failure then
failure
else
update_db_version(migration_version);
do_migration_loop(db, logger, migration_version, t, update_db_version)
more then
logInfo(logger, "[more] No Database migration found from version " + db_ver + " to version " + migration_version);
do_migration_loop(db, logger, db_ver, t, update_db_version)
equal then
logInfo(logger, "[equal] No Database migration found from version " + db_ver + " to version " + migration_version);
do_migration_loop(db, logger, db_ver, t, update_db_version)
equal_but_sign then
logInfo(logger, "Database migration found from version " + db_ver + " to version " + migration_version+ " by hash difference ");
if migrate_to(db, logger) is failure then
failure
else
update_db_version(migration_version);
do_migration_loop(db, logger, migration_version, t, update_db_version)
}
}.
public define Maybe(One)
do_migration
(
SQLite3DataBase db,
Logger logger,
DB_Version current_db_version,
(DB_Version) -> One update_db_version,
List(Migration) all_migrations,
)=
if is_empty_database(db) then // new empty database
logInfo(logger, "No database found. Creating a new one...");
if last(all_migrations) is success(migration) then
do_table_indexes(db, logger, migration, update_db_version)
else
logError(logger, "No database migration found. Can't create Database");
failure
else
(
// with db_version = get_db_version,
// //if app version is same as db_version, there is nothing to migrate
// if compare_db_version(db_version, current_version) = 0 then
// logInfo(logger, "Database version "+db_version+". No database migration found.");
// success(unique)
// else
do_migration_loop(db, logger, current_db_version, all_migrations, update_db_version)
).
public define Maybe(One)
do_create_indexes
(
SQLite3DataBase db,
Logger logger,
List(Migration) all_migrations,
)=
if last(all_migrations) is
{
failure then logCriticalError(logger, "Index creation: migration list is empty!"); failure,
success(last_migration) then
if last_migration is migration(_, _, _, create_indexes) then
if create_indexes(db, logger) is success(_) then
success(unique)
else
failure
}.