use anyhow::Result;
use rusqlite::{OptionalExtension, params};
use super::SqliteStore;
const DONE_KEY: &str = "transcript_backfill_v1";
impl SqliteStore {
pub fn transcript_backfill_done(&self) -> Result<bool> {
let value: Option<String> = self
.reader()
.query_row(
"SELECT value FROM index_meta WHERE key = ?1",
params![DONE_KEY],
|r| r.get(0),
)
.optional()?;
Ok(value.as_deref() == Some("done"))
}
pub fn mark_transcript_backfill_done(&self) -> Result<()> {
self.conn().execute(
"INSERT OR REPLACE INTO index_meta (key, value) VALUES (?1, 'done')",
params![DONE_KEY],
)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_new_store_has_not_backfilled() {
let store = SqliteStore::in_memory("m".to_string()).unwrap();
assert!(!store.transcript_backfill_done().unwrap());
}
#[test]
fn marking_the_backfill_done_survives_a_reopen() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("brain.db");
SqliteStore::open(&path, "m".to_string())
.unwrap()
.mark_transcript_backfill_done()
.unwrap();
let reopened = SqliteStore::open(&path, "m".to_string()).unwrap();
assert!(reopened.transcript_backfill_done().unwrap());
}
}