diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 15:20:13 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 15:20:13 +0000 |
| commit | 933c637de34095f1ea1ab88b02626bbf22eec7f4 (patch) | |
| tree | ee7bd3d7a2b18163c0fdbe061ef53a73b74907ab /src/db/mod.rs | |
| parent | 7946e9ffc48748992f051ada15bec772b16a585d (diff) | |
| download | aur-build-933c637de34095f1ea1ab88b02626bbf22eec7f4.tar.gz aur-build-933c637de34095f1ea1ab88b02626bbf22eec7f4.zip | |
db: the schema gets an owner — every statement moves into db/, and the writer stops handing out connections
Twelve modules outside `db/` contained hand-rolled SQL — 22 statements in
`ops.rs`, 17 in `approve.rs`, 15 in `candidates.rs`, 11 in `publish.rs`, and a
dozen more scattered through `watch`, `pipeline`, `http` — which meant the
blast radius of a schema change was "any module", and `db/` was a directory
rather than a boundary. The audit trail alone was defined in four places with
three different detail payloads.
Every statement now lives in `db/`. New modules `db::candidates` and
`db::approvals` own their tables (including the 21-column candidate select,
spelled once as `CANDIDATE_COLS` instead of four times); `db::queries` gains
the metrics reads, the retention reads and writes, the publish writes
(`insert_release_in`, `cas_accepted_release_in`, `remove_index_packages_in`),
the staleness reads and the source-cache lookups; `db::builds` gains
`set_release_in`. Where a mutation is a *decision* — the collapse rules, the
approval gate, the publish compare-and-swap — the decision stays where it was
and the statements are called through the transaction handle, so the atomicity
is unchanged and only the SQL moved.
`audit_in` is the in-transaction audit append, and `audit` is now a wrapper
over it: a decision and its audit row commit together or not at all, which is
what I10 actually claims.
Enforcement is two-layered, because one layer is a lie. `Writer::call`, `tx`
and `send` are `pub(crate)`, which keeps the crate's *users* out — the
integration tests and the shim binaries — but `pub(crate)` is crate-wide, so
on its own it would not stop `approve.rs` from writing SQL tomorrow. The layer
that does is `tests/schema_ownership.rs`: it walks `src/` and fails the build
if a statement literal or a rusqlite statement call appears outside `src/db/`.
It was checked against a deliberate violation. The test harness still needs to
construct states no typed API reaches, so the raw door exists but only under
the `test-support` feature, which nothing but this crate's own dev-dependency
enables; a production binary cannot see it.
Diffstat (limited to 'src/db/mod.rs')
| -rw-r--r-- | src/db/mod.rs | 63 |
1 files changed, 58 insertions, 5 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index b810453..2cf766b 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -8,7 +8,9 @@ //! design entirely. Readers are thread-local, read-only connections; WAL lets //! them proceed concurrently with the writer. +pub mod approvals; pub mod builds; +pub mod candidates; pub mod migrations; pub mod model; pub mod queries; @@ -139,9 +141,16 @@ impl Writer { /// Run `f` on the write connection and wait for its result. /// - /// No transaction may span a build step: `f` should be short. Use - /// [`Writer::tx`] for atomic multi-statement mutations. - pub fn call<T>(&self, f: impl FnOnce(&mut Connection) -> Result<T> + Send + 'static) -> Result<T> + /// No transaction may span a build step: `f` should be short. + /// Use [`Writer::tx`] for atomic multi-statement mutations. + /// + /// `pub(crate)`, not `pub`: the schema is the system's most protected asset + /// and it has one owner, this module (§6 M-1). Code outside `db/` asks for a + /// typed query instead of writing SQL. `pub(crate)` is what keeps the + /// *crate's* users (the integration tests, the shim binaries) out; inside + /// the crate the convention is enforced by `tests/schema_ownership.rs`, + /// which fails the build if a SQL statement appears outside `src/db/`. + pub(crate) fn call<T>(&self, f: impl FnOnce(&mut Connection) -> Result<T> + Send + 'static) -> Result<T> where T: Send + 'static, { @@ -156,7 +165,7 @@ impl Writer { /// Run `f` inside a single `BEGIN IMMEDIATE` transaction on the write /// connection. This is how every multi-statement mutation is expressed. - pub fn tx<T>( + pub(crate) fn tx<T>( &self, f: impl FnOnce(&mut rusqlite::Transaction<'_>) -> Result<T> + Send + 'static, ) -> Result<T> @@ -174,7 +183,7 @@ impl Writer { } /// Fire-and-forget write (audit rows, bookkeeping where the result is unused). - pub fn send(&self, f: BoxedCall) -> Result<()> { + pub(crate) fn send(&self, f: BoxedCall) -> Result<()> { if !self.is_alive() { return Err(Error::db("writer actor gone")); } @@ -188,6 +197,36 @@ impl Writer { } } +/// The raw-SQL door, open only for the test harness (§6 M-9). +/// +/// A fixture sometimes has to put the database in a state the typed API only +/// reaches through several steps — a release published without a build, a +/// pointer moved out from under a review. That is a reason to have a door, not +/// a reason to leave it unlocked in the binary an operator runs, so it exists +/// only under the `test-support` feature, which nothing but this crate's own +/// dev-dependency enables. +#[cfg(feature = "test-support")] +impl Writer { + /// Run `f` in a transaction. Test fixtures only. + pub fn tx_for_tests<T>( + &self, + f: impl FnOnce(&mut rusqlite::Transaction<'_>) -> Result<T> + Send + 'static, + ) -> Result<T> + where + T: Send + 'static, + { + self.tx(f) + } + + /// Run `f` on the write connection. Test fixtures only. + pub fn call_for_tests<T>(&self, f: impl FnOnce(&mut Connection) -> Result<T> + Send + 'static) -> Result<T> + where + T: Send + 'static, + { + self.call(f) + } +} + /// Thread-local read-only connection pool. /// /// One connection per thread per database file, opened read-only. HTTP handlers @@ -263,6 +302,20 @@ pub fn backup_to(writer: &Writer, dest: &Path) -> Result<()> { }) } +/// The schema version a database file reports. A backup that does not open and +/// answer with a migrated schema is a file, not a backup, and the only way to +/// find that out is to ask it. +/// +/// Opened read-write and untouched by the connection pragmas: the point is to +/// read one number from a file we are not going to modify, and a WAL database +/// cannot even be opened read-only (it needs its sidecar). +pub fn schema_version(path: &Path) -> Result<i64> { + let conn = rusqlite::Connection::open(path) + .map_err(|e| Error::db(format!("{} does not open: {e}", path.display())))?; + conn.query_row("PRAGMA user_version", [], |r| r.get(0)) + .map_err(|e| Error::db(format!("{} has no schema version: {e}", path.display()))) +} + /// WAL size + checkpoint lag, surfaced on the Ops page and in metrics. pub fn wal_stats(db: &Db) -> (u64, u64) { let db_file = std::fs::metadata(db.writer.path()).map(|m| m.len()).unwrap_or(0); |
