Commit 878378945311ec155cb57b3cd0e373235e8ce9bc

Authored by Cédric RICARD
1 parent 2efabe08

MF migration under PostgreSQL

calexium_lib/database/db_utils.anubis
... ... @@ -10,7 +10,7 @@
10 10 read tools/basis.anubis
11 11 read system/logger.anubis
12 12 read system/string.anubis
13   -read data_base/sqlite.anubis
  13 +read data_base/db_tools.anubis
14 14  
15 15 read calexium_lib/net_services_protocols/logger_service.anubis
16 16  
... ... @@ -33,10 +33,10 @@ define One
33 33 else
34 34 unique.
35 35  
36   -public define Result(SQLite3Error, One)
  36 +public define Result(DbError, One)
37 37 sql_transaction
38 38 (
39   - SQLite3DataBase db,
  39 + Database db,
40 40 String sql_command,
41 41 String message
42 42 ) =
... ... @@ -45,14 +45,15 @@ public define Result(SQLite3Error, One)
45 45 if db_do_transaction(
46 46 db,
47 47 (One _) |->
48   - if sql_query(db, sql_command) is
  48 + if db_query(db, sql_command) is
49 49 {
50 50 error(err) then logError("DB", db_error(err,message));failure //error in the SQL request
51   - ok(cursor) then success(unique)
  51 + ok(_,cursor,_) then success(unique)
52 52 },
53   - success( (SQLite3Error err) |-> logError("DB", db_error(err,message))),
  53 + success( (DbError err) |-> logError("DB", db_error(err,message))),
54 54 60000, // max 60s
55   - 100 // retry every 100 ms
  55 + 100, // retry every 100 ms
  56 + (DbError _) |-> false
56 57 ) is
57 58 {
58 59 error(err_and_result) then
... ... @@ -65,19 +66,19 @@ public define Result(SQLite3Error, One)
65 66 ok(unique)
66 67 }.
67 68  
68   -
69 69  
70   -public define SQLite3QueryResult
  70 +
  71 +public define DbQueryResult
71 72 sql_query_timeout
72 73 (
73   - SQLite3DataBase db, //database handle
74   - String sql_query, //sql query itself
75   - List(SQLite3Bind) initial_bindings,
76   - String msg //message to be shown if an error occure
  74 + Database db, //database handle
  75 + String sql_query, //sql query itself
  76 + List(DbBind) initial_bindings,
  77 + String msg //message to be shown if an error occure
77 78 ) =
78 79 //we try with 30 sec of timeout
79 80 with t0 = unow,
80   - if sql_query_timeout(db, sql_query, initial_bindings, 60, 100) is
  81 + if sql_query_timeout(db, sql_query, initial_bindings, 60, 100, (DbError _) |-> false) is
81 82 {
82 83 error(sql_error) then logError("DB", db_error(sql_error,msg));
83 84 __logLongQueries(t0, sql_query);
... ... @@ -88,16 +89,16 @@ public define SQLite3QueryResult
88 89 }.
89 90  
90 91 // deprecated. Use one of the previous ones.
91   -public define Maybe(SQLite3HeadersOrRow -> SQLite3Row)
  92 +public define Maybe(SQLite3HeadersOrRow -> DbRow)
92 93 sql_query_timeout
93 94 (
94   - SQLite3DataBase db, //database handle
  95 + Database db, //database handle
95 96 String sql_query, //sql query itself
96 97 String msg //message to be shown if an error occure
97 98 ) =
98 99 //we try with 30 sec of timeout
99 100 with t0 = unow,
100   - if sql_query_timeout(db, sql_query, [], 60, 100) is
  101 + if sql_query_timeout(db, sql_query, [], 60, 100, (DbError _) |-> false) is
101 102 {
102 103 error(sql_error) then logError("DB", db_error(sql_error,msg));
103 104 __logLongQueries(t0, sql_query);
... ... @@ -107,7 +108,7 @@ public define Maybe(SQLite3HeadersOrRow -> SQLite3Row)
107 108 success((SQLite3HeadersOrRow h_or_r) |-> if h_or_r is
108 109 {
109 110 headers then
110   - with cols = map((String c) |-> (SQLite3Datum)text(c), headers(unique)),
  111 + with cols = map((String c) |-> (DbDatum)db_text(c), headers(unique)),
111 112 row((Int n) |-> force(nth(n, cols), no_such_column)),
112 113 next_row then cursor(unique)
113 114 })
... ... @@ -118,8 +119,8 @@ public define Maybe(SQLite3HeadersOrRow -> SQLite3Row)
118 119 public define List(String)
119 120 db_get_string_list
120 121 (
121   - One -> SQLite3Row table_cursor,
122   - List(String) so_far
  122 + One -> DbRow table_cursor,
  123 + List(String) so_far
123 124 ) =
124 125 if table_cursor(unique) is
125 126 {
... ... @@ -134,7 +135,7 @@ public define List(String)
134 135 public define List(String)
135 136 db_get_string_list
136 137 (
137   - SQLite3HeadersOrRow -> SQLite3Row table_cursor,
  138 + SQLite3HeadersOrRow -> DbRow table_cursor,
138 139 List(String) so_far
139 140 ) =
140 141 if table_cursor(next_row) is
... ... @@ -146,10 +147,11 @@ public define List(String)
146 147 db_get_string_list(table_cursor, [s . so_far])
147 148 }.
148 149  
  150 +// Old sqlite3 API
149 151 public define List(Int)
150 152 db_get_integer_list
151 153 (
152   - SQLite3HeadersOrRow -> SQLite3Row table_cursor,
  154 + SQLite3HeadersOrRow -> DbRow table_cursor,
153 155 List(Int) so_far
154 156 ) =
155 157 if table_cursor(next_row) is
... ... @@ -165,7 +167,7 @@ public define List(Int)
165 167 public define List(Int)
166 168 db_get_integer_list
167 169 (
168   - One -> SQLite3Row table_cursor,
  170 + One -> DbRow table_cursor,
169 171 List(Int) so_far
170 172 ) =
171 173 if table_cursor(unique) is
... ... @@ -181,4 +183,37 @@ public define List(Int)
181 183  
182 184  
183 185  
184   -
  186 +// -- Migration HELPERS ---------------
  187 +
  188 + public define DbBind
  189 + bind_String
  190 + (
  191 + String name,
  192 + String value
  193 + ) =
  194 + db_bind(force(sub_string(name, 1, length(name) - 1), name), db_text(value)).
  195 +
  196 + public define DbBind
  197 + bind_ByteArray
  198 + (
  199 + String name,
  200 + ByteArray value
  201 + ) =
  202 + db_bind(force(sub_string(name, 1, length(name) - 1), name), db_blob(value)).
  203 +
  204 + public define DbBind
  205 + bind_Int
  206 + (
  207 + String name,
  208 + Int value
  209 + ) =
  210 + db_bind(force(sub_string(name, 1, length(name) - 1), name), db_integer(value)).
  211 +
  212 + public define DbBind
  213 + bind_NULL
  214 + (
  215 + String name,
  216 + ) =
  217 + db_bind(force(sub_string(name, 1, length(name) - 1), name), null).
  218 +
  219 +
... ...
calexium_lib/database/migration.anubis
... ... @@ -11,14 +11,14 @@ read system/files.anubis
11 11 read system/logger.anubis
12 12 read system/string.anubis
13 13 read data_base/alter_table.anubis
14   -read data_base/sqlite.anubis
  14 +read data_base/db_tools.anubis
15 15  
16 16 read calexium_lib/database/db_utils.anubis
17 17 read calexium_lib/net_services_protocols/logger_service.anubis
18 18  
19 19 read mf_constants.anubis
20 20  
21   -read settings.anubis
  21 + read settings.anubis
22 22 read migration_common.anubis
23 23  
24 24 read tools/mf_loggers.anubis
... ... @@ -60,19 +60,10 @@ public define Int
60 60 },
61 61 compare(split(version1, '.'), split(version2, '.')).
62 62  
63   -define String
64   - get_version_settings
65   - (
66   - SQLite3DataBase db
67   - )=
68   - if is_table_exists(db, "settings") then
69   - select_settings(db, "MF_version", "0.0.0.0")
70   - else "0.0.0.0".
71   -
72 63 public define One
73 64 update_version_settings
74 65 (
75   - SQLite3DataBase db,
  66 + Database db,
76 67 String version
77 68 )=
78 69 update_settings(db, "MF_version", version).
... ... @@ -81,21 +72,21 @@ public define Maybe(One)
81 72 backup_database
82 73 (
83 74 Logger logger,
84   - String current_version,
  75 +// String current_version,
85 76 String db_path,
86 77 String main_db_name,
87 78 List(String) other_db_names,
88 79 )
89 80 =
90   - with should_copy = if file_exists(db_path + main_db_name) then
91   - if sqlite3_open(db_path + main_db_name) is
92   - {
93   - error(sql_error) then false, // maybe not created yet
94   - ok(SQLite3DataBase db) then
95   - with ver = get_version_settings(db),
96   - ver != current_version
97   - }
98   - else false,
  81 +// with should_copy = if file_exists(db_path + main_db_name) then
  82 +// if sqlite3_open(db_path + main_db_name) is
  83 +// {
  84 +// error(sql_error) then false, // maybe not created yet
  85 +// ok(Database db) then
  86 +// with ver = get_version_settings(db),
  87 +// ver != current_version
  88 +// }
  89 +// else false,
99 90  
100 91 if should_copy then
101 92 with dir_save = db_path + dir_save_database,
... ... @@ -138,9 +129,9 @@ public define Maybe(One)
138 129  
139 130 public type Migration:
140 131 migration(String version,
141   - (SQLite3DataBase, Logger) -> Maybe(One) migrate_to,
142   - (SQLite3DataBase, Logger) -> Maybe(One) create_tables,
143   - (SQLite3DataBase, Logger) -> Maybe(One) create_indexes,
  132 + (Database, Logger) -> Maybe(One) migrate_to,
  133 + (Database, Logger) -> Maybe(One) create_tables,
  134 + (Database, Logger) -> Maybe(One) create_indexes,
144 135 ).
145 136  
146 137  
... ... @@ -148,7 +139,7 @@ public type Migration:
148 139 define Maybe(One)
149 140 do_migration_loop
150 141 (
151   - SQLite3DataBase db,
  142 + Database db,
152 143 Logger logger,
153 144 String current_version,
154 145 String db_ver,
... ... @@ -191,10 +182,12 @@ define Maybe(One)
191 182 public define Maybe(One)
192 183 do_migration
193 184 (
194   - SQLite3DataBase db,
195   - Logger logger,
196   - String current_version,
197   - List(Migration) all_migrations,
  185 + Database db,
  186 + Logger logger,
  187 + String current_version,
  188 + (Database) |-> String get_db_version,
  189 + (Database, String) |-> One update_db_version,
  190 + List(Migration) all_migrations,
198 191 )=
199 192 if is_empty_database(db) then // new empty database
200 193 logInfo(logger, "No database found. Creating a new one...");
... ... @@ -208,7 +201,7 @@ public define Maybe(One)
208 201 public define Maybe(One)
209 202 do_create_indexes
210 203 (
211   - SQLite3DataBase db,
  204 + Database db,
212 205 Logger logger,
213 206 String current_version,
214 207 List(Migration) all_migrations,
... ...
calexium_lib/database/migration_common.anubis
... ... @@ -8,7 +8,7 @@
8 8  
9 9 read tools/basis.anubis
10 10 read system/logger.anubis
11   -read data_base/sqlite.anubis
  11 +read data_base/db_tools.anubis
12 12 read data_base/alter_table.anubis
13 13 read data_base/sqlite_foreign_key.anubis
14 14  
... ... @@ -27,7 +27,7 @@ public type DbTable:
27 27 public define Bool
28 28 is_table_exists
29 29 (
30   - SQLite3DataBase db,
  30 + Database db,
31 31 String dbName,
32 32 String table_name
33 33 )=
... ... @@ -46,7 +46,7 @@ public define Bool
46 46 public define Bool
47 47 is_table_exists
48 48 (
49   - SQLite3DataBase db,
  49 + Database db,
50 50 String table_name
51 51 )=
52 52 is_table_exists(db, "main", table_name).
... ... @@ -54,7 +54,7 @@ public define Bool
54 54 public define Bool
55 55 is_empty_database
56 56 (
57   - SQLite3DataBase db,
  57 + Database db,
58 58 String dbName,
59 59 )=
60 60 if sql_query_timeout(db, "SELECT * FROM " + dbName + ".sqlite_master", [], "is_empty_database("+dbName+")") is
... ... @@ -72,21 +72,21 @@ public define Bool
72 72 public define Bool
73 73 is_empty_database
74 74 (
75   - SQLite3DataBase db,
  75 + Database db,
76 76 )=
77 77 is_empty_database(db, "main").
78 78  
79 79 public define Maybe(One)
80 80 drop_all_triggers
81 81 (
82   - SQLite3DataBase db,
  82 + Database db,
83 83 Logger log
84 84 ) =
85   - if sqlite3_query(db, "SELECT name FROM sqlite_master WHERE type = 'trigger'", []) is
  85 + if db_query(db, "SELECT name FROM sqlite_master WHERE type = 'trigger'", []) is
86 86 {
87 87 error(err) then logError(log, db_error(err, "drop_all_triggers")); failure,
88 88 ok(_, cursor, _) then
89   - with drop_trigger = (String name) |-> if sqlite3_query(db, "DROP TRIGGER " + name, []) is
  89 + with drop_trigger = (String name) |-> if db_query(db, "DROP TRIGGER " + name, []) is
90 90 {
91 91 error(err2) then logError(log, db_error(err2, "Failed to drop trigger '" + name + "'")),
92 92 ok(_, cursor, _) then unique
... ... @@ -98,7 +98,7 @@ public define Maybe(One)
98 98 public define Maybe(One)
99 99 create_tables
100 100 (
101   - SQLite3DataBase db,
  101 + Database db,
102 102 Logger logger,
103 103 List(DbTable) tables,
104 104 String version_string
... ... @@ -134,7 +134,7 @@ public define Maybe(One)
134 134 public define Maybe(One)
135 135 create_indexes
136 136 (
137   - SQLite3DataBase db,
  137 + Database db,
138 138 Logger logger,
139 139 List(String) indexes,
140 140 String version_string
... ... @@ -155,7 +155,7 @@ public define Maybe(One)
155 155 public define One
156 156 make_foreign_key
157 157 (
158   - SQLite3DataBase db,
  158 + Database db,
159 159 String table_name,
160 160 String field_name,
161 161 String foreign_table_name,
... ... @@ -175,7 +175,7 @@ public define One
175 175 public define Maybe(One)
176 176 make_foreign_keys
177 177 (
178   - SQLite3DataBase db,
  178 + Database db,
179 179 Logger logger,
180 180 List((String, String, String, String, FK_Null, FK_Cascade)) relations,
181 181 String version_string
... ...
calexium_lib/database/settings.anubis
... ... @@ -6,7 +6,7 @@
6 6 */
7 7  
8 8 read tools/basis.anubis
9   -read data_base/sqlite.anubis
  9 +read data_base/db_tools.anubis
10 10  
11 11 read calexium_lib/database/db_utils.anubis
12 12  
... ... @@ -14,7 +14,7 @@ read calexium_lib/database/db_utils.anubis
14 14 public define Maybe(String)
15 15 select_settings
16 16 (
17   - SQLite3DataBase db,
  17 + Database db,
18 18 String var,
19 19 )=
20 20 if sql_query_timeout(db, "SELECT var_value FROM settings WHERE var_name=" + db_make_sql_string(var) + ";", "select_settings") is
... ... @@ -32,7 +32,7 @@ public define Maybe(String)
32 32 public define String
33 33 select_settings
34 34 (
35   - SQLite3DataBase db,
  35 + Database db,
36 36 String var,
37 37 String default
38 38 )=
... ... @@ -45,7 +45,7 @@ public define String
45 45 public define Int
46 46 select_settings
47 47 (
48   - SQLite3DataBase db,
  48 + Database db,
49 49 String var,
50 50 Int default
51 51 )=
... ... @@ -63,7 +63,7 @@ public define Int
63 63 public define Bool
64 64 select_settings
65 65 (
66   - SQLite3DataBase db,
  66 + Database db,
67 67 String var,
68 68 Bool default
69 69 )=
... ... @@ -81,7 +81,7 @@ public define Bool
81 81 public define One
82 82 update_settings
83 83 (
84   - SQLite3DataBase db,
  84 + Database db,
85 85 String var,
86 86 String value
87 87 )=
... ...