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/staleness.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/db/staleness.rs (limited to 'src/db/staleness.rs') diff --git a/src/db/staleness.rs b/src/db/staleness.rs new file mode 100644 index 0000000..8987e85 --- /dev/null +++ b/src/db/staleness.rs @@ -0,0 +1,135 @@ +use rusqlite::{params, OptionalExtension}; + +use crate::db::*; +use crate::error::Result; +use crate::time; + +// ------------------------------------------------------------ staleness reads -- + +pub fn published_world_members(db: &Db) -> Result> { + db.read(|c| { + let mut st = c.prepare( + "SELECT p.name, w.members_json + FROM release r + JOIN pkgbase p ON p.id = r.pkgbase_id + JOIN build b ON b.id = r.build_id + JOIN world_snapshot w ON w.id = b.world_snapshot_id + WHERE r.state = 'published'", + )?; + let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?; + Ok(rows.collect::<::rusqlite::Result>>()?) + }) +} + +pub fn served_vs_offered_pkgvers(db: &Db) -> Result> { + db.read(|c| { + let mut st = c.prepare( + "SELECT p.name, r.pkgver, ( + SELECT c.pkgver FROM candidate c + WHERE c.pkgbase_id = r.pkgbase_id + ORDER BY c.id DESC LIMIT 1) + FROM release r + JOIN pkgbase p ON p.current_accepted_release_id = r.id + WHERE r.state = 'published'", + )?; + let rows = st.query_map([], |r| { + Ok(( + r.get::<_, String>(0)?, + r.get::<_, String>(1)?, + r.get::<_, Option>(2)?.unwrap_or_default(), + )) + })?; + Ok(rows.collect::<::rusqlite::Result>>()?) + }) +} + +pub fn index_recipes(db: &Db) -> Result> { + db.read(|c| { + let mut stmt = c.prepare( + "SELECT pk.name, s.srcinfo_json FROM package pk + JOIN release r ON r.id = pk.release_id + JOIN candidate cnd ON cnd.id = r.candidate_id + JOIN snapshot s ON s.id = cnd.snapshot_id + WHERE pk.state='in_index'", + )?; + let rows = stmt.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn approval_note(db: &Db, approval_id: i64) -> Result { + db.read(|c| { + c.query_row("SELECT COALESCE(note,'') FROM approval WHERE id=?1", params![approval_id], |r| r.get::<_, String>(0)) + .optional() + .map(|o| o.unwrap_or_default()) + .map_err(Into::into) + }) +} + +pub fn candidate_snapshot_id(db: &Db, candidate_id: i64) -> Result> { + db.read(|c| { + Ok(c.query_row("SELECT snapshot_id FROM candidate WHERE id=?1", params![candidate_id], |r| r.get::<_, i64>(0)) + .optional()?) + }) +} + +pub fn subscription_poll_interval(db: &Db, pkgbase: &str) -> Result> { + db.read(|c| { + Ok(c.query_row( + "SELECT poll_interval_secs FROM subscription WHERE pkgbase=?1", + params![pkgbase], + |r| r.get::<_, Option>(0), + ) + .optional()? + .flatten()) + }) +} + +pub fn put_source_cache(writer: &Writer, url: &str, digest: &str, size: u64, path: &str) -> Result<()> { + let (u, d, p) = (url.to_string(), digest.to_string(), path.to_string()); + let at = time::now(); + writer.tx(move |tx| { + tx.execute( + "INSERT INTO source_cache (url, digest, size, fetched_at, path) VALUES (?1,?2,?3,?4,?5) + ON CONFLICT(url, digest) DO UPDATE SET fetched_at=excluded.fetched_at", + params![u, d, size as i64, at, p], + )?; + Ok(()) + }) +} + +pub fn source_cache_path(db: &Db, url: &str, digest: &str) -> Result> { + db.read(|c| { + Ok(c.query_row( + "SELECT path FROM source_cache WHERE url=?1 AND digest=?2", + params![url, digest], + |r| r.get::<_, String>(0), + ) + .optional()?) + }) +} + +pub struct NewArtifact<'a> { + pub digest: &'a str, + pub size: i64, + pub path: &'a str, + pub signature_path: Option<&'a str>, + pub build_log_path: Option<&'a str>, + pub build_id: &'a str, + pub at: &'a str, +} + +pub fn insert_artifact_in(tx: &rusqlite::Transaction<'_>, a: &NewArtifact<'_>) -> Result<()> { + tx.execute( + "INSERT INTO artifact (digest, size, path, signature_path, build_log_path, build_id, created_at) + VALUES (?1,?2,?3,?4,?5,?6,?7) + ON CONFLICT(digest) DO UPDATE SET build_id=excluded.build_id", + params![a.digest, a.size, a.path, a.signature_path, a.build_log_path, a.build_id, a.at], + )?; + Ok(()) +} + -- cgit v1.2.3