OXYGENHUB
# a9f1c0bf403a oxygenhub / crates / hub / src / db / tlog.rs
1.6 KiB · Rust · last changed The hub's transparency log (step 1 of docs/transparency.md) 18 days ago source blame history raw
1
//! The transparency log's index (migration 0076): which entry logged which
2
//! object. The log itself is files (`crate::tlog`).
3
4
use super::Pool;
5
use sqlx::FromRow;
6
7
#[derive(Clone, Debug, FromRow)]
8
pub struct Logged {
9
    pub idx: i64,
10
    pub object: String,
11
    pub kind: String,
12
    pub repo_id: Option<i64>,
13
    pub received_ms: i64,
14
}
15
16
/// The entry that logged `object` as `kind`, if one did.
17
pub async fn entry_of(pool: &Pool, object: &str, kind: &str) -> sqlx::Result<Option<i64>> {
18
    sqlx::query_scalar("SELECT idx FROM tlog_entries WHERE object = $1 AND kind = $2").bind(object).bind(kind).fetch_optional(pool).await
19
}
20
21
pub async fn record(pool: &Pool, idx: i64, object: &str, kind: &str, repo_id: Option<i64>, received_ms: i64) -> sqlx::Result<()> {
22
    sqlx::query("INSERT INTO tlog_entries (idx, object, kind, repo_id, received_ms) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING")
23
        .bind(idx)
24
        .bind(object)
25
        .bind(kind)
26
        .bind(repo_id)
27
        .bind(received_ms)
28
        .execute(pool)
29
        .await?;
30
    Ok(())
31
}
32
33
/// The newest entries, newest first; of one repository when given.
34
pub async fn recent(pool: &Pool, repo_id: Option<i64>, limit: i64) -> sqlx::Result<Vec<Logged>> {
35
    match repo_id {
36
        Some(r) => {
37
            sqlx::query_as("SELECT * FROM tlog_entries WHERE repo_id = $1 ORDER BY idx DESC LIMIT $2")
38
                .bind(r)
39
                .bind(limit)
40
                .fetch_all(pool)
41
                .await
42
        }
43
        None => sqlx::query_as("SELECT * FROM tlog_entries ORDER BY idx DESC LIMIT $1").bind(limit).fetch_all(pool).await,
44
    }
45
}