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

Commit 0cba188

Browse files
committed
libsql: attach databases from other namespaces as readonly
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.
1 parent 340e278 commit 0cba188

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

sqld/src/connection/libsql.rs

+20
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,26 @@ where
212212
)?;
213213
conn.conn
214214
.pragma_update(None, "max_page_count", max_db_size)?;
215+
let dbs_path = path
216+
.as_ref()
217+
.parent()
218+
.unwrap_or_else(|| std::path::Path::new(".."))
219+
.canonicalize()
220+
.unwrap_or_else(|_| std::path::PathBuf::from(".."));
221+
for entry in (std::fs::read_dir(&dbs_path)?).flatten() {
222+
if let Some(namespace) = entry.file_name().to_str() {
223+
let db_path = entry.path().join("data");
224+
let query = format!(
225+
"ATTACH DATABASE 'file:{}?mode=ro' AS \"{namespace}\"",
226+
db_path.display()
227+
);
228+
if let Err(e) = conn.conn.execute(&query, ()) {
229+
tracing::warn!("Failed to attach database {}: {}", db_path.display(), e);
230+
} else {
231+
tracing::debug!("Attached {} as {namespace}", db_path.display());
232+
}
233+
}
234+
}
215235
Ok(conn)
216236
})
217237
.await

0 commit comments

Comments
 (0)