chore: rustfmt the workspace and make clippy -D warnings pass
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -43,10 +43,23 @@ const COLUMNS: &str = "id,relation_id,relation_version,source_orbit_id,source_ap
|
||||
|
||||
fn read_run(r: &rusqlite::Row<'_>) -> rusqlite::Result<RefreshRun> {
|
||||
Ok(RefreshRun {
|
||||
id: r.get(0)?, relation_id: r.get(1)?, relation_version: r.get(2)?, source_orbit_id: r.get(3)?, source_application_id: r.get(4)?,
|
||||
target_id: r.get(5)?, requester: r.get(6)?, requester_user_id: r.get(7)?, authorized_by: r.get(8)?, status: r.get(9)?,
|
||||
blocked_reason: r.get(10)?, capture_job_id: r.get(11)?, snapshot_id: r.get(12)?, restore_job_id: r.get(13)?,
|
||||
created_at: r.get(14)?, updated_at: r.get(15)?, expires_at: r.get(16)?,
|
||||
id: r.get(0)?,
|
||||
relation_id: r.get(1)?,
|
||||
relation_version: r.get(2)?,
|
||||
source_orbit_id: r.get(3)?,
|
||||
source_application_id: r.get(4)?,
|
||||
target_id: r.get(5)?,
|
||||
requester: r.get(6)?,
|
||||
requester_user_id: r.get(7)?,
|
||||
authorized_by: r.get(8)?,
|
||||
status: r.get(9)?,
|
||||
blocked_reason: r.get(10)?,
|
||||
capture_job_id: r.get(11)?,
|
||||
snapshot_id: r.get(12)?,
|
||||
restore_job_id: r.get(13)?,
|
||||
created_at: r.get(14)?,
|
||||
updated_at: r.get(15)?,
|
||||
expires_at: r.get(16)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -105,25 +118,52 @@ pub(super) fn migrate(conn: &Connection) -> Result<()> {
|
||||
}
|
||||
|
||||
fn get(conn: &Connection, id: &str) -> Result<Option<RefreshRun>> {
|
||||
Ok(conn.query_row(&format!("SELECT {COLUMNS} FROM refresh_runs WHERE id=?1"), params![id], read_run).optional()?)
|
||||
Ok(conn
|
||||
.query_row(
|
||||
&format!("SELECT {COLUMNS} FROM refresh_runs WHERE id=?1"),
|
||||
params![id],
|
||||
read_run,
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
/// The run that owns a restore job, if the job was queued by a refresh. Admin restores have none.
|
||||
pub(super) fn run_for_restore(conn: &Connection, restore_job: &str) -> Result<Option<RefreshRun>> {
|
||||
Ok(conn.query_row(&format!("SELECT {COLUMNS} FROM refresh_runs WHERE restore_job_id=?1"), params![restore_job], read_run).optional()?)
|
||||
Ok(conn
|
||||
.query_row(
|
||||
&format!("SELECT {COLUMNS} FROM refresh_runs WHERE restore_job_id=?1"),
|
||||
params![restore_job],
|
||||
read_run,
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
fn relation_of(conn: &Connection, id: &str) -> Result<Option<Relation>> {
|
||||
Ok(conn.query_row(&format!("SELECT {RELATION_COLUMNS} FROM orbit_relations WHERE id=?1"), params![id], read_relation).optional()?)
|
||||
Ok(conn
|
||||
.query_row(
|
||||
&format!("SELECT {RELATION_COLUMNS} FROM orbit_relations WHERE id=?1"),
|
||||
params![id],
|
||||
read_relation,
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
fn names(conn: &Connection, relation: &Relation) -> rusqlite::Result<(String, String)> {
|
||||
let source: String = conn
|
||||
.query_row("SELECT name FROM orbits WHERE id=?1", params![relation.source_orbit_id], |r| r.get(0))
|
||||
.query_row(
|
||||
"SELECT name FROM orbits WHERE id=?1",
|
||||
params![relation.source_orbit_id],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?
|
||||
.unwrap_or_default();
|
||||
let target_profile: Option<String> =
|
||||
conn.query_row("SELECT profile FROM restore_targets WHERE id=?1", params![relation.target_id], |r| r.get(0)).optional()?;
|
||||
let target_profile: Option<String> = conn
|
||||
.query_row(
|
||||
"SELECT profile FROM restore_targets WHERE id=?1",
|
||||
params![relation.target_id],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
let target = target_profile
|
||||
.and_then(|p| serde_json::from_str::<serde_json::Value>(&p).ok())
|
||||
.and_then(|p| p["name"].as_str().map(str::to_string))
|
||||
@@ -133,8 +173,14 @@ fn names(conn: &Connection, relation: &Relation) -> rusqlite::Result<(String, St
|
||||
|
||||
/// Why this run must not go on, if anything has changed since it was started. Nothing here is cached:
|
||||
/// the relation, the requester and the grant that authorised the run are read again each time.
|
||||
pub(super) fn invalid_reason(conn: &Connection, run: &RefreshRun, now: i64) -> Result<Option<&'static str>> {
|
||||
let Some(relation) = relation_of(conn, &run.relation_id)? else { return Ok(Some("relation_ended")) };
|
||||
pub(super) fn invalid_reason(
|
||||
conn: &Connection,
|
||||
run: &RefreshRun,
|
||||
now: i64,
|
||||
) -> Result<Option<&'static str>> {
|
||||
let Some(relation) = relation_of(conn, &run.relation_id)? else {
|
||||
return Ok(Some("relation_ended"));
|
||||
};
|
||||
if !relation.active {
|
||||
return Ok(Some("relation_ended"));
|
||||
}
|
||||
@@ -142,7 +188,11 @@ pub(super) fn invalid_reason(conn: &Connection, run: &RefreshRun, now: i64) -> R
|
||||
return Ok(Some("relation_unusable"));
|
||||
}
|
||||
if let Some(user) = &run.requester_user_id {
|
||||
let active: bool = conn.query_row("SELECT EXISTS(SELECT 1 FROM users WHERE id=?1 AND active=1)", params![user], |r| r.get(0))?;
|
||||
let active: bool = conn.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM users WHERE id=?1 AND active=1)",
|
||||
params![user],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
if !active {
|
||||
return Ok(Some("user_disabled"));
|
||||
}
|
||||
@@ -163,7 +213,13 @@ pub(super) fn invalid_reason(conn: &Connection, run: &RefreshRun, now: i64) -> R
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn finish(conn: &Connection, run: &str, status: &str, reason: Option<&str>, now: i64) -> Result<()> {
|
||||
fn finish(
|
||||
conn: &Connection,
|
||||
run: &str,
|
||||
status: &str,
|
||||
reason: Option<&str>,
|
||||
now: i64,
|
||||
) -> Result<()> {
|
||||
conn.execute(
|
||||
"UPDATE refresh_runs SET status=?2, blocked_reason=?3, updated_at=?4 WHERE id=?1 AND status IN ('capturing','restoring','recovery_required')",
|
||||
params![run, status, reason, now],
|
||||
@@ -180,11 +236,27 @@ pub(super) fn block(conn: &Connection, run: &RefreshRun, reason: &str, now: i64)
|
||||
params![job, now],
|
||||
)?;
|
||||
}
|
||||
finish(conn, &run.id, if reason == "expired" { "expired" } else { "blocked" }, Some(reason), now)
|
||||
finish(
|
||||
conn,
|
||||
&run.id,
|
||||
if reason == "expired" {
|
||||
"expired"
|
||||
} else {
|
||||
"blocked"
|
||||
},
|
||||
Some(reason),
|
||||
now,
|
||||
)
|
||||
}
|
||||
|
||||
fn restore_status(conn: &Connection, job: &str) -> Result<Option<String>> {
|
||||
Ok(conn.query_row("SELECT status FROM restore_jobs WHERE id=?1", params![job], |r| r.get(0)).optional()?)
|
||||
Ok(conn
|
||||
.query_row(
|
||||
"SELECT status FROM restore_jobs WHERE id=?1",
|
||||
params![job],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
impl Db {
|
||||
@@ -210,9 +282,15 @@ impl Db {
|
||||
.query_row(&format!("SELECT {COLUMNS} FROM refresh_runs WHERE requester=?1 AND idempotency_key=?2"), params![requester, idempotency_key], read_run)
|
||||
.optional()?;
|
||||
if let Some(run) = existing {
|
||||
return Ok(if run.relation_id == relation_id { Ok((run, false)) } else { Err(StartError::KeyReused) });
|
||||
return Ok(if run.relation_id == relation_id {
|
||||
Ok((run, false))
|
||||
} else {
|
||||
Err(StartError::KeyReused)
|
||||
});
|
||||
}
|
||||
let Some(relation) = relation_of(&tx, relation_id)?.filter(|r| r.active) else { return Ok(Err(StartError::UnknownRelation)) };
|
||||
let Some(relation) = relation_of(&tx, relation_id)?.filter(|r| r.active) else {
|
||||
return Ok(Err(StartError::UnknownRelation));
|
||||
};
|
||||
if let Some(reason) = unusable(&tx, &relation, now)? {
|
||||
return Ok(Err(StartError::Unusable(reason)));
|
||||
}
|
||||
@@ -231,7 +309,8 @@ impl Db {
|
||||
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 {
|
||||
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();
|
||||
@@ -258,19 +337,28 @@ impl Db {
|
||||
pub fn active_refresh_runs(&self) -> Result<Vec<RefreshRun>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare(&format!("SELECT {COLUMNS} FROM refresh_runs WHERE status IN ('capturing','restoring','recovery_required') ORDER BY created_at"))?;
|
||||
let rows = stmt.query_map([], read_run)?.collect::<rusqlite::Result<_>>()?;
|
||||
let rows = stmt
|
||||
.query_map([], read_run)?
|
||||
.collect::<rusqlite::Result<_>>()?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
/// Recent runs (all of them, or only those of one requester / a set of relations), newest first.
|
||||
pub fn refresh_runs(&self, requester_user: Option<&str>, relations: Option<&[String]>, limit: i64) -> Result<Vec<RefreshRun>> {
|
||||
pub fn refresh_runs(
|
||||
&self,
|
||||
requester_user: Option<&str>,
|
||||
relations: Option<&[String]>,
|
||||
limit: i64,
|
||||
) -> Result<Vec<RefreshRun>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare(&format!(
|
||||
"SELECT {COLUMNS} FROM refresh_runs WHERE (?1 IS NULL AND ?2 IS NULL) OR requester_user_id = ?1
|
||||
OR relation_id IN (SELECT value FROM json_each(?2)) ORDER BY created_at DESC, rowid DESC LIMIT ?3"
|
||||
))?;
|
||||
let relations_json = relations.map(serde_json::to_string).transpose()?;
|
||||
let rows = stmt.query_map(params![requester_user, relations_json, limit], read_run)?.collect::<rusqlite::Result<_>>()?;
|
||||
let rows = stmt
|
||||
.query_map(params![requester_user, relations_json, limit], read_run)?
|
||||
.collect::<rusqlite::Result<_>>()?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
@@ -278,10 +366,20 @@ impl Db {
|
||||
pub fn refresh_progress(&self, run: &RefreshRun) -> Result<Progress> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let capture: Option<(String, String, i64)> = conn
|
||||
.query_row("SELECT status, phase, updated_at FROM capture_jobs WHERE id=?1", params![run.capture_job_id], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))
|
||||
.query_row(
|
||||
"SELECT status, phase, updated_at FROM capture_jobs WHERE id=?1",
|
||||
params![run.capture_job_id],
|
||||
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let restore: Option<(String, String)> = match &run.restore_job_id {
|
||||
Some(job) => conn.query_row("SELECT status, phase FROM restore_jobs WHERE id=?1", params![job], |r| Ok((r.get(0)?, r.get(1)?))).optional()?,
|
||||
Some(job) => conn
|
||||
.query_row(
|
||||
"SELECT status, phase FROM restore_jobs WHERE id=?1",
|
||||
params![job],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)
|
||||
.optional()?,
|
||||
None => None,
|
||||
};
|
||||
let relation = relation_of(&conn, &run.relation_id)?;
|
||||
@@ -311,18 +409,32 @@ impl Db {
|
||||
|
||||
/// Queue the restore of the run's own snapshot. Everything is checked again inside the transaction, the
|
||||
/// restore job is linked only if the run does not have one yet, and a second call changes nothing.
|
||||
pub fn enqueue_refresh_restore(&self, id: &str, artifact: &RestoreArtifact, identity_clash: bool, now: i64) -> Result<Linked> {
|
||||
pub fn enqueue_refresh_restore(
|
||||
&self,
|
||||
id: &str,
|
||||
artifact: &RestoreArtifact,
|
||||
identity_clash: bool,
|
||||
now: i64,
|
||||
) -> Result<Linked> {
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
let outcome = (|| -> Result<Linked> {
|
||||
let Some(run) = get(&tx, id)? else { return Ok(Linked::NotReady) };
|
||||
let Some(run) = get(&tx, id)? else {
|
||||
return Ok(Linked::NotReady);
|
||||
};
|
||||
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)?)))
|
||||
.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 Some((status, Some(snapshot))) = capture.filter(|c| c.0 == "completed") else {
|
||||
return Ok(Linked::NotReady);
|
||||
};
|
||||
let _ = status;
|
||||
if let Some(reason) = invalid_reason(&tx, &run, now)? {
|
||||
block(&tx, &run, reason, now)?;
|
||||
@@ -334,7 +446,13 @@ impl Db {
|
||||
}
|
||||
let snapshot = Uuid::parse_str(&snapshot)?;
|
||||
let job = super::restore::enqueue_restore_in(
|
||||
&tx, snapshot, Uuid::parse_str(&run.source_application_id)?, Uuid::parse_str(&run.target_id)?, &run.requester, artifact, now,
|
||||
&tx,
|
||||
snapshot,
|
||||
Uuid::parse_str(&run.source_application_id)?,
|
||||
Uuid::parse_str(&run.target_id)?,
|
||||
&run.requester,
|
||||
artifact,
|
||||
now,
|
||||
)?;
|
||||
let Some(job) = job else {
|
||||
block(&tx, &run, "target_rejected", now)?;
|
||||
@@ -366,17 +484,25 @@ impl Db {
|
||||
}
|
||||
|
||||
/// Cancel a run before anything has been applied. `Err` carries why it could not be.
|
||||
pub fn cancel_refresh_run(&self, id: &str, now: i64) -> Result<std::result::Result<RefreshRun, CancelError>> {
|
||||
pub fn cancel_refresh_run(
|
||||
&self,
|
||||
id: &str,
|
||||
now: i64,
|
||||
) -> Result<std::result::Result<RefreshRun, CancelError>> {
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
let result = (|| -> Result<std::result::Result<RefreshRun, CancelError>> {
|
||||
let Some(run) = get(&tx, id)? else { return Ok(Err(CancelError::Unknown)) };
|
||||
let Some(run) = get(&tx, id)? else {
|
||||
return Ok(Err(CancelError::Unknown));
|
||||
};
|
||||
if !ACTIVE.contains(&run.status.as_str()) || run.status == "recovery_required" {
|
||||
return Ok(Err(CancelError::TooLate));
|
||||
}
|
||||
if let Some(job) = &run.restore_job_id {
|
||||
match restore_status(&tx, job)?.as_deref() {
|
||||
Some("applying") | Some("recovery_required") | Some("completed") => return Ok(Err(CancelError::TooLate)),
|
||||
Some("applying") | Some("recovery_required") | Some("completed") => {
|
||||
return Ok(Err(CancelError::TooLate))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -426,7 +552,9 @@ pub struct Progress {
|
||||
}
|
||||
|
||||
fn sync_in(tx: &Transaction<'_>, id: &str, now: i64) -> Result<Sync> {
|
||||
let Some(run) = get(tx, id)? else { return Ok(Sync::Unchanged) };
|
||||
let Some(run) = get(tx, id)? else {
|
||||
return Ok(Sync::Unchanged);
|
||||
};
|
||||
if !ACTIVE.contains(&run.status.as_str()) {
|
||||
return Ok(Sync::Unchanged);
|
||||
}
|
||||
@@ -435,11 +563,21 @@ fn sync_in(tx: &Transaction<'_>, id: &str, now: i64) -> Result<Sync> {
|
||||
None => None,
|
||||
};
|
||||
// Until the restore starts applying, any change in what authorised the run stops it.
|
||||
let past_the_point_of_no_return = matches!(restore.as_deref(), Some("applying" | "recovery_required" | "completed" | "failed" | "reconciled"));
|
||||
let past_the_point_of_no_return = matches!(
|
||||
restore.as_deref(),
|
||||
Some("applying" | "recovery_required" | "completed" | "failed" | "reconciled")
|
||||
);
|
||||
if !past_the_point_of_no_return {
|
||||
if let Some(reason) = invalid_reason(tx, &run, now)? {
|
||||
block(tx, &run, reason, now)?;
|
||||
return Ok(Sync::Moved(if reason == "expired" { "expired" } else { "blocked" }.into()));
|
||||
return Ok(Sync::Moved(
|
||||
if reason == "expired" {
|
||||
"expired"
|
||||
} else {
|
||||
"blocked"
|
||||
}
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
let move_to = |status: &str| -> Result<Sync> {
|
||||
@@ -449,11 +587,19 @@ fn sync_in(tx: &Transaction<'_>, id: &str, now: i64) -> Result<Sync> {
|
||||
match run.status.as_str() {
|
||||
"capturing" => {
|
||||
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)?)))
|
||||
.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()?;
|
||||
match capture {
|
||||
Some((status, _)) if status == "failed" => move_to("capture_failed"),
|
||||
Some((status, Some(snapshot))) if status == "completed" && run.restore_job_id.is_none() => Ok(Sync::NeedRestore(snapshot)),
|
||||
Some((status, Some(snapshot)))
|
||||
if status == "completed" && run.restore_job_id.is_none() =>
|
||||
{
|
||||
Ok(Sync::NeedRestore(snapshot))
|
||||
}
|
||||
None => move_to("capture_failed"),
|
||||
_ => Ok(Sync::Unchanged),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user