Skip to content

Commit

Permalink
clean up exports to not export internals unless we're running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Sep 20, 2023
1 parent 79bd14a commit 9c4eaef
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 51 deletions.
2 changes: 1 addition & 1 deletion core/rs/bundle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ panic = "abort"
panic = "abort"

[features]
test = []
test = ["crsql_core/test"]
loadable_extension = [
"sqlite_nostd/loadable_extension",
"crsql_fractindex_core/loadable_extension",
Expand Down
9 changes: 5 additions & 4 deletions core/rs/bundle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use core::alloc::GlobalAlloc;
use core::alloc::Layout;
use core::ffi::{c_char, c_int};
use core::panic::PanicInfo;
pub use crsql_core;
use crsql_core;
use crsql_core::sqlite3_crsqlcore_init;
#[cfg(feature = "test")]
pub use crsql_core::test_exports;
use crsql_fractindex_core::sqlite3_crsqlfractionalindex_init;
pub use sqlite_nostd as sqlite;
use sqlite_nostd as sqlite;
use sqlite_nostd::SQLite3Allocator;

// This must be our allocator so we can transfer ownership of memory to SQLite and have SQLite free that memory for us.
Expand All @@ -21,13 +23,12 @@ static ALLOCATOR: SQLite3Allocator = SQLite3Allocator {};

// This must be our panic handler for WASM builds. For simplicity, we make it our panic handler for
// all builds. Abort is also more portable than unwind, enabling us to go to more embedded use cases.
#[cfg(not(feature = "test"))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
core::intrinsics::abort()
}

#[cfg(all(not(target_family = "wasm"), not(feature = "test")))]
#[cfg(not(target_family = "wasm"))]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

Expand Down
1 change: 1 addition & 0 deletions core/rs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ panic = "abort"
panic = "abort"

[features]
test = []
loadable_extension = ["sqlite_nostd/loadable_extension"]
static = ["sqlite_nostd/static"]
omit_load_extension = ["sqlite_nostd/omit_load_extension"]
2 changes: 1 addition & 1 deletion core/rs/core/src/changes_vtab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::c::{
};
use crate::changes_vtab_read::changes_union_query;
use crate::pack_columns::bind_package_to_stmt;
use crate::unpack_columns;
use crate::pack_columns::unpack_columns;

fn changes_crsr_finalize(crsr: *mut crsql_Changes_cursor) -> c_int {
// Assign pointers to null after freeing
Expand Down
2 changes: 1 addition & 1 deletion core/rs/core/src/changes_vtab_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::c::crsql_ExtData;
use crate::c::{crsql_Changes_vtab, CrsqlChangesColumn};
use crate::compare_values::crsql_compare_sqlite_values;
use crate::pack_columns::bind_package_to_stmt;
use crate::pack_columns::{unpack_columns, ColumnValue};
use crate::stmt_cache::reset_cached_stmt;
use crate::tableinfo::{crsql_ensure_table_infos_are_up_to_date, TableInfo};
use crate::util::slab_rowid;
use crate::{unpack_columns, ColumnValue};

/**
* did_cid_win does not take into account the causal length.
Expand Down
27 changes: 19 additions & 8 deletions core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
mod alter;
mod automigrate;
mod backfill;
#[cfg(feature = "test")]
pub mod bootstrap;
#[cfg(not(feature = "test"))]
mod bootstrap;
#[cfg(feature = "test")]
pub mod c;
#[cfg(not(feature = "test"))]
mod c;
mod changes_vtab;
mod changes_vtab_read;
mod changes_vtab_write;
Expand All @@ -17,29 +23,34 @@ mod create_cl_set_vtab;
mod create_crr;
mod ext_data;
mod is_crr;
#[cfg(feature = "test")]
pub mod pack_columns;
#[cfg(not(feature = "test"))]
mod pack_columns;
mod stmt_cache;
#[cfg(feature = "test")]
pub mod tableinfo;
#[cfg(not(feature = "test"))]
mod tableinfo;
mod teardown;
#[cfg(feature = "test")]
pub mod test_exports;
mod triggers;
mod unpack_columns_vtab;
mod util;

use core::{ffi::c_char, slice};
extern crate alloc;
pub use automigrate::*;
pub use backfill::*;
use automigrate::*;
use backfill::*;
use core::ffi::{c_int, CStr};
use create_crr::create_crr;
pub use is_crr::*;
use pack_columns::crsql_pack_columns;
pub use pack_columns::unpack_columns;
pub use pack_columns::ColumnValue;
use is_crr::*;
use sqlite::ResultCode;
use sqlite_nostd as sqlite;
use sqlite_nostd::{Connection, Context, Value};
use tableinfo::is_table_compatible;
pub use teardown::*;
use teardown::*;

pub extern "C" fn crsql_as_table(
ctx: *mut sqlite::context,
Expand Down Expand Up @@ -103,7 +114,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(
-1,
sqlite::UTF8,
None,
Some(crsql_pack_columns),
Some(pack_columns::crsql_pack_columns),
None,
None,
None,
Expand Down
2 changes: 1 addition & 1 deletion core/rs/core/src/pack_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn unpack_columns(data: &[u8]) -> Result<Vec<ColumnValue>, ResultCode> {

pub fn bind_package_to_stmt(
stmt: *mut sqlite::stmt,
values: &Vec<crate::ColumnValue>,
values: &Vec<ColumnValue>,
offset: usize,
) -> Result<ResultCode, ResultCode> {
for (i, val) in values.iter().enumerate() {
Expand Down
4 changes: 4 additions & 0 deletions core/rs/core/src/test_exports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub use crate::bootstrap;
pub use crate::c;
pub use crate::pack_columns;
pub use crate::tableinfo;
2 changes: 1 addition & 1 deletion core/rs/core/src/unpack_columns_vtab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sqlite::{Connection, Context, Value};
use sqlite_nostd as sqlite;
use sqlite_nostd::ResultCode;

use crate::{unpack_columns, ColumnValue};
use crate::pack_columns::{unpack_columns, ColumnValue};

#[derive(Debug)]
enum Columns {
Expand Down
2 changes: 1 addition & 1 deletion core/rs/integration_check/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["staticlib"]

[dependencies]
sqlite_nostd = { path="../sqlite-rs-embedded/sqlite_nostd", features=["static", "omit_load_extension"] }
crsql_bundle = { path="../bundle", features=["static", "omit_load_extension"] }
crsql_bundle = { path="../bundle", features=["static", "omit_load_extension", "test"] }
cargo-valgrind = "2.1.0"
libc-print = "0.1.21"

Expand Down
4 changes: 2 additions & 2 deletions core/rs/integration_check/src/t/pack_columns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crsql_bundle::crsql_core::unpack_columns;
use crsql_bundle::crsql_core::ColumnValue;
use crsql_bundle::test_exports::pack_columns::unpack_columns;
use crsql_bundle::test_exports::pack_columns::ColumnValue;
use sqlite::{Connection, ResultCode};
use sqlite_nostd as sqlite;

Expand Down
63 changes: 32 additions & 31 deletions core/rs/integration_check/src/t/tableinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use alloc::ffi::CString;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::{ffi::c_char, mem};
use crsql_bundle::crsql_core::{self, tableinfo::TableInfo};
use crsql_bundle::test_exports;
use crsql_bundle::test_exports::tableinfo::TableInfo;
use sqlite::Connection;
use sqlite_nostd as sqlite;

Expand Down Expand Up @@ -41,8 +42,8 @@ fn test_ensure_table_infos_are_up_to_date() {
)
.expect("made foo clock");

let ext_data = unsafe { crsql_core::c::crsql_newExtData(raw_db, make_site()) };
crsql_core::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);
let ext_data = unsafe { test_exports::c::crsql_newExtData(raw_db, make_site()) };
test_exports::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);

let mut table_infos = unsafe {
mem::ManuallyDrop::new(Box::from_raw((*ext_data).tableInfos as *mut Vec<TableInfo>))
Expand All @@ -54,7 +55,7 @@ fn test_ensure_table_infos_are_up_to_date() {
// we're going to change table infos so we can check that it does not get filled again since no schema changes happened
table_infos[0].tbl_name = "bar".to_string();

crsql_core::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);
test_exports::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);

assert_eq!(table_infos.len(), 1);
assert_eq!(table_infos[0].tbl_name, "bar");
Expand All @@ -73,7 +74,7 @@ fn test_ensure_table_infos_are_up_to_date() {
)
.expect("made boo clock");

crsql_core::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);
test_exports::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);

assert_eq!(table_infos.len(), 2);
assert_eq!(table_infos[0].tbl_name, "foo");
Expand All @@ -86,12 +87,12 @@ fn test_ensure_table_infos_are_up_to_date() {
c.exec_safe("DROP TABLE foo__crsql_clock")
.expect("dropped boo");

crsql_core::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);
test_exports::tableinfo::crsql_ensure_table_infos_are_up_to_date(raw_db, ext_data, err);

assert_eq!(table_infos.len(), 0);

unsafe {
crsql_core::c::crsql_freeExtData(ext_data);
test_exports::c::crsql_freeExtData(ext_data);
};
}

Expand All @@ -110,7 +111,7 @@ fn test_pull_table_info() {
)
.expect("made foo");

let tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "foo", err)
let tbl_info = test_exports::tableinfo::pull_table_info(raw_db, "foo", err)
.expect("pulled table info for foo");
assert_eq!(tbl_info.pks.len(), 1);
assert_eq!(tbl_info.pks[0].name, "a");
Expand All @@ -128,7 +129,7 @@ fn test_pull_table_info() {

c.exec_safe("CREATE TABLE boo (a INTEGER, b TEXT NOT NULL, c NUMBER NOT NULL, d FLOAT NOT NULL, e NOT NULL, PRIMARY KEY(b, c, d, e));")
.expect("made boo");
let tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "boo", err)
let tbl_info = test_exports::tableinfo::pull_table_info(raw_db, "boo", err)
.expect("pulled table info for boo");
assert_eq!(tbl_info.pks.len(), 4);
assert_eq!(tbl_info.pks[0].name, "b");
Expand Down Expand Up @@ -159,28 +160,28 @@ fn test_is_table_compatible() {

// no pks
c.exec_safe("CREATE TABLE foo (a);").expect("made foo");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "foo", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "foo", err)
.expect("checked if foo is compatible");
assert_eq!(is_compatible, false);

// pks
c.exec_safe("CREATE TABLE bar (a PRIMARY KEY NOT NULL);")
.expect("made bar");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "bar", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "bar", err)
.expect("checked if bar is compatible");
assert_eq!(is_compatible, true);

// nullable pks
c.exec_safe("CREATE TABLE bal (a PRIMARY KEY);")
.expect("made bal");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "bal", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "bal", err)
.expect("checked if bal is compatible");
assert_eq!(is_compatible, false);

// nullable composite pks
c.exec_safe("CREATE TABLE baf (a NOT NULL, b, PRIMARY KEY(a, b));")
.expect("made baf");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "baf", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "baf", err)
.expect("checked if baf is compatible");
assert_eq!(is_compatible, false);

Expand All @@ -189,7 +190,7 @@ fn test_is_table_compatible() {
.expect("made baz");
c.exec_safe("CREATE INDEX bar_i ON baz (b);")
.expect("made index");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "baz", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "baz", err)
.expect("checked if baz is compatible");
assert_eq!(is_compatible, true);

Expand All @@ -198,55 +199,55 @@ fn test_is_table_compatible() {
.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", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "booz", err)
.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 NOT NULL, b NOT NULL);")
.expect("made buzz");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "buzz", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "buzz", err)
.expect("checked if buzz is compatible");
assert_eq!(is_compatible, false);

// not null and dflt
c.exec_safe("CREATE TABLE boom (a PRIMARY KEY NOT NULL, b NOT NULL DEFAULT 1);")
.expect("made boom");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "boom", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "boom", err)
.expect("checked if boom is compatible");
assert_eq!(is_compatible, true);

// fk constraint
c.exec_safe("CREATE TABLE zoom (a PRIMARY KEY NOT NULL, b, FOREIGN KEY(b) REFERENCES foo(a));")
.expect("made zoom");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "zoom", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "zoom", err)
.expect("checked if zoom is compatible");
assert_eq!(is_compatible, false);

// strict mode should be ok
c.exec_safe("CREATE TABLE atable (\"id\" TEXT PRIMARY KEY) STRICT;")
.expect("made atable");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "atable", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "atable", err)
.expect("checked if atable is compatible");
assert_eq!(is_compatible, true);

// no autoincrement
c.exec_safe("CREATE TABLE woom (a integer primary key autoincrement not null);")
.expect("made woom");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "woom", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "woom", err)
.expect("checked if woom is compatible");
assert_eq!(is_compatible, false);

// aliased rowid
c.exec_safe("CREATE TABLE loom (a integer primary key not null);")
.expect("made loom");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "loom", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "loom", err)
.expect("checked if loom is compatible");
assert_eq!(is_compatible, true);

c.exec_safe("CREATE TABLE atable2 (\"id\" TEXT PRIMARY KEY NOT NULL, x TEXT) STRICT;")
.expect("made atable2");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "atable2", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "atable2", err)
.expect("checked if atable2 is compatible");
assert_eq!(is_compatible, true);

Expand All @@ -259,7 +260,7 @@ fn test_is_table_compatible() {
) STRICT;",
)
.expect("made ydoc");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "atable2", err)
let is_compatible = test_exports::tableinfo::is_table_compatible(raw_db, "atable2", err)
.expect("checked if atable2 is compatible");
assert_eq!(is_compatible, true);
}
Expand All @@ -279,22 +280,22 @@ fn test_create_clock_table_from_table_info() {
c.exec_safe("CREATE TABLE boo (a primary key not null, b, c);")
.expect("made boo");

let foo_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "foo", err)
let foo_tbl_info = test_exports::tableinfo::pull_table_info(raw_db, "foo", err)
.expect("pulled table info for foo");
let bar_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "bar", err)
let bar_tbl_info = test_exports::tableinfo::pull_table_info(raw_db, "bar", err)
.expect("pulled table info for bar");
let baz_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "baz", err)
let baz_tbl_info = test_exports::tableinfo::pull_table_info(raw_db, "baz", err)
.expect("pulled table info for baz");
let boo_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "boo", err)
let boo_tbl_info = test_exports::tableinfo::pull_table_info(raw_db, "boo", err)
.expect("pulled table info for boo");

crsql_core::bootstrap::create_clock_table(raw_db, &foo_tbl_info, err)
test_exports::bootstrap::create_clock_table(raw_db, &foo_tbl_info, err)
.expect("created clock table for foo");
crsql_core::bootstrap::create_clock_table(raw_db, &bar_tbl_info, err)
test_exports::bootstrap::create_clock_table(raw_db, &bar_tbl_info, err)
.expect("created clock table for bar");
crsql_core::bootstrap::create_clock_table(raw_db, &baz_tbl_info, err)
test_exports::bootstrap::create_clock_table(raw_db, &baz_tbl_info, err)
.expect("created clock table for baz");
crsql_core::bootstrap::create_clock_table(raw_db, &boo_tbl_info, err)
test_exports::bootstrap::create_clock_table(raw_db, &boo_tbl_info, err)
.expect("created clock table for boo");

// todo: Check that clock tables have expected schema(s)
Expand Down

0 comments on commit 9c4eaef

Please sign in to comment.