use rusqlite::{params, OptionalExtension, Row}; use crate::db::*; use crate::error::Result; // ---------------------------------------------------------------- pkgbase -- #[derive(Debug, Clone)] pub struct Pkgbase { pub id: i64, pub name: String, pub aur_url: String, pub mirror_path: Option, pub aur_head: Option, pub current_accepted_release_id: Option, pub state: String, pub unpinable: bool, } fn row_to_pkgbase(r: &Row<'_>) -> rusqlite::Result { Ok(Pkgbase { id: r.get(0)?, name: r.get(1)?, aur_url: r.get(2)?, mirror_path: r.get(3)?, aur_head: r.get(4)?, current_accepted_release_id: r.get(5)?, state: r.get(6)?, unpinable: r.get::<_, i64>(7)? != 0, }) } const PKGBASE_COLS: &str = "id, name, aur_url, mirror_path, aur_head, \ current_accepted_release_id, state, unpinable"; pub fn get_pkgbase(db: &Db, name: &str) -> Result> { db.read(|c| { c.query_row( &format!("SELECT {PKGBASE_COLS} FROM pkgbase WHERE name = ?1"), params![name], row_to_pkgbase, ) .optional() .map_err(Into::into) }) } pub fn get_pkgbase_by_id(db: &Db, id: i64) -> Result> { db.read(|c| { c.query_row( &format!("SELECT {PKGBASE_COLS} FROM pkgbase WHERE id = ?1"), params![id], row_to_pkgbase, ) .optional() .map_err(Into::into) }) } /// The release a pkgbase currently points at, if it points at one. This is a /// pointer stored on the pkgbase row, so reading it is a persistence question, /// not a review one — the watcher needs it to decide what a change is a change /// *from*, and review must not be the only way to ask. pub fn accepted_release_for(db: &Db, pb: &Pkgbase) -> Result> { match pb.current_accepted_release_id { Some(id) => release_by_id(db, id), None => Ok(None), } } pub fn set_aur_head(writer: &Writer, pkgbase_id: i64, head: &str, mirror: &str) -> Result<()> { let (h, m) = (head.to_string(), mirror.to_string()); writer.tx(move |tx| { tx.execute( "UPDATE pkgbase SET aur_head=?1, mirror_path=?2 WHERE id=?3", params![h, m, pkgbase_id], )?; Ok(()) }) } /// Compare-and-set the accepted-release pointer. /// /// Test-tier only, and the reason is worth keeping next to it: production /// performs this CAS inside a transaction through `cas_accepted_release_in`, /// which is what `pipeline::publish` calls at publish.rs:208. This standalone /// form takes a `Writer` and is called by nothing in the crate — `tests/db.rs` /// alone pins that the pointer moves once and refuses the second time. The two /// names differ by three characters, which is exactly how a name-match census /// once credited this function with a production caller it never had. #[cfg(feature = "test-support")] pub fn cas_accepted_release( writer: &Writer, pkgbase_id: i64, expected: Option, new_id: i64, ) -> Result { writer.tx(move |tx| { let n = tx.execute( "UPDATE pkgbase SET current_accepted_release_id=?1 WHERE id=?2 AND current_accepted_release_id IS ?3", params![new_id, pkgbase_id, expected], )?; Ok(n == 1) }) }