forge_workspace/store/
model_catalog.rs1use redb::TableDefinition;
10
11use super::Db;
12
13const MODEL_CATALOG: TableDefinition<&str, &[u8]> = TableDefinition::new("model_catalog");
14
15pub fn drop_table(db: &Db) -> anyhow::Result<bool> {
19 let txn = db.database().begin_write()?;
20 let dropped = txn.delete_table(MODEL_CATALOG)?;
23 txn.commit()?;
24 Ok(dropped)
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30 use tempfile::tempdir;
31
32 #[test]
35 fn drop_table_reports_whether_the_table_was_there() {
36 let dir = tempdir().expect("tempdir");
37 let db = Db::open(&dir.path().join("db.redb")).expect("open db");
38 assert!(
39 !drop_table(&db).expect("drop an absent table"),
40 "a store that never cached a catalog drops nothing",
41 );
42
43 {
44 let txn = db.database().begin_write().expect("begin");
45 {
46 let mut table = txn.open_table(MODEL_CATALOG).expect("open table");
47 table
48 .insert("https://openrouter.ai/api/v1", "cached".as_bytes())
49 .expect("insert a cached catalog");
50 }
51 txn.commit().expect("commit");
52 }
53
54 assert!(drop_table(&db).expect("drop the table"), "a planted table drops to true");
55
56 let txn = db.database().begin_read().expect("begin read");
57 assert!(txn.open_table(MODEL_CATALOG).is_err(), "the table is gone rather than emptied");
58 }
59}