From fd3ecf91686b11feea078531baeddb60e6941116 Mon Sep 17 00:00:00 2001 From: aur-build Date: Tue, 17 Nov 2026 20:59:57 +0000 Subject: feat: SQLite schema, migrations, single writer actor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- tests/common/mod.rs | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 tests/common/mod.rs (limited to 'tests/common/mod.rs') diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..4cf78ec --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,177 @@ +//! Shared test scaffolding: an isolated server root in a tempdir with a migrated +//! database and a running writer actor. + +#![allow(dead_code)] + +use std::path::{Path, PathBuf}; + +use aur_build::config::Config; +use aur_build::db::{self, Db}; + +pub struct Env { + pub tmp: tempfile::TempDir, + pub cfg: Config, + pub db: Db, +} + +impl Env { + pub fn new() -> Self { + let tmp = tempfile::tempdir().expect("tempdir"); + let cfg = Config { + root: tmp.path().to_path_buf(), + signing_key_id: "TESTKEY".into(), + ..Config::default() + }; + cfg.ensure_dirs().expect("ensure_dirs"); + db::migrate(&cfg.db_path()).expect("migrate"); + let db = Db::open_migrated(&cfg.db_path()).expect("open db"); + let rid = db::ensure_repo( + &db.writer, + &cfg.repo_name, + &cfg.repo_arch_dir().display().to_string(), + &cfg.arch, + &cfg.signing_key_id, + ) + .expect("repo row"); + assert!(rid > 0); + Self { tmp, cfg, db } + } + + pub fn root(&self) -> &Path { + self.tmp.path() + } + + pub fn path(&self, p: &str) -> PathBuf { + self.tmp.path().join(p) + } + + /// Insert a pkgbase + active subscription and return the pkgbase id. + pub fn subscribe(&self, name: &str) -> i64 { + let rid = db::repo_id(&self.db).unwrap(); + let url = format!("file://{}/aur/{}.git", self.root().display(), name); + db::subscribe(&self.db.writer, rid, name, &url).unwrap(); + db::get_pkgbase(&self.db, name).unwrap().unwrap().id + } + + /// Drop a row back in via raw SQL (tests need to construct states the API + /// only reaches through several steps). + pub fn exec(&self, sql: &str, params: &[P]) -> usize { + let sql = sql.to_string(); + let params: Vec

= params.to_vec(); + self.db + .writer + .tx(move |tx| { + let refs: Vec<&dyn rusqlite::ToSql> = params + .iter() + .map(|p| match p { + P::I(i) => i as &dyn rusqlite::ToSql, + P::S(s) => s as &dyn rusqlite::ToSql, + }) + .collect(); + Ok(tx.execute(&sql, rusqlite::params_from_iter(refs.iter()))?) + }) + .unwrap() + } + + pub fn query_i64(&self, sql: &str, params: &[P]) -> Option { + let sql = sql.to_string(); + let params: Vec

= params.to_vec(); + self.db + .read(move |c| { + let refs: Vec<&dyn rusqlite::ToSql> = params + .iter() + .map(|p| match p { + P::I(i) => i as &dyn rusqlite::ToSql, + P::S(s) => s as &dyn rusqlite::ToSql, + }) + .collect(); + use rusqlite::OptionalExtension; + Ok(c.query_row(&sql, rusqlite::params_from_iter(refs.iter()), |r| r.get::<_, i64>(0)) + .optional()?) + }) + .unwrap() + } +} + +/// A test parameter. Owned so it can cross the writer-actor channel. +#[derive(Debug, Clone)] +pub enum P { + I(i64), + S(String), +} + +impl From for P { + fn from(i: i64) -> Self { + P::I(i) + } +} +impl From<&str> for P { + fn from(s: &str) -> Self { + P::S(s.to_string()) + } +} +impl From for P { + fn from(s: String) -> Self { + P::S(s) + } +} + +/// Shorthand for building a parameter slice. +pub fn params(v: Vec>) -> Vec

{ + v.into_iter().map(Into::into).collect() +} + +impl Drop for Env { + fn drop(&mut self) { + self.db.writer.shutdown(); + } +} + +/// A minimal PKGBUILD for tests that need recipe bytes without a fixture dir. +pub fn pkgbuild(pkgbase: &str, pkgver: &str, pkgrel: &str, extra: &str) -> String { + format!( + "pkgbase={pkgbase}\npkgver={pkgver}\npkgrel={pkgrel}\npkgdesc=test\narch=(x86_64)\nurl=https://example.invalid\nlicense=(MIT)\n{extra}\nbuild() {{ :\n}}\npackage_{pkgbase}() {{ :\n}}\n" + ) +} + +/// Build a throwaway git repo with the given files and return its path. +pub fn make_git_repo(dir: &Path, files: &[(&str, &str)], msg: &str) -> String { + std::fs::create_dir_all(dir).unwrap(); + git(dir, &["init", "-q", "-b", "master"]); + git(dir, &["config", "user.email", "t@example.invalid"]); + git(dir, &["config", "user.name", "t"]); + for (name, body) in files { + let p = dir.join(name); + if let Some(parent) = p.parent() { + std::fs::create_dir_all(parent).unwrap(); + } + std::fs::write(p, body).unwrap(); + } + git(dir, &["add", "-A"]); + git(dir, &["commit", "-q", "-m", msg]); + git(dir, &["rev-parse", "HEAD"]) +} + +pub fn git_add_commit(dir: &Path, files: &[(&str, &str)], msg: &str) -> String { + for (name, body) in files { + std::fs::write(dir.join(name), body).unwrap(); + } + git(dir, &["add", "-A"]); + git(dir, &["commit", "-q", "-m", msg]); + git(dir, &["rev-parse", "HEAD"]) +} + +pub fn git(dir: &Path, args: &[&str]) -> String { + let out = std::process::Command::new("git") + .arg("-C") + .arg(dir) + .args(args) + .output() + .expect("git"); + assert!( + out.status.success(), + "git {args:?} failed: {}", + String::from_utf8_lossy(&out.stderr) + ); + String::from_utf8_lossy(&out.stdout).trim().to_string() +} -- cgit v1.2.3