//! Candidate creation and the collapse-in-place rule (§7.1). //! //! One review object per pkgbase at a time. A newer detection **collapses into** //! the open review rather than queueing beside it, and the review's `base` is //! fixed at creation to the pkgbase's `current_accepted_release` — that is what //! makes "diff against the last accepted version, never the last seen one" (I2) //! true no matter how many detections arrive while nobody is looking. //! //! This module is the *rule*, not the SQL: every statement it needs lives in //! `db::candidates` and is called through the transaction handle, so the whole //! decision is atomic and the schema still has one owner. use crate::db::{self, candidates as sql, Db, Pkgbase, ReleaseRow}; use crate::error::Result; pub use crate::db::candidates::{ all as all_candidates, by_id as candidate_by_id, mark_viewed, open_reviews_for, pending as pending_candidates, reopen_rejected, set_diff_digest, set_missing_deps, set_risk, NewCandidate, }; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Upsert { /// A new review row was opened. Created { id: i64, kind: String }, /// The open review was updated in place; no new row, `base` untouched (I7). Collapsed { id: i64, revision: i64 }, /// A human had already seen the old diff, so it was frozen as `superseded` /// and a fresh review opened (decision 8). Superseded { old: i64, new: i64 }, /// This exact identity was rejected before; do not nag (§7.1). PreviouslyRejected, /// Nothing changed relative to what is already recorded. Unchanged, } /// Insert-or-collapse a candidate for `pb`. /// /// `accepted` is the pkgbase's current accepted release, or `None` when this is /// a first introduction (§7.1b). pub fn upsert_candidate(db: &Db, pb: &Pkgbase, accepted: Option<&ReleaseRow>, nc: &NewCandidate) -> Result { let kind = if accepted.is_some() { db::candidate_kind::NEW_VERSION } else { db::candidate_kind::FIRST_INTRODUCTION }; let base = accepted.map(|a| a.id); let pkgbase_id = pb.id; let nc = nc.clone(); db.writer.tx(move |tx| { if let Some(open) = sql::open_in(tx, pkgbase_id)? { if open.identity_hash == nc.identity_hash { return Ok(Upsert::Unchanged); } if !open.viewed() { // Collapse in place: same base, no new row, revision bumped. let revision = sql::collapse_in(tx, &open, &nc)?; return Ok(Upsert::Collapsed { id: open.id, revision }); } // A human saw the old diff: freeze it for the audit trail and open a // fresh review against the same base. sql::supersede_in(tx, open.id)?; let new_id = sql::insert_in(tx, pkgbase_id, base, kind, &nc)?; return Ok(Upsert::Superseded { old: open.id, new: new_id }); } // No open review. A previously rejected identity must not be re-raised. if sql::rejected_before_in(tx, pkgbase_id, &nc.identity_hash)? { return Ok(Upsert::PreviouslyRejected); } // Same identity already approved/recorded: nothing to do. if sql::recorded_before_in(tx, pkgbase_id, &nc.identity_hash)? { return Ok(Upsert::Unchanged); } let id = sql::insert_in(tx, pkgbase_id, base, kind, &nc)?; Ok(Upsert::Created { id, kind: kind.to_string() }) }) } /// Convenience used by the watcher: the accepted release of a pkgbase row. pub fn accepted_for(db: &Db, pb: &Pkgbase) -> Result> { match pb.current_accepted_release_id { Some(id) => db::release_by_id(db, id), None => Ok(None), } }