From e3e043644d12dc592cf2952ffe1b038ccbfdfa9e Mon Sep 17 00:00:00 2001 From: le king fu Date: Mon, 26 Oct 2026 21:24:25 -0400 Subject: [PATCH] fix: scrub the errors of repair_migrations through repair_at (#497) repair_migrations built its two errors without scrub: rusqlite appends the full path of a database it cannot open, so "Cannot open database" carried the profile folder, and the OS user name with it, to the Journal. The body moves to repair_at(db_path), which passes every error it returns through scrub once, at its exit, with the database's parent folder; the command resolves the path, then calls it. The test replaces the database file with a folder of the same name: the exists() guard passes and rusqlite fails to open it even as root, where a read-only file would simply open read-only. It first checks that rusqlite's own message names the folder, then that repair_at's error names the file and , and no segment of the folder. Co-Authored-By: Claude Opus 5.5 --- src-tauri/src/commands/profile_commands.rs | 122 ++++++++++++++------- 1 file changed, 82 insertions(+), 40 deletions(-) diff --git a/src-tauri/src/commands/profile_commands.rs b/src-tauri/src/commands/profile_commands.rs index 7f5ca13..a04cbd6 100644 --- a/src-tauri/src/commands/profile_commands.rs +++ b/src-tauri/src/commands/profile_commands.rs @@ -300,56 +300,69 @@ fn hex_decode(hex: &str) -> Result, String> { #[tauri::command] pub fn repair_migrations(app: tauri::AppHandle, db_filename: String) -> Result { let db_path = profile_db_path(&app, &db_filename)?; + repair_at(&db_path) +} - if !db_path.exists() { - return Ok(false); - } +/// The body of `repair_migrations`, on a resolved database path (#497). +/// Every error it returns goes through `scrub` once, at its exit: rusqlite +/// appends the full path of a database it cannot open, the webview writes the +/// error to the Journal, and an error line of the Journal can be attached to a +/// feedback report (Loi 25). +fn repair_at(db_path: &Path) -> Result { + let result = (|| -> Result { + if !db_path.exists() { + return Ok(false); + } - let conn = rusqlite::Connection::open(&db_path) - .map_err(|e| format!("Cannot open database: {}", e))?; + let conn = rusqlite::Connection::open(db_path) + .map_err(|e| format!("Cannot open database: {}", e))?; - let table_exists: bool = conn - .query_row( - "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name='_sqlx_migrations'", - [], - |row| row.get(0), - ) - .unwrap_or(false); - - if !table_exists { - return Ok(false); - } - - // Current migration SQL — must match the vec in lib.rs - let migrations: &[(i64, &str)] = &[ - (1, database::SCHEMA), - (2, database::SEED_CATEGORIES), - ]; - - let mut repaired = false; - for (version, sql) in migrations { - let expected_checksum = Sha384::digest(sql.as_bytes()).to_vec(); - - // Check if this migration exists with a different checksum - let needs_repair: bool = conn + let table_exists: bool = conn .query_row( - "SELECT COUNT(*) > 0 FROM _sqlx_migrations WHERE version = ?1 AND checksum != ?2", - rusqlite::params![version, expected_checksum], + "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name='_sqlx_migrations'", + [], |row| row.get(0), ) .unwrap_or(false); - if needs_repair { - conn.execute( - "UPDATE _sqlx_migrations SET checksum = ?1 WHERE version = ?2", - rusqlite::params![expected_checksum, version], - ) - .map_err(|e| format!("Cannot repair migration {}: {}", version, e))?; - repaired = true; + if !table_exists { + return Ok(false); } - } - Ok(repaired) + // Current migration SQL — must match the vec in lib.rs + let migrations: &[(i64, &str)] = &[ + (1, database::SCHEMA), + (2, database::SEED_CATEGORIES), + ]; + + let mut repaired = false; + for (version, sql) in migrations { + let expected_checksum = Sha384::digest(sql.as_bytes()).to_vec(); + + // Check if this migration exists with a different checksum + let needs_repair: bool = conn + .query_row( + "SELECT COUNT(*) > 0 FROM _sqlx_migrations WHERE version = ?1 AND checksum != ?2", + rusqlite::params![version, expected_checksum], + |row| row.get(0), + ) + .unwrap_or(false); + + if needs_repair { + conn.execute( + "UPDATE _sqlx_migrations SET checksum = ?1 WHERE version = ?2", + rusqlite::params![expected_checksum, version], + ) + .map_err(|e| format!("Cannot repair migration {}: {}", version, e))?; + repaired = true; + } + } + + Ok(repaired) + })(); + // `scrub` leaves the message as it is for an empty folder. + let dir = db_path.parent().unwrap_or(Path::new("")); + result.map_err(|e| profile_db::scrub(e, dir)) } #[cfg(test)] @@ -532,4 +545,33 @@ mod tests { ] ); } + + // -- repair_migrations names no folder (#497) ----------------------------- + + #[test] + fn repair_at_names_the_file_and_no_folder_when_the_database_cannot_open() { + // A folder where the database file should be: the exists() guard + // passes and rusqlite cannot open it, even as root (the CI runs as + // root, and a read-only file would simply open read-only). + let dir = profile_db::TestDir::new("repair"); + let db_path = dir.join("profile_0a1b2c3d.db"); + fs::create_dir(&db_path).unwrap(); + let folder = dir.to_str().unwrap().to_string(); + + // The probe proves something only if rusqlite's own message names the folder. + let raw = rusqlite::Connection::open(&db_path).unwrap_err().to_string(); + assert!(raw.contains(&folder), "{}", raw); + + let error = repair_at(&db_path).unwrap_err(); + + assert!(error.starts_with("Cannot open database: "), "{}", error); + assert!(error.contains(""), "{}", error); + assert!(error.contains("profile_0a1b2c3d.db"), "{}", error); + for segment in dir.components().filter_map(|c| match c { + std::path::Component::Normal(name) => name.to_str(), + _ => None, + }) { + assert!(!error.contains(segment), "{:?} in {}", segment, error); + } + } }