Skip to content

Commit

Permalink
libsql: Fix local sync internal connection
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed Oct 27, 2023
1 parent 5d676e6 commit 75e738d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion libsql/examples/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions libsql/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Result<Database> {
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 },
})
}

Expand Down Expand Up @@ -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<dyn crate::connection::Conn + Send + Sync>
};

Ok(Connection { conn })
}
Expand Down
4 changes: 2 additions & 2 deletions libsql/src/local/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl Database {
}

#[cfg(feature = "replication")]
pub fn open_local_sync(db_path: impl Into<String>) -> Result<Database> {
pub fn open_local_sync(db_path: impl Into<String>, flags: OpenFlags) -> Result<Database> {
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 {
Expand Down

0 comments on commit 75e738d

Please sign in to comment.