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/packages.rs | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/db/packages.rs (limited to 'src/db/packages.rs') diff --git a/src/db/packages.rs b/src/db/packages.rs new file mode 100644 index 0000000..6708c99 --- /dev/null +++ b/src/db/packages.rs @@ -0,0 +1,137 @@ +use rusqlite::{params, OptionalExtension}; + +use crate::db::*; +use crate::error::Result; + +// ---------------------------------------------------------------- package -- + +#[derive(Debug, Clone)] +pub struct PackageRow { + pub id: i64, + pub release_id: i64, + pub name: String, + pub arch: String, + pub version: String, + pub artifact_id: Option, + pub pkgbase: String, + pub size: Option, + pub published_at: Option, +} + +pub fn index_packages(db: &Db) -> Result> { + db.read(|c| { + let mut stmt = c.prepare( + "SELECT p.id, p.release_id, p.name, p.arch, p.version, p.artifact_id, b.name, + a.size, r.published_at + FROM package p + JOIN release r ON r.id = p.release_id + JOIN pkgbase b ON b.id = r.pkgbase_id + LEFT JOIN artifact a ON a.id = p.artifact_id + WHERE p.state = 'in_index' + ORDER BY p.name", + )?; + let rows = stmt.query_map([], |r| { + Ok(PackageRow { + id: r.get(0)?, + release_id: r.get(1)?, + name: r.get(2)?, + arch: r.get(3)?, + version: r.get(4)?, + artifact_id: r.get(5)?, + pkgbase: r.get(6)?, + size: r.get(7)?, + published_at: r.get(8)?, + }) + })?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn index_version(db: &Db, name: &str) -> Result> { + db.read(|c| { + c.query_row( + "SELECT p.version FROM package p WHERE p.name=?1 AND p.state='in_index' + ORDER BY p.id DESC LIMIT 1", + params![name], + |r| r.get(0), + ) + .optional() + .map_err(Into::into) + }) +} + +pub fn index_artifact(db: &Db, name: &str) -> Result> { + db.read(|c| { + c.query_row( + "SELECT p.version, a.digest FROM package p + JOIN artifact a ON a.id = p.artifact_id + WHERE p.name=?1 AND p.state='in_index' ORDER BY p.id DESC LIMIT 1", + params![name], + |r| Ok((r.get(0)?, r.get(1)?)), + ) + .optional() + .map_err(Into::into) + }) +} + +pub fn is_ours(db: &Db, name: &str) -> Result { + db.read(|c| { + c.query_row( + "SELECT 1 FROM package WHERE name=?1 AND state='in_index' LIMIT 1", + params![name], + |r| r.get::<_, i64>(0), + ) + .optional() + .map(|o| o.is_some()) + .map_err(Into::into) + }) +} + +pub fn our_package_names(db: &Db) -> Result> { + db.read(|c| { + let mut stmt = c.prepare("SELECT DISTINCT name FROM package WHERE state='in_index'")?; + let rows = stmt.query_map([], |r| r.get::<_, String>(0))?; + let mut out = std::collections::BTreeSet::new(); + for r in rows { + out.insert(r?); + } + Ok(out) + }) +} + +pub fn accepted_recipe(db: &Db, release: &ReleaseRow) -> Result> { + let cid = match release.candidate_id { + Some(c) => c, + None => return Ok(None), + }; + db.read(move |c| { + use rusqlite::OptionalExtension; + Ok(c.query_row( + "SELECT s.srcinfo_json FROM snapshot s + JOIN candidate cnd ON cnd.snapshot_id = s.id + WHERE cnd.id = ?1", + params![cid], + |r| r.get::<_, String>(0), + ) + .optional()? + .and_then(|j| serde_json::from_str::(&j).ok())) + }) +} + +pub fn builds_for_pkgbase(db: &Db, pkgbase_id: i64, limit: i64) -> Result> { + db.read(move |c| { + let mut st = c.prepare( + "SELECT id, pkgbase_id, candidate_id, release_id, kind, world_snapshot_id, build_profile, + state, blocked_on_json, current_step, lease_owner, lease_expires_at, attempt, + priority, cancel_requested, error + FROM build WHERE pkgbase_id=?1 ORDER BY rowid DESC LIMIT ?2", + )?; + let rows = st.query_map(params![pkgbase_id, limit], crate::db::builds::row_to_build)?; + rows.collect::<::rusqlite::Result>>().map_err(Into::into) + }) +} + -- cgit v1.2.3