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:
le king fu 2026-10-26 21:24:25 -04:00
commit e3e043644d

View file

@ -300,56 +300,69 @@ fn hex_decode(hex: &str) -> Result<Vec<u8>, String> {
#[tauri::command]
pub fn repair_migrations(app: tauri::AppHandle, db_filename: String) -> Result<bool, String> {
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<bool, String> {
let result = (|| -> Result<bool, String> {
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("<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);
}
}
}