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/retention.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/db/retention.rs (limited to 'src/db/retention.rs') diff --git a/src/db/retention.rs b/src/db/retention.rs new file mode 100644 index 0000000..ca468e1 --- /dev/null +++ b/src/db/retention.rs @@ -0,0 +1,91 @@ +use rusqlite::params; + +use crate::db::*; +use crate::error::Result; +use crate::time; + +// -------------------------------------------------------------- retention reads -- + +pub fn pkgbase_release_pointers(db: &Db) -> Result)>> { + db.read(|c| { + let mut st = c.prepare("SELECT id, current_accepted_release_id FROM pkgbase ORDER BY id")?; + let rows = st.query_map([], |r| Ok((r.get::<_, i64>(0)?, r.get::<_, Option>(1)?)))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn published_release_ids(db: &Db, pkgbase_id: i64) -> Result> { + db.read(|c| { + let mut st = c.prepare("SELECT id FROM release WHERE pkgbase_id=?1 AND state='published' ORDER BY id DESC")?; + let rows = st.query_map(params![pkgbase_id], |r| r.get::<_, i64>(0))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn serving_release_ids(db: &Db, pkgbase_id: i64) -> Result> { + db.read(|c| { + let mut st = c.prepare( + "SELECT DISTINCT r.id FROM release r JOIN package p ON p.release_id=r.id + WHERE r.pkgbase_id=?1 AND p.state='in_index' ORDER BY r.id DESC", + )?; + let rows = st.query_map(params![pkgbase_id], |r| r.get::<_, i64>(0))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub type ArtifactPaths = (Option, Option, Option); + +pub fn release_artifact_paths(db: &Db, release_id: i64) -> Result> { + db.read(|c| { + let mut st = c.prepare( + "SELECT a.path, a.signature_path, a.build_log_path FROM artifact a + JOIN package p ON p.artifact_id = a.id WHERE p.release_id = ?1", + )?; + let rows = st.query_map(params![release_id], |r| { + Ok((r.get::<_, Option>(0)?, r.get::<_, Option>(1)?, r.get::<_, Option>(2)?)) + })?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn mark_release_purged(writer: &Writer, release_id: i64) -> Result<()> { + let at = time::now(); + writer.tx(move |tx| { + tx.execute("UPDATE release SET purged_at=?1 WHERE id=?2", params![at, release_id])?; + tx.execute( + "UPDATE artifact SET purged_at=?1 WHERE purged_at IS NULL AND id IN + (SELECT a.id FROM artifact a JOIN package p ON p.artifact_id=a.id WHERE p.release_id=?2)", + params![at, release_id], + )?; + Ok(()) + }) +} + +pub fn record_gc_run(writer: &Writer, at: &str, kept: i64, evicted: i64, files_removed: i64, bytes_freed: i64, staging_swept: i64) -> Result<()> { + let at = at.to_string(); + writer.tx(move |tx| { + tx.execute( + "INSERT INTO gc_run (ran_at, releases_kept, releases_evicted, files_removed, bytes_freed, staging_swept) + VALUES (?1,?2,?3,?4,?5,?6)", + params![at, kept, evicted, files_removed, bytes_freed, staging_swept], + )?; + Ok(()) + }) +} + -- cgit v1.2.3