Coverage / crates/hub/src/db/attachments.rs 84.4%
27 of 32 lines run, 4 of 12 functions · run #536 on main · run missed
| 1 | |
|
| 2 | |
|
| 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 | |
|
| 18 | 6× | pub async fn add( |
| 19 | 6× | pool: &Pool, |
| 20 | 6× | repo_id: i64, |
| 21 | 6× | name: &str, |
| 22 | 6× | size: i64, |
| 23 | 6× | hash: &str, |
| 24 | 6× | content_type: &str, |
| 25 | 6× | uploader_id: Option<i64>, |
| 26 | 6× | ) -> sqlx::Result<Attachment> { |
| 27 | 6× | sqlx::query_as( |
| 28 | 6× | "INSERT INTO attachments (repo_id, name, size, hash, content_type, uploader_id, created_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", |
| 29 | 6× | ) |
| 30 | 6× | .bind(repo_id) |
| 31 | 6× | .bind(name) |
| 32 | 6× | .bind(size) |
| 33 | 6× | .bind(hash) |
| 34 | 6× | .bind(content_type) |
| 35 | 6× | .bind(uploader_id) |
| 36 | 6× | .bind(now_ms()) |
| 37 | 6× | .fetch_one(pool) |
| 38 | 6× | .await |
| 39 | 6× | } |
| 40 | |
|
| 41 | |
|
| 42 | 3× | pub async fn get(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result<Option<Attachment>> { |
| 43 | 3× | sqlx::query_as("SELECT * FROM attachments WHERE repo_id = $1 AND id = $2").bind(repo_id).bind(id).fetch_optional(pool).await |
| 44 | 3× | } |
| 45 | |
|
| 46 | |
|
| 47 | 0× | pub async fn hash_in_use(pool: &Pool, hash: &str) -> sqlx::Result<bool> { |
| 48 | 0× | let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM attachments WHERE hash = $1").bind(hash).fetch_one(pool).await?; |
| 49 | 0× | Ok(n > 0) |
| 50 | 0× | } |