diff options
| author | aur-build <aur-build@localhost> | 2026-11-17 20:59:57 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-17 20:59:57 +0000 |
| commit | fd3ecf91686b11feea078531baeddb60e6941116 (patch) | |
| tree | 7e76c0d8a2041bf0d8d227939db9e55ea5b2afb5 /src/db/model.rs | |
| parent | 3762c7ce9f765283f47e5716b708e04590339375 (diff) | |
| download | aur-build-fd3ecf91686b11feea078531baeddb60e6941116.tar.gz aur-build-fd3ecf91686b11feea078531baeddb60e6941116.zip | |
feat: SQLite schema, migrations, single writer actor
The §11 data model as four migrations, plus the §11a concurrency model:
one writer thread owning the write connection (mutations arrive as
closures over a crossbeam channel, so SQLITE_BUSY is not part of the
design) and thread-local read-only connections under WAL.
Invariants that are database guarantees rather than conventions:
I7 partial unique index -> at most one pending_review per pkgbase
I3 UNIQUE(pkgbase_id, identity_hash) -> no duplicate candidates
I10 audit table rejects UPDATE and DELETE by trigger
§16 current_accepted_release_id moves only by compare-and-swap
Also: vercmp + dependency-string parsing (ALPM algorithm, no libalpm),
hash helpers including recipe_hash over the whole AUR tree, the clock
helper that stamps every write, and the config/root layout.
Part of P0 (DESIGN.md §17).
Diffstat (limited to 'src/db/model.rs')
| -rw-r--r-- | src/db/model.rs | 286 |
1 files changed, 286 insertions, 0 deletions
diff --git a/src/db/model.rs b/src/db/model.rs new file mode 100644 index 0000000..599fc21 --- /dev/null +++ b/src/db/model.rs @@ -0,0 +1,286 @@ +//! Row types and small value objects that appear in JSON columns. + +use serde::{Deserialize, Serialize}; + +use crate::hash; + +/// One resolved upstream VCS source (DESIGN.md §3, "Three things to pin"). +/// +/// The commit SHA alone is sufficient: a git commit transitively commits to its +/// tree, and a gitlink in that tree commits to any submodule's commit. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct UpstreamPin { + /// Index into the PKGBUILD `source=()` array. + pub index: usize, + pub url: String, + /// The ref we asked for: `HEAD`, a tag, or an explicit commit. + pub ref_: String, + /// Branch the ref resolved to, when known. + pub branch: Option<String>, + /// Resolved commit SHA. This is the pin. + pub commit: String, +} + +/// A source entry with the digest *we* computed, regardless of what the +/// PKGBUILD declares (§5.3). +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SourceDigest { + pub index: usize, + pub url: String, + /// `static` | `vcs` | `local` + pub kind: String, + /// sha256, or the resolved VCS commit for vcs sources. + pub digest: String, + pub size: u64, + /// True when the PKGBUILD declared `SKIP` (or nothing) for this source. + pub skip_declared: bool, +} + +/// A member of a world snapshot: one of *our* packages the build ran against. +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct WorldMember { + pub pkgbase: String, + pub version: String, +} + +/// The identity tuple of a candidate (§3). Every element is part of the key. +#[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<UpstreamPin>, + pub source_digests: Vec<SourceDigest>, +} + +impl Identity { + /// The hash that makes I3 a fact: two byte-identical inputs never produce + /// two candidates, and any change to what becomes a byte in the artifact + /// produces a new one. + pub fn hash(&self) -> String { + hash::canonical_json(self) + } +} + +/// Candidate/review states (§7.1). +pub mod candidate_state { + pub const PENDING: &str = "pending_review"; + pub const APPROVED: &str = "approved"; + pub const REJECTED: &str = "rejected"; + pub const SUPERSEDED: &str = "superseded"; + pub const BLOCKED: &str = "blocked"; +} + +pub mod candidate_kind { + pub const NEW_VERSION: &str = "new_version"; + pub const FIRST_INTRODUCTION: &str = "first_introduction"; +} + +pub mod release_state { + pub const PUBLISHED: &str = "published"; + pub const WITHDRAWN: &str = "withdrawn"; + pub const FAILED_UNBUILT: &str = "failed_unbuilt"; +} + +pub mod build_state { + pub const QUEUED: &str = "queued"; + pub const LEASED: &str = "leased"; + pub const RUNNING: &str = "running"; + pub const BLOCKED_MISSING_DEP: &str = "blocked_missing_dep"; + pub const SUCCEEDED: &str = "succeeded"; + pub const FAILED: &str = "failed"; + pub const CANCELLED: &str = "cancelled"; +} + +pub mod step_state { + pub const PENDING: &str = "pending"; + pub const RUNNING: &str = "running"; + pub const SUCCEEDED: &str = "succeeded"; + pub const FAILED: &str = "failed"; + pub const INTERRUPTED: &str = "interrupted"; + pub const SKIPPED: &str = "skipped"; +} + +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"; +} + +pub mod subscription_state { + pub const ACTIVE: &str = "active"; + pub const PAUSED: &str = "paused"; + pub const ORPHANED: &str = "orphaned"; + pub const REMOVED: &str = "removed"; +} + +/// The pipeline steps (§8.1). Order matters: a build resumes at the first step +/// in this list without a `succeeded` row. +pub const BUILD_STEPS: &[&str] = &[ + "materialize", + "plan", + "chroot", + "sources", + "pkgver", + "build", + "collect", + "verify", + "ingest", + "publish", +]; + +/// A `source=()` entry after parsing. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SourceEntry { + pub index: usize, + /// The URL as written, minus the filename fragment. + pub url: String, + /// Explicit filename (`::name` / `::` prefix handling). + pub filename: Option<String>, + /// `#commit=` fragment. + pub commit: Option<String>, + /// `#tag=` fragment. + pub tag: Option<String>, + /// `#branch=` fragment. + pub branch: Option<String>, + /// VCS kind if the url is prefixed `git+`, `svn+`, `hg+`, `bzr+`. + pub vcs: Option<String>, + /// True for files shipped inside the AUR repo (no scheme). + pub local: bool, +} + +impl SourceEntry { + pub fn is_vcs(&self) -> bool { + self.vcs.is_some() + } +} + +/// A parsed recipe: what `.SRCINFO` / `PKGBUILD` tell us. +#[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<String>, + pub license: Vec<String>, + pub groups: Vec<String>, + pub depends: Vec<String>, + pub makedepends: Vec<String>, + pub checkdepends: Vec<String>, + pub optdepends: Vec<String>, + pub provides: Vec<String>, + pub conflicts: Vec<String>, + pub source: Vec<String>, + pub sha256sums: Vec<String>, + pub b2sums: Vec<String>, + pub validpgpkeys: Vec<String>, + pub options: Vec<String>, + pub backup: Vec<String>, + pub install: Option<String>, + /// Names of the binary packages this pkgbase emits (split pkgbases have N). + pub pkgnames: Vec<String>, + /// Per-package depends, for split pkgbases. + pub pkg_depends: Vec<(String, Vec<String>)>, + /// Raw PKGBUILD text, for display and diffing. + pub pkgbuild: String, + /// Raw .SRCINFO text when it was present. + pub srcinfo: Option<String>, + /// True if `--skippgpcheck` appears in the PKGBUILD. + pub skipgpcheck: bool, +} + +impl Recipe { + /// `epoch:pkgver-pkgrel` as displayed to consumers. + pub fn full_version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } +} + +/// A source row as stored on a candidate/release. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ReleaseRow { + pub id: i64, + pub pkgbase_id: i64, + pub candidate_id: Option<i64>, + pub approval_id: Option<i64>, + 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<i64>, + pub state: String, + pub published_at: Option<String>, +} + +impl ReleaseRow { + pub fn version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } + pub fn pins(&self) -> Vec<UpstreamPin> { + serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() + } + pub fn digests(&self) -> Vec<SourceDigest> { + 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<i64>, + 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<String>, + pub risk_level: String, + pub risk_signals_json: String, + pub missing_deps_json: String, + pub state: String, + pub detected_at: String, + pub resolved_at: Option<String>, + 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<UpstreamPin> { + serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() + } + pub fn digests(&self) -> Vec<SourceDigest> { + serde_json::from_str(&self.source_digests_json).unwrap_or_default() + } + pub fn risk_signals(&self) -> Vec<String> { + serde_json::from_str(&self.risk_signals_json).unwrap_or_default() + } + pub fn missing_deps(&self) -> Vec<String> { + serde_json::from_str(&self.missing_deps_json).unwrap_or_default() + } +} + +/// Parse a JSON column with a fallback, so one bad row never takes down a page. +pub fn json_or<T: serde::de::DeserializeOwned>(s: &str, fallback: T) -> T { + serde_json::from_str(s).unwrap_or(fallback) +} |
