diff --git a/core/rs/core/src/lib.rs b/core/rs/core/src/lib.rs index 34a163819..0fa0df24b 100644 --- a/core/rs/core/src/lib.rs +++ b/core/rs/core/src/lib.rs @@ -6,7 +6,7 @@ mod alter; mod automigrate; mod backfill; -mod bootstrap; +pub mod bootstrap; pub mod c; mod changes_vtab; mod changes_vtab_read; diff --git a/core/rs/core/src/tableinfo.rs b/core/rs/core/src/tableinfo.rs index 9cb4b97ad..57e5108a5 100644 --- a/core/rs/core/src/tableinfo.rs +++ b/core/rs/core/src/tableinfo.rs @@ -11,7 +11,6 @@ use alloc::vec::Vec; use core::ffi::c_char; use core::ffi::c_int; use core::ffi::c_void; -use core::mem; use core::mem::forget; use num_traits::ToPrimitive; use sqlite_nostd as sqlite; diff --git a/core/rs/core/src/util.rs b/core/rs/core/src/util.rs index 314f7f5c8..195bb9692 100644 --- a/core/rs/core/src/util.rs +++ b/core/rs/core/src/util.rs @@ -156,5 +156,23 @@ mod tests { use super::*; #[test] - fn test_slab_rowid() {} + fn test_slab_rowid() { + let foo_slab = slab_rowid(0, 1); + let bar_slab = slab_rowid(1, 2); + let baz_slab = slab_rowid(2, 3); + + assert_eq!(foo_slab, 1); + assert_eq!(bar_slab, 2 + crate::consts::ROWID_SLAB_SIZE); + assert_eq!(baz_slab, 3 + crate::consts::ROWID_SLAB_SIZE * 2); + assert_eq!(slab_rowid(0, crate::consts::ROWID_SLAB_SIZE), 0); + assert_eq!(slab_rowid(0, crate::consts::ROWID_SLAB_SIZE + 1), 1); + + let foo_slab = slab_rowid(0, crate::consts::ROWID_SLAB_SIZE + 1); + let bar_slab = slab_rowid(1, crate::consts::ROWID_SLAB_SIZE + 2); + let baz_slab = slab_rowid(2, crate::consts::ROWID_SLAB_SIZE * 2 + 3); + + assert_eq!(foo_slab, 1); + assert_eq!(bar_slab, 2 + crate::consts::ROWID_SLAB_SIZE); + assert_eq!(baz_slab, 3 + crate::consts::ROWID_SLAB_SIZE * 2); + } } diff --git a/core/rs/integration-check/tests/tableinfo.rs b/core/rs/integration-check/tests/tableinfo.rs index a8d32ee95..70329211b 100644 --- a/core/rs/integration-check/tests/tableinfo.rs +++ b/core/rs/integration-check/tests/tableinfo.rs @@ -8,7 +8,7 @@ use sqlite::Connection; use sqlite_nostd as sqlite; // janx hax to do an `afterAll` cleanup -integration_utils::counter_setup!(1); +integration_utils::counter_setup!(4); fn make_err_ptr() -> *mut *mut c_char { let mut inner_ptr: *mut c_char = std::ptr::null_mut(); @@ -21,7 +21,6 @@ fn make_site() -> *mut c_char { inner_ptr } -// https://github.com/vlcn-io/cr-sqlite/pull/353/files#diff-6097dc2328b422bd90f6f46c9cd066c15238a757d3a7a92fb6d1964468657f7cL1 #[test] fn test_ensure_table_infos_are_up_to_date() { let db = integration_utils::opendb().expect("Opened DB"); @@ -201,6 +200,13 @@ fn test_is_table_compatible() { assert_eq!(is_compatible, true); // pks + other unique indices + c.exec_safe("CREATE TABLE booz (a PRIMARY KEY, b);") + .expect("made booz"); + c.exec_safe("CREATE UNIQUE INDEX booz_b ON booz (b);") + .expect("made index"); + let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "booz", make_err_ptr()) + .expect("checked if booz is compatible"); + assert_eq!(is_compatible, false); // not null and no dflt c.exec_safe("CREATE TABLE buzz (a PRIMARY KEY, b NOT NULL);") @@ -269,59 +275,39 @@ fn test_is_table_compatible() { decrement_counter(); } -// https://github.com/vlcn-io/cr-sqlite/pull/353/files#diff-6d6b961ba2bd1905d0f357b72927a8707992abc533322fe95f7d134421919ef1R83 #[test] fn test_create_clock_table_from_table_info() { - // static void testCreateClockTable() { - // printf("CreateClockTable\n"); - - // sqlite3 *db; - // int rc; - // crsql_TableInfo *tc1; - // crsql_TableInfo *tc2; - // crsql_TableInfo *tc3; - // crsql_TableInfo *tc4; - // char *err = 0; - - // rc = sqlite3_open(":memory:", &db); - // sqlite3_exec(db, "CREATE TABLE foo (a, b, primary key (a, b))", 0, 0, 0); - // sqlite3_exec(db, "CREATE TABLE bar (a primary key)", 0, 0, 0); - // sqlite3_exec(db, "CREATE TABLE baz (a primary key, b)", 0, 0, 0); - // sqlite3_exec(db, "CREATE TABLE boo (a primary key, b, c)", 0, 0, 0); - - // rc = crsql_pull_table_info(db, "foo", &tc1, &err); - // CHECK_OK - // rc = crsql_pull_table_info(db, "bar", &tc2, &err); - // CHECK_OK - // rc = crsql_pull_table_info(db, "baz", &tc3, &err); - // CHECK_OK - // rc = crsql_pull_table_info(db, "boo", &tc4, &err); - // CHECK_OK - - // rc = crsql_create_clock_table(db, tc1, &err); - // CHECK_OK - // rc = crsql_create_clock_table(db, tc2, &err); - // CHECK_OK - // rc = crsql_create_clock_table(db, tc3, &err); - // CHECK_OK - // rc = crsql_create_clock_table(db, tc4, &err); - // CHECK_OK - - // crsql_free_table_info(tc1); - // crsql_free_table_info(tc2); - // crsql_free_table_info(tc3); - // crsql_free_table_info(tc4); - - // // TODO: check that the tables have the expected schema - - // printf("\t\e[0;32mSuccess\e[0m\n"); - // crsql_close(db); - // return; - - // fail: - // printf("err: %s %d\n", err, rc); - // sqlite3_free(err); - // crsql_close(db); - // assert(rc == SQLITE_OK); - // } + let db = integration_utils::opendb().expect("Opened DB"); + let c = &db.db; + let raw_db = db.db.db; + + c.exec_safe("CREATE TABLE foo (a, b, primary key (a, b));") + .expect("made foo"); + c.exec_safe("CREATE TABLE bar (a primary key);") + .expect("made bar"); + c.exec_safe("CREATE TABLE baz (a primary key, b);") + .expect("made baz"); + c.exec_safe("CREATE TABLE boo (a primary key, b, c);") + .expect("made boo"); + + let foo_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "foo", make_err_ptr()) + .expect("pulled table info for foo"); + let bar_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "bar", make_err_ptr()) + .expect("pulled table info for bar"); + let baz_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "baz", make_err_ptr()) + .expect("pulled table info for baz"); + let boo_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "boo", make_err_ptr()) + .expect("pulled table info for boo"); + + crsql_core::bootstrap::create_clock_table(raw_db, &foo_tbl_info, make_err_ptr()) + .expect("created clock table for foo"); + crsql_core::bootstrap::create_clock_table(raw_db, &bar_tbl_info, make_err_ptr()) + .expect("created clock table for bar"); + crsql_core::bootstrap::create_clock_table(raw_db, &baz_tbl_info, make_err_ptr()) + .expect("created clock table for baz"); + crsql_core::bootstrap::create_clock_table(raw_db, &boo_tbl_info, make_err_ptr()) + .expect("created clock table for boo"); + + // todo: Check that clock tables have expected schema(s) + decrement_counter(); }