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/invariants.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/invariants.rs')
| -rw-r--r-- | src/db/invariants.rs | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/db/invariants.rs b/src/db/invariants.rs new file mode 100644 index 0000000..398590b --- /dev/null +++ b/src/db/invariants.rs @@ -0,0 +1,123 @@ + +use crate::db::*; +use crate::error::Result; + +// ------------------------------------------------------- invariant helpers -- + +pub fn unapproved_index_entries(db: &Db) -> Result<Vec<String>> { + db.read(|c| { + let mut stmt = c.prepare( + "SELECT p.name FROM package p + JOIN release r ON r.id = p.release_id + WHERE p.state='in_index' AND (r.approval_id IS NULL OR NOT EXISTS + (SELECT 1 FROM approval a WHERE a.id = r.approval_id AND a.decision='approved'))", + )?; + let rows = stmt.query_map([], |r| r.get::<_, String>(0))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn index_pkgbase_drift(db: &Db) -> Result<(Vec<String>, Vec<String>)> { + db.read(|c| { + let mut orphans = Vec::new(); + let mut stmt = c.prepare( + "SELECT DISTINCT b.name FROM package p + JOIN release r ON r.id = p.release_id + JOIN pkgbase b ON b.id = r.pkgbase_id + WHERE p.state='in_index' + AND r.id NOT IN (SELECT current_accepted_release_id FROM pkgbase + WHERE current_accepted_release_id IS NOT NULL)", + )?; + let rows = stmt.query_map([], |r| r.get::<_, String>(0))?; + for r in rows { + orphans.push(r?); + } + let mut missing = Vec::new(); + let mut stmt = c.prepare( + "SELECT b.name FROM pkgbase b + JOIN subscription s ON s.pkgbase = b.name AND s.state IN ('active','orphaned') + WHERE b.current_accepted_release_id IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM package p WHERE p.release_id = b.current_accepted_release_id + AND p.state='in_index')", + )?; + let rows = stmt.query_map([], |r| r.get::<_, String>(0))?; + for r in rows { + missing.push(r?); + } + Ok((orphans, missing)) + }) +} + +#[derive(Debug, Clone)] +pub struct Unbuilt { + pub pkgbase: String, + pub approved: String, + pub serving: String, + pub error: String, + pub build_id: String, +} + +pub fn approved_but_unbuilt(db: &Db) -> Result<Vec<Unbuilt>> { + db.read(|c| { + let mut stmt = c.prepare( + "SELECT p.name, + cnd.epoch, cnd.pkgver, cnd.pkgrel, + ser.epoch, ser.pkgver, ser.pkgrel, + COALESCE(b.error, ''), b.id + FROM build b + JOIN pkgbase p ON p.id = b.pkgbase_id + LEFT JOIN candidate cnd ON cnd.id = b.candidate_id + LEFT JOIN release ser ON ser.id = p.current_accepted_release_id + AND ser.state='published' + WHERE b.state = 'failed' + AND b.candidate_id IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM release r WHERE r.candidate_id = b.candidate_id) + AND b.id = (SELECT b2.id FROM build b2 + WHERE b2.candidate_id = b.candidate_id + ORDER BY b2.rowid DESC LIMIT 1) + ORDER BY p.name", + )?; + let rows = stmt.query_map([], |r| { + Ok(( + r.get::<_, Option<String>>(0)?, + r.get::<_, Option<String>>(1)?, + r.get::<_, Option<String>>(2)?, + r.get::<_, Option<String>>(3)?, + r.get::<_, Option<String>>(4)?, + r.get::<_, Option<String>>(5)?, + r.get::<_, Option<String>>(6)?, + r.get::<_, String>(7)?, + r.get::<_, String>(8)?, + )) + })?; + let mut out = Vec::new(); + for r in rows { + let (pkgbase, ae, av, ar, se, sv, sr, error, build_id) = r?; + let (pkgbase, av, ar) = match (pkgbase, av, ar) { + (Some(p), Some(v), Some(rel)) => (p, v, rel), + _ => { + return Err(crate::error::Error::Db( + "failed build row without a candidate version".into(), + )) + } + }; + out.push(Unbuilt { + pkgbase, + approved: crate::pkgver::full_version(&ae.unwrap_or_default(), &av, &ar), + serving: match (sv, sr) { + (Some(v), Some(rel)) => { + crate::pkgver::full_version(&se.unwrap_or_default(), &v, &rel) + } + _ => String::new(), + }, + error, + build_id, + }); + } + Ok(out) + }) +} |
