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/subscriptions.rs | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/db/subscriptions.rs (limited to 'src/db/subscriptions.rs') diff --git a/src/db/subscriptions.rs b/src/db/subscriptions.rs new file mode 100644 index 0000000..947a205 --- /dev/null +++ b/src/db/subscriptions.rs @@ -0,0 +1,122 @@ +use rusqlite::{params, OptionalExtension, Row}; + +use crate::db::*; +use crate::error::Result; +use crate::time; + +// ------------------------------------------------------------- subscriptions -- + +#[derive(Debug, Clone)] +pub struct Subscription { + pub id: i64, + pub pkgbase: String, + pub aur_url: String, + pub maintainer: Option, + pub state: SubscriptionState, + pub next_check_at: Option, + pub last_checked_at: Option, + pub last_error: Option, + pub poll_interval_secs: Option, +} + +fn row_to_sub(r: &Row<'_>) -> rusqlite::Result { + Ok(Subscription { + id: r.get(0)?, + pkgbase: r.get(1)?, + aur_url: r.get(2)?, + maintainer: r.get(3)?, + state: crate::db::state_col::(4, &r.get::<_, String>(4)?)?, + next_check_at: r.get(5)?, + last_checked_at: r.get(6)?, + last_error: r.get(7)?, + poll_interval_secs: r.get(8)?, + }) +} + +const SUB_COLS: &str = "id, pkgbase, aur_url, maintainer, state, next_check_at, last_checked_at, \ + last_error, poll_interval_secs"; + +pub fn list_subscriptions(db: &Db) -> Result> { + db.read(|c| { + let mut stmt = c.prepare(&format!("SELECT {SUB_COLS} FROM subscription ORDER BY pkgbase"))?; + let rows = stmt.query_map([], row_to_sub)?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + +pub fn get_subscription(db: &Db, pkgbase: &str) -> Result> { + db.read(|c| { + c.query_row( + &format!("SELECT {SUB_COLS} FROM subscription WHERE pkgbase = ?1"), + params![pkgbase], + row_to_sub, + ) + .optional() + .map_err(Into::into) + }) +} + +pub fn subscribe(writer: &Writer, repo_id: i64, pkgbase: &str, aur_url: &str) -> Result { + let (p, u) = (pkgbase.to_string(), aur_url.to_string()); + let at = time::now(); + writer.tx(move |tx| { + tx.execute( + "INSERT INTO pkgbase (name, aur_url, created_at) VALUES (?1,?2,?3) + ON CONFLICT(name) DO UPDATE SET aur_url = excluded.aur_url", + params![p, u, at], + )?; + tx.execute( + "INSERT INTO subscription (repo_id, pkgbase, aur_url, state, next_check_at, created_at) + VALUES (?1,?2,?3,'active',?4,?5) + ON CONFLICT(repo_id, pkgbase) DO UPDATE SET state='active', aur_url=excluded.aur_url", + params![repo_id, p, u, at.clone(), at], + )?; + Ok(1) + }) +} + +pub fn set_subscription_state(writer: &Writer, pkgbase: &str, state: SubscriptionState) -> Result<()> { + let (p, s) = (pkgbase.to_string(), state.to_string()); + writer.tx(move |tx| { + tx.execute( + "UPDATE subscription SET state=?1 WHERE pkgbase=?2", + params![s, p], + )?; + Ok(()) + }) +} + +pub fn note_check(writer: &Writer, pkgbase: &str, next_check_at: &str, err: Option<&str>) -> Result<()> { + let (p, n) = (pkgbase.to_string(), next_check_at.to_string()); + let e = err.map(|s| s.to_string()); + let at = time::now(); + writer.tx(move |tx| { + tx.execute( + "UPDATE subscription SET last_checked_at=?1, next_check_at=?2, last_error=?3 + WHERE pkgbase=?4", + params![at, n, e, p], + )?; + Ok(()) + }) +} + +pub fn due_subscriptions(db: &Db, now: &str) -> Result> { + db.read(|c| { + let mut stmt = c.prepare(&format!( + "SELECT {SUB_COLS} FROM subscription + WHERE state IN ('active','orphaned') AND (next_check_at IS NULL OR next_check_at <= ?1) + ORDER BY next_check_at IS NULL DESC, next_check_at LIMIT 200" + ))?; + let rows = stmt.query_map(params![now], row_to_sub)?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} + -- cgit v1.2.3