From 00d18668500d41879cbfd64869b442908ea41041 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Thu, 21 Dec 2023 16:33:14 -0500 Subject: [PATCH] create migration for upgrading to 0.16 --- core/rs/core/src/bootstrap.rs | 39 +++++++++++++++++++++++++++-------- core/rs/core/src/consts.rs | 2 ++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/core/rs/core/src/bootstrap.rs b/core/rs/core/src/bootstrap.rs index 7b314cb12..fe1d4d0c8 100644 --- a/core/rs/core/src/bootstrap.rs +++ b/core/rs/core/src/bootstrap.rs @@ -147,23 +147,19 @@ fn maybe_update_db_inner( } } - if recorded_version < consts::CRSQLITE_VERSION && !is_blank_slate { + if recorded_version < consts::CRSQLITE_VERSION_0_15_0 && !is_blank_slate { // todo: return an error message to the user that their version is // not supported - let cstring = CString::new(format!("Opening a db created with cr-sqlite version {} is not supported. Upcoming release 0.15.0 is a breaking change.", recorded_version))?; + let cstring = CString::new(format!("Opening a db created with cr-sqlite version {recorded_version} is not supported. Upcoming release 0.15.0 is a breaking change."))?; unsafe { (*err_msg) = cstring.into_raw(); return Err(ResultCode::ERROR); } } - // if recorded_version < consts::CRSQLITE_VERSION_0_13_0 { - // update_to_0_13_0(db)?; - // } - - // if recorded_version < consts::CRSQLITE_VERSION_0_15_0 { - // update_to_0_15_0(db)?; - // } + if recorded_version < consts::CRSQLITE_VERSION_0_16_0 && !is_blank_slate { + update_to_0_16_0(db)?; + } // write the db version if we migrated to a new one or we are a blank slate db if recorded_version < consts::CRSQLITE_VERSION || is_blank_slate { @@ -176,6 +172,31 @@ fn maybe_update_db_inner( Ok(ResultCode::OK) } +fn update_to_0_16_0(db: *mut sqlite3) -> Result { + let stmt = db.prepare_v2( + "SELECT tbl_name FROM sqlite_master WHERE type='table' AND tbl_name LIKE '%__crsql_clock'", + )?; + + loop { + match stmt.step()? { + ResultCode::ROW => { + db.exec_safe(&format!( + "UPDATE {tbl_name} SET site_id = 0 WHERE site_id IS NULL", + tbl_name = stmt.column_text(0)?, + ))?; + } + ResultCode::DONE => { + break; + } + rc => { + return Err(rc); + } + } + } + + Ok(ResultCode::OK) +} + /** * The clock table holds the versions for each column of a given row. * diff --git a/core/rs/core/src/consts.rs b/core/rs/core/src/consts.rs index 2c686e2aa..a935c551a 100644 --- a/core/rs/core/src/consts.rs +++ b/core/rs/core/src/consts.rs @@ -13,6 +13,8 @@ pub const TBL_SCHEMA: &'static str = "crsql_master"; // 00_05_01_01 pub const CRSQLITE_VERSION: i32 = 16_01_00; pub const CRSQLITE_VERSION_STR: &'static str = "0.16.1"; +pub const CRSQLITE_VERSION_0_15_0: i32 = 15_00_00; +pub const CRSQLITE_VERSION_0_16_0: i32 = 16_00_00; pub const SITE_ID_LEN: i32 = 16; pub const ROWID_SLAB_SIZE: i64 = 10000000000000; // db version is a signed 64bit int since sqlite doesn't support saving and