diff --git a/crates/tessera-hub/src/api/restore.rs b/crates/tessera-hub/src/api/restore.rs index 87bf06c..2726a59 100644 --- a/crates/tessera-hub/src/api/restore.rs +++ b/crates/tessera-hub/src/api/restore.rs @@ -5,7 +5,7 @@ use super::{ use crate::state::HubState; use axum::{ body::Body, - extract::{rejection::JsonRejection, ConnectInfo, Path, State}, + extract::{rejection::JsonRejection, ConnectInfo, Path, Query, State}, http::{header, HeaderMap, StatusCode}, response::{IntoResponse, Response}, routing::{get, post}, @@ -29,7 +29,7 @@ pub(super) fn routes() -> Router { .route("/api/v1/restore-targets", post(register)) .route("/api/v1/capture-profiles/:id/restore-targets", get(targets)) .route("/api/v1/snapshots/:id/restore-targets", get(snapshot_targets)) - .route("/api/v1/restore-jobs", post(enqueue)) + .route("/api/v1/restore-jobs", post(enqueue).get(recent)) .route("/api/v1/snapshots/:id/restore-jobs", get(history)) .route("/api/v1/restore-jobs/:id", get(job).delete(cancel)) .route("/api/v1/restore-jobs/:id/reconcile", post(reconcile)) @@ -222,6 +222,26 @@ async fn history( .map_err(internal) } +#[derive(Deserialize)] +struct Recent { + limit: Option, +} + +/// The newest restore jobs across all snapshots. The activity log reads this instead of asking snapshot by snapshot. +async fn recent( + ConnectInfo(peer): ConnectInfo, + State(state): State, + headers: HeaderMap, + Query(query): Query, +) -> Result>, StatusCode> { + require_admin(&state, peer, &headers)?; + state + .db + .restore_recent(query.limit.unwrap_or(50).clamp(1, 200), now()) + .map(Json) + .map_err(internal) +} + async fn cancel( ConnectInfo(peer): ConnectInfo, State(state): State, diff --git a/crates/tessera-hub/src/db/restore.rs b/crates/tessera-hub/src/db/restore.rs index ee3824e..0964db5 100644 --- a/crates/tessera-hub/src/db/restore.rs +++ b/crates/tessera-hub/src/db/restore.rs @@ -181,6 +181,15 @@ impl Db { Ok(rows.collect::>()?) } + /// The newest restore jobs across every snapshot, newest first, for the activity log. + pub fn restore_recent(&self, limit: u32, now: i64) -> Result> { + let conn = self.conn.lock().unwrap(); + expire_destructive(&conn, now)?; + let mut stmt = conn.prepare(&format!("{VIEW} ORDER BY j.rowid DESC LIMIT ?1"))?; + let rows = stmt.query_map([limit], read_job)?; + Ok(rows.collect::>()?) + } + pub fn snapshot_in_restore(&self, id: Uuid) -> Result { let conn = self.conn.lock().unwrap(); Ok(conn.query_row( @@ -544,6 +553,9 @@ mod tests { .unwrap()); let interrupted = db.restore_job(job.id, late).unwrap().unwrap(); assert_eq!(interrupted.status, "recovery_required"); + let recent = db.restore_recent(10, late).unwrap(); + assert_eq!(recent.iter().map(|item| item.id).collect::>(), vec![next.id, job.id]); + assert_eq!(db.restore_recent(1, late).unwrap().len(), 1); assert_eq!(interrupted.safety_snapshot_id, Some(safety)); assert!(db.snapshot_in_restore(snapshot).unwrap()); assert!(db.snapshot_in_restore(safety).unwrap()); diff --git a/docs/DATABASE-CAPTURE.md b/docs/DATABASE-CAPTURE.md index 5c2c8e6..f70080c 100644 --- a/docs/DATABASE-CAPTURE.md +++ b/docs/DATABASE-CAPTURE.md @@ -135,7 +135,7 @@ Use `{"reset":"recreate_database"}` to recreate the registered PostgreSQL databa A MySQL target uses `"engine":"mysql"` and the same `connection` fields as a direct or SSH MySQL source. `recreate_database` drops and recreates the registered database before applying SQL. `use_target_definer` rewrites supported dump DEFINER clauses to the connected MySQL user. -Choose the snapshot and target in **Restore**. The required confirmation includes both UUIDs. The hub verifies the safety archive before issuing the single-use apply permit. If the job reports `recovery_required`, stop any old apply process and verify its database session has ended. Recover the target with the linked safety archive, verify data and permissions, then confirm reconciliation to release the lock. Session-authenticated file exports use a filename in the hub's private `exports` directory. +Open the snapshot and choose its recovery options, then pick the target in **Restore**. **Activity** lists recent restore jobs across all snapshots (`GET /api/v1/restore-jobs?limit=`, newest first, at most 200) and flags any that need reconciliation. The required confirmation includes both UUIDs. The hub verifies the safety archive before issuing the single-use apply permit. If the job reports `recovery_required`, stop any old apply process and verify its database session has ended. Recover the target with the linked safety archive, verify data and permissions, then confirm reconciliation to release the lock. Session-authenticated file exports use a filename in the hub's private `exports` directory. ## Import legacy archives