OXYGENHUB

Coverage / crates/hub/src/db/attachments.rs 84.4%

27 of 32 lines run, 4 of 12 functions · run #545 on main · run missed

1
//! Attachments (oxygenhub#350): files people attach to issues, comments and
2
//! proposals, by content in the assets store.
3
4
use super::{now_ms, Pool};
5
6
#[derive(Clone, Debug, sqlx::FromRow)]
7
pub struct Attachment {
8
    pub id: i64,
9
    pub repo_id: i64,
10
    pub name: String,
11
    pub size: i64,
12
    pub hash: String,
13
    pub content_type: String,
14
    pub uploader_id: Option<i64>,
15
    pub created_ms: i64,
16
}
17
186×
pub async fn add(
196×
    pool: &Pool,
206×
    repo_id: i64,
216×
    name: &str,
226×
    size: i64,
236×
    hash: &str,
246×
    content_type: &str,
256×
    uploader_id: Option<i64>,
266×
) -> sqlx::Result<Attachment> {
276×
    sqlx::query_as(
286×
        "INSERT INTO attachments (repo_id, name, size, hash, content_type, uploader_id, created_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *",
296×
    )
306×
    .bind(repo_id)
316×
    .bind(name)
326×
    .bind(size)
336×
    .bind(hash)
346×
    .bind(content_type)
356×
    .bind(uploader_id)
366×
    .bind(now_ms())
376×
    .fetch_one(pool)
386×
    .await
396×
}
40
41
/// One of a repository's attachments.
423×
pub async fn get(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Attachment>> {
433×
    sqlx::query_as("SELECT * FROM attachments WHERE repo_id = $1 AND id = $2").bind(repo_id).bind(id).fetch_optional(pool).await
443×
}
45
46
/// Whether any attachment keeps the blob `hash`.
470×
pub async fn hash_in_use(pool: &Pool, hash: &str) -> sqlx::Result<bool> {
480×
    let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM attachments WHERE hash = $1").bind(hash).fetch_one(pool).await?;
490×
    Ok(n > 0)
500×
}