From 154f6f6dd284c11da82cf1188ff8549db6459228 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 20 Nov 2026 17:26:52 +0000 Subject: db: close what nothing reaches, and say what cannot be closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit db handed out 226 items and 36 of them were named by nobody at all — not by the crate, not by the tests. Making them private is the whole of this commit, but the interesting part is what the compiler said afterwards. Twenty-five closed cleanly. Eleven did not, and they are the ones worth writing down: AuditRow, NoticeRow, PackageRow, Subscription, StepRow, Unbuilt, OpenReview, ReviewSubject, Migration, Writer, predecessors. None is named outside db, yet none can be made private, because a public query returns them and callers bind them with `let` and call methods on them. They are load-bearing and invisible to a name search. So the census's `dead` count means "the name is never written outside the module", which is a lead rather than a verdict, and the census now says so: the way to find out which flagged items are real is to privatize them and let rustc answer. Those eleven are budgeted with that reason, not zeroed to make a number look good. Nineteen items turned out to be dead code rather than merely unexported — set_release, set_candidate, set_pkgbase, set_profile, set_kind, current_state, index_pkgbase_drift, wal_stats, call_for_tests, put_notice, is_ours, list_pkgbases, set_unpinable, artifact_id_by_digest_in, subscription_poll_interval, the four never-emitted notice kinds, and StateEnum::as_str, which the impl_state_enum macro was required to define for all five enums while nothing called it through the trait. Privatizing is what proved they were unused; leaving them private-but-present would have just hidden dead code behind a narrower address. Ten items became pub(crate): the Writer actor's call/tx/send surface, Readers, the StateEnum trait, json_or, predecessors, DEFAULT_SETTINGS. `Db.readers` was named nowhere outside db, so the field closed with the type. db: 226 pub / 36 dead-only -> 201 pub / 11 dead-only. 277 passed / 0 failed, clippy -D warnings clean, schema_ownership and architecture green. --- src/db/builds/mod.rs | 27 ++++--------------- src/db/builds/steps.rs | 45 +------------------------------- src/db/invariants.rs | 30 ---------------------- src/db/migrations.rs | 2 +- src/db/mod.rs | 67 +++++++++++++++++++----------------------------- src/db/model/mod.rs | 19 ++++---------- src/db/model/state.rs | 12 ++------- src/db/notices.rs | 18 ------------- src/db/packages.rs | 12 --------- src/db/pkgbases.rs | 20 --------------- src/db/publish_writes.rs | 2 +- src/db/retention.rs | 2 +- src/db/staleness.rs | 11 -------- 13 files changed, 42 insertions(+), 225 deletions(-) (limited to 'src/db') diff --git a/src/db/builds/mod.rs b/src/db/builds/mod.rs index 2e116bb..2035cfd 100644 --- a/src/db/builds/mod.rs +++ b/src/db/builds/mod.rs @@ -286,27 +286,10 @@ pub fn cancel_requested(db: &Db, id: &str) -> Result { // legal moves. mod steps; + pub use steps::{ - StepRow, - begin_step, - blocked_builds, - cancel_superseded, - claim_batch, - current_state, - finish_step, - in_flight, - queue_depth, - requeue_transient, - retry, - set_blocked_on, - set_candidate, - set_kind, - set_pkgbase, - set_profile, - set_release, - set_release_in, - set_world, - step_status, - steps, - touch, + begin_step, blocked_builds, cancel_superseded, claim_batch, + finish_step, in_flight, queue_depth, requeue_transient, + retry, set_blocked_on, set_release_in, set_world, + step_status, steps, touch, }; diff --git a/src/db/builds/steps.rs b/src/db/builds/steps.rs index 64d57c7..01d7c40 100644 --- a/src/db/builds/steps.rs +++ b/src/db/builds/steps.rs @@ -1,4 +1,4 @@ -use rusqlite::{params, Connection, OptionalExtension}; +use rusqlite::{params, OptionalExtension}; use super::transition_in_tx; use crate::db::{BuildState, Db, StepState}; @@ -163,10 +163,6 @@ pub fn set_release_in(tx: &rusqlite::Transaction<'_>, id: &str, release_id: i64) Ok(()) } -pub fn set_release(db: &Db, id: &str, release_id: i64) -> Result<()> { - let id = id.to_string(); - db.writer.tx(move |tx| set_release_in(tx, &id, release_id)) -} pub fn set_world(db: &Db, id: &str, world_id: i64) -> Result<()> { let id = id.to_string(); @@ -176,37 +172,9 @@ pub fn set_world(db: &Db, id: &str, world_id: i64) -> Result<()> { }) } -pub fn set_candidate(db: &Db, id: &str, candidate_id: i64) -> Result<()> { - let id = id.to_string(); - db.writer.tx(move |tx| { - tx.execute("UPDATE build SET candidate_id=?1 WHERE id=?2", params![candidate_id, id])?; - Ok(()) - }) -} -pub fn set_pkgbase(db: &Db, id: &str, pkgbase_id: i64) -> Result<()> { - let id = id.to_string(); - db.writer.tx(move |tx| { - tx.execute("UPDATE build SET pkgbase_id=?1 WHERE id=?2", params![pkgbase_id, id])?; - Ok(()) - }) -} -pub fn set_profile(db: &Db, id: &str, profile: &str) -> Result<()> { - let (id, p) = (id.to_string(), profile.to_string()); - db.writer.tx(move |tx| { - tx.execute("UPDATE build SET build_profile=?1 WHERE id=?2", params![p, id])?; - Ok(()) - }) -} -pub fn set_kind(db: &Db, id: &str, kind: &str) -> Result<()> { - let (id, k) = (id.to_string(), kind.to_string()); - db.writer.tx(move |tx| { - tx.execute("UPDATE build SET kind=?1 WHERE id=?2", params![k, id])?; - Ok(()) - }) -} pub fn set_blocked_on(db: &Db, id: &str, blocked_on: &[String]) -> Result<()> { let id = id.to_string(); @@ -217,17 +185,6 @@ pub fn set_blocked_on(db: &Db, id: &str, blocked_on: &[String]) -> Result<()> { }) } -pub fn current_state(c: &Connection, id: &str) -> Result> { - let s: Option = c - .query_row("SELECT state FROM build WHERE id=?1", params![id], |r| r.get(0)) - .optional()?; - match s { - None => Ok(None), - Some(text) => BuildState::parse(&text) - .map(Some) - .ok_or_else(|| Error::invalid(format!("build {id} has a state outside the state machine: {text:?}"))), - } -} pub fn claim_batch(db: &Db, owner: &str, limit: usize, lease_secs: i64) -> Result> { if limit == 0 { diff --git a/src/db/invariants.rs b/src/db/invariants.rs index 398590b..9853003 100644 --- a/src/db/invariants.rs +++ b/src/db/invariants.rs @@ -21,36 +21,6 @@ pub fn unapproved_index_entries(db: &Db) -> Result> { }) } -pub fn index_pkgbase_drift(db: &Db) -> Result<(Vec, Vec)> { - 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 { diff --git a/src/db/migrations.rs b/src/db/migrations.rs index 0a1ca27..cb01765 100644 --- a/src/db/migrations.rs +++ b/src/db/migrations.rs @@ -334,7 +334,7 @@ pub static MIGRATIONS: &[Migration] = &[ Migration { version: 8, name: "retention", sql: M_0008_RETENTION }, ]; -pub const DEFAULT_SETTINGS: &[(&str, &str)] = &[ +pub(crate) const DEFAULT_SETTINGS: &[(&str, &str)] = &[ ("max_concurrent_builds", "1"), ("build_timeout", "5400"), ("poll_interval", "86400"), diff --git a/src/db/mod.rs b/src/db/mod.rs index d4befcc..13d7d9d 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -25,32 +25,32 @@ mod subscriptions; mod world; pub use migrations::MIGRATIONS; +pub(crate) use model::StateEnum; +pub(crate) use model::json_or; pub use model::{ - BUILD_STEPS, BuildState, CandidateRow, CandidateState, Identity, Recipe, ReleaseRow, - ReleaseState, SourceDigest, SourceEntry, StageBCacheRow, StateEnum, StepState, - SubscriptionState, UpstreamPin, WorldMember, candidate_kind, json_or, notice_kind, + BUILD_STEPS, BuildState, CandidateRow, CandidateState, + Identity, Recipe, ReleaseRow, ReleaseState, + SourceDigest, SourceEntry, StageBCacheRow, StepState, + SubscriptionState, UpstreamPin, WorldMember, candidate_kind, + notice_kind, }; -pub use audit::{AuditEntry, AuditRow, audit, audit_in, audit_system, list_audit}; -pub use invariants::{Unbuilt, approved_but_unbuilt, index_pkgbase_drift, unapproved_index_entries}; +pub use audit::{AuditEntry, audit, audit_in, audit_system, list_audit}; +pub use invariants::{approved_but_unbuilt, unapproved_index_entries}; pub use metrics::{ artifact_bytes_served, build_state_counts, count_index_packages, count_pending_reviews, - count_published_releases, count_subscriptions, count_unacknowledged_notices, - last_gc_age_secs, last_poll_age_secs, oldest_queued_age_secs, pending_review_ages_secs, + count_published_releases, count_subscriptions, count_unacknowledged_notices, last_gc_age_secs, + last_poll_age_secs, oldest_queued_age_secs, pending_review_ages_secs, }; -pub use notices::{NoticeRow, acknowledge_notice, list_notices, put_notice, put_notice_once}; +pub use notices::{acknowledge_notice, list_notices, put_notice_once}; pub use packages::{ - PackageRow, accepted_recipe, builds_for_pkgbase, index_artifact, index_packages, - index_version, is_ours, our_package_names, -}; -pub use pkgbases::{ - Pkgbase, cas_accepted_release, get_pkgbase, get_pkgbase_by_id, list_pkgbases, set_aur_head, - set_unpinable, + accepted_recipe, builds_for_pkgbase, index_artifact, index_packages, + index_version, our_package_names, }; +pub use pkgbases::{Pkgbase, cas_accepted_release, get_pkgbase, get_pkgbase_by_id, set_aur_head}; pub use publish_writes::{ - NewRelease, accepted_pointer_in, artifact_id_by_digest_in, artifact_path, - cas_accepted_release_in, insert_package_in, insert_release_in, remove_index_packages_in, - remove_package_by_name_in, + NewRelease, accepted_pointer_in, artifact_path, cas_accepted_release_in, + insert_package_in, insert_release_in, remove_index_packages_in, remove_package_by_name_in, }; pub use releases::{ accepted_release, all_published_releases, release_by_id, releases_for_pkgbase, @@ -58,19 +58,19 @@ pub use releases::{ }; pub use repos::{ensure_repo, repo_id}; pub use retention::{ - ArtifactPaths, mark_release_purged, pkgbase_release_pointers, published_release_ids, - record_gc_run, release_artifact_paths, serving_release_ids, + mark_release_purged, pkgbase_release_pointers, published_release_ids, record_gc_run, + release_artifact_paths, serving_release_ids, }; pub use settings::{all_settings, set_setting, setting, setting_i64}; pub use snapshots::{SnapshotRow, put_snapshot, snapshot_by_commit, snapshot_by_id}; pub use stage_b::{stage_b_cache_get, stage_b_cache_put, stage_b_recent}; pub use staleness::{ - NewArtifact, approval_note, candidate_snapshot_id, index_recipes, insert_artifact_in, - published_world_members, put_source_cache, served_vs_offered_pkgvers, source_cache_path, - subscription_poll_interval, + NewArtifact, approval_note, candidate_snapshot_id, index_recipes, + insert_artifact_in, published_world_members, put_source_cache, served_vs_offered_pkgvers, + source_cache_path, }; pub use subscriptions::{ - Subscription, due_subscriptions, get_subscription, list_subscriptions, note_check, + due_subscriptions, get_subscription, list_subscriptions, note_check, set_subscription_state, subscribe, }; pub use world::{put_world_snapshot, world_members}; @@ -88,7 +88,6 @@ pub(crate) fn state_col(col: usize, text: &str) -> rusqlite::Resul }) } - use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -197,7 +196,7 @@ impl Writer { &self.path } - pub fn is_alive(&self) -> bool { + fn is_alive(&self) -> bool { self.alive.load(Ordering::SeqCst) } @@ -256,16 +255,9 @@ impl Writer { { self.tx(f) } - - pub fn call_for_tests(&self, f: impl FnOnce(&mut Connection) -> Result + Send + 'static) -> Result - where - T: Send + 'static, - { - self.call(f) - } } -pub struct Readers { +pub(crate) struct Readers { path: PathBuf, } @@ -300,7 +292,7 @@ impl Readers { #[derive(Clone)] pub struct Db { pub writer: Arc, - pub readers: Arc, + pub(crate) readers: Arc, } impl Db { @@ -338,10 +330,3 @@ pub fn schema_version(path: &Path) -> Result { .map_err(|e| Error::db(format!("{} has no schema version: {e}", path.display()))) } -pub fn wal_stats(db: &Db) -> (u64, u64) { - let db_file = std::fs::metadata(db.writer.path()).map(|m| m.len()).unwrap_or(0); - let mut wal = db.writer.path().as_os_str().to_os_string(); - wal.push("-wal"); - let wal_file = std::fs::metadata(&wal).map(|m| m.len()).unwrap_or(0); - (db_file, wal_file) -} diff --git a/src/db/model/mod.rs b/src/db/model/mod.rs index 741f1f4..acfcda5 100644 --- a/src/db/model/mod.rs +++ b/src/db/model/mod.rs @@ -193,7 +193,7 @@ impl CandidateRow { } } -pub fn json_or(s: &str, fallback: T) -> T { +pub(crate) fn json_or(s: &str, fallback: T) -> T { serde_json::from_str(s).unwrap_or(fallback) } @@ -202,16 +202,7 @@ pub fn json_or(s: &str, fallback: T) -> T { mod build_state; mod state; -pub use build_state::{ - BUILD_STEPS, - BuildState, - StepState, -}; -pub use state::{ - CandidateState, - ReleaseState, - StateEnum, - SubscriptionState, - candidate_kind, - notice_kind, -}; +pub(crate) use state::StateEnum; + +pub use build_state::{BUILD_STEPS, BuildState, StepState}; +pub use state::{CandidateState, ReleaseState, SubscriptionState, candidate_kind, notice_kind}; diff --git a/src/db/model/state.rs b/src/db/model/state.rs index 4882428..777840f 100644 --- a/src/db/model/state.rs +++ b/src/db/model/state.rs @@ -53,7 +53,7 @@ impl CandidateState { } } - pub fn predecessors(to: CandidateState) -> Vec<&'static str> { + pub(crate) fn predecessors(to: CandidateState) -> Vec<&'static str> { CandidateState::ALL .iter() .filter(|from| from.can_transition(to)) @@ -118,10 +118,9 @@ impl std::fmt::Display for ReleaseState { } } -pub trait StateEnum: Copy + Sized { +pub(crate) trait StateEnum: Copy + Sized { const KIND: &'static str; fn parse(text: &str) -> Option; - fn as_str(&self) -> &'static str; } macro_rules! impl_state_enum { @@ -131,9 +130,6 @@ macro_rules! impl_state_enum { fn parse(text: &str) -> Option { <$t>::parse(text) } - fn as_str(&self) -> &'static str { - <$t>::as_str(self) - } } }; } @@ -147,12 +143,8 @@ impl_state_enum!(SubscriptionState, "subscription"); pub mod notice_kind { pub const PIN_DRIFT: &str = "pin_drift"; pub const RECIPE_DRIFT: &str = "recipe_drift"; - pub const DIGEST_DRIFT: &str = "digest_drift"; pub const INTEGRITY_ALERT: &str = "integrity_alert"; - pub const UNPINABLE_PKGVER: &str = "unpinable_pkgver"; - pub const ORPHANED: &str = "orphaned"; pub const DELETED: &str = "deleted"; - pub const RENAMED: &str = "renamed"; pub const DOWNGRADE: &str = "downgrade"; } diff --git a/src/db/notices.rs b/src/db/notices.rs index 4e0e4f2..36263a4 100644 --- a/src/db/notices.rs +++ b/src/db/notices.rs @@ -6,24 +6,6 @@ use crate::time; // ----------------------------------------------------------------- notice -- -pub fn put_notice( - writer: &Writer, - pkgbase_id: i64, - kind: &str, - severity: &str, - detail: serde_json::Value, -) -> Result { - let (k, s, d) = (kind.to_string(), severity.to_string(), detail.to_string()); - let at = time::now(); - writer.tx(move |tx| { - tx.execute( - "INSERT INTO notice (pkgbase_id, kind, severity, detail_json, created_at) - VALUES (?1,?2,?3,?4,?5)", - params![pkgbase_id, k, s, d, at], - )?; - Ok(tx.last_insert_rowid()) - }) -} pub fn put_notice_once( writer: &Writer, diff --git a/src/db/packages.rs b/src/db/packages.rs index 6708c99..0a302b0 100644 --- a/src/db/packages.rs +++ b/src/db/packages.rs @@ -78,18 +78,6 @@ pub fn index_artifact(db: &Db, name: &str) -> Result> { }) } -pub fn is_ours(db: &Db, name: &str) -> Result { - db.read(|c| { - c.query_row( - "SELECT 1 FROM package WHERE name=?1 AND state='in_index' LIMIT 1", - params![name], - |r| r.get::<_, i64>(0), - ) - .optional() - .map(|o| o.is_some()) - .map_err(Into::into) - }) -} pub fn our_package_names(db: &Db) -> Result> { db.read(|c| { diff --git a/src/db/pkgbases.rs b/src/db/pkgbases.rs index a652093..bf1fa7b 100644 --- a/src/db/pkgbases.rs +++ b/src/db/pkgbases.rs @@ -57,17 +57,6 @@ pub fn get_pkgbase_by_id(db: &Db, id: i64) -> Result> { }) } -pub fn list_pkgbases(db: &Db) -> Result> { - db.read(|c| { - let mut stmt = c.prepare(&format!("SELECT {PKGBASE_COLS} FROM pkgbase ORDER BY name"))?; - let rows = stmt.query_map([], row_to_pkgbase)?; - let mut out = Vec::new(); - for r in rows { - out.push(r?); - } - Ok(out) - }) -} pub fn set_aur_head(writer: &Writer, pkgbase_id: i64, head: &str, mirror: &str) -> Result<()> { let (h, m) = (head.to_string(), mirror.to_string()); @@ -80,15 +69,6 @@ pub fn set_aur_head(writer: &Writer, pkgbase_id: i64, head: &str, mirror: &str) }) } -pub fn set_unpinable(writer: &Writer, pkgbase_id: i64, unpinable: bool) -> Result<()> { - writer.tx(move |tx| { - tx.execute( - "UPDATE pkgbase SET unpinable=?1 WHERE id=?2", - params![unpinable as i64, pkgbase_id], - )?; - Ok(()) - }) -} pub fn cas_accepted_release( writer: &Writer, diff --git a/src/db/publish_writes.rs b/src/db/publish_writes.rs index 27ab4f6..e5ce73f 100644 --- a/src/db/publish_writes.rs +++ b/src/db/publish_writes.rs @@ -12,7 +12,7 @@ pub fn artifact_path(db: &Db, id: i64) -> Result> { }) } -pub fn artifact_id_by_digest_in(tx: &rusqlite::Transaction<'_>, digest: &str) -> Result> { +fn artifact_id_by_digest_in(tx: &rusqlite::Transaction<'_>, digest: &str) -> Result> { Ok(tx .query_row("SELECT id FROM artifact WHERE digest=?1", params![digest], |r| r.get::<_, i64>(0)) .optional()?) diff --git a/src/db/retention.rs b/src/db/retention.rs index ca468e1..3e6f22f 100644 --- a/src/db/retention.rs +++ b/src/db/retention.rs @@ -45,7 +45,7 @@ pub fn serving_release_ids(db: &Db, pkgbase_id: i64) -> Result> { }) } -pub type ArtifactPaths = (Option, Option, Option); +type ArtifactPaths = (Option, Option, Option); pub fn release_artifact_paths(db: &Db, release_id: i64) -> Result> { db.read(|c| { diff --git a/src/db/staleness.rs b/src/db/staleness.rs index 8987e85..59fccee 100644 --- a/src/db/staleness.rs +++ b/src/db/staleness.rs @@ -77,17 +77,6 @@ pub fn candidate_snapshot_id(db: &Db, candidate_id: i64) -> Result> }) } -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()); -- cgit v1.2.3