wsb 0.248.6265

Workspace tools for every project under $PRJ: git across submodules, template stamping, GitHub, and the workbench
Documentation
use crate::prelude::*;

/// Which database a file is, and so which migrations it runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Schema {
    /// A worktree's own `.wsb/wsb.db`: its catalogue, notes, layout and the
    /// rest of what belongs to that checkout.
    Worktree,
    /// The machine's `wsb.db` in the machine directory: every agent session
    /// on the machine.
    Sessions,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
    pub schema: Schema,
    pub db_path: String,
    pub db_max_connections: u32,
    pub db_connection_timeout_seconds: u64,
    pub db_enable_wal: bool,
    pub db_enable_foreign_keys: bool,
    pub db_busy_timeout_ms: u32,
}

impl Default for DatabaseConfig {
    fn default() -> Self {
        Self {
            schema: Schema::Worktree,
            db_path: ".wsb/wsb.db".to_string(),
            db_max_connections: 10,
            db_connection_timeout_seconds: 30,
            db_enable_wal: true,
            db_enable_foreign_keys: true,
            db_busy_timeout_ms: 30000,
        }
    }
}

impl DatabaseConfig {
    /// A worktree's database at `db_path`.
    pub fn new(db_path: &Path) -> Self {
        Self {
            db_path: db_path.to_string_lossy().to_string(),
            ..Default::default()
        }
    }

    /// The machine's sessions database at `db_path`.
    pub fn sessions(db_path: &Path) -> Self {
        Self {
            schema: Schema::Sessions,
            ..Self::new(db_path)
        }
    }
}