Skip to main content

kardo_core/db/
mod.rs

1//! Database layer for Kardo — `SQLite` with WAL mode.
2//!
3//! Two databases:
4//! - **Project DB** (`<project>/.kardo/kardo.db`): per-project data
5//! - **App DB** (`~/Library/Application Support/com.kardo.app/app.db`): global data
6
7mod migrations;
8mod project_db;
9mod app_db;
10
11pub use app_db::{AppDb, RecentProject};
12pub use project_db::{BaselineRecord, ClassificationRecord, ProjectDb, ScanHistoryRecord, SearchResult};
13
14use rusqlite::Connection;
15use thiserror::Error;
16
17/// Database error type for kardo-core storage operations.
18#[derive(Error, Debug)]
19pub enum DbError {
20    /// `SQLite` error from rusqlite.
21    #[error("SQLite error: {0}")]
22    Sqlite(#[from] rusqlite::Error),
23
24    /// Schema migration failure.
25    #[error("Migration failed: {0}")]
26    Migration(String),
27
28    /// Filesystem IO error.
29    #[error("IO error: {0}")]
30    Io(#[from] std::io::Error),
31}
32
33pub type DbResult<T> = Result<T, DbError>;
34
35/// Apply standard SQLite pragmas for performance and safety.
36pub fn apply_pragmas(conn: &Connection) -> DbResult<()> {
37    conn.execute_batch(
38        "PRAGMA journal_mode = WAL;
39         PRAGMA synchronous = NORMAL;
40         PRAGMA wal_autocheckpoint = 1000;
41         PRAGMA busy_timeout = 5000;
42         PRAGMA cache_size = -64000;
43         PRAGMA foreign_keys = ON;
44         PRAGMA temp_store = MEMORY;",
45    )?;
46    Ok(())
47}
48
49#[cfg(test)]
50mod tests;