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/publish_writes.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/db/publish_writes.rs (limited to 'src/db/publish_writes.rs') diff --git a/src/db/publish_writes.rs b/src/db/publish_writes.rs new file mode 100644 index 0000000..27ab4f6 --- /dev/null +++ b/src/db/publish_writes.rs @@ -0,0 +1,121 @@ +use rusqlite::{params, params_from_iter, OptionalExtension}; + +use crate::db::*; +use crate::error::Result; + +// ------------------------------------------------------------ publish writes -- + +pub fn artifact_path(db: &Db, id: i64) -> Result> { + db.read(|c| { + Ok(c.query_row("SELECT path FROM artifact WHERE id=?1", params![id], |r| r.get::<_, String>(0)) + .optional()?) + }) +} + +pub 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()?) +} + +pub fn accepted_pointer_in(tx: &rusqlite::Transaction<'_>, pkgbase_id: i64) -> Result> { + tx.query_row( + "SELECT current_accepted_release_id FROM pkgbase WHERE id=?1", + params![pkgbase_id], + |r| r.get::<_, Option>(0), + ) + .optional() + .map(Option::flatten) + .map_err(Into::into) +} + +pub fn cas_accepted_release_in( + tx: &rusqlite::Transaction<'_>, + pkgbase_id: i64, + new_release_id: i64, + expected: Option, +) -> Result { + let moved = tx.execute( + "UPDATE pkgbase SET current_accepted_release_id=?1 WHERE id=?2 AND current_accepted_release_id IS ?3", + params![new_release_id, pkgbase_id, expected], + )?; + Ok(moved == 1) +} + +pub fn remove_index_packages_in(tx: &rusqlite::Transaction<'_>, names: &[String]) -> Result<()> { + if names.is_empty() { + return Ok(()); + } + let ph = names.iter().map(|_| "?").collect::>().join(","); + let sql = format!("UPDATE package SET state='removed' WHERE state='in_index' AND name IN ({ph})"); + let mut stmt = tx.prepare(&sql)?; + stmt.execute(params_from_iter(names.iter().map(|n| n as &dyn rusqlite::ToSql)))?; + Ok(()) +} + +pub fn remove_package_by_name_in(tx: &rusqlite::Transaction<'_>, repo_id: i64, name: &str) -> Result<()> { + tx.execute( + "UPDATE package SET state='removed' WHERE repo_id=?1 AND name=?2", + params![repo_id, name], + )?; + Ok(()) +} + +pub struct NewRelease<'a> { + pub pkgbase_id: i64, + pub candidate_id: Option, + pub approval_id: Option, + pub aur_commit: &'a str, + pub recipe_hash: &'a str, + pub upstream_pins_json: &'a str, + pub source_digests_json: &'a str, + pub epoch: &'a str, + pub pkgver: &'a str, + pub pkgrel: &'a str, + pub world_snapshot_id: Option, + pub build_id: &'a str, +} + +pub fn insert_release_in(tx: &mut rusqlite::Transaction<'_>, r: &NewRelease<'_>, at: &str) -> Result { + tx.execute( + "INSERT INTO release (pkgbase_id, candidate_id, approval_id, aur_commit, recipe_hash, + upstream_pins_json, source_digests_json, epoch, pkgver, pkgrel, + world_snapshot_id, state, build_id, published_at, created_at) + VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,'published',?12,?13,?13)", + params![ + r.pkgbase_id, + r.candidate_id, + r.approval_id, + r.aur_commit, + r.recipe_hash, + r.upstream_pins_json, + r.source_digests_json, + r.epoch, + r.pkgver, + r.pkgrel, + r.world_snapshot_id, + r.build_id, + at + ], + )?; + Ok(tx.last_insert_rowid()) +} + +pub fn insert_package_in( + tx: &rusqlite::Transaction<'_>, + repo_id: i64, + release_id: i64, + name: &str, + arch: &str, + version: &str, + digest: &str, +) -> Result<()> { + let artifact_id = artifact_id_by_digest_in(tx, digest)?; + tx.execute( + "INSERT INTO package (repo_id, release_id, name, arch, version, artifact_id, state) + VALUES (?1,?2,?3,?4,?5,?6,'in_index')", + params![repo_id, release_id, name, arch, version, artifact_id], + )?; + Ok(()) +} + -- cgit v1.2.3