Skip to main content

headless_lms_models/
page_history_spec_files.rs

1//! The files each page-history version's private specs reference.
2//!
3//! Append-only, because history is. A restore has to be able to bring back a version whose specs
4//! name these files, so recording them here keeps [`crate::exercise_spec_uploads`]' reaper away
5//! from a file for as long as any snapshot names it.
6//!
7//! Only the private specs': a restore re-derives the other two, so an old version's derived files
8//! are never brought back.
9
10use crate::prelude::*;
11
12/// Records the files the private specs of one history version name. Duplicates within
13/// `file_upload_ids` collapse: several tasks in a page may name the same file, and one row per
14/// version and file is all a reference check needs.
15pub async fn insert_many(
16    conn: &mut PgConnection,
17    page_history_id: Uuid,
18    file_upload_ids: &[Uuid],
19) -> ModelResult<()> {
20    sqlx::query!(
21        "
22INSERT INTO page_history_spec_files (page_history_id, file_upload_id)
23SELECT DISTINCT $1::uuid,
24  file_upload_id
25FROM UNNEST($2::uuid []) AS t(file_upload_id)
26",
27        page_history_id,
28        file_upload_ids
29    )
30    .execute(conn)
31    .await?;
32    Ok(())
33}