Skip to main content

forge_workspace/store/
model_catalog.rs

1//! The retired `model_catalog` table, kept only to drop it.
2//!
3//! `f5a8292c` deleted the OpenRouter catalog machinery and the store
4//! module that owned this table, which left its rows behind in every
5//! store that had cached a catalog. Nothing in the tree reads or writes
6//! it now, so there is nothing to drain before [`drop_table`] removes
7//! it.
8
9use redb::TableDefinition;
10
11use super::Db;
12
13const MODEL_CATALOG: TableDefinition<&str, &[u8]> = TableDefinition::new("model_catalog");
14
15/// Remove the table. Returns whether a table was there to drop.
16/// Callers compact afterwards, because dropping a table reclaims
17/// nothing on its own.
18pub fn drop_table(db: &Db) -> anyhow::Result<bool> {
19    let txn = db.database().begin_write()?;
20    // `false` for a store that never had the table, which is not an
21    // error: the boot drops cleanly either way.
22    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    /// Dropping reports whether the table was there, so a store that
33    /// never cached a catalog does not read as a failed drop.
34    #[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}