feat: restore inside the Vault; the Refresh page and file export UI are gone
- POST /refresh-runs accepts snapshot_id: restore a snapshot somebody picked (a restorable capture of the relation's own source, never a safety copy or another orbit's) through the same run engine - refresh-relations carries source_orbit_id; snapshots carry kind (capture|safety), and the UI hides safety copies - snapshot detail: Restore replaces Export file on hub (disabled with No restore point without a relation); modal with a restore point selector, a ten second countdown with Abort, and a four step progress view - administrators keep Other registered target; the Refresh menu and refresh-only screen are removed - proven with a real agent and PostgreSQL in the browser: capture, restore to Development, abort, no relation Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -82,6 +82,17 @@ 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:";
|
||||
|
||||
/// A relation's source orbit, for checking that a snapshot belongs to it.
|
||||
impl Db {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn confirmation_text(source: &str, target: &str, version: i64) -> String {
|
||||
format!("REFRESH {source} TO {target} v{version}")
|
||||
}
|
||||
@@ -271,6 +282,7 @@ impl Db {
|
||||
requester_user_id: Option<&str>,
|
||||
authorized_by: &str,
|
||||
idempotency_key: &str,
|
||||
snapshot: Option<Uuid>,
|
||||
ttl_secs: i64,
|
||||
max_active: i64,
|
||||
now: i64,
|
||||
@@ -308,18 +320,27 @@ impl Db {
|
||||
if running >= max_active {
|
||||
return Ok(Err(StartError::TooManyRuns));
|
||||
}
|
||||
let application = Uuid::parse_str(&relation.source_application_id)?;
|
||||
let Some(capture) = super::jobs::insert_capture_job(&tx, application, requester, now)?
|
||||
else {
|
||||
return Ok(Err(StartError::SourceUnavailable));
|
||||
};
|
||||
let id = Uuid::new_v4().to_string();
|
||||
// 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}"),
|
||||
None => {
|
||||
let application = Uuid::parse_str(&relation.source_application_id)?;
|
||||
let Some(capture) =
|
||||
super::jobs::insert_capture_job(&tx, application, requester, now)?
|
||||
else {
|
||||
return Ok(Err(StartError::SourceUnavailable));
|
||||
};
|
||||
capture.to_string()
|
||||
}
|
||||
};
|
||||
tx.execute(
|
||||
"INSERT INTO refresh_runs(id,relation_id,relation_version,source_orbit_id,source_application_id,target_id,requester,requester_user_id,
|
||||
authorized_by,idempotency_key,status,capture_job_id,created_at,updated_at,expires_at)
|
||||
VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,'capturing',?11,?12,?12,?13)",
|
||||
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.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)))
|
||||
})()?;
|
||||
@@ -425,17 +446,24 @@ impl Db {
|
||||
if run.status != "capturing" || run.restore_job_id.is_some() {
|
||||
return Ok(Linked::NotReady);
|
||||
}
|
||||
let capture: Option<(String, Option<String>)> = tx
|
||||
.query_row(
|
||||
"SELECT status, snapshot_id FROM capture_jobs WHERE id=?1",
|
||||
params![run.capture_job_id],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let Some((status, Some(snapshot))) = capture.filter(|c| c.0 == "completed") else {
|
||||
return Ok(Linked::NotReady);
|
||||
let snapshot = if run.capture_job_id.starts_with(EXISTING) {
|
||||
let Some(snapshot) = run.snapshot_id.clone() else {
|
||||
return Ok(Linked::NotReady);
|
||||
};
|
||||
snapshot
|
||||
} else {
|
||||
let capture: Option<(String, Option<String>)> = tx
|
||||
.query_row(
|
||||
"SELECT status, snapshot_id FROM capture_jobs WHERE id=?1",
|
||||
params![run.capture_job_id],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let Some((_, Some(snapshot))) = capture.filter(|c| c.0 == "completed") else {
|
||||
return Ok(Linked::NotReady);
|
||||
};
|
||||
snapshot
|
||||
};
|
||||
let _ = status;
|
||||
if let Some(reason) = invalid_reason(&tx, &run, now)? {
|
||||
block(&tx, &run, reason, now)?;
|
||||
return Ok(Linked::Blocked(reason));
|
||||
@@ -585,6 +613,13 @@ 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) => {
|
||||
// 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())),
|
||||
_ => Ok(Sync::Unchanged),
|
||||
}
|
||||
}
|
||||
"capturing" => {
|
||||
let capture: Option<(String, Option<String>)> = tx
|
||||
.query_row(
|
||||
|
||||
Reference in New Issue
Block a user