huddle_core/storage/
mod.rs1pub mod repo;
2pub mod schema;
3
4use rusqlite::Connection;
5use std::path::Path;
6use std::sync::{Arc, Mutex};
7
8use crate::error::Result;
9
10pub type Db = Arc<Mutex<Connection>>;
11
12pub fn open_db(path: &Path) -> Result<Db> {
13 let conn = Connection::open(path)?;
14 conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;")?;
15 run_migrations(&conn)?;
16 Ok(Arc::new(Mutex::new(conn)))
17}
18
19pub fn open_db_in_memory() -> Result<Db> {
20 let conn = Connection::open_in_memory()?;
21 conn.execute_batch("PRAGMA foreign_keys=ON;")?;
22 run_migrations(&conn)?;
23 Ok(Arc::new(Mutex::new(conn)))
24}
25
26fn run_migrations(conn: &Connection) -> Result<()> {
27 for migration in schema::MIGRATIONS {
28 conn.execute_batch(migration)?;
29 }
30 Ok(())
31}