(
&self,
chunk: &DraftChunkRecord,
dedup_key: Option<&str>,
)
| 648 | } |
| 649 | |
| 650 | pub async fn put_draft_chunk( |
| 651 | &self, |
| 652 | chunk: &DraftChunkRecord, |
| 653 | dedup_key: Option<&str>, |
| 654 | ) -> PersistenceResult<String> { |
| 655 | let payload = draft_chunk_payload_from_record(chunk)?; |
| 656 | // RETURNING "id" gives us the row's effective id regardless of |
| 657 | // whether INSERT inserted a fresh row or ON CONFLICT updated an |
| 658 | // existing one. Callers report this id to clients so the response |
| 659 | // can never advertise a chunk_id that isn't actually persisted. |
| 660 | let row = sqlx::query( |
| 661 | r#" |
| 662 | INSERT INTO "objects" ( |
| 663 | "object_type", "id", "scope", "status", "dedup_key", "hit_count", "payload", "created_at_ms", "updated_at_ms" |
| 664 | ) |
| 665 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9) |
| 666 | ON CONFLICT ("object_type", "scope", "dedup_key") WHERE "dedup_key" IS NOT NULL DO UPDATE SET |
| 667 | "hit_count" = "objects"."hit_count" + excluded."hit_count", |
| 668 | "updated_at_ms" = excluded."updated_at_ms" |
| 669 | RETURNING "id" |
| 670 | "#, |
| 671 | ) |
| 672 | .bind(DRAFT_CHUNK_OBJECT_TYPE) |
| 673 | .bind(&chunk.id) |
| 674 | .bind(&chunk.sandbox_id) |
| 675 | .bind(&chunk.status) |
| 676 | .bind(dedup_key) |
| 677 | .bind(i64::from(chunk.hit_count)) |
| 678 | .bind(payload) |
| 679 | .bind(chunk.first_seen_ms) |
| 680 | .bind(chunk.last_seen_ms) |
| 681 | .fetch_one(&self.pool) |
| 682 | .await |
| 683 | .map_err(|e| map_db_error(&e))?; |
| 684 | Ok(row.get::<String, _>("id")) |
| 685 | } |
| 686 | |
| 687 | pub async fn get_draft_chunk(&self, id: &str) -> PersistenceResult<Option<DraftChunkRecord>> { |
| 688 | let row = sqlx::query( |
no test coverage detected