diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 15:20:13 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 15:20:13 +0000 |
| commit | 933c637de34095f1ea1ab88b02626bbf22eec7f4 (patch) | |
| tree | ee7bd3d7a2b18163c0fdbe061ef53a73b74907ab /src/watch | |
| parent | 7946e9ffc48748992f051ada15bec772b16a585d (diff) | |
| download | aur-build-933c637de34095f1ea1ab88b02626bbf22eec7f4.tar.gz aur-build-933c637de34095f1ea1ab88b02626bbf22eec7f4.zip | |
db: the schema gets an owner — every statement moves into db/, and the writer stops handing out connections
Twelve modules outside `db/` contained hand-rolled SQL — 22 statements in
`ops.rs`, 17 in `approve.rs`, 15 in `candidates.rs`, 11 in `publish.rs`, and a
dozen more scattered through `watch`, `pipeline`, `http` — which meant the
blast radius of a schema change was "any module", and `db/` was a directory
rather than a boundary. The audit trail alone was defined in four places with
three different detail payloads.
Every statement now lives in `db/`. New modules `db::candidates` and
`db::approvals` own their tables (including the 21-column candidate select,
spelled once as `CANDIDATE_COLS` instead of four times); `db::queries` gains
the metrics reads, the retention reads and writes, the publish writes
(`insert_release_in`, `cas_accepted_release_in`, `remove_index_packages_in`),
the staleness reads and the source-cache lookups; `db::builds` gains
`set_release_in`. Where a mutation is a *decision* — the collapse rules, the
approval gate, the publish compare-and-swap — the decision stays where it was
and the statements are called through the transaction handle, so the atomicity
is unchanged and only the SQL moved.
`audit_in` is the in-transaction audit append, and `audit` is now a wrapper
over it: a decision and its audit row commit together or not at all, which is
what I10 actually claims.
Enforcement is two-layered, because one layer is a lie. `Writer::call`, `tx`
and `send` are `pub(crate)`, which keeps the crate's *users* out — the
integration tests and the shim binaries — but `pub(crate)` is crate-wide, so
on its own it would not stop `approve.rs` from writing SQL tomorrow. The layer
that does is `tests/schema_ownership.rs`: it walks `src/` and fails the build
if a statement literal or a rusqlite statement call appears outside `src/db/`.
It was checked against a deliberate violation. The test harness still needs to
construct states no typed API reaches, so the raw door exists but only under
the `test-support` feature, which nothing but this crate's own dev-dependency
enables; a production binary cannot see it.
Diffstat (limited to 'src/watch')
| -rw-r--r-- | src/watch/detect.rs | 12 | ||||
| -rw-r--r-- | src/watch/digests.rs | 31 | ||||
| -rw-r--r-- | src/watch/staleness.rs | 67 |
3 files changed, 18 insertions, 92 deletions
diff --git a/src/watch/detect.rs b/src/watch/detect.rs index 71d1b29..deb0749 100644 --- a/src/watch/detect.rs +++ b/src/watch/detect.rs @@ -84,19 +84,9 @@ pub fn check_pkgbase(cfg: &Config, db: &Db, name: &str) -> Result<CheckReport> { } fn schedule_next(db: &Db, name: &str, pb: &Pkgbase) { - let interval = db - .read(|c| { - use rusqlite::OptionalExtension; - Ok(c.query_row( - "SELECT poll_interval_secs FROM subscription WHERE pkgbase=?1", - rusqlite::params![name], - |r| r.get::<_, Option<i64>>(0), - ) - .optional()?) - }) + let interval = db::subscription_poll_interval(db, name) .ok() .flatten() - .flatten() .unwrap_or(crate::db::setting_i64(db, "poll_interval", 86400)); let next = time::secs_ahead(interval); let _ = db::note_check(&db.writer, name, &next, None); diff --git a/src/watch/digests.rs b/src/watch/digests.rs index 07551ca..cb7bdbc 100644 --- a/src/watch/digests.rs +++ b/src/watch/digests.rs @@ -11,9 +11,8 @@ //! files are already in the mirror. use crate::config::Config; -use crate::db::*; +use crate::db::{self, *}; use crate::error::Result; -use crate::time; use crate::watch::mirror::Tree; const SKIP: &str = "SKIP"; @@ -144,37 +143,19 @@ pub fn download_and_cache(cfg: &Config, db: &Db, url: &str) -> Result<(String, u } else { let _ = std::fs::remove_file(&tmp); } - let at = time::now(); let dest_s = dest.display().to_string(); - let u = url.to_string(); - let digest_for_row = digest.clone(); - db.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", - rusqlite::params![u, digest_for_row, size as i64, at, dest_s], - )?; - Ok(()) - })?; + db::put_source_cache(&db.writer, url, &digest, size, &dest_s)?; Ok((digest, size)) } /// The cached file holding `digest`, if we still have it. The build uses this /// so the bytes it consumes are the bytes that were hashed at detection time. pub fn cached_path(db: &Db, url: &str, digest: &str) -> Option<std::path::PathBuf> { - let p: Option<String> = db - .read(|c| { - use rusqlite::OptionalExtension; - Ok(c.query_row( - "SELECT path FROM source_cache WHERE url=?1 AND digest=?2", - rusqlite::params![url, digest], - |r| r.get(0), - ) - .optional()?) - }) + db::source_cache_path(db, url, digest) .ok() - .flatten(); - p.map(std::path::PathBuf::from).filter(|p| p.exists()) + .flatten() + .map(std::path::PathBuf::from) + .filter(|p| p.exists()) } /// True when a `SKIP` on a non-VCS source means the recipe permanently refuses diff --git a/src/watch/staleness.rs b/src/watch/staleness.rs index 210632e..cc7242c 100644 --- a/src/watch/staleness.rs +++ b/src/watch/staleness.rs @@ -35,23 +35,10 @@ pub struct Stale { /// Every stale package, derived from world snapshots against the live index. pub fn stale_rows(db: &Db) -> Result<Vec<Stale>> { - let rows: Vec<(String, String, String, String)> = 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)?, String::new(), String::new())) - })?; - Ok(rows.filter_map(|r| r.ok()).collect::<Vec<_>>()) - })?; + let rows = db::published_world_members(db)?; let mut out = Vec::new(); - for (pkgbase, members_json, _, _) in rows { + for (pkgbase, members_json) in rows { let members: Vec<db::WorldMember> = serde_json::from_str(&members_json).unwrap_or_default(); for m in members { // A package is not stale relative to itself. @@ -93,25 +80,7 @@ pub fn stale_for(db: &Db, pkgbase: &str) -> Result<Vec<Stale>> { /// Derived on read from the newest candidate per pkgbase against the accepted /// release, so it disappears the moment either side moves. pub fn downgrades(db: &Db) -> Result<Vec<Stale>> { - let rows: Vec<(String, String, String)> = 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<String>>(2)?.unwrap_or_default(), - )) - })?; - Ok(rows.filter_map(|r| r.ok()).collect::<Vec<_>>()) - })?; + let rows = db::served_vs_offered_pkgvers(db)?; let mut out = Vec::new(); for (pkgbase, served, offered) in rows { if offered.is_empty() || !crate::pkgver::is_downgrade(&served, &offered) { @@ -137,28 +106,14 @@ pub fn dependents_of(db: &Db, name: &str) -> Result<Vec<String>> { if p.name == name { continue; } - let depends: bool = db.read(|c| { - use rusqlite::OptionalExtension; - let json: Option<String> = c - .query_row( - "SELECT 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.name = ?1 AND pk.state='in_index'", - rusqlite::params![p.name], - |r| r.get::<_, String>(0), - ) - .optional()?; - Ok(json - .and_then(|j| serde_json::from_str::<crate::db::model::Recipe>(&j).ok()) - .map(|r| { - crate::pkgver::dep_names(&r.depends) - .into_iter() - .any(|d| d == name) - }) - .unwrap_or(false)) - })?; + let depends = db::recipe_json_of_index_package(db, &p.name)? + .and_then(|j| serde_json::from_str::<crate::db::model::Recipe>(&j).ok()) + .map(|r| { + crate::pkgver::dep_names(&r.depends) + .into_iter() + .any(|d| d == name) + }) + .unwrap_or(false); if depends { out.push(p.name); } |
