diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 16:49:40 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 16:49:40 +0000 |
| commit | 41fa1684de8eb13c5b76c3a3e174fece04ac5afa (patch) | |
| tree | 491a2faf2dd2314f537300ccaf1e0150b54d1123 /src/db/candidates.rs | |
| parent | 101f077200e80c7869e4549db645ed94b01acb1b (diff) | |
| download | aur-build-41fa1684de8eb13c5b76c3a3e174fece04ac5afa.tar.gz aur-build-41fa1684de8eb13c5b76c3a3e174fece04ac5afa.zip | |
db: the review, step, release and subscription states are enums too
M-8 named four more machines after `build_state`, and each had the same defect
in miniature: `&str` constants, a writer that took any `&str`, and a rule that
lived only in the caller's discipline. They are `CandidateState`, `StepState`,
`ReleaseState` and `SubscriptionState` now, all parsed through one
`db::state_col` at the row boundary — a column value the code cannot name fails
the read rather than becoming a state it did not ask for.
The one that mattered is the review machine. `resolve_in(tx, id, &str)` would
move a candidate to any state from any state, so an approval racing a
rejection, or a stale worker resolving a review that had already been
superseded, silently overwrote what a human had decided — the direct route to
breaking I2 and I3 with no symptom anywhere. It is a compare-and-swap on
`CandidateState` now: a decided review is only ever frozen, never re-decided,
and a refusal names both states. `supersede_in` goes through it, so the freeze
cannot resurrect a superseded review either.
`StepState` gives I5 teeth. `begin_step` upserted over any existing checkpoint,
including one recorded `succeeded`, so a resume that asked to re-run a finished
step could rewrite the very row that told it to skip; the upsert now refuses to
touch a done checkpoint and `finish_step` is a CAS. The blocked path
(`running → pending`) and the reaper's `running → interrupted` are unchanged,
and both are in the table.
Two behaviour changes, both for the better and both tested: the UI's pause
button now says "orphaned: nothing to pause" instead of rewriting an orphaned
subscription back to a pausable state, and a step checkpoint that is already
done makes `begin_step` an error rather than a quiet overwrite.
Five tests in `tests/state_machine.rs` cover the review CAS in both directions,
the frozen checkpoint, the interrupted/blocked transitions, and the unknown
value in a candidate and a step row.
Diffstat (limited to 'src/db/candidates.rs')
| -rw-r--r-- | src/db/candidates.rs | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/src/db/candidates.rs b/src/db/candidates.rs index e41fb94..63c089f 100644 --- a/src/db/candidates.rs +++ b/src/db/candidates.rs @@ -10,7 +10,7 @@ use rusqlite::{params, OptionalExtension, Row, Transaction}; -use crate::db::{CandidateRow, Db}; +use crate::db::{CandidateRow, CandidateState, Db}; use crate::error::{Error, Result}; use crate::time; @@ -41,7 +41,7 @@ fn row_to_candidate(r: &Row<'_>) -> rusqlite::Result<CandidateRow> { risk_level: r.get(14)?, risk_signals_json: r.get(15)?, missing_deps_json: r.get(16)?, - state: r.get(17)?, + state: crate::db::state_col::<CandidateState>(17, &r.get::<_, String>(17)?)?, detected_at: r.get(18)?, resolved_at: r.get(19)?, pkgbase: r.get(20)?, @@ -92,7 +92,7 @@ impl OpenReview { pub struct ReviewSubject { pub id: i64, pub pkgbase_id: i64, - pub state: String, + pub state: CandidateState, pub identity_hash: String, pub diff_digest: String, /// The release this review was opened against (I2). @@ -115,7 +115,7 @@ pub fn review_subject_in(tx: &Transaction<'_>, candidate_id: i64) -> Result<Opti Ok(ReviewSubject { id: r.get(0)?, pkgbase_id: r.get(1)?, - state: r.get(2)?, + state: crate::db::state_col::<CandidateState>(2, &r.get::<_, String>(2)?)?, identity_hash: r.get(3)?, diff_digest: r.get(4)?, base_release_id: r.get(5)?, @@ -129,16 +129,42 @@ pub fn review_subject_in(tx: &Transaction<'_>, candidate_id: i64) -> Result<Opti } /// Take a candidate out of the open state: approved, rejected or superseded. -pub fn resolve_in(tx: &Transaction<'_>, id: i64, state: &str, at: &str) -> Result<()> { - tx.execute("UPDATE candidate SET state=?1, resolved_at=?2 WHERE id=?3", params![state, at, id])?; +/// +/// The write is a compare-and-swap on [`CandidateState`]: a decided review is +/// never re-decided, so an approval racing a rejection — or a stale worker +/// resolving a review that has since been superseded — fails instead of +/// silently overwriting what a human did. +pub fn resolve_in(tx: &Transaction<'_>, id: i64, to: CandidateState, at: &str) -> Result<()> { + let froms = CandidateState::predecessors(to); + let placeholders = froms.iter().map(|_| "?").collect::<Vec<_>>().join(","); + let sql = format!( + "UPDATE candidate SET state=?1, resolved_at=?2 WHERE id=?3 AND state IN ({placeholders})" + ); + let state = to.as_str(); + let mut args: Vec<&dyn rusqlite::ToSql> = vec![&state, &at, &id]; + for f in &froms { + args.push(f); + } + let n = tx.execute(&sql, rusqlite::params_from_iter(args))?; + if n == 0 { + let was: Option<String> = tx + .query_row("SELECT state FROM candidate WHERE id=?1", params![id], |r| r.get(0)) + .optional()?; + return Err(match was { + Some(w) => Error::conflict(format!("illegal candidate transition {w} → {to} for review {id}")), + None => Error::not_found(format!("no candidate {id} to resolve")), + }); + } Ok(()) } /// A candidate's state, read inside a transaction (the reject path needs it /// before it writes). -pub fn state_in(tx: &Transaction<'_>, id: i64) -> Result<Option<String>> { +pub fn state_in(tx: &Transaction<'_>, id: i64) -> Result<Option<CandidateState>> { Ok(tx - .query_row("SELECT state FROM candidate WHERE id=?1", params![id], |r| r.get::<_, String>(0)) + .query_row("SELECT state FROM candidate WHERE id=?1", params![id], |r| { + crate::db::state_col::<CandidateState>(0, &r.get::<_, String>(0)?) + }) .optional()?) } @@ -202,11 +228,8 @@ pub fn collapse_in(tx: &mut Transaction<'_>, open: &OpenReview, nc: &NewCandidat /// Freeze a reviewed candidate as `superseded` so a fresh review can open. pub fn supersede_in(tx: &Transaction<'_>, id: i64) -> Result<()> { - tx.execute( - "UPDATE candidate SET state='superseded', resolved_at=?1 WHERE id=?2", - params![time::now(), id], - )?; - Ok(()) + let at = time::now(); + resolve_in(tx, id, CandidateState::Superseded, &at) } /// Has this exact identity been rejected before? If so the watcher stays quiet |
