diff options
| author | dev <dev@localhost> | 2026-11-20 17:13:44 +0000 |
|---|---|---|
| committer | dev <dev@localhost> | 2026-11-20 17:13:44 +0000 |
| commit | 8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4 (patch) | |
| tree | 2ce6c30a688ef3fb15af5639e22cfc230f8f3627 /src/db/retention.rs | |
| parent | b73bb60403c3f2e7fc55d5038831912e8371e3a6 (diff) | |
| download | aur-build-8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4.tar.gz aur-build-8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4.zip | |
db: one module per aggregate, and an interface you can read
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).
Diffstat (limited to 'src/db/retention.rs')
| -rw-r--r-- | src/db/retention.rs | 91 |
1 files changed, 91 insertions, 0 deletions
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<Vec<(i64, Option<i64>)>> { + 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<i64>>(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<Vec<i64>> { + 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<Vec<i64>> { + 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<String>, Option<String>, Option<String>); + +pub fn release_artifact_paths(db: &Db, release_id: i64) -> Result<Vec<ArtifactPaths>> { + 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<String>>(0)?, r.get::<_, Option<String>>(1)?, r.get::<_, Option<String>>(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(()) + }) +} + |
