feat: undo a restore from the safety copy it took
- POST /refresh-runs accepts undo_of: restores the safety copy of a completed restore back onto the same target through the normal run engine (checks, countdown, progress, and a new safety copy so the undo can be undone); refused when something was restored after it or the copy is gone - a safety copy is accepted as a source only for the target it was taken from (hub and agent both check) - run views carry undo and undo_available; audited as refresh.undo_started - the modal offers Undo last restore and, after a restore, Undo this restore - proven with a real agent and PostgreSQL: restore, undo back to the original contents, undo of the undo Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -84,9 +84,77 @@ pub enum StartError {
|
||||
/// What the confirmation must say: it names exactly what the person saw, including the version.
|
||||
/// `capture_job_id` of a run that restores a snapshot somebody picked instead of capturing a new one.
|
||||
const EXISTING: &str = "existing:";
|
||||
/// Same, for a run that puts a target back from the safety copy of an earlier restore.
|
||||
const UNDO: &str = "undo:";
|
||||
|
||||
/// Does this run take its snapshot from the request instead of from a capture of its own?
|
||||
fn picked_snapshot(run: &RefreshRun) -> bool {
|
||||
run.capture_job_id.starts_with(EXISTING) || run.capture_job_id.starts_with(UNDO)
|
||||
}
|
||||
|
||||
impl RefreshRun {
|
||||
/// This run put a target back from a safety copy.
|
||||
pub fn is_undo(&self) -> bool {
|
||||
self.capture_job_id.starts_with(UNDO)
|
||||
}
|
||||
}
|
||||
|
||||
/// A relation's source orbit, for checking that a snapshot belongs to it.
|
||||
/// Why a restore cannot be undone.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum UndoRefusal {
|
||||
/// No such run, not of this relation, not completed, or it took no safety copy.
|
||||
NothingToUndo,
|
||||
/// Something else was restored onto the target after it. Undoing an older restore would also undo that.
|
||||
NotTheLatest,
|
||||
}
|
||||
|
||||
impl Db {
|
||||
/// The safety copy that undoes `run`, if the run is a completed restore of this relation and the newest
|
||||
/// restore of its target. Whether the copy still exists in the vault is for the caller to check.
|
||||
pub fn undo_candidate(
|
||||
&self,
|
||||
run: &str,
|
||||
relation: &str,
|
||||
) -> Result<std::result::Result<Uuid, UndoRefusal>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let Some(run) =
|
||||
get(&conn, run)?.filter(|r| r.relation_id == relation && r.status == "completed")
|
||||
else {
|
||||
return Ok(Err(UndoRefusal::NothingToUndo));
|
||||
};
|
||||
let Some(job) = &run.restore_job_id else {
|
||||
return Ok(Err(UndoRefusal::NothingToUndo));
|
||||
};
|
||||
let safety: Option<Option<String>> = conn
|
||||
.query_row(
|
||||
"SELECT safety_id FROM restore_jobs WHERE id = ?1 AND status = 'completed'",
|
||||
params![job],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
let Some(Some(safety)) = safety else {
|
||||
return Ok(Err(UndoRefusal::NothingToUndo));
|
||||
};
|
||||
// Jobs are numbered in the order they were queued.
|
||||
let later: bool = conn.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM restore_jobs WHERE target_id = ?1
|
||||
AND rowid > (SELECT rowid FROM restore_jobs WHERE id = ?2)
|
||||
AND status IN ('queued','running','applying','completed','recovery_required'))",
|
||||
params![run.target_id, job],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
if later {
|
||||
return Ok(Err(UndoRefusal::NotTheLatest));
|
||||
}
|
||||
Ok(Ok(Uuid::parse_str(&safety)?))
|
||||
}
|
||||
|
||||
pub fn relation_target(&self, relation: &str) -> Result<Option<String>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
Ok(relation_of(&conn, relation)?.map(|r| r.target_id))
|
||||
}
|
||||
|
||||
pub fn relation_source_orbit(&self, relation: &str) -> Result<Option<String>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
Ok(relation_of(&conn, relation)?.map(|r| r.source_orbit_id))
|
||||
@@ -282,7 +350,8 @@ impl Db {
|
||||
requester_user_id: Option<&str>,
|
||||
authorized_by: &str,
|
||||
idempotency_key: &str,
|
||||
snapshot: Option<Uuid>,
|
||||
// a snapshot to restore instead of capturing one, and whether it is an undo of an earlier restore
|
||||
snapshot: Option<(Uuid, bool)>,
|
||||
ttl_secs: i64,
|
||||
max_active: i64,
|
||||
now: i64,
|
||||
@@ -324,7 +393,8 @@ impl Db {
|
||||
// A run either takes a fresh backup first, or restores a snapshot the person picked: then it has
|
||||
// no capture job, only a placeholder, and the snapshot is recorded from the start.
|
||||
let capture = match snapshot {
|
||||
Some(_) => format!("{EXISTING}{id}"),
|
||||
Some((_, true)) => format!("{UNDO}{id}"),
|
||||
Some((_, false)) => format!("{EXISTING}{id}"),
|
||||
None => {
|
||||
let application = Uuid::parse_str(&relation.source_application_id)?;
|
||||
let Some(capture) =
|
||||
@@ -340,7 +410,7 @@ impl Db {
|
||||
authorized_by,idempotency_key,status,capture_job_id,snapshot_id,created_at,updated_at,expires_at)
|
||||
VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,'capturing',?11,?12,?13,?13,?14)",
|
||||
params![id, relation.id, relation.version, relation.source_orbit_id, relation.source_application_id, relation.target_id,
|
||||
requester, requester_user_id, authorized_by, idempotency_key, capture, snapshot.map(|s| s.to_string()), now, now + ttl_secs],
|
||||
requester, requester_user_id, authorized_by, idempotency_key, capture, snapshot.map(|(s, _)| s.to_string()), now, now + ttl_secs],
|
||||
)?;
|
||||
Ok(Ok((get(&tx, &id)?.expect("just inserted"), true)))
|
||||
})()?;
|
||||
@@ -446,7 +516,7 @@ impl Db {
|
||||
if run.status != "capturing" || run.restore_job_id.is_some() {
|
||||
return Ok(Linked::NotReady);
|
||||
}
|
||||
let snapshot = if run.capture_job_id.starts_with(EXISTING) {
|
||||
let snapshot = if picked_snapshot(&run) {
|
||||
let Some(snapshot) = run.snapshot_id.clone() else {
|
||||
return Ok(Linked::NotReady);
|
||||
};
|
||||
@@ -613,7 +683,7 @@ fn sync_in(tx: &Transaction<'_>, id: &str, now: i64) -> Result<Sync> {
|
||||
Ok(Sync::Moved(status.into()))
|
||||
};
|
||||
match run.status.as_str() {
|
||||
"capturing" if run.capture_job_id.starts_with(EXISTING) => {
|
||||
"capturing" if picked_snapshot(&run) => {
|
||||
// Nothing to capture: the snapshot was chosen when the run was started.
|
||||
match (&run.snapshot_id, &run.restore_job_id) {
|
||||
(Some(snapshot), None) => Ok(Sync::NeedRestore(snapshot.clone())),
|
||||
|
||||
Reference in New Issue
Block a user