Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration for upgrading to 0.16 #418

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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