Skip to content

Commit

Permalink
feat: WAL mode by dafault on local databases
Browse files Browse the repository at this point in the history
  • Loading branch information
levydsa committed Dec 4, 2024
1 parent 1317f25 commit abe95cf
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/target
.DS_Store
test*.db
test*.db*
*.o
local.db
example
Expand Down
29 changes: 4 additions & 25 deletions flake.lock

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

17 changes: 11 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils = {
url = "github:numtide/flake-utils";
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay }:
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem
(system:
let
Expand All @@ -26,7 +22,6 @@
with pkgs;
mkShell {
nativeBuildInputs = [
zig
pkg-config
rust-bindgen
cmake
Expand All @@ -41,6 +36,16 @@
darwin.apple_sdk.frameworks.CoreServices
];

CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS= lib.lists.fold (a: b: "${a} ${b}") "" [
"-Ctarget-feature=-crt-static"
"-Clink-arg=-target"
"-Clink-arg=x86_64-apple-darwin"
];
CC_x86_64_apple_darwin =
"${pkgs.pkgsCross.x86_64-darwin.stdenv.cc}/bin/x86_64-apple-darwin-clang";
CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER =
"${pkgs.pkgsCross.x86_64-darwin.stdenv.cc}/bin/x86_64-apple-darwin-clang";

CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Ctarget-feature=-crt-static";
CC_x86_64_unknown_linux_gnu =
"${pkgs.pkgsCross.gnu64.stdenv.cc}/bin/x86_64-unknown-linux-gnu-gcc";
Expand Down
122 changes: 119 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ pub extern "C" fn libsql_database_init(desc: c::libsql_database_desc_t) -> c::li
_ => db,
};

RT.block_on(db.build())
RT.block_on(async {
let db = db.build().await?;
let conn = db.connect()?;
conn.query("PRAGMA journal_mode=WAL", ()).await?;
Ok(db)
})
}
(None, Some(url), Some(auth_token)) => {
let db = libsql::Builder::new_remote(
Expand Down Expand Up @@ -998,7 +1003,10 @@ const _: () = {
let _: [unsafe extern "C" fn(_, _) -> _; 2] =
[c::libsql_transaction_prepare, libsql_transaction_prepare];

let _: [unsafe extern "C" fn(_) -> _; 2] = [c::libsql_statement_column_count, libsql_statement_column_count];
let _: [unsafe extern "C" fn(_) -> _; 2] = [
c::libsql_statement_column_count,
libsql_statement_column_count,
];
let _: [unsafe extern "C" fn(_) -> _; 2] = [c::libsql_statement_query, libsql_statement_query];
let _: [unsafe extern "C" fn(_) -> _; 2] =
[c::libsql_statement_execute, libsql_statement_execute];
Expand Down Expand Up @@ -1041,7 +1049,7 @@ const _: () = {

#[cfg(test)]
mod tests {
use crate::{libsql_database_deinit, libsql_setup};
use crate::{libsql_database_deinit, libsql_row_empty, libsql_setup};

use super::c::*;
use anyhow::Result;
Expand Down Expand Up @@ -1181,4 +1189,112 @@ mod tests {
Ok(())
}
}

#[test]
fn test_multiconnection() -> Result<()> {
unsafe {
let path = CString::new("./test2.db")?;
let key = CString::new("super_secret")?;

let desc = libsql_database_desc_t {
path: path.as_ptr(),
encryption_key: key.as_ptr(),
..Default::default()
};

let db = libsql_database_init(desc);
assert!(
db.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(db.err))
);

{
let conn = libsql_database_connect(db);
assert!(conn.err.is_null());

let sql = CString::new("create table if not exists test (i integer);")?;
let batch = libsql_connection_batch(conn, sql.as_ptr());
assert!(
batch.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(batch.err))
);

// leak connection
}

{
let conn = libsql_database_connect(db);
assert!(
conn.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(conn.err))
);

let sql = CString::new("insert into test values (1)")?;
let stmt = libsql_connection_prepare(conn, sql.as_ptr());
assert!(
stmt.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(stmt.err)),
);

let exec = libsql_statement_execute(stmt);
assert!(
exec.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(stmt.err)),
);
assert_eq!(exec.rows_changed, 1);

libsql_connection_deinit(conn);
libsql_statement_deinit(stmt);

// leak connection
}

{
let conn = libsql_database_connect(db);
assert!(
conn.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(conn.err))
);

let sql = CString::new("select i from test")?;
let stmt = libsql_connection_prepare(conn, sql.as_ptr());
assert!(
stmt.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(stmt.err)),
);

let rows = libsql_statement_query(stmt);
assert!(
rows.err.is_null(),
"{:?}",
CStr::from_ptr(libsql_error_message(rows.err)),
);

loop {
let row = libsql_rows_next(rows);
if libsql_row_empty(row) {
break;
}

dbg!(libsql_row_value(row, 0).ok.value.integer);
}

libsql_connection_deinit(conn);
libsql_statement_deinit(stmt);

// leak connection
}

libsql_database_deinit(db);

Ok(())
}
}
}

0 comments on commit abe95cf

Please sign in to comment.