Merge pull request #49 from PR0M3TH3AN/codex/fix-failing-workflow-tests

Fix restore with backup ID
This commit is contained in:
thePR0M3TH3AN
2025-05-21 20:05:32 -04:00
committed by GitHub

View File

@@ -60,7 +60,8 @@ fn main() -> Result<()> {
Commands::Init => {
info!("Database initialised at {}", cfg.db_path.display());
let cwd = env::current_dir().context("getting current directory")?;
let count = scan::scan_directory(&mut conn, &cwd).context("initial scan failed")?;
let count =
scan::scan_directory(&mut conn, &cwd).context("initial scan failed")?;
info!("Initial scan complete indexed/updated {count} files");
}
@@ -75,8 +76,11 @@ fn main() -> Result<()> {
if dirty {
let dirty_ids = take_dirty(&conn)?;
for id in dirty_ids {
let path: String =
conn.query_row("SELECT path FROM files WHERE id = ?1", [id], |r| r.get(0))?;
let path: String = conn.query_row(
"SELECT path FROM files WHERE id = ?1",
[id],
|r| r.get(0),
)?;
scan::scan_directory(&mut conn, Path::new(&path))?;
}
} else {
@@ -106,33 +110,38 @@ fn main() -> Result<()> {
}
Commands::Restore { backup_path } => {
drop(conn);
if backup_path.exists() { db::restore(&backup_path, &cfg.db_path)
.with_context(|| {
drop(conn); // close connection so the restore can overwrite the DB file
if backup_path.exists() {
// User pointed to an actual backup file on disk
db::restore(&backup_path, &cfg.db_path).with_context(|| {
format!("Failed to restore DB from {}", backup_path.display())
})?;
} else {
// Assume they passed just the file-name that lives in the standard backups dir
let backups_dir = cfg.db_path.parent().unwrap().join("backups");
let manager = BackupManager::new(&cfg.db_path, &backups_dir)?;
let name = backup_path
.file_name()
.and_then(|n| n.to_str())
.context("invalid backup file name")?;
manager
.restore_from_backup(name)
.with_context(|| {
manager.restore_from_backup(name).with_context(|| {
format!("Failed to restore DB from {}", backup_path.display())
})?;
}
println!("Restored DB from {}", backup_path.display());
// Re-open so the rest of the program talks to the fresh database
db::open(&cfg.db_path).with_context(|| {
format!("Could not open restored DB at {}", cfg.db_path.display())
})?;
info!("Successfully opened restored database.");
}
/* ---- passthrough sub-modules (some still stubs) ---------- */
/* ---- passthrough sub-modules ---------------------------- */
Commands::Link(link_cmd) => cli::link::run(&link_cmd, &mut conn, args.format)?,
Commands::Coll(coll_cmd) => cli::coll::run(&coll_cmd, &mut conn, args.format)?,
Commands::View(view_cmd) => cli::view::run(&view_cmd, &mut conn, args.format)?,
@@ -146,7 +155,6 @@ fn main() -> Result<()> {
}
Ok(())
}
/* ─────────────────── helpers & sub-routines ─────────────────── */