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 <profile folder>, and no segment of the folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
75b876a05a
commit
e3e043644d
1 changed files with 82 additions and 40 deletions
|
|
@ -300,12 +300,21 @@ fn hex_decode(hex: &str) -> Result<Vec<u8>, String> {
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn repair_migrations(app: tauri::AppHandle, db_filename: String) -> Result<bool, String> {
|
pub fn repair_migrations(app: tauri::AppHandle, db_filename: String) -> Result<bool, String> {
|
||||||
let db_path = profile_db_path(&app, &db_filename)?;
|
let db_path = profile_db_path(&app, &db_filename)?;
|
||||||
|
repair_at(&db_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<bool, String> {
|
||||||
|
let result = (|| -> Result<bool, String> {
|
||||||
if !db_path.exists() {
|
if !db_path.exists() {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let conn = rusqlite::Connection::open(&db_path)
|
let conn = rusqlite::Connection::open(db_path)
|
||||||
.map_err(|e| format!("Cannot open database: {}", e))?;
|
.map_err(|e| format!("Cannot open database: {}", e))?;
|
||||||
|
|
||||||
let table_exists: bool = conn
|
let table_exists: bool = conn
|
||||||
|
|
@ -350,6 +359,10 @@ pub fn repair_migrations(app: tauri::AppHandle, db_filename: String) -> Result<b
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(repaired)
|
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)]
|
#[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("<profile folder>"), "{}", 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue