From 75e738dd7ceb631f3e7e9fc73588ab0562ea7795 Mon Sep 17 00:00:00 2001 From: Lucio Franco Date: Fri, 27 Oct 2023 13:09:28 -0400 Subject: [PATCH] libsql: Fix local sync internal connection --- libsql/examples/replica.rs | 2 +- libsql/src/database.rs | 16 +++++++++++----- libsql/src/local/database.rs | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/libsql/examples/replica.rs b/libsql/examples/replica.rs index 78e6fef44a..055cdece5c 100644 --- a/libsql/examples/replica.rs +++ b/libsql/examples/replica.rs @@ -15,7 +15,7 @@ async fn main() { let db = Database::open_with_remote_sync( db_file.path().to_str().unwrap(), - "libsql://localhost:8080", + "http://localhost:8080", auth_token, ) .await diff --git a/libsql/src/database.rs b/libsql/src/database.rs index 84e732b359..224e3e5080 100644 --- a/libsql/src/database.rs +++ b/libsql/src/database.rs @@ -90,8 +90,10 @@ cfg_replication! { /// Open a local database file with the ability to sync from snapshots from local filesystem. #[cfg(feature = "replication")] pub async fn open_with_local_sync(db_path: impl Into) -> Result { + let db = crate::local::Database::open_local_sync(db_path, OpenFlags::default())?; + Ok(Database { - db_type: DbType::File { path: db_path.into(), flags: OpenFlags::default() }, + db_type: DbType::Sync { db }, }) } @@ -233,11 +235,15 @@ impl Database { let conn = db.connect()?; let local = LibsqlConnection { conn }; - let writer = local.conn.writer().unwrap().clone(); - - let remote = crate::replication::RemoteConnection::new(local, writer); - let conn = std::sync::Arc::new(remote); + let conn = if let Some(writer) = local.conn.writer() { + let writer = writer.clone(); + let remote = crate::replication::RemoteConnection::new(local, writer); + std::sync::Arc::new(remote) + } else { + std::sync::Arc::new(local) + as std::sync::Arc + }; Ok(Connection { conn }) } diff --git a/libsql/src/local/database.rs b/libsql/src/local/database.rs index 74144b414b..84fa91c8ba 100644 --- a/libsql/src/local/database.rs +++ b/libsql/src/local/database.rs @@ -65,9 +65,9 @@ impl Database { } #[cfg(feature = "replication")] - pub fn open_local_sync(db_path: impl Into) -> Result { + pub fn open_local_sync(db_path: impl Into, flags: OpenFlags) -> Result { let db_path = db_path.into(); - let mut db = Database::open(&db_path, OpenFlags::default())?; + let mut db = Database::open(&db_path, flags)?; let replicator = Replicator::new(db_path).map_err(|e| ConnectionFailed(format!("{e}")))?; db.replication_ctx = Some(ReplicationContext {