Skip to content

Commit

Permalink
create migration for upgrading to 0.16
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Dec 21, 2023
1 parent 36a6563 commit ec058b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
39 changes: 30 additions & 9 deletions core/rs/core/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -176,6 +172,31 @@ fn maybe_update_db_inner(
Ok(ResultCode::OK)
}

fn update_to_0_16_0(db: *mut sqlite3) -> Result<ResultCode, ResultCode> {
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.
*
Expand Down
15 changes: 3 additions & 12 deletions core/rs/core/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
pub const TBL_SITE_ID: &'static str = "crsql_site_id";
pub const TBL_SCHEMA: &'static str = "crsql_master";
// pub const CRSQLITE_VERSION_0_15_0: i32 = 15_00_00;
// pub const CRSQLITE_VERSION_0_13_0: i32 = 13_00_00;
// MM_mm_pp_xx
// so a 1.0.0 release is:
// 01_00_00_00 -> 1000000
// a 0.5 release is:
// 00_05_00_00 -> 50000
// a 0.5.1 is:
// 00_05_01_00
// and, if we ever need it, we can track individual builds of a patch release
// 00_05_01_01
pub const CRSQLITE_VERSION: i32 = 15_00_00;
pub const CRSQLITE_VERSION_0_15_0: i32 = 15_00_00;
pub const CRSQLITE_VERSION_0_16_0: i32 = 16_00_00;
pub const CRSQLITE_VERSION: i32 = CRSQLITE_VERSION_0_16_0;
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
Expand Down

0 comments on commit ec058b0

Please sign in to comment.