Skip to content

Move to site version for tracking changes #2

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions core/rs/bundle_static/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions core/rs/core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/rs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sqlite_nostd = { path="../sqlite-rs-embedded/sqlite_nostd" }
bytes = { version = "1.5", default-features = false }
num-traits = { version = "0.2.17", default-features = false }
num-derive = "0.4.1"
libc-print = "*"

[dev-dependencies]

Expand Down
8 changes: 4 additions & 4 deletions core/rs/core/src/automigrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ fn maybe_recreate_index(
let fetch_idx_cols_mem = mem_db.prepare_v2(IDX_COLS_SQL)?;
let fetch_idx_cols_local = local_db.prepare_v2(IDX_COLS_SQL)?;

let mem_result = fetch_idx_cols_mem.step()?;
let local_result = fetch_idx_cols_local.step()?;
let mut mem_result = fetch_idx_cols_mem.step()?;
let mut local_result = fetch_idx_cols_local.step()?;
while mem_result == ResultCode::ROW && local_result == ResultCode::ROW {
if fetch_idx_cols_mem.column_text(0) != fetch_idx_cols_local.column_text(0) {
// We cannot alter a table against which we have open statements
Expand All @@ -433,8 +433,8 @@ fn maybe_recreate_index(
drop(fetch_idx_cols_mem);
return recreate_index(local_db, idx);
}
fetch_idx_cols_mem.step()?;
fetch_idx_cols_local.step()?;
mem_result = fetch_idx_cols_mem.step()?;
local_result = fetch_idx_cols_local.step()?;
}

if mem_result != local_result {
Expand Down
65 changes: 51 additions & 14 deletions core/rs/core/src/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::string::ToString;
use core::ffi::{c_char, c_int};

use crate::{consts, tableinfo::TableInfo};
Expand Down Expand Up @@ -56,6 +57,14 @@ pub extern "C" fn crsql_init_peer_tracking_table(db: *mut sqlite3) -> c_int {
}
}

// #[no_mangle]
pub extern "C" fn crsql_init_db_versions_table(db: *mut sqlite3) -> c_int {
match db.exec_safe("CREATE TABLE IF NOT EXISTS crsql_db_versions (\"site_id\" BLOB NOT NULL PRIMARY KEY, \"db_version\" INTEGER NOT NULL) STRICT;") {
Ok(_) => ResultCode::OK as c_int,
Err(code) => code as c_int
}
}

fn has_table(db: *mut sqlite3, table_name: &str) -> Result<bool, ResultCode> {
let stmt =
db.prepare_v2("SELECT 1 FROM sqlite_master WHERE type = 'table' AND tbl_name = ?")?;
Expand Down Expand Up @@ -157,12 +166,10 @@ fn maybe_update_db_inner(
}
}

// 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_17_0 && !is_blank_slate {
// if let Err(e) = update_to_0_17_0(db) {
// return Err(e);
// }
// }

// write the db version if we migrated to a new one or we are a blank slate db
Expand Down Expand Up @@ -206,30 +213,60 @@ pub fn create_clock_table(
col_name TEXT NOT NULL,
col_version INTEGER NOT NULL,
db_version INTEGER NOT NULL,
site_id INTEGER NOT NULL DEFAULT 0,
site_id INTEGER NOT NULL DEFAULT 0,
seq INTEGER NOT NULL,
PRIMARY KEY (key, col_name)
) WITHOUT ROWID, STRICT",
table_name = crate::util::escape_ident(table_name),
))?;

// TODO: (we probably need to recreate index when upgrading)
db.exec_safe(
&format!(
"CREATE INDEX IF NOT EXISTS \"{table_name}__crsql_clock_dbv_idx\" ON \"{table_name}__crsql_clock\" (\"db_version\")",
&format!(
"CREATE INDEX IF NOT EXISTS \"{table_name}__crsql_clock_dbv_idx\" ON \"{table_name}__crsql_clock\" (\"site_id\", \"db_version\")",
table_name = crate::util::escape_ident(table_name),
))?;
))?;
// db.exec_safe(
// &format!(
// "CREATE INDEX IF NOT EXISTS \"{table_name}__crsql_clock_sitev_idx\" ON \"{table_name}__crsql_clock\" (\"site_version\")",
// table_name = crate::util::escape_ident(table_name),
// ))?;
db.exec_safe(
&format!(
&format!(
"CREATE TABLE IF NOT EXISTS \"{table_name}__crsql_pks\" (__crsql_key INTEGER PRIMARY KEY, {pk_list})",
table_name = table_name,
pk_list = pk_list,
)
)
)?;
db.exec_safe(
&format!(
&format!(
"CREATE UNIQUE INDEX IF NOT EXISTS \"{table_name}__crsql_pks_pks\" ON \"{table_name}__crsql_pks\" ({pk_list})",
table_name = table_name,
pk_list = pk_list
)
)
)
}

fn update_to_0_17_0(db: *mut sqlite3) -> Result<(), ResultCode> {
let stmt = db.prepare_v2(
"SELECT tbl_name FROM sqlite_master WHERE type = 'table' AND tbl_name LIKE '%__crsql_clock'",
)?;

let mut names = alloc::vec::Vec::new();

while stmt.step()? == ResultCode::ROW {
names.push(stmt.column_text(0)?.to_string());
}

for name in names {
db.exec_safe(&format!(
"ALTER TABLE {name} ADD COLUMN site_version INTEGER NOT NULL DEFAULT 0"
))?;
db.exec_safe(&format!(
"CREATE INDEX IF NOT EXISTS \"{table_name}_sitev_idx\" ON \"{table_name}\" (\"site_version\")",
table_name = crate::util::escape_ident(&name),
))?;
}

Ok(())
}
Loading
Loading