//! The transparency log's index (migration 0076): which entry logged which //! object. The log itself is files (`crate::tlog`). use super::Pool; use sqlx::FromRow; #[derive(Clone, Debug, FromRow)] pub struct Logged { pub idx: i64, pub object: String, pub kind: String, pub repo_id: Option, pub received_ms: i64, } /// The entry that logged `object` as `kind`, if one did. pub async fn entry_of(pool: &Pool, object: &str, kind: &str) -> sqlx::Result> { sqlx::query_scalar("SELECT idx FROM tlog_entries WHERE object = $1 AND kind = $2").bind(object).bind(kind).fetch_optional(pool).await } pub async fn record(pool: &Pool, idx: i64, object: &str, kind: &str, repo_id: Option, received_ms: i64) -> sqlx::Result<()> { sqlx::query("INSERT INTO tlog_entries (idx, object, kind, repo_id, received_ms) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING") .bind(idx) .bind(object) .bind(kind) .bind(repo_id) .bind(received_ms) .execute(pool) .await?; Ok(()) } /// The newest entries, newest first; of one repository when given. pub async fn recent(pool: &Pool, repo_id: Option, limit: i64) -> sqlx::Result> { match repo_id { Some(r) => { sqlx::query_as("SELECT * FROM tlog_entries WHERE repo_id = $1 ORDER BY idx DESC LIMIT $2") .bind(r) .bind(limit) .fetch_all(pool) .await } None => sqlx::query_as("SELECT * FROM tlog_entries ORDER BY idx DESC LIMIT $1").bind(limit).fetch_all(pool).await, } }