From 933c637de34095f1ea1ab88b02626bbf22eec7f4 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 15:20:13 +0000 Subject: =?UTF-8?q?db:=20the=20schema=20gets=20an=20owner=20=E2=80=94=20ev?= =?UTF-8?q?ery=20statement=20moves=20into=20db/,=20and=20the=20writer=20st?= =?UTF-8?q?ops=20handing=20out=20connections?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/db/approvals.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/db/approvals.rs (limited to 'src/db/approvals.rs') diff --git a/src/db/approvals.rs b/src/db/approvals.rs new file mode 100644 index 0000000..f04d641 --- /dev/null +++ b/src/db/approvals.rs @@ -0,0 +1,70 @@ +//! The approval table: the record of a human decision, and the only thing that +//! makes a candidate publishable (§7.1a, I1). +//! +//! An approval row is what an index entry must trace back to. The rules about +//! *when* one may be written — same identity, same diff, same base — are domain +//! logic in `review::approve`; what lives here is the SQL, so the schema has one +//! owner (§6 M-1). + +use rusqlite::{params, OptionalExtension, Transaction}; + +use crate::db::Db; +use crate::error::Result; + +/// A human decision about one candidate, ready to record. +pub struct Decision<'a> { + pub candidate_id: i64, + pub base_release_id: Option, + pub at: &'a str, + pub decision: &'a str, + pub risk_level: &'a str, + pub diff_digest: &'a str, + pub identity_hash: &'a str, + pub note: Option<&'a str>, +} + +/// Record a decision on a candidate and return the approval row's id. +pub fn record_in(tx: &mut Transaction<'_>, d: &Decision<'_>) -> Result { + tx.execute( + "INSERT INTO approval (candidate_id, base_release_id, decided_at, decision, risk_level, + diff_digest, identity_hash, note) + VALUES (?1,?2,?3,?4,?5,?6,?7,?8)", + params![ + d.candidate_id, + d.base_release_id, + d.at, + d.decision, + d.risk_level, + d.diff_digest, + d.identity_hash, + d.note + ], + )?; + Ok(tx.last_insert_rowid()) +} + +/// Record a rejection by copying the candidate's own row into the approval +/// table. The decision has to describe the bytes that *were* rejected, so it is +/// read from the row rather than repeated by the caller (§7.1). +pub fn record_rejection_in(tx: &mut Transaction<'_>, candidate_id: i64, at: &str, identity_hash: &str, note: &str) -> Result<()> { + tx.execute( + "INSERT INTO approval (candidate_id, base_release_id, decided_at, decision, risk_level, + diff_digest, identity_hash, note) + SELECT id, base_release_id, ?1, 'rejected', risk_level, diff_digest, ?2, ?3 + FROM candidate WHERE id=?4", + params![at, identity_hash, note, candidate_id], + )?; + Ok(()) +} + +/// The decision recorded against a candidate, if one was. +pub fn for_candidate(db: &Db, candidate_id: i64) -> Result> { + db.read(|c| { + Ok(c.query_row( + "SELECT id, decision FROM approval WHERE candidate_id=?1", + params![candidate_id], + |r| Ok((r.get(0)?, r.get(1)?)), + ) + .optional()?) + }) +} -- cgit v1.2.3