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/model/mod.rs | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 src/db/model/mod.rs (limited to 'src/db/model/mod.rs') diff --git a/src/db/model/mod.rs b/src/db/model/mod.rs new file mode 100644 index 0000000..741f1f4 --- /dev/null +++ b/src/db/model/mod.rs @@ -0,0 +1,217 @@ +use serde::{Deserialize, Serialize}; + +use crate::hash; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct UpstreamPin { + pub index: usize, + pub url: String, + pub ref_: String, + pub branch: Option, + pub commit: String, +} + +#[derive(Debug, Clone)] +pub struct StageBCacheRow { + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub pins: Vec, +} + +impl StageBCacheRow { + pub fn version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SourceDigest { + pub index: usize, + pub url: String, + pub kind: String, + pub digest: String, + pub size: u64, + pub skip_declared: bool, +} + +impl SourceDigest { + pub fn is_vcs(&self) -> bool { + matches!(self.kind.as_str(), "vcs" | "git" | "svn" | "hg" | "bzr") + } +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct WorldMember { + pub pkgbase: String, + pub version: String, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Identity { + pub aur_commit: String, + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub upstream_pins: Vec, + pub source_digests: Vec, +} + +impl Identity { + pub fn hash(&self) -> crate::error::Result { + Ok(format!("sha256:{}", hash::hex(hash::canonical_json(self)?.as_bytes()))) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SourceEntry { + pub index: usize, + pub url: String, + pub filename: Option, + pub commit: Option, + pub tag: Option, + pub branch: Option, + pub vcs: Option, + pub local: bool, +} + +impl SourceEntry { + pub fn is_vcs(&self) -> bool { + self.vcs.is_some() + } +} + +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Recipe { + pub pkgbase: String, + pub pkgver: String, + pub pkgrel: String, + pub epoch: String, + pub pkgdesc: String, + pub url: String, + pub arch: Vec, + pub license: Vec, + pub groups: Vec, + pub depends: Vec, + pub makedepends: Vec, + pub checkdepends: Vec, + pub optdepends: Vec, + pub provides: Vec, + pub conflicts: Vec, + pub source: Vec, + pub sha256sums: Vec, + pub b2sums: Vec, + pub validpgpkeys: Vec, + pub options: Vec, + pub backup: Vec, + pub install: Option, + pub pkgnames: Vec, + pub pkg_depends: Vec<(String, Vec)>, + pub pkgbuild: String, + pub srcinfo: Option, + pub skipgpcheck: bool, + #[serde(skip, default)] + pub source_entries: Vec, +} + +impl Recipe { + pub fn full_version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ReleaseRow { + pub id: i64, + pub pkgbase_id: i64, + pub candidate_id: Option, + pub approval_id: Option, + pub aur_commit: String, + pub recipe_hash: String, + pub upstream_pins_json: String, + pub source_digests_json: String, + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub world_snapshot_id: Option, + pub state: ReleaseState, + pub published_at: Option, +} + +impl ReleaseRow { + pub fn version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } + pub fn pins(&self) -> Vec { + serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() + } + pub fn digests(&self) -> Vec { + serde_json::from_str(&self.source_digests_json).unwrap_or_default() + } +} + +#[derive(Debug, Clone, Serialize)] +pub struct CandidateRow { + pub id: i64, + pub pkgbase_id: i64, + pub base_release_id: Option, + pub kind: String, + pub snapshot_id: i64, + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub upstream_pins_json: String, + pub source_digests_json: String, + pub identity_hash: String, + pub diff_digest: String, + pub revision: i64, + pub viewed_at: Option, + pub risk_level: String, + pub risk_signals_json: String, + pub missing_deps_json: String, + pub state: CandidateState, + pub detected_at: String, + pub resolved_at: Option, + pub pkgbase: String, +} + +impl CandidateRow { + pub fn version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } + pub fn pins(&self) -> Vec { + serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() + } + pub fn digests(&self) -> Vec { + serde_json::from_str(&self.source_digests_json).unwrap_or_default() + } + pub fn risk_signals(&self) -> Vec { + serde_json::from_str(&self.risk_signals_json).unwrap_or_default() + } + pub fn missing_deps(&self) -> Vec { + serde_json::from_str(&self.missing_deps_json).unwrap_or_default() + } +} + +pub fn json_or(s: &str, fallback: T) -> T { + serde_json::from_str(s).unwrap_or(fallback) +} + +// The state machines sit beside the row shapes rather than inside them: a row +// says what is stored, a state machine says which moves are legal. +mod build_state; +mod state; + +pub use build_state::{ + BUILD_STEPS, + BuildState, + StepState, +}; +pub use state::{ + CandidateState, + ReleaseState, + StateEnum, + SubscriptionState, + candidate_kind, + notice_kind, +}; -- cgit v1.2.3