summaryrefslogtreecommitdiffstats
path: root/src/db/audit.rs
diff options
context:
space:
mode:
authordev <dev@localhost>2026-11-20 17:13:44 +0000
committerdev <dev@localhost>2026-11-20 17:13:44 +0000
commit8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4 (patch)
tree2ce6c30a688ef3fb15af5639e22cfc230f8f3627 /src/db/audit.rs
parentb73bb60403c3f2e7fc55d5038831912e8371e3a6 (diff)
downloadaur-build-8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4.tar.gz
aur-build-8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4.zip
db: one module per aggregate, and an interface you can read
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).
Diffstat (limited to 'src/db/audit.rs')
-rw-r--r--src/db/audit.rs126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/db/audit.rs b/src/db/audit.rs
new file mode 100644
index 0000000..ce9d0c8
--- /dev/null
+++ b/src/db/audit.rs
@@ -0,0 +1,126 @@
+use rusqlite::params;
+
+use crate::db::*;
+use crate::error::Result;
+use crate::time;
+
+// ------------------------------------------------------------------- audit --
+
+pub fn audit(
+ writer: &Writer,
+ actor_kind: &str,
+ actor: &str,
+ action: &str,
+ entity: &str,
+ entity_id: &str,
+ detail: serde_json::Value,
+) -> Result<()> {
+ let at = time::now();
+ let actor_kind = actor_kind.to_string();
+ let actor = actor.to_string();
+ let action = action.to_string();
+ let entity = entity.to_string();
+ let entity_id = entity_id.to_string();
+ writer.tx(move |tx| {
+ audit_in(
+ tx,
+ &AuditEntry {
+ at: &at,
+ actor_kind: &actor_kind,
+ actor: &actor,
+ action: &action,
+ entity: &entity,
+ entity_id: &entity_id,
+ detail,
+ },
+ )
+ })
+}
+
+#[derive(Debug)]
+pub struct AuditEntry<'a> {
+ pub at: &'a str,
+ pub actor_kind: &'a str,
+ pub actor: &'a str,
+ pub action: &'a str,
+ pub entity: &'a str,
+ pub entity_id: &'a str,
+ pub detail: serde_json::Value,
+}
+
+impl<'a> AuditEntry<'a> {
+ pub fn human(
+ at: &'a str,
+ actor: &'a str,
+ action: &'a str,
+ entity: &'a str,
+ entity_id: &'a str,
+ detail: serde_json::Value,
+ ) -> Self {
+ Self { at, actor_kind: "human", actor, action, entity, entity_id, detail }
+ }
+
+ pub fn system(
+ at: &'a str,
+ action: &'a str,
+ entity: &'a str,
+ entity_id: &'a str,
+ detail: serde_json::Value,
+ ) -> Self {
+ Self { at, actor_kind: "system", actor: "", action, entity, entity_id, detail }
+ }
+}
+
+pub fn audit_in(tx: &rusqlite::Transaction<'_>, e: &AuditEntry<'_>) -> Result<()> {
+ tx.execute(
+ "INSERT INTO audit (at, actor_kind, actor, action, entity, entity_id, detail_json)
+ VALUES (?1,?2,?3,?4,?5,?6,?7)",
+ params![e.at, e.actor_kind, e.actor, e.action, e.entity, e.entity_id, e.detail.to_string()],
+ )?;
+ Ok(())
+}
+
+pub fn audit_system(writer: &Writer, action: &str, entity: &str, entity_id: &str, detail: serde_json::Value) -> Result<()> {
+ audit(writer, "system", "", action, entity, entity_id, detail)
+}
+
+// ------------------------------------------------------------------ audit --
+
+#[derive(Debug, Clone)]
+pub struct AuditRow {
+ pub id: i64,
+ pub at: String,
+ pub actor_kind: String,
+ pub actor: String,
+ pub action: String,
+ pub entity: String,
+ pub entity_id: String,
+ pub detail_json: String,
+}
+
+pub fn list_audit(db: &Db, limit: i64) -> Result<Vec<AuditRow>> {
+ db.read(|c| {
+ let mut stmt = c.prepare(
+ "SELECT id, at, actor_kind, actor, action, entity, entity_id, detail_json
+ FROM audit ORDER BY id DESC LIMIT ?1",
+ )?;
+ let rows = stmt.query_map(params![limit], |r| {
+ Ok(AuditRow {
+ id: r.get(0)?,
+ at: r.get(1)?,
+ actor_kind: r.get(2)?,
+ actor: r.get(3)?,
+ action: r.get(4)?,
+ entity: r.get(5)?,
+ entity_id: r.get(6)?,
+ detail_json: r.get(7)?,
+ })
+ })?;
+ let mut out = Vec::new();
+ for r in rows {
+ out.push(r?);
+ }
+ Ok(out)
+ })
+}
+