|
1 | 1 | extern crate alloc; |
2 | 2 |
|
3 | 3 | use alloc::format; |
4 | | -use alloc::string::String; |
| 4 | +use alloc::string::{String, ToString}; |
| 5 | +use alloc::vec::Vec; |
5 | 6 | use core::ffi::c_int; |
6 | 7 | use core::slice; |
7 | 8 |
|
8 | 9 | use sqlite::{ResultCode, Value}; |
9 | 10 | use sqlite_nostd as sqlite; |
10 | 11 | use sqlite_nostd::{Connection, Context}; |
11 | 12 |
|
12 | | -use crate::{create_auto_tx_function, create_sqlite_text_fn}; |
13 | 13 | use crate::error::{PSResult, SQLiteError}; |
14 | 14 | use crate::util::quote_identifier; |
| 15 | +use crate::{create_auto_tx_function, create_sqlite_text_fn}; |
15 | 16 |
|
16 | 17 | fn powersync_drop_view_impl( |
17 | 18 | ctx: *mut sqlite::context, |
@@ -62,7 +63,9 @@ fn powersync_internal_table_name_impl( |
62 | 63 | let local_db = ctx.db_handle(); |
63 | 64 |
|
64 | 65 | // language=SQLite |
65 | | - let stmt1 = local_db.prepare_v2("SELECT json_extract(?1, '$.name') as name, ifnull(json_extract(?1, '$.local_only'), 0)")?; |
| 66 | + let stmt1 = local_db.prepare_v2( |
| 67 | + "SELECT json_extract(?1, '$.name') as name, ifnull(json_extract(?1, '$.local_only'), 0)", |
| 68 | + )?; |
66 | 69 | stmt1.bind_text(1, schema, sqlite::Destructor::STATIC)?; |
67 | 70 |
|
68 | 71 | let step_result = stmt1.step()?; |
@@ -115,26 +118,80 @@ fn powersync_init_impl( |
115 | 118 | let local_db = ctx.db_handle(); |
116 | 119 |
|
117 | 120 | // language=SQLite |
118 | | - local_db.exec_safe("\ |
119 | | -CREATE TABLE IF NOT EXISTS ps_migration(id INTEGER PRIMARY KEY, down_migrations TEXT)")?; |
| 121 | + local_db.exec_safe( |
| 122 | + "\ |
| 123 | +CREATE TABLE IF NOT EXISTS ps_migration(id INTEGER PRIMARY KEY, down_migrations TEXT)", |
| 124 | + )?; |
120 | 125 |
|
121 | 126 | // language=SQLite |
122 | | - let stmt = local_db.prepare_v2("SELECT ifnull(max(id), 0) as version FROM ps_migration")?; |
123 | | - let rc = stmt.step()?; |
| 127 | + let current_version_stmt = |
| 128 | + local_db.prepare_v2("SELECT ifnull(max(id), 0) as version FROM ps_migration")?; |
| 129 | + let rc = current_version_stmt.step()?; |
124 | 130 | if rc != ResultCode::ROW { |
125 | 131 | return Err(SQLiteError::from(ResultCode::ABORT)); |
126 | 132 | } |
127 | 133 |
|
128 | | - let version = stmt.column_int(0)?; |
129 | | - |
130 | | - if version > 2 { |
131 | | - // We persist down migrations, but don't support running them yet |
132 | | - return Err(SQLiteError(ResultCode::MISUSE, Some(String::from("Downgrade not supported")))); |
| 134 | + const CODE_VERSION: i32 = 2; |
| 135 | + |
| 136 | + let mut current_version = current_version_stmt.column_int(0)?; |
| 137 | + |
| 138 | + while current_version > CODE_VERSION { |
| 139 | + // Run down migrations. |
| 140 | + // This is rare, we don't worry about optimizing this. |
| 141 | + |
| 142 | + current_version_stmt.reset()?; |
| 143 | + |
| 144 | + let down_migrations_stmt = local_db.prepare_v2("select e.value ->> 'sql' as sql from (select id, down_migrations from ps_migration where id > ?1 order by id desc limit 1) m, json_each(m.down_migrations) e")?; |
| 145 | + down_migrations_stmt.bind_int(1, CODE_VERSION); |
| 146 | + |
| 147 | + let mut down_sql: Vec<String> = alloc::vec![]; |
| 148 | + |
| 149 | + while down_migrations_stmt.step()? == ResultCode::ROW { |
| 150 | + let sql = down_migrations_stmt.column_text(0)?; |
| 151 | + down_sql.push(sql.to_string()); |
| 152 | + } |
| 153 | + |
| 154 | + for sql in down_sql { |
| 155 | + let rs = local_db.exec_safe(&sql); |
| 156 | + if let Err(code) = rs { |
| 157 | + return Err(SQLiteError( |
| 158 | + code, |
| 159 | + Some(format!( |
| 160 | + "Down migration failed for {:} {:}", |
| 161 | + current_version, sql |
| 162 | + )), |
| 163 | + )); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Refresh the version |
| 168 | + current_version_stmt.reset()?; |
| 169 | + let rc = current_version_stmt.step()?; |
| 170 | + if rc != ResultCode::ROW { |
| 171 | + return Err(SQLiteError( |
| 172 | + rc, |
| 173 | + Some("Down migration failed - could not get version".to_string()), |
| 174 | + )); |
| 175 | + } |
| 176 | + let new_version = current_version_stmt.column_int(0)?; |
| 177 | + if new_version >= current_version { |
| 178 | + // Database down from version $currentVersion to $version failed - version not updated after dow migration |
| 179 | + return Err(SQLiteError( |
| 180 | + ResultCode::ABORT, |
| 181 | + Some(format!( |
| 182 | + "Down migration failed - version not updated from {:}", |
| 183 | + current_version |
| 184 | + )), |
| 185 | + )); |
| 186 | + } |
| 187 | + current_version = new_version; |
133 | 188 | } |
134 | 189 |
|
135 | | - if version < 1 { |
| 190 | + if current_version < 1 { |
136 | 191 | // language=SQLite |
137 | | - local_db.exec_safe(" |
| 192 | + local_db |
| 193 | + .exec_safe( |
| 194 | + " |
138 | 195 | CREATE TABLE ps_oplog( |
139 | 196 | bucket TEXT NOT NULL, |
140 | 197 | op_id INTEGER NOT NULL, |
@@ -164,31 +221,28 @@ CREATE TABLE ps_untyped(type TEXT NOT NULL, id TEXT NOT NULL, data TEXT, PRIMARY |
164 | 221 | CREATE TABLE ps_crud (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT); |
165 | 222 |
|
166 | 223 | INSERT INTO ps_migration(id, down_migrations) VALUES(1, NULL); |
167 | | -").into_db_result(local_db)?; |
| 224 | +", |
| 225 | + ) |
| 226 | + .into_db_result(local_db)?; |
168 | 227 | } |
169 | 228 |
|
170 | | - if version < 2 { |
| 229 | + if current_version < 2 { |
171 | 230 | // language=SQLite |
172 | 231 | local_db.exec_safe("\ |
173 | 232 | CREATE TABLE ps_tx(id INTEGER PRIMARY KEY NOT NULL, current_tx INTEGER, next_tx INTEGER); |
174 | 233 | INSERT INTO ps_tx(id, current_tx, next_tx) VALUES(1, NULL, 1); |
175 | 234 |
|
176 | 235 | ALTER TABLE ps_crud ADD COLUMN tx_id INTEGER; |
177 | 236 |
|
178 | | -INSERT INTO ps_migration(id, down_migrations) VALUES(2, json_array(json_object('sql', 'DELETE FROM ps_migrations WHERE id >= 2', 'params', json_array()), json_object('sql', 'DROP TABLE ps_tx', 'params', json_array()), json_object('sql', 'ALTER TABLE ps_crud DROP COLUMN tx_id', 'params', json_array()))); |
| 237 | +INSERT INTO ps_migration(id, down_migrations) VALUES(2, json_array(json_object('sql', 'DELETE FROM ps_migration WHERE id >= 2', 'params', json_array()), json_object('sql', 'DROP TABLE ps_tx', 'params', json_array()), json_object('sql', 'ALTER TABLE ps_crud DROP COLUMN tx_id', 'params', json_array()))); |
179 | 238 | ").into_db_result(local_db)?; |
180 | 239 | } |
181 | 240 |
|
182 | 241 | Ok(String::from("")) |
183 | 242 | } |
184 | 243 |
|
185 | | - |
186 | 244 | create_auto_tx_function!(powersync_init_tx, powersync_init_impl); |
187 | | -create_sqlite_text_fn!( |
188 | | - powersync_init, |
189 | | - powersync_init_tx, |
190 | | - "powersync_init" |
191 | | -); |
| 245 | +create_sqlite_text_fn!(powersync_init, powersync_init_tx, "powersync_init"); |
192 | 246 |
|
193 | 247 | pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> { |
194 | 248 | // This entire module is just making it easier to edit sqlite_master using queries. |
@@ -259,7 +313,6 @@ pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> { |
259 | 313 | None, |
260 | 314 | )?; |
261 | 315 |
|
262 | | - |
263 | 316 | db.create_function_v2( |
264 | 317 | "powersync_internal_table_name", |
265 | 318 | 1, |
@@ -324,14 +377,16 @@ BEGIN |
324 | 377 | END;")?; |
325 | 378 |
|
326 | 379 | // language=SQLite |
327 | | - db.exec_safe("\ |
| 380 | + db.exec_safe( |
| 381 | + "\ |
328 | 382 | CREATE TEMP VIEW powersync_tables(name, internal_name, local_only) |
329 | 383 | AS SELECT |
330 | 384 | powersync_external_table_name(name) as name, |
331 | 385 | name as internal_name, |
332 | 386 | name GLOB 'ps_data_local__*' as local_only |
333 | 387 | FROM sqlite_master |
334 | | - WHERE type = 'table' AND name GLOB 'ps_data_*';")?; |
| 388 | + WHERE type = 'table' AND name GLOB 'ps_data_*';", |
| 389 | + )?; |
335 | 390 |
|
336 | 391 | Ok(()) |
337 | 392 | } |
0 commit comments