Menu
Public repository / 8e8b37791b10d6119df0a2cff4be3c35376e8ea1
AkurAI-IDP / src/db/mod.rs
Latest change 067df512e550e011fdb00cebb40754b0aa5429d9 - release: v0.1.1 by Ólafur Búi Ólafsson
pub mod schema;
use crate::config;
use rusqlite::Connection;
use std::sync::LazyLock;
use std::sync::Mutex;
static DB: LazyLock<Mutex<Connection>> = LazyLock::new(|| {
let cfg = config::get();
if let Some(parent) = std::path::Path::new(&cfg.db_path).parent() {
std::fs::create_dir_all(parent).ok();
}
let conn = Connection::open(&cfg.db_path).expect("failed to open SQLite database");
conn.execute_batch("PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;")
.expect("failed to set pragmas");
schema::create_tables(&conn);
Mutex::new(conn)
});
pub fn with_db<F, R>(f: F) -> R
where
F: FnOnce(&Connection) -> R,
{
let conn = DB.lock().unwrap();
f(&conn)
}