Commit 3fe910d34cca92bbae0f1a6f17ae26039403d163
1 parent
670efd29
replace read by transmit
Showing
3 changed files
with
215 additions
and
207 deletions
Show diff stats
database/db_utils.anubis
| ... | ... | @@ -13,7 +13,7 @@ read system/string.anubis |
| 13 | 13 | read data_base/db_tools.anubis |
| 14 | 14 | read data_base/sqlite.anubis |
| 15 | 15 | |
| 16 | -read calexium_lib/database/db_types.anubis | |
| 16 | +transmit calexium_lib/database/db_types.anubis | |
| 17 | 17 | read calexium_lib/net_services_protocols/logger_service.anubis |
| 18 | 18 | |
| 19 | 19 | public define Int one_year_seconds = 365 * 86400. // amount of seconds during 1 year | ... | ... |
database/migration_sqlite3.anubis
| 1 | -/* | |
| 2 | - * Created by PyramIDE. | |
| 3 | - * User: David René | |
| 4 | - * Date: 31/08/2011 | |
| 5 | - * Time: 2:09 | |
| 6 | - * | |
| 7 | - */ | |
| 8 | - | |
| 9 | -read tools/basis.anubis | |
| 10 | -read system/files.anubis | |
| 11 | -read system/logger.anubis | |
| 12 | -read system/string.anubis | |
| 13 | -read data_base/sqlite.anubis | |
| 14 | -read calexium_lib/database/alter_table.anubis | |
| 15 | -read calexium_lib/net_services_protocols/logger_service.anubis | |
| 16 | - | |
| 17 | -read migration_common_sqlite3.anubis | |
| 18 | - | |
| 19 | - | |
| 20 | -define String dir_save_database = "/db_backup/". | |
| 21 | - | |
| 22 | - | |
| 23 | -/** | |
| 24 | - * Compare two version string. Returns -1 if first is lower than second, 1 if first is greater than second, and 0 if equals. | |
| 25 | - */ | |
| 26 | -public define Int | |
| 27 | - compare_db_version | |
| 28 | - ( | |
| 29 | - String version1, | |
| 30 | - String version2 | |
| 31 | - ) = | |
| 32 | - with compare = (List(String) v1, List(String) v2) |-compare-> (Int) | |
| 33 | - if v1 is | |
| 34 | - { | |
| 35 | - [] then | |
| 36 | - if v2 is | |
| 37 | - { | |
| 38 | - [] then 0, | |
| 39 | - [h2 . t2] then -1 | |
| 40 | - }, | |
| 41 | - [h1 . t1] then | |
| 42 | - if v2 is | |
| 43 | - { | |
| 44 | - [] then 1, | |
| 45 | - [h2 . t2] then | |
| 46 | - if h1 = h2 then compare(t1, t2) | |
| 47 | - else | |
| 48 | - with v1i = force_Type(decimal_scan(h1), 0), | |
| 49 | - v2i = force_Type(decimal_scan(h2), 0), | |
| 50 | - if v1i < v2i then -1 | |
| 51 | - else if v1i > v2i then 1 | |
| 52 | - else 0 | |
| 53 | - }, | |
| 54 | - }, | |
| 55 | - compare(split(version1, '.'), split(version2, '.')). | |
| 56 | - | |
| 57 | -public define Maybe(One) | |
| 58 | - backup_database | |
| 59 | - ( | |
| 60 | - Logger logger, | |
| 61 | -// String current_version, | |
| 62 | - String db_path, | |
| 63 | - String main_db_name, | |
| 64 | - List(String) other_db_names, | |
| 65 | - ) | |
| 66 | - = | |
| 67 | -// with should_copy = if file_exists(db_path + main_db_name) then | |
| 68 | -// if sqlite3_open(db_path + main_db_name) is | |
| 69 | -// { | |
| 70 | -// error(sql_error) then false, // maybe not created yet | |
| 71 | -// ok(Database db) then | |
| 72 | -// with ver = get_version_settings(db), | |
| 73 | -// ver != current_version | |
| 74 | -// } | |
| 75 | -// else false, | |
| 76 | - | |
| 77 | - //if should_copy then | |
| 78 | - with dir_save = db_path + dir_save_database, | |
| 79 | - if (Maybe(String))make_directory(dir_save) is | |
| 80 | - { | |
| 81 | - failure then logError(logger, "Can't create directory '"+db_path + dir_save_database+"'"); failure, | |
| 82 | - success(_) then | |
| 83 | - with copy_with_log = (String src, String dst) |-> | |
| 84 | - if file_exists(src) then | |
| 85 | - if copy_file(src, dst) is | |
| 86 | - { | |
| 87 | - cant_read_file then logError(logger, "Can't read source file '" + src + "'."); failure, | |
| 88 | - cant_create_file then logError(logger, "Can't create destination file '" + dst + "'."); failure, | |
| 89 | - copy_error then logError(logger, "Can't copy file '" + src + "' to file '" + dst + "'."); failure, | |
| 90 | - copy_file_mode_error then logError(logger, "Can't get file mode for source file '" + src + "'."); failure, | |
| 91 | - copy_file_times_error then logError(logger, "Can't set file times into file '" + dst + "'."); failure, | |
| 92 | - copy_ok(_) then logInfo(logger, "File '" + dst + "' saved"); success(unique) | |
| 93 | - } | |
| 94 | - else success(unique), | |
| 95 | - with copy_db = (List(String) names) |-copy_db-> | |
| 96 | - if names is | |
| 97 | - { | |
| 98 | - [] then success(unique), | |
| 99 | - [h . t] then | |
| 100 | - if copy_with_log(db_path + h, dir_save + h) is | |
| 101 | - { | |
| 102 | - failure then failure, | |
| 103 | - success(_) then copy_db(t) | |
| 104 | - } | |
| 105 | - }, | |
| 106 | - copy_db([main_db_name . other_db_names]) | |
| 107 | - } | |
| 108 | - //else success(unique) | |
| 109 | - . | |
| 110 | - | |
| 111 | - | |
| 112 | - | |
| 113 | - | |
| 114 | - | |
| 115 | - | |
| 116 | -public type Migration: | |
| 117 | - migration(String version, | |
| 118 | - (SQLite3DataBase, Logger) -> Maybe(One) migrate_to, | |
| 119 | - (SQLite3DataBase, Logger) -> Maybe(One) create_tables, | |
| 120 | - (SQLite3DataBase, Logger) -> Maybe(One) create_indexes, | |
| 121 | - ). | |
| 122 | - | |
| 123 | - | |
| 124 | - | |
| 125 | -define Maybe(One) | |
| 126 | - do_migration_loop | |
| 127 | - ( | |
| 128 | - SQLite3DataBase db, | |
| 129 | - Logger logger, | |
| 130 | - String current_version, | |
| 131 | - String db_ver, | |
| 132 | - List(Migration) migrations, | |
| 133 | - Maybe(Migration) need_to_create_table, // success in case of new DB or when no migration have been done | |
| 134 | - (SQLite3DataBase, String) -> One update_db_version, | |
| 135 | - ) = | |
| 136 | - if migrations is | |
| 137 | - { | |
| 138 | - [] then | |
| 139 | - if need_to_create_table is | |
| 140 | - { | |
| 141 | - failure then | |
| 142 | - update_db_version(db, current_version); | |
| 143 | - success(unique), | |
| 144 | - success(last) then | |
| 145 | - if last is migration(_, _, create_tables, create_indexes) then | |
| 146 | - if create_tables(db, logger) is success(_) then | |
| 147 | - if create_indexes(db, logger) is success(_) then | |
| 148 | - update_db_version(db, current_version); | |
| 149 | - success(unique) | |
| 150 | - else | |
| 151 | - failure | |
| 152 | - else | |
| 153 | - failure | |
| 154 | - }, | |
| 155 | - [h . t] then | |
| 156 | - if h is migration(version, migrate_to, _, _) then | |
| 157 | - if compare_db_version(db_ver, version) < 0 then | |
| 158 | - logInfo(logger, "Migrating Database from version " + db_ver + " to version " + version); | |
| 159 | - if migrate_to(db, logger) is failure then | |
| 160 | - failure | |
| 161 | - else | |
| 162 | - do_migration_loop(db, logger, current_version, version, t, failure, update_db_version) | |
| 163 | - else | |
| 164 | - do_migration_loop(db, logger, current_version, db_ver, t, success(h), update_db_version), | |
| 165 | - }. | |
| 166 | - | |
| 167 | -/* SQLite 3 version */ | |
| 168 | -public define Maybe(One) | |
| 169 | - do_migration | |
| 170 | - ( | |
| 171 | - SQLite3DataBase db, | |
| 172 | - Logger logger, | |
| 173 | - String current_version, | |
| 174 | - (SQLite3DataBase) -> String get_db_version, | |
| 175 | - (SQLite3DataBase, String) -> One update_db_version, | |
| 176 | - List(Migration) all_migrations, | |
| 177 | - )= | |
| 178 | - if is_empty_database(db) then // new empty database | |
| 179 | - logInfo(logger, "No database found. Creating a new one..."); | |
| 180 | - do_migration_loop(db, logger, current_version, "", [], last(all_migrations), update_db_version) | |
| 181 | - else | |
| 182 | - ( | |
| 183 | - with db_version = get_db_version(db), | |
| 184 | - do_migration_loop(db, logger, current_version, db_version, all_migrations, failure, update_db_version) | |
| 185 | - ). | |
| 186 | - | |
| 187 | -public define Maybe(One) | |
| 188 | - do_create_indexes | |
| 189 | - ( | |
| 190 | - SQLite3DataBase db, | |
| 191 | - Logger logger, | |
| 192 | - String current_version, | |
| 193 | - List(Migration) all_migrations, | |
| 194 | - )= | |
| 195 | - if last(all_migrations) is | |
| 196 | - { | |
| 197 | - failure then logCriticalError(logger, "Index creation: migration list is empty!"); failure, | |
| 198 | - success(last_migration) then | |
| 199 | - if last_migration is migration(_, _, _, create_indexes) then | |
| 200 | - if create_indexes(db, logger) is success(_) then | |
| 201 | - success(unique) | |
| 202 | - else | |
| 203 | - failure | |
| 204 | - }. | |
| 1 | +/* | |
| 2 | + * Created by PyramIDE. | |
| 3 | + * User: David René | |
| 4 | + * Date: 31/08/2011 | |
| 5 | + * Time: 2:09 | |
| 6 | + * | |
| 7 | + */ | |
| 8 | + | |
| 9 | +read tools/basis.anubis | |
| 10 | +read system/files.anubis | |
| 11 | +read system/logger.anubis | |
| 12 | +read system/string.anubis | |
| 13 | +read data_base/sqlite.anubis | |
| 14 | +read calexium_lib/database/alter_table.anubis | |
| 15 | +read calexium_lib/net_services_protocols/logger_service.anubis | |
| 16 | + | |
| 17 | +read migration_common_sqlite3.anubis | |
| 18 | + | |
| 19 | + | |
| 20 | +define String dir_save_database = "/db_backup/". | |
| 21 | + | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * Compare two version string. Returns -1 if first is lower than second, 1 if first is greater than second, and 0 if equals. | |
| 25 | + */ | |
| 26 | +public define Int | |
| 27 | + compare_db_version | |
| 28 | + ( | |
| 29 | + String version1, | |
| 30 | + String version2 | |
| 31 | + ) = | |
| 32 | + with compare = (List(String) v1, List(String) v2) |-compare-> (Int) | |
| 33 | + if v1 is | |
| 34 | + { | |
| 35 | + [] then | |
| 36 | + if v2 is | |
| 37 | + { | |
| 38 | + [] then 0, | |
| 39 | + [h2 . t2] then -1 | |
| 40 | + }, | |
| 41 | + [h1 . t1] then | |
| 42 | + if v2 is | |
| 43 | + { | |
| 44 | + [] then 1, | |
| 45 | + [h2 . t2] then | |
| 46 | + if h1 = h2 then compare(t1, t2) | |
| 47 | + else | |
| 48 | + with v1i = force_Type(decimal_scan(h1), 0), | |
| 49 | + v2i = force_Type(decimal_scan(h2), 0), | |
| 50 | + if v1i < v2i then -1 | |
| 51 | + else if v1i > v2i then 1 | |
| 52 | + else 0 | |
| 53 | + }, | |
| 54 | + }, | |
| 55 | + compare(split(version1, '.'), split(version2, '.')). | |
| 56 | + | |
| 57 | +public define Maybe(One) | |
| 58 | + backup_database | |
| 59 | + ( | |
| 60 | + Logger logger, | |
| 61 | +// String current_version, | |
| 62 | + String db_path, | |
| 63 | + String main_db_name, | |
| 64 | + List(String) other_db_names, | |
| 65 | + ) | |
| 66 | + = | |
| 67 | +// with should_copy = if file_exists(db_path + main_db_name) then | |
| 68 | +// if sqlite3_open(db_path + main_db_name) is | |
| 69 | +// { | |
| 70 | +// error(sql_error) then false, // maybe not created yet | |
| 71 | +// ok(Database db) then | |
| 72 | +// with ver = get_version_settings(db), | |
| 73 | +// ver != current_version | |
| 74 | +// } | |
| 75 | +// else false, | |
| 76 | + | |
| 77 | + //if should_copy then | |
| 78 | + with dir_save = db_path + dir_save_database, | |
| 79 | + if (Maybe(String))make_directory(dir_save) is | |
| 80 | + { | |
| 81 | + failure then logError(logger, "Can't create directory '"+db_path + dir_save_database+"'"); failure, | |
| 82 | + success(_) then | |
| 83 | + with copy_with_log = (String src, String dst) |-> | |
| 84 | + if file_exists(src) then | |
| 85 | + if copy_file(src, dst) is | |
| 86 | + { | |
| 87 | + cant_read_file then logError(logger, "Can't read source file '" + src + "'."); failure, | |
| 88 | + cant_create_file then logError(logger, "Can't create destination file '" + dst + "'."); failure, | |
| 89 | + copy_error then logError(logger, "Can't copy file '" + src + "' to file '" + dst + "'."); failure, | |
| 90 | + copy_file_mode_error then logError(logger, "Can't get file mode for source file '" + src + "'."); failure, | |
| 91 | + copy_file_times_error then logError(logger, "Can't set file times into file '" + dst + "'."); failure, | |
| 92 | + copy_ok(_) then logInfo(logger, "File '" + dst + "' saved"); success(unique) | |
| 93 | + } | |
| 94 | + else success(unique), | |
| 95 | + with copy_db = (List(String) names) |-copy_db-> | |
| 96 | + if names is | |
| 97 | + { | |
| 98 | + [] then success(unique), | |
| 99 | + [h . t] then | |
| 100 | + if copy_with_log(db_path + h, dir_save + h) is | |
| 101 | + { | |
| 102 | + failure then failure, | |
| 103 | + success(_) then copy_db(t) | |
| 104 | + } | |
| 105 | + }, | |
| 106 | + copy_db([main_db_name . other_db_names]) | |
| 107 | + } | |
| 108 | + //else success(unique) | |
| 109 | + . | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | +public type Migration: | |
| 117 | + migration(String version, | |
| 118 | + (SQLite3DataBase, Logger) -> Maybe(One) migrate_to, | |
| 119 | + (SQLite3DataBase, Logger) -> Maybe(One) create_tables, | |
| 120 | + (SQLite3DataBase, Logger) -> Maybe(One) create_indexes, | |
| 121 | + ). | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | +define Maybe(One) | |
| 126 | + do_migration_loop | |
| 127 | + ( | |
| 128 | + SQLite3DataBase db, | |
| 129 | + Logger logger, | |
| 130 | + String current_version, //current version of app | |
| 131 | + String db_ver, //version found in db settings | |
| 132 | + List(Migration) migrations, | |
| 133 | + Maybe(Migration) need_to_create_table, // success in case of new DB or when no migration have been done | |
| 134 | + (SQLite3DataBase, String) -> One update_db_version, | |
| 135 | + ) = | |
| 136 | + if migrations is | |
| 137 | + { | |
| 138 | + [] then | |
| 139 | + if need_to_create_table is | |
| 140 | + { | |
| 141 | + failure then | |
| 142 | + update_db_version(db, current_version); | |
| 143 | + success(unique), | |
| 144 | + success(last) then | |
| 145 | + if last is migration(_, _, create_tables, create_indexes) then | |
| 146 | + if create_tables(db, logger) is success(_) then | |
| 147 | + if create_indexes(db, logger) is success(_) then | |
| 148 | + update_db_version(db, current_version); | |
| 149 | + success(unique) | |
| 150 | + else | |
| 151 | + failure | |
| 152 | + else | |
| 153 | + failure | |
| 154 | + }, | |
| 155 | + [h . t] then | |
| 156 | + if h is migration(version, migrate_to, _, _) then | |
| 157 | + if compare_db_version(db_ver, version) < 0 then | |
| 158 | + logInfo(logger, "Migrating Database from version " + db_ver + " to version " + version); | |
| 159 | + if migrate_to(db, logger) is failure then | |
| 160 | + failure | |
| 161 | + else | |
| 162 | + do_migration_loop(db, logger, current_version, version, t, failure, update_db_version) | |
| 163 | + else | |
| 164 | + do_migration_loop(db, logger, current_version, db_ver, t, success(h), update_db_version), | |
| 165 | + }. | |
| 166 | + | |
| 167 | +/* SQLite 3 version */ | |
| 168 | +public define Maybe(One) | |
| 169 | + do_migration | |
| 170 | + ( | |
| 171 | + SQLite3DataBase db, | |
| 172 | + Logger logger, | |
| 173 | + String current_version, | |
| 174 | + (SQLite3DataBase) -> String get_db_version, | |
| 175 | + (SQLite3DataBase, String) -> One update_db_version, | |
| 176 | + List(Migration) all_migrations, | |
| 177 | + )= | |
| 178 | + if is_empty_database(db) then // new empty database | |
| 179 | + logInfo(logger, "No database found. Creating a new one..."); | |
| 180 | + do_migration_loop(db, logger, current_version, "", [], last(all_migrations), update_db_version) | |
| 181 | + else | |
| 182 | + ( | |
| 183 | + with db_version = get_db_version(db), | |
| 184 | + //if app version is same as db_version, there is nothing to migrate | |
| 185 | + if compare_db_version(db_version, current_version) = 0 then | |
| 186 | + logInfo(logger, "Database version "+db_version+". No database migration found."); | |
| 187 | + success(unique) | |
| 188 | + else | |
| 189 | + do_migration_loop(db, logger, current_version, db_version, all_migrations, failure, update_db_version) | |
| 190 | + ). | |
| 191 | + | |
| 192 | +public define Maybe(One) | |
| 193 | + do_create_indexes | |
| 194 | + ( | |
| 195 | + SQLite3DataBase db, | |
| 196 | + Logger logger, | |
| 197 | + String current_version, | |
| 198 | + List(Migration) all_migrations, | |
| 199 | + )= | |
| 200 | + if last(all_migrations) is | |
| 201 | + { | |
| 202 | + failure then logCriticalError(logger, "Index creation: migration list is empty!"); failure, | |
| 203 | + success(last_migration) then | |
| 204 | + if last_migration is migration(_, _, _, create_indexes) then | |
| 205 | + if create_indexes(db, logger) is success(_) then | |
| 206 | + success(unique) | |
| 207 | + else | |
| 208 | + failure | |
| 209 | + }. | ... | ... |
database/settings.anubis
| ... | ... | @@ -85,9 +85,12 @@ public define One |
| 85 | 85 | String var, |
| 86 | 86 | String value |
| 87 | 87 | )= |
| 88 | + with binds = (List(SQLite3Bind))[bind_String("@name", var), | |
| 89 | + bind_String("@value", value)], | |
| 90 | + | |
| 88 | 91 | if select_settings(db, var) is success(_) then |
| 89 | - forget(sql_query_timeout(db, "UPDATE settings SET var_value = @value WHERE var_name = @name;", [bind_String("@name", var), bind_String("@value", value)], "update_settings")) | |
| 92 | + forget(sql_query_timeout(db, "UPDATE settings SET var_value = @value WHERE var_name = @name;", binds, "update_settings")) | |
| 90 | 93 | else |
| 91 | - forget(sql_query_timeout(db, "INSERT INTO settings (var_name, var_value) VALUES (@name, @value)", [bind_String("@name", var), bind_String("@value", value)], "update_settings")) | |
| 94 | + forget(sql_query_timeout(db, "INSERT INTO settings (var_name, var_value) VALUES (@name, @value)", binds, "update_settings")) | |
| 92 | 95 | . |
| 93 | 96 | ... | ... |