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 22, 2023
1 parent 53ae92e commit 00d1866
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 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
2 changes: 2 additions & 0 deletions core/rs/core/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 00d1866

Please sign in to comment.