| 1 |
|
| 2 |
|
| 3 |
use sqlx::PgPool; |
| 4 |
|
| 5 |
use crate::db::UserId; |
| 6 |
use crate::error::Result; |
| 7 |
|
| 8 |
|
| 9 |
pub async fn record_pending_upload( |
| 10 |
pool: &PgPool, |
| 11 |
user_id: UserId, |
| 12 |
s3_key: &str, |
| 13 |
bucket: &str, |
| 14 |
) -> Result<()> { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
sqlx::query( |
| 24 |
"INSERT INTO pending_uploads (user_id, s3_key, bucket) VALUES ($1, $2, $3) |
| 25 |
ON CONFLICT (s3_key) DO UPDATE SET created_at = NOW() |
| 26 |
WHERE pending_uploads.user_id = EXCLUDED.user_id", |
| 27 |
) |
| 28 |
.bind(user_id) |
| 29 |
.bind(s3_key) |
| 30 |
.bind(bucket) |
| 31 |
.execute(pool) |
| 32 |
.await?; |
| 33 |
Ok(()) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
pub async fn remove_pending_upload<'e>( |
| 42 |
executor: impl sqlx::PgExecutor<'e>, |
| 43 |
user_id: UserId, |
| 44 |
s3_key: &str, |
| 45 |
) -> Result<()> { |
| 46 |
sqlx::query("DELETE FROM pending_uploads WHERE s3_key = $1 AND user_id = $2") |
| 47 |
.bind(s3_key) |
| 48 |
.bind(user_id) |
| 49 |
.execute(executor) |
| 50 |
.await?; |
| 51 |
Ok(()) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
pub async fn get_stale_pending_uploads( |
| 56 |
pool: &PgPool, |
| 57 |
max_age: chrono::Duration, |
| 58 |
) -> Result<Vec<(String, String)>> { |
| 59 |
let cutoff = chrono::Utc::now() - max_age; |
| 60 |
let rows: Vec<(String, String)> = sqlx::query_as( |
| 61 |
"SELECT s3_key, bucket FROM pending_uploads WHERE created_at < $1", |
| 62 |
) |
| 63 |
.bind(cutoff) |
| 64 |
.fetch_all(pool) |
| 65 |
.await?; |
| 66 |
Ok(rows) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
pub async fn delete_pending_uploads(pool: &PgPool, s3_keys: &[String]) -> Result<()> { |
| 71 |
sqlx::query("DELETE FROM pending_uploads WHERE s3_key = ANY($1)") |
| 72 |
.bind(s3_keys) |
| 73 |
.execute(pool) |
| 74 |
.await?; |
| 75 |
Ok(()) |
| 76 |
} |
| 77 |
|