Files
gitlore/src/core/db.rs
Taylor Eernisse 7aaa51f645 feat(core): Implement infrastructure layer for CLI operations
Establishes foundational modules that all other components depend on.

src/core/config.rs - Configuration management:
- JSON-based config file with Zod-like validation via serde
- GitLab settings: base URL, token environment variable
- Project list with paths to track
- Sync settings: backfill days, stale lock timeout, cursor rewind
- Storage settings: database path, payload compression toggle
- XDG-compliant config path resolution via dirs crate
- Loads GITLAB_TOKEN from configured environment variable

src/core/db.rs - Database connection and migrations:
- Opens or creates SQLite database with WAL mode for concurrency
- Embeds migration SQL as const strings (001-005)
- Runs migrations idempotently with checksum verification
- Provides thread-safe connection management

src/core/error.rs - Unified error handling:
- GiError enum with variants for all failure modes
- Config, Database, GitLab, Ingestion, Lock, IO, Parse errors
- thiserror derive for automatic Display/Error impls
- Result type alias for ergonomic error propagation

src/core/lock.rs - Distributed sync locking:
- File-based locks to prevent concurrent syncs
- Stale lock detection with configurable timeout
- Force override for recovery scenarios
- Lock file contains PID and timestamp for debugging

src/core/paths.rs - Path resolution:
- XDG Base Directory Specification compliance
- Config: ~/.config/gi/config.json
- Data: ~/.local/share/gi/gi.db
- Creates parent directories on first access

src/core/payloads.rs - Raw payload storage:
- Optional gzip compression for storage efficiency
- SHA-256 content addressing for deduplication
- Type-prefixed keys (issue:, discussion:, note:)
- Batch insert with UPSERT for idempotent ingestion

src/core/time.rs - Timestamp utilities:
- Relative time parsing (7d, 2w, 1m) for --since flag
- ISO 8601 date parsing for absolute dates
- Human-friendly relative time formatting

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 11:28:07 -05:00

209 lines
6.2 KiB
Rust

//! 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<Connection> {
// 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<String>) {
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)
}