From 8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 20 Nov 2026 17:13:44 +0000 Subject: db: one module per aggregate, and an interface you can read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit queries.rs was 1,501 lines holding 83 queries across seventeen aggregates, separated by comment banners that said "settings", "release", "notices" and then kept them in one file. The banners were the design; they just never got files. Each one has one now, verbatim — the split is provably content-only, the moved text being identical to the original as a sorted set of lines. model.rs and builds.rs were each two concerns deep, so they became directories. model.rs held what a row looks like and, separately, the state machines that say which moves are legal; builds.rs held the build machine and, below its own `step rows` banner, the step machine. The impl_state_enum! macro implements StateEnum for all five enums from one place, so it moved with its invocations rather than being exported across a boundary to chase them. They are directories rather than new sibling modules on purpose: `db::model::CandidateState` and `db::builds::steps` are used across the crate and by the integration tests, and renaming them would be a bigger change than the one being made. No call site and no test changed. db/mod.rs now lists its interface instead of spraying one. `pub use model::*` and `pub use queries::*` are gone, replaced by seventeen explicit exports, and the query modules are private — so a query that was not chosen for export is not reachable, and the width of the module is visible from its own text rather than only from a grep. The ratchet caught the win immediately: db.glob went 2 -> 0, which failed a_shrunk_interface_is_written_back until the budget was written back at the new value. db is off the glob allowlist now rather than holding a stale zero. 277 passed / 0 failed, clippy -D warnings clean, largest file in src/db 363 lines (candidates.rs, unchanged from before this work). --- src/db/pkgbases.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/db/pkgbases.rs (limited to 'src/db/pkgbases.rs') diff --git a/src/db/pkgbases.rs b/src/db/pkgbases.rs new file mode 100644 index 0000000..a652093 --- /dev/null +++ b/src/db/pkgbases.rs @@ -0,0 +1,108 @@ +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) + }) +} + +pub fn list_pkgbases(db: &Db) -> Result> { + db.read(|c| { + let mut stmt = c.prepare(&format!("SELECT {PKGBASE_COLS} FROM pkgbase ORDER BY name"))?; + let rows = stmt.query_map([], row_to_pkgbase)?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +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(()) + }) +} + +pub fn set_unpinable(writer: &Writer, pkgbase_id: i64, unpinable: bool) -> Result<()> { + writer.tx(move |tx| { + tx.execute( + "UPDATE pkgbase SET unpinable=?1 WHERE id=?2", + params![unpinable as i64, pkgbase_id], + )?; + Ok(()) + }) +} + +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) + }) +} + -- cgit v1.2.3