Skip to main content

agent_orchestrator/persistence/
sqlite.rs

1use anyhow::{Context, Result};
2use rusqlite::Connection;
3use std::path::Path;
4use std::time::Duration;
5
6/// Busy-timeout applied to SQLite connections opened by the persistence layer.
7pub const SQLITE_BUSY_TIMEOUT_MS: u64 = 5000;
8
9/// Opens a SQLite connection and applies orchestrator-specific pragmas.
10pub fn open_conn(db_path: &Path) -> Result<Connection> {
11    let conn = Connection::open(db_path).context("failed to open sqlite db")?;
12    configure_conn(&conn)?;
13    Ok(conn)
14}
15
16/// Applies shared SQLite connection settings.
17pub fn configure_conn(conn: &Connection) -> Result<()> {
18    conn.busy_timeout(Duration::from_millis(SQLITE_BUSY_TIMEOUT_MS))
19        .context("failed to set sqlite busy timeout")?;
20    conn.execute_batch(
21        r#"
22        PRAGMA foreign_keys = ON;
23        "#,
24    )
25    .context("failed to configure sqlite pragmas")?;
26    Ok(())
27}