Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
libsql: attach databases from other namespaces as readonly
Browse files Browse the repository at this point in the history
With this proof-of-concept patch, other namespaces hosted
on the same sqld machine are now attached in readonly mode,
so that users can read from other databases when connected
to a particular one.
  • Loading branch information
psarna committed Oct 13, 2023
1 parent 340e278 commit a26e84b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions sqld/src/connection/libsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ where
)?;
conn.conn
.pragma_update(None, "max_page_count", max_db_size)?;
let dbs_path = path
.as_ref()
.parent()
.unwrap_or_else(|| std::path::Path::new(".."))
.canonicalize()
.unwrap_or_else(|_| std::path::PathBuf::from(".."));
for entry in (std::fs::read_dir(&dbs_path)?).flatten() {
if let Some(namespace) = entry.file_name().to_str() {
let db_path = entry.path().join("data");
let query = format!(
"ATTACH DATABASE 'file:{}?mode=ro' AS {namespace}",
db_path.display()
);
if let Err(e) = conn.conn.execute(&query, ()) {
tracing::warn!("Failed to attach database {}: {}", db_path.display(), e);
} else {
tracing::debug!("Attached {} as {namespace}", db_path.display());
}
}
}
Ok(conn)
})
.await
Expand Down

0 comments on commit a26e84b

Please sign in to comment.