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:
@@ -39,6 +39,14 @@ test("what the person is told never contains the database's own words", () => {
|
||||
assert.equal(outcomeOf(run("restoring", "safety"), { target: "Staging" }).tone, "busy");
|
||||
});
|
||||
|
||||
test("an undo is worded as putting the target back, and its own safety copy keeps it undoable", () => {
|
||||
const done = outcomeOf({ ...run("completed"), undo: true }, { target: "Staging", orbit: "ledger", when: "Nov 15, 11:57 AM" });
|
||||
assert.equal(done.tone, "good");
|
||||
assert.match(done.title, /Staging was rolled back/);
|
||||
assert.match(done.detail, /holds what it held before that restore/);
|
||||
assert.match(outcomeOf({ ...run("restore_failed", "apply_failed"), undo: true }, { target: "Staging" }).title, /undo failed/i);
|
||||
});
|
||||
|
||||
test("restore points are named by environment, and by target only when two share one", () => {
|
||||
const rel = (id, env, name) => ({ id, target_environment: env, target_name: name, usable: true });
|
||||
assert.deepEqual(
|
||||
|
||||
@@ -135,6 +135,10 @@ export type RefreshRelation = {
|
||||
target_name: string; target_environment: Environment | null; usable: boolean; unusable_reason: string | null;
|
||||
};
|
||||
export type RefreshRun = {
|
||||
/** Put a target back from the safety copy of an earlier restore. */
|
||||
undo?: boolean;
|
||||
/** A completed restore that can still be undone (the newest of its target, safety copy still there). */
|
||||
undo_available?: boolean;
|
||||
id: string; relation_id: string; source_name: string; target_name: string; status: string; blocked_reason: string | null;
|
||||
capture_status: string | null; capture_phase: string | null; snapshot_taken_at: number | null;
|
||||
restore_status: string | null; restore_phase: string | null;
|
||||
@@ -143,13 +147,13 @@ export type RefreshRun = {
|
||||
export const fetchRefreshRelations = () => api<RefreshRelation[]>("/api/v1/refresh-relations");
|
||||
export const fetchRefreshRuns = () => api<RefreshRun[]>("/api/v1/refresh-runs");
|
||||
/** Restore `snapshot_id` onto the relation's target (or, without one, capture first and restore that). */
|
||||
export const startRefresh = (relation: RefreshRelation, idempotency_key: string, snapshot_id?: string) =>
|
||||
export const startRefresh = (relation: RefreshRelation, idempotency_key: string, pick: { snapshot_id?: string; undo_of?: string } = {}) =>
|
||||
api<RefreshRun>("/api/v1/refresh-runs", json("POST", {
|
||||
relation_id: relation.id,
|
||||
// exactly what the person was shown, so a relation changed in the meantime is refused
|
||||
confirmation: `REFRESH ${relation.source_name} TO ${relation.target_name} v${relation.version}`,
|
||||
idempotency_key,
|
||||
...(snapshot_id ? { snapshot_id } : {}),
|
||||
...(pick.undo_of ? { undo_of: pick.undo_of } : pick.snapshot_id ? { snapshot_id: pick.snapshot_id } : {}),
|
||||
}));
|
||||
export const fetchRefreshRun = (id: string) => api<RefreshRun>(`/api/v1/refresh-runs/${id}`);
|
||||
export const cancelRefresh = (id: string) => api<RefreshRun>(`/api/v1/refresh-runs/${id}`, { method: "DELETE" });
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { motion } from "motion/react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { cancelRefresh, fetchRefreshRun, refusalReason, startRefresh, type RefreshRelation, type RefreshRun, type Snapshot } from "../../api";
|
||||
import { cancelRefresh, fetchRefreshRun, fetchRefreshRuns, refusalReason, startRefresh, type RefreshRelation, type RefreshRun, type Snapshot } from "../../api";
|
||||
import { formatBytes, formatWhen } from "../../lib/format";
|
||||
import { isFinal, outcomeOf, progressOf, relationLabels, STAGES } from "../../lib/restoreRun";
|
||||
import { Button } from "../ui/Button";
|
||||
@@ -17,6 +17,7 @@ const REFUSALS: Record<string, string> = {
|
||||
relation_unusable: "This restore point cannot be used right now. Ask an administrator.",
|
||||
too_many_runs: "Too many restores are running at once. Try again in a few minutes.",
|
||||
snapshot_unusable: "This backup cannot be restored to that database.",
|
||||
nothing_to_undo: "There is nothing to undo any more: something was restored after it, or its safety copy was removed.",
|
||||
source_unavailable: "The source cannot be reached right now.",
|
||||
};
|
||||
|
||||
@@ -42,6 +43,8 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
|
||||
const [runId, setRunId] = useState<string>();
|
||||
const [refusal, setRefusal] = useState<string>();
|
||||
const [other, setOther] = useState(false);
|
||||
// Set while an undo is being started or followed: the run whose restore is being put back.
|
||||
const [undoOf, setUndoOf] = useState<string>();
|
||||
// One key per attempt: the same press can never start two restores, even if the request is retried.
|
||||
const attempt = useRef(globalThis.crypto?.randomUUID?.() ?? String(Math.random()));
|
||||
|
||||
@@ -66,7 +69,7 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
|
||||
if (left <= 0) {
|
||||
setPhase("running");
|
||||
if (!point) return;
|
||||
startRefresh(point, attempt.current, snapshot.id)
|
||||
startRefresh(point, attempt.current, undoOf ? { undo_of: undoOf } : { snapshot_id: snapshot.id })
|
||||
.then((started) => setRunId(started.id))
|
||||
.catch((error) => {
|
||||
setRefusal(REFUSALS[refusalReason(error) ?? ""] ?? "The restore could not be started.");
|
||||
@@ -77,7 +80,7 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
|
||||
}
|
||||
const timer = window.setTimeout(() => setLeft((value) => value - 1), 1000);
|
||||
return () => clearTimeout(timer);
|
||||
}, [phase, left, point, snapshot.id]);
|
||||
}, [phase, left, point, snapshot.id, undoOf]);
|
||||
|
||||
const abortable = phase === "countdown" || (phase === "running" && !finished && progress.index < 2);
|
||||
const abort = async () => {
|
||||
@@ -89,7 +92,15 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
|
||||
}
|
||||
};
|
||||
const again = () => {
|
||||
setPhase("idle"); setLeft(COUNTDOWN); setRunId(undefined); setRefusal(undefined);
|
||||
setPhase("idle"); setLeft(COUNTDOWN); setRunId(undefined); setRefusal(undefined); setUndoOf(undefined);
|
||||
attempt.current = globalThis.crypto?.randomUUID?.() ?? String(Math.random());
|
||||
};
|
||||
|
||||
// The newest restore on the chosen restore point, if it can still be undone.
|
||||
const recent = useQuery({ queryKey: ["restore-runs"], queryFn: fetchRefreshRuns, enabled: phase === "idle", retry: false, refetchInterval: 15_000 });
|
||||
const undoable = Array.isArray(recent.data) ? recent.data.find((r) => r.relation_id === point?.id && r.undo_available) : undefined;
|
||||
const beginUndo = (of: string) => {
|
||||
setRefusal(undefined); setRunId(undefined); setUndoOf(of); setLeft(COUNTDOWN); setPhase("countdown");
|
||||
attempt.current = globalThis.crypto?.randomUUID?.() ?? String(Math.random());
|
||||
};
|
||||
|
||||
@@ -132,11 +143,17 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
|
||||
</div>
|
||||
<div className="flex gap-2.5">
|
||||
<Button variant="danger" className="flex-1" disabled={!abortable} onClick={() => void abort()}>Abort</Button>
|
||||
<Button variant="primary" className="flex-1" disabled={!point || phase !== "idle"} onClick={() => { setRefusal(undefined); setLeft(COUNTDOWN); setPhase("countdown"); }}>
|
||||
<Button variant="primary" className="flex-1" disabled={!point || phase !== "idle"} onClick={() => { setRefusal(undefined); setUndoOf(undefined); setLeft(COUNTDOWN); setPhase("countdown"); }}>
|
||||
Begin restore
|
||||
</Button>
|
||||
</div>
|
||||
{refusal && <p role="alert" className="text-sm text-orbit-red">{refusal}</p>}
|
||||
{undoable && phase === "idle" && !runId && (
|
||||
<div className="restore-undo">
|
||||
<p>The last restore on {target} was {formatWhen(new Date(undoable.updated_at * 1000).toISOString())}. Its safety copy can put {target} back as it was before.</p>
|
||||
<Button onClick={() => beginUndo(undoable.id)}>Undo last restore</Button>
|
||||
</div>
|
||||
)}
|
||||
{admin && database && phase === "idle" && !runId && (
|
||||
<button type="button" className="footer-link self-start" onClick={() => setOther(true)}>Other registered target…</button>
|
||||
)}
|
||||
@@ -155,7 +172,7 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
|
||||
</svg>
|
||||
<span className="restore-ring-text"><b>{left}</b><i>seconds</i></span>
|
||||
</div>
|
||||
<p className="restore-count-note">Restoring to {target} in {left}s.<br />Abort restores nothing.</p>
|
||||
<p className="restore-count-note">{undoOf ? `Undoing the last restore on ${target} in ${left}s.` : `Restoring to ${target} in ${left}s.`}<br />Abort restores nothing.</p>
|
||||
</motion.div>
|
||||
)}
|
||||
{phase === "running" && (
|
||||
@@ -182,7 +199,10 @@ export function RestoreWizard({ snapshot, relations, admin, onBusyChange }: {
|
||||
<div role="status" className={`restore-outcome ${outcome.tone}`}>
|
||||
<p>{outcome.title}</p>
|
||||
<p>{outcome.detail}</p>
|
||||
<Button onClick={again}>{outcome.tone === "good" ? "Restore again" : "Try again"}</Button>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{run.data?.undo_available && <Button onClick={() => beginUndo(run.data!.id)}>Undo this restore</Button>}
|
||||
<Button onClick={again}>{outcome.tone === "good" ? "Restore again" : "Try again"}</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{run.isError && <p role="alert" className="text-sm text-orbit-amber">Reconnecting to the restore…</p>}
|
||||
|
||||
@@ -1219,3 +1219,6 @@ button.act-row[data-group="needs"]:hover { background: #20161a; }
|
||||
.restore-outcome.good p:first-child { color: var(--color-orbit-green, #8fd0b0); }
|
||||
.restore-outcome.bad { border-color: #6b2f26; }
|
||||
.restore-outcome.bad p:first-child { color: var(--color-orbit-red); }
|
||||
|
||||
.restore-undo { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; padding: 12px; border: 1px dashed var(--line); border-radius: 12px; }
|
||||
.restore-undo p { margin: 0; font-size: 13px; line-height: 1.5; color: var(--quiet); }
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
export const STAGES = ["Checking", "Safety copy", "Applying", "Completed"] as const;
|
||||
|
||||
type RunLike = { status: string; restore_phase: string | null; blocked_reason?: string | null };
|
||||
type RunLike = { status: string; restore_phase: string | null; blocked_reason?: string | null; undo?: boolean };
|
||||
|
||||
const STOPPED = new Set(["restore_failed", "recovery_required", "blocked", "expired", "capture_failed"]);
|
||||
|
||||
@@ -52,6 +52,17 @@ export function outcomeOf(
|
||||
names: { target: string; orbit?: string; when?: string },
|
||||
): Outcome {
|
||||
const { target } = names;
|
||||
if (run.undo) {
|
||||
switch (run.status) {
|
||||
case "completed":
|
||||
case "reconciled":
|
||||
return { tone: "good", title: `${target} was rolled back`, detail: `${target} holds what it held before that restore. A safety copy of the state you undid was kept, so this can be undone too.` };
|
||||
case "restore_failed":
|
||||
return { tone: "bad", title: "The undo failed", detail: `${target} was most likely not changed, but ask an administrator to check it before you rely on it.` };
|
||||
default:
|
||||
break; // the other outcomes read the same for an undo
|
||||
}
|
||||
}
|
||||
switch (run.status) {
|
||||
case "completed":
|
||||
case "reconciled":
|
||||
|
||||
Reference in New Issue
Block a user