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). --- src/time.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/time.rs (limited to 'src/time.rs') diff --git a/src/time.rs b/src/time.rs new file mode 100644 index 0000000..69fbc3c --- /dev/null +++ b/src/time.rs @@ -0,0 +1,69 @@ +//! Clock helpers. +//! +//! DESIGN.md §16: "use DB time (`strftime` at write) for all ordering and audit +//! stamps". We store timestamps as UTC ISO-8601 with second resolution so that +//! lexicographic ordering equals chronological ordering, and every write path +//! funnels through [`now`] so a test can pin the clock. + +use chrono::{DateTime, SecondsFormat, TimeZone, Utc}; + +/// Current time as a canonical DB timestamp: `2026-11-17T20:43:24Z`. +pub fn now() -> String { + // A test may pin the clock (see `pin`); production reads the real clock. + if let Some(pinned) = pinned() { + return pinned; + } + fmt(Utc::now()) +} + +fn fmt(t: DateTime) -> String { + t.to_rfc3339_opts(SecondsFormat::Secs, true) +} + +fn pinned() -> Option { + CLOCK.with(|c| c.borrow().clone()) +} + +thread_local! { + static CLOCK: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; +} + +/// Pin the clock for the current thread (tests only). Returns the previous value. +pub fn pin(ts: &str) -> Option { + CLOCK.with(|c| { + let mut slot = c.borrow_mut(); + (*slot).replace(ts.to_string()) + }) +} + +/// Unpin the clock for the current thread. +pub fn unpin() { + CLOCK.with(|c| *c.borrow_mut() = None); +} + +/// Parse a DB timestamp back into a `DateTime`. +pub fn parse(ts: &str) -> Option> { + DateTime::parse_from_rfc3339(ts) + .ok() + .map(|d| d.with_timezone(&Utc)) + .or_else(|| { + chrono::NaiveDateTime::parse_from_str(ts, "%Y-%m-%d %H:%M:%S") + .ok() + .map(|n| Utc.from_utc_datetime(&n)) + }) +} + +/// Whole seconds between two timestamps (`b - a`), or `None` if unparseable. +pub fn delta_secs(a: &str, b: &str) -> Option { + Some(parse(b)?.signed_duration_since(parse(a)?).num_seconds()) +} + +/// A timestamp `secs` seconds in the past, canonical format. +pub fn secs_ago(secs: i64) -> String { + fmt(Utc::now() - chrono::Duration::seconds(secs)) +} + +/// A timestamp `secs` seconds in the future, canonical format. +pub fn secs_ahead(secs: i64) -> String { + fmt(Utc::now() + chrono::Duration::seconds(secs)) +} -- cgit v1.2.3