//! Database connection and migration management. //! //! Uses rusqlite with WAL mode for crash safety. use rusqlite::Connection; use sqlite_vec::sqlite3_vec_init; use std::fs; use std::path::Path; use tracing::{debug, info}; use super::error::{GiError, Result}; /// Embedded migrations - compiled into the binary. const MIGRATIONS: &[(&str, &str)] = &[ ("001", include_str!("../../migrations/001_initial.sql")), ("002", include_str!("../../migrations/002_issues.sql")), ("003", include_str!("../../migrations/003_indexes.sql")), ("004", include_str!("../../migrations/004_discussions_payload.sql")), ("005", include_str!("../../migrations/005_assignees_milestone_duedate.sql")), ]; /// Create a database connection with production-grade pragmas. pub fn create_connection(db_path: &Path) -> Result { // Register sqlite-vec extension globally (safe to call multiple times) #[allow(clippy::missing_transmute_annotations)] unsafe { rusqlite::ffi::sqlite3_auto_extension(Some(std::mem::transmute( sqlite3_vec_init as *const (), ))); } // Ensure parent directory exists if let Some(parent) = db_path.parent() { fs::create_dir_all(parent)?; } let conn = Connection::open(db_path)?; // Production-grade pragmas for single-user CLI conn.pragma_update(None, "journal_mode", "WAL")?; conn.pragma_update(None, "synchronous", "NORMAL")?; // Safe for WAL on local disk conn.pragma_update(None, "foreign_keys", "ON")?; conn.pragma_update(None, "busy_timeout", 5000)?; // 5s wait on lock contention conn.pragma_update(None, "temp_store", "MEMORY")?; // Small speed win debug!(db_path = %db_path.display(), "Database connection created"); Ok(conn) } /// Run all pending migrations using embedded SQL. pub fn run_migrations(conn: &Connection) -> Result<()> { // Get current schema version let has_version_table: bool = conn .query_row( "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name='schema_version'", [], |row| row.get(0), ) .unwrap_or(false); let current_version: i32 = if has_version_table { conn.query_row( "SELECT COALESCE(MAX(version), 0) FROM schema_version", [], |row| row.get(0), ) .unwrap_or(0) } else { 0 }; for (version_str, sql) in MIGRATIONS { let version: i32 = version_str.parse().expect("Invalid migration version"); if version <= current_version { debug!(version, "Migration already applied"); continue; } conn.execute_batch(sql) .map_err(|e| GiError::MigrationFailed { version, message: e.to_string(), source: Some(e), })?; info!(version, "Migration applied"); } Ok(()) } /// Run migrations from filesystem (for testing or custom migrations). #[allow(dead_code)] pub fn run_migrations_from_dir(conn: &Connection, migrations_dir: &Path) -> Result<()> { let has_version_table: bool = conn .query_row( "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name='schema_version'", [], |row| row.get(0), ) .unwrap_or(false); let current_version: i32 = if has_version_table { conn.query_row( "SELECT COALESCE(MAX(version), 0) FROM schema_version", [], |row| row.get(0), ) .unwrap_or(0) } else { 0 }; let mut migrations: Vec<_> = fs::read_dir(migrations_dir)? .filter_map(|entry| entry.ok()) .filter(|entry| entry.path().extension().is_some_and(|ext| ext == "sql")) .collect(); migrations.sort_by_key(|entry| entry.file_name()); for entry in migrations { let filename = entry.file_name(); let filename_str = filename.to_string_lossy(); let version: i32 = match filename_str.split('_').next().and_then(|v| v.parse().ok()) { Some(v) => v, None => continue, }; if version <= current_version { continue; } let sql = fs::read_to_string(entry.path())?; conn.execute_batch(&sql) .map_err(|e| GiError::MigrationFailed { version, message: e.to_string(), source: Some(e), })?; info!(version, file = %filename_str, "Migration applied"); } Ok(()) } /// Verify database pragmas are set correctly. /// Used by gi doctor command. pub fn verify_pragmas(conn: &Connection) -> (bool, Vec) { let mut issues = Vec::new(); let journal_mode: String = conn .pragma_query_value(None, "journal_mode", |row| row.get(0)) .unwrap_or_default(); if journal_mode != "wal" { issues.push(format!("journal_mode is {journal_mode}, expected 'wal'")); } let foreign_keys: i32 = conn .pragma_query_value(None, "foreign_keys", |row| row.get(0)) .unwrap_or(0); if foreign_keys != 1 { issues.push(format!("foreign_keys is {foreign_keys}, expected 1")); } let busy_timeout: i32 = conn .pragma_query_value(None, "busy_timeout", |row| row.get(0)) .unwrap_or(0); if busy_timeout != 5000 { issues.push(format!("busy_timeout is {busy_timeout}, expected 5000")); } let synchronous: i32 = conn .pragma_query_value(None, "synchronous", |row| row.get(0)) .unwrap_or(0); // NORMAL = 1 if synchronous != 1 { issues.push(format!("synchronous is {synchronous}, expected 1 (NORMAL)")); } (issues.is_empty(), issues) } /// Get current schema version. pub fn get_schema_version(conn: &Connection) -> i32 { let has_version_table: bool = conn .query_row( "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name='schema_version'", [], |row| row.get(0), ) .unwrap_or(false); if !has_version_table { return 0; } conn.query_row( "SELECT COALESCE(MAX(version), 0) FROM schema_version", [], |row| row.get(0), ) .unwrap_or(0) }