diff options
Diffstat (limited to 'src/db/approvals.rs')
| -rw-r--r-- | src/db/approvals.rs | 70 |
1 files changed, 70 insertions, 0 deletions
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<i64>, + 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<i64> { + 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<Option<(i64, String)>> { + 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()?) + }) +} |
