summaryrefslogtreecommitdiffstats
path: root/src/review/candidates.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 15:20:13 +0000
committeraur-build <aur-build@localhost>2026-11-18 15:20:13 +0000
commit933c637de34095f1ea1ab88b02626bbf22eec7f4 (patch)
treeee7bd3d7a2b18163c0fdbe061ef53a73b74907ab /src/review/candidates.rs
parent7946e9ffc48748992f051ada15bec772b16a585d (diff)
downloadaur-build-933c637de34095f1ea1ab88b02626bbf22eec7f4.tar.gz
aur-build-933c637de34095f1ea1ab88b02626bbf22eec7f4.zip
db: the schema gets an owner — every statement moves into db/, and the writer stops handing out connections
Twelve modules outside `db/` contained hand-rolled SQL — 22 statements in `ops.rs`, 17 in `approve.rs`, 15 in `candidates.rs`, 11 in `publish.rs`, and a dozen more scattered through `watch`, `pipeline`, `http` — which meant the blast radius of a schema change was "any module", and `db/` was a directory rather than a boundary. The audit trail alone was defined in four places with three different detail payloads. Every statement now lives in `db/`. New modules `db::candidates` and `db::approvals` own their tables (including the 21-column candidate select, spelled once as `CANDIDATE_COLS` instead of four times); `db::queries` gains the metrics reads, the retention reads and writes, the publish writes (`insert_release_in`, `cas_accepted_release_in`, `remove_index_packages_in`), the staleness reads and the source-cache lookups; `db::builds` gains `set_release_in`. Where a mutation is a *decision* — the collapse rules, the approval gate, the publish compare-and-swap — the decision stays where it was and the statements are called through the transaction handle, so the atomicity is unchanged and only the SQL moved. `audit_in` is the in-transaction audit append, and `audit` is now a wrapper over it: a decision and its audit row commit together or not at all, which is what I10 actually claims. Enforcement is two-layered, because one layer is a lie. `Writer::call`, `tx` and `send` are `pub(crate)`, which keeps the crate's *users* out — the integration tests and the shim binaries — but `pub(crate)` is crate-wide, so on its own it would not stop `approve.rs` from writing SQL tomorrow. The layer that does is `tests/schema_ownership.rs`: it walks `src/` and fails the build if a statement literal or a rusqlite statement call appears outside `src/db/`. It was checked against a deliberate violation. The test harness still needs to construct states no typed API reaches, so the raw door exists but only under the `test-support` feature, which nothing but this crate's own dev-dependency enables; a production binary cannot see it.
Diffstat (limited to 'src/review/candidates.rs')
-rw-r--r--src/review/candidates.rs273
1 files changed, 21 insertions, 252 deletions
diff --git a/src/review/candidates.rs b/src/review/candidates.rs
index 566434b..7cedf4b 100644
--- a/src/review/candidates.rs
+++ b/src/review/candidates.rs
@@ -5,23 +5,18 @@
//! 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 rusqlite::OptionalExtension;
-
-use crate::db::{self, *};
+use crate::db::{self, candidates as sql, Db, Pkgbase, ReleaseRow};
use crate::error::Result;
-use crate::time;
-#[derive(Debug, Clone)]
-pub struct NewCandidate {
- pub snapshot_id: i64,
- pub epoch: String,
- pub pkgver: String,
- pub pkgrel: String,
- pub upstream_pins_json: String,
- pub source_digests_json: String,
- pub identity_hash: String,
-}
+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 {
@@ -43,267 +38,41 @@ pub enum Upsert {
/// `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<Upsert> {
- let kind = if accepted.is_some() { candidate_kind::NEW_VERSION } else { candidate_kind::FIRST_INTRODUCTION };
+ 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| {
- let existing: Option<(i64, String, Option<String>, i64)> = tx
- .query_row(
- "SELECT id, identity_hash, viewed_at, revision FROM candidate
- WHERE pkgbase_id=?1 AND state='pending_review'",
- rusqlite::params![pkgbase_id],
- |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)),
- )
- .optional()?;
-
- if let Some((id, identity, viewed, revision)) = existing {
- if identity == nc.identity_hash {
+ if let Some(open) = sql::open_in(tx, pkgbase_id)? {
+ if open.identity_hash == nc.identity_hash {
return Ok(Upsert::Unchanged);
}
- if viewed.is_none() {
+ if !open.viewed() {
// Collapse in place: same base, no new row, revision bumped.
- tx.execute(
- "UPDATE candidate SET snapshot_id=?1, epoch=?2, pkgver=?3, pkgrel=?4,
- upstream_pins_json=?5, source_digests_json=?6, identity_hash=?7,
- revision=?8, detected_at=?9, diff_digest=''
- WHERE id=?10",
- rusqlite::params![
- nc.snapshot_id, nc.epoch, nc.pkgver, nc.pkgrel,
- nc.upstream_pins_json, nc.source_digests_json, nc.identity_hash,
- revision + 1, time::now(), id
- ],
- )?;
- return Ok(Upsert::Collapsed { id, revision: revision + 1 });
+ 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.
- tx.execute(
- "UPDATE candidate SET state='superseded', resolved_at=?1 WHERE id=?2",
- rusqlite::params![time::now(), id],
- )?;
- let new_id = insert(tx, pkgbase_id, base, kind, &nc)?;
- return Ok(Upsert::Superseded { old: id, new: new_id });
+ 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.
- let rejected: Option<i64> = tx
- .query_row(
- "SELECT id FROM candidate WHERE pkgbase_id=?1 AND identity_hash=?2 AND state='rejected'
- ORDER BY id DESC LIMIT 1",
- rusqlite::params![pkgbase_id, nc.identity_hash],
- |r| r.get(0),
- )
- .optional()?;
- if rejected.is_some() {
+ if sql::rejected_before_in(tx, pkgbase_id, &nc.identity_hash)? {
return Ok(Upsert::PreviouslyRejected);
}
// Same identity already approved/recorded: nothing to do.
- let same: Option<i64> = tx
- .query_row(
- "SELECT id FROM candidate WHERE pkgbase_id=?1 AND identity_hash=?2
- ORDER BY id DESC LIMIT 1",
- rusqlite::params![pkgbase_id, nc.identity_hash],
- |r| r.get(0),
- )
- .optional()?;
- if same.is_some() {
+ if sql::recorded_before_in(tx, pkgbase_id, &nc.identity_hash)? {
return Ok(Upsert::Unchanged);
}
- let id = insert(tx, pkgbase_id, base, kind, &nc)?;
+ let id = sql::insert_in(tx, pkgbase_id, base, kind, &nc)?;
Ok(Upsert::Created { id, kind: kind.to_string() })
})
}
-fn insert(
- tx: &mut rusqlite::Transaction<'_>,
- pkgbase_id: i64,
- base: Option<i64>,
- kind: &str,
- nc: &NewCandidate,
-) -> Result<i64> {
- tx.execute(
- "INSERT INTO candidate (pkgbase_id, base_release_id, kind, snapshot_id, epoch, pkgver, pkgrel,
- upstream_pins_json, source_digests_json, identity_hash, state, detected_at)
- VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,'pending_review',?11)",
- rusqlite::params![
- pkgbase_id, base, kind, nc.snapshot_id, nc.epoch, nc.pkgver, nc.pkgrel,
- nc.upstream_pins_json, nc.source_digests_json, nc.identity_hash, time::now()
- ],
- )?;
- Ok(tx.last_insert_rowid())
-}
-
-impl NewCandidate {
- /// The `epoch:pkgver-pkgrel` this candidate would publish.
- pub fn version(&self) -> String {
- crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel)
- }
-}
-
-/// Every candidate regardless of state, newest first (`aur-build reviews --all`).
-pub fn all_candidates(db: &Db) -> Result<Vec<CandidateRow>> {
- db.read(|c| {
- let mut stmt = c.prepare(
- "SELECT c.id, c.pkgbase_id, c.base_release_id, c.kind, c.snapshot_id, c.epoch, c.pkgver,
- c.pkgrel, c.upstream_pins_json, c.source_digests_json, c.identity_hash,
- c.diff_digest, c.revision, c.viewed_at, c.risk_level, c.risk_signals_json,
- c.missing_deps_json, c.state, c.detected_at, c.resolved_at, b.name
- FROM candidate c JOIN pkgbase b ON b.id = c.pkgbase_id
- ORDER BY c.detected_at DESC LIMIT 500",
- )?;
- let rows = stmt.query_map([], row_to_candidate)?;
- let mut out = Vec::new();
- for r in rows {
- out.push(r?);
- }
- Ok(out)
- })
-}
-
-/// Mark a candidate `viewed` the first time a human opens it. That timestamp is
-/// what decides whether a later detection collapses or supersedes (§7.1).
-pub fn mark_viewed(db: &Db, id: i64) -> Result<()> {
- let at = time::now();
- db.writer.tx(move |tx| {
- tx.execute(
- "UPDATE candidate SET viewed_at=?1 WHERE id=?2 AND viewed_at IS NULL",
- rusqlite::params![at, id],
- )?;
- Ok(())
- })
-}
-
-pub fn pending_candidates(db: &Db) -> Result<Vec<CandidateRow>> {
- db.read(|c| {
- let mut stmt = c.prepare(
- "SELECT c.id, c.pkgbase_id, c.base_release_id, c.kind, c.snapshot_id, c.epoch, c.pkgver,
- c.pkgrel, c.upstream_pins_json, c.source_digests_json, c.identity_hash,
- c.diff_digest, c.revision, c.viewed_at, c.risk_level, c.risk_signals_json,
- c.missing_deps_json, c.state, c.detected_at, c.resolved_at, b.name
- FROM candidate c JOIN pkgbase b ON b.id = c.pkgbase_id
- WHERE c.state='pending_review'
- ORDER BY (c.risk_level='high') DESC, c.detected_at",
- )?;
- let rows = stmt.query_map([], row_to_candidate)?;
- let mut out = Vec::new();
- for r in rows {
- out.push(r?);
- }
- Ok(out)
- })
-}
-
-pub fn candidate_by_id(db: &Db, id: i64) -> Result<Option<CandidateRow>> {
- db.read(|c| {
- Ok(c.query_row(
- "SELECT c.id, c.pkgbase_id, c.base_release_id, c.kind, c.snapshot_id, c.epoch, c.pkgver,
- c.pkgrel, c.upstream_pins_json, c.source_digests_json, c.identity_hash,
- c.diff_digest, c.revision, c.viewed_at, c.risk_level, c.risk_signals_json,
- c.missing_deps_json, c.state, c.detected_at, c.resolved_at, b.name
- FROM candidate c JOIN pkgbase b ON b.id = c.pkgbase_id WHERE c.id=?1",
- rusqlite::params![id],
- row_to_candidate,
- )
- .optional()?)
- })
-}
-
-fn row_to_candidate(r: &rusqlite::Row<'_>) -> rusqlite::Result<CandidateRow> {
- Ok(CandidateRow {
- id: r.get(0)?,
- pkgbase_id: r.get(1)?,
- base_release_id: r.get(2)?,
- kind: r.get(3)?,
- snapshot_id: r.get(4)?,
- epoch: r.get(5)?,
- pkgver: r.get(6)?,
- pkgrel: r.get(7)?,
- upstream_pins_json: r.get(8)?,
- source_digests_json: r.get(9)?,
- identity_hash: r.get(10)?,
- diff_digest: r.get(11)?,
- revision: r.get(12)?,
- viewed_at: r.get(13)?,
- risk_level: r.get(14)?,
- risk_signals_json: r.get(15)?,
- missing_deps_json: r.get(16)?,
- state: r.get(17)?,
- detected_at: r.get(18)?,
- resolved_at: r.get(19)?,
- pkgbase: r.get(20)?,
- })
-}
-
-/// Open reviews for a pkgbase (usually zero or one, by I7).
-pub fn open_reviews_for(db: &Db, pkgbase_id: i64) -> Result<Vec<CandidateRow>> {
- db.read(|c| {
- let mut stmt = c.prepare(
- "SELECT c.id, c.pkgbase_id, c.base_release_id, c.kind, c.snapshot_id, c.epoch, c.pkgver,
- c.pkgrel, c.upstream_pins_json, c.source_digests_json, c.identity_hash,
- c.diff_digest, c.revision, c.viewed_at, c.risk_level, c.risk_signals_json,
- c.missing_deps_json, c.state, c.detected_at, c.resolved_at, b.name
- FROM candidate c JOIN pkgbase b ON b.id = c.pkgbase_id
- WHERE c.pkgbase_id=?1 AND c.state='pending_review'",
- )?;
- let rows = stmt.query_map(rusqlite::params![pkgbase_id], row_to_candidate)?;
- let mut out = Vec::new();
- for r in rows {
- out.push(r?);
- }
- Ok(out)
- })
-}
-
-/// §7.1: a rejection can be re-opened. We retire the rejected row so it stops
-/// suppressing the identity, and the next detection opens a fresh review.
-pub fn reopen_rejected(db: &Db, id: i64) -> Result<()> {
- let at = time::now();
- db.writer.tx(move |tx| {
- let n = tx.execute(
- "UPDATE candidate SET state='superseded', resolved_at=?1 WHERE id=?2 AND state='rejected'",
- rusqlite::params![at, id],
- )?;
- if n == 0 {
- return Err(crate::error::Error::conflict("candidate is not rejected"));
- }
- Ok(())
- })
-}
-
-/// Record the risk assessment computed at review time.
-pub fn set_risk(db: &Db, id: i64, level: &str, signals: &[String]) -> Result<()> {
- let lvl = level.to_string();
- let sig = serde_json::to_string(signals)?;
- db.writer.tx(move |tx| {
- tx.execute(
- "UPDATE candidate SET risk_level=?1, risk_signals_json=?2 WHERE id=?3",
- rusqlite::params![lvl, sig, id],
- )?;
- Ok(())
- })
-}
-
-pub fn set_missing_deps(db: &Db, id: i64, missing: &[String]) -> Result<()> {
- let m = serde_json::to_string(missing)?;
- db.writer.tx(move |tx| {
- tx.execute(
- "UPDATE candidate SET missing_deps_json=?1 WHERE id=?2",
- rusqlite::params![m, id],
- )?;
- Ok(())
- })
-}
-
-pub fn set_diff_digest(db: &Db, id: i64, digest: &str) -> Result<()> {
- let d = digest.to_string();
- db.writer.tx(move |tx| {
- tx.execute("UPDATE candidate SET diff_digest=?1 WHERE id=?2", rusqlite::params![d, id])?;
- Ok(())
- })
-}
-
/// Convenience used by the watcher: the accepted release of a pkgbase row.
pub fn accepted_for(db: &Db, pb: &Pkgbase) -> Result<Option<ReleaseRow>> {
match pb.current_accepted_release_id {