From 60cbc6d8ea3b483da3ae6c217c495dbd06638abf Mon Sep 17 00:00:00 2001 From: RS2007 Date: Sun, 2 Nov 2025 16:22:41 +0530 Subject: [PATCH] migrating from_uri to database opts --- bindings/python/src/lib.rs | 4 +- cli/app.rs | 18 +++-- cli/mcp_server.rs | 2 +- core/lib.rs | 39 +--------- .../query_processing/encryption.rs | 74 +++---------------- 5 files changed, 26 insertions(+), 111 deletions(-) diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 3ce9daf82..9ebcb3547 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -6,7 +6,7 @@ use std::cell::RefCell; use std::num::NonZeroUsize; use std::rc::Rc; use std::sync::Arc; -use turso_core::Value; +use turso_core::{DatabaseOpts, Value}; mod errors; @@ -317,7 +317,7 @@ impl Drop for Connection { #[allow(clippy::arc_with_non_send_sync)] #[pyfunction(signature = (path))] pub fn connect(path: &str) -> Result { - match turso_core::Connection::from_uri(path, true, false, false, false, false, false, false) { + match turso_core::Connection::from_uri(path, DatabaseOpts::default()) { Ok((io, conn)) => Ok(Connection { conn, _io: io }), Err(e) => Err(PyErr::new::(format!( "Failed to create connection: {e:?}" diff --git a/cli/app.rs b/cli/app.rs index fd776b28c..d33677d7d 100644 --- a/cli/app.rs +++ b/cli/app.rs @@ -32,7 +32,8 @@ use std::{ use tracing_appender::non_blocking::WorkerGuard; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; use turso_core::{ - Connection, Database, LimboError, OpenFlags, QueryMode, Statement, StepResult, Value, + Connection, Database, DatabaseOpts, LimboError, OpenFlags, QueryMode, Statement, StepResult, + Value, }; #[derive(Parser, Debug)] @@ -188,16 +189,17 @@ impl Limbo { .as_ref() .map_or(":memory:".to_string(), |p| p.to_string_lossy().to_string()); let indexes_enabled = opts.experimental_indexes.unwrap_or(true); + let (io, conn) = if db_file.contains([':', '?', '&', '#']) { Connection::from_uri( &db_file, - indexes_enabled, - opts.experimental_mvcc, - opts.experimental_views, - opts.experimental_strict, - opts.experimental_encryption, - opts.experimental_index_method, - opts.experimental_autovacuum, + DatabaseOpts::new() + .with_indexes(indexes_enabled) + .with_mvcc(opts.experimental_mvcc) + .with_views(opts.experimental_views) + .with_strict(opts.experimental_strict) + .with_encryption(opts.experimental_encryption) + .with_index_method(opts.experimental_index_method), )? } else { let flags = if opts.readonly { diff --git a/cli/mcp_server.rs b/cli/mcp_server.rs index e4f1083f1..ee790aa3e 100644 --- a/cli/mcp_server.rs +++ b/cli/mcp_server.rs @@ -408,7 +408,7 @@ impl TursoMcpServer { // Open the new database connection let conn = if path == ":memory:" || path.contains([':', '?', '&', '#']) { - match Connection::from_uri(&path, true, false, false, false, false, false, false) { + match Connection::from_uri(&path, DatabaseOpts::default()) { Ok((_io, c)) => c, Err(e) => return format!("Failed to open database '{path}': {e}"), } diff --git a/core/lib.rs b/core/lib.rs index f9b11580c..399020ff1 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -1530,39 +1530,13 @@ impl Connection { } #[cfg(feature = "fs")] - #[allow(clippy::too_many_arguments)] - pub fn from_uri( - uri: &str, - use_indexes: bool, - mvcc: bool, - views: bool, - strict: bool, - // flag to opt-in encryption support - encryption: bool, - // flag to opt-in custom modules support - custom_modules: bool, - // flag to opt-in autovacuum support - autovacuum: bool, - ) -> Result<(Arc, Arc)> { + pub fn from_uri(uri: &str, db_opts: DatabaseOpts) -> Result<(Arc, Arc)> { use crate::util::MEMORY_PATH; let opts = OpenOptions::parse(uri)?; let flags = opts.get_flags()?; if opts.path == MEMORY_PATH || matches!(opts.mode, OpenMode::Memory) { let io = Arc::new(MemoryIO::new()); - let db = Database::open_file_with_flags( - io.clone(), - MEMORY_PATH, - flags, - DatabaseOpts::new() - .with_mvcc(mvcc) - .with_indexes(use_indexes) - .with_views(views) - .with_strict(strict) - .with_encryption(encryption) - .with_index_method(custom_modules) - .with_autovacuum(autovacuum), - None, - )?; + let db = Database::open_file_with_flags(io.clone(), MEMORY_PATH, flags, db_opts, None)?; let conn = db.connect()?; return Ok((io, conn)); } @@ -1584,14 +1558,7 @@ impl Connection { &opts.path, opts.vfs.as_ref(), flags, - DatabaseOpts::new() - .with_mvcc(mvcc) - .with_indexes(use_indexes) - .with_views(views) - .with_strict(strict) - .with_encryption(encryption) - .with_index_method(custom_modules) - .with_autovacuum(autovacuum), + db_opts, encryption_opts.clone(), )?; if let Some(modeof) = opts.modeof { diff --git a/tests/integration/query_processing/encryption.rs b/tests/integration/query_processing/encryption.rs index ea10f4221..2a7a90d7a 100644 --- a/tests/integration/query_processing/encryption.rs +++ b/tests/integration/query_processing/encryption.rs @@ -1,7 +1,7 @@ use crate::common::{do_flush, run_query, run_query_on_row, TempDatabase}; use rand::{rng, RngCore}; use std::panic; -use turso_core::Row; +use turso_core::{DatabaseOpts, Row}; const ENABLE_ENCRYPTION: bool = true; @@ -48,13 +48,7 @@ fn test_per_page_encryption() -> anyhow::Result<()> { ); let (_io, conn) = turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), )?; let mut row_count = 0; run_query_on_row(&tmp_db, &conn, "SELECT * FROM test", |row: &Row| { @@ -72,13 +66,7 @@ fn test_per_page_encryption() -> anyhow::Result<()> { ); let (_io, conn) = turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), )?; run_query( &tmp_db, @@ -95,13 +83,7 @@ fn test_per_page_encryption() -> anyhow::Result<()> { ); let (_io, conn) = turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), )?; run_query( &tmp_db, @@ -126,13 +108,7 @@ fn test_per_page_encryption() -> anyhow::Result<()> { ); let (_io, conn) = turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), )?; let should_panic = panic::catch_unwind(panic::AssertUnwindSafe(|| { run_query_on_row(&tmp_db, &conn, "SELECT * FROM test", |_row: &Row| {}).unwrap(); @@ -148,13 +124,7 @@ fn test_per_page_encryption() -> anyhow::Result<()> { let should_panic = panic::catch_unwind(panic::AssertUnwindSafe(|| { turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), ) .unwrap(); })); @@ -171,13 +141,7 @@ fn test_per_page_encryption() -> anyhow::Result<()> { let should_panic = panic::catch_unwind(panic::AssertUnwindSafe(|| { turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), ) .unwrap(); })); @@ -247,13 +211,7 @@ fn test_non_4k_page_size_encryption() -> anyhow::Result<()> { ); let (_io, conn) = turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), )?; run_query_on_row(&tmp_db, &conn, "SELECT * FROM test", |row: &Row| { assert_eq!(row.get::(0).unwrap(), 1); @@ -314,13 +272,7 @@ fn test_corruption_turso_magic_bytes() -> anyhow::Result<()> { let should_panic = panic::catch_unwind(panic::AssertUnwindSafe(|| { let (_io, conn) = turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), ) .unwrap(); run_query_on_row(&tmp_db, &conn, "SELECT * FROM test", |_row: &Row| {}).unwrap(); @@ -408,13 +360,7 @@ fn test_corruption_associated_data_bytes() -> anyhow::Result<()> { let should_panic = panic::catch_unwind(panic::AssertUnwindSafe(|| { let (_io, conn) = turso_core::Connection::from_uri( &uri, - true, - false, - false, - false, - ENABLE_ENCRYPTION, - false, - false, + DatabaseOpts::new().with_encryption(ENABLE_ENCRYPTION), ) .unwrap(); run_query_on_row(&test_tmp_db, &conn, "SELECT * FROM test", |_row: &Row| {})