Appointment.slot nullable ON DELETE SET NULL (dangling-ref booking contract)
Browse files at this commitd9b4e8eb1104fbc5d4069671476147f78750914dParent: fcca31c- fix
- appointment.slot nullable ON DELETE SET NULL (dangling-ref booking contract)
7 changed files +191 -148
- migrations/school/20260912000004_food_appointments_fees.sql +3 -1
- src/db/subject.rs +98 -91
- src/db/term.rs +66 -56
- src/domain/course.rs +6 -0
- src/domain/course_session.rs +6 -0
- src/domain/subject.rs +6 -0
- src/domain/term.rs +6 -0
migrations/school/20260912000004_food_appointments_fees.sqlmodified+3 -1
| @@ -41,7 +41,9 @@ CREATE INDEX appointment_slot_series ON appointment_slot (series); | ||
| 41 | 41 | |
| 42 | 42 | CREATE TABLE appointment ( |
| 43 | 43 | id uuid PRIMARY KEY, |
| 44 | - slot uuid NOT NULL REFERENCES appointment_slot(id) ON DELETE NO ACTION, | |
| 44 | + -- Nullable + SET NULL ports the old dangling-ref contract: a settled | |
| 45 | + -- booking whose slot was deleted still renders, without a window. | |
| 46 | + slot uuid NULL REFERENCES appointment_slot(id) ON DELETE SET NULL, | |
| 45 | 47 | requester uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, |
| 46 | 48 | status TEXT NOT NULL DEFAULT 'pending' |
| 47 | 49 | CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')), |
src/db/subject.rsmodified+98 -91
| @@ -1,19 +1,17 @@ | ||
| 1 | 1 | //! The `subject` table: the count-and-create that pins a topic to a live |
| 2 | 2 | //! course row, the bulk id listing, the field-scoped PATCH, and the guarded |
| 3 | -//! delete whose `WHERE` reads the row's own reference counters. The type and | |
| 3 | +//! delete whose guard reads the row's own reference counters. The type and | |
| 4 | 4 | //! its validation live in [`crate::domain::subject`]. |
| 5 | 5 | |
| 6 | -use surrealdb::types::{RecordId, SurrealValue}; | |
| 7 | - | |
| 8 | -use crate::constant::ENROLLMENT_COUNT_FIELD; | |
| 9 | -use crate::database::{Database, transaction_with_retry}; | |
| 10 | -use crate::db::cap; | |
| 6 | +use crate::constant::SUBJECT_TABLE; | |
| 7 | +use crate::database::Database; | |
| 11 | 8 | use crate::db::field_update::FieldUpdate; |
| 12 | 9 | use crate::db::page::PagedList; |
| 13 | 10 | use crate::domain::course::CourseId; |
| 14 | 11 | use crate::domain::subject::{Subject, SubjectDescription, SubjectId, SubjectName}; |
| 15 | 12 | use crate::error::AppError; |
| 16 | 13 | |
| 14 | + | |
| 17 | 15 | pub async fn create( |
| 18 | 16 | db: &Database, |
| 19 | 17 | course: &CourseId, |
| @@ -26,24 +24,40 @@ pub async fn create( | ||
| 26 | 24 | name, |
| 27 | 25 | description, |
| 28 | 26 | }; |
| 29 | - // The course row is *written* (bumped and put back), not read, so this | |
| 30 | - // collides with `Course::delete`'s cascade: a topic that outlives its | |
| 31 | - // course 404s through `subject_with_course` forever, while | |
| 32 | - // `must_exist` still accepts its id — so a bank template can be | |
| 33 | - // tagged with a subject nobody can reach. See [`cap::touch_and_create`]. | |
| 34 | - cap::touch_and_create( | |
| 35 | - &course.record(), | |
| 36 | - ENROLLMENT_COUNT_FIELD, | |
| 37 | - &subject.id.record(), | |
| 38 | - &subject, | |
| 39 | - db, | |
| 27 | + // The course foreign key is the existence proof the old bump-and-restore | |
| 28 | + // "touch" trick faked: a topic whose course is already gone — or which is | |
| 29 | + // deleted while this insert is in flight — is refused here, so a subject | |
| 30 | + // can never outlive its course and a bank template can never be tagged | |
| 31 | + // with a subject nobody can reach. `23503` is the parent-gone refusal, | |
| 32 | + // the same answer the touch produced. | |
| 33 | + let created = sqlx::query_as!( | |
| 34 | + Subject, | |
| 35 | + r#"INSERT INTO subject (id, course, name, description, exam_question_count, homework_count) | |
| 36 | + VALUES ($1, $2, $3, $4, 0, 0) | |
| 37 | + RETURNING id, course, name, description"#, | |
| 38 | + subject.id, | |
| 39 | + subject.course, | |
| 40 | + subject.name, | |
| 41 | + subject.description, | |
| 40 | 42 | ) |
| 41 | - .await? | |
| 42 | - .ok_or(AppError::NotFound) | |
| 43 | + .fetch_one(db) | |
| 44 | + .await; | |
| 45 | + match created { | |
| 46 | + Ok(created) => Ok(created), | |
| 47 | + Err(err) if crate::database::foreign_key_violation(&err) => Err(AppError::NotFound), | |
| 48 | + Err(err) => Err(err.into()), | |
| 49 | + } | |
| 43 | 50 | } |
| 44 | 51 | |
| 45 | 52 | pub async fn read(db: &Database, id: &SubjectId) -> Result<Option<Subject>, AppError> { |
| 46 | - Ok(db.select(id.record()).await?) | |
| 53 | + let subject = sqlx::query_as!( | |
| 54 | + Subject, | |
| 55 | + r#"SELECT id, course, name, description FROM subject WHERE id = $1"#, | |
| 56 | + id, | |
| 57 | + ) | |
| 58 | + .fetch_optional(db) | |
| 59 | + .await?; | |
| 60 | + Ok(subject) | |
| 47 | 61 | } |
| 48 | 62 | |
| 49 | 63 | /// Several subjects in one query — the bulk half of a list endpoint that |
| @@ -52,25 +66,27 @@ pub async fn list_by_ids(db: &Database, ids: &[&SubjectId]) -> Result<Vec<Subjec | ||
| 52 | 66 | if ids.is_empty() { |
| 53 | 67 | return Ok(Vec::new()); |
| 54 | 68 | } |
| 55 | - let records: Vec<RecordId> = ids.iter().map(|id| id.record()).collect(); | |
| 56 | - let mut result = db | |
| 57 | - .query("SELECT * FROM subject WHERE id IN $ids") | |
| 58 | - .bind(("ids", records)) | |
| 59 | - .await? | |
| 60 | - .check()?; | |
| 61 | - Ok(result.take::<Vec<Subject>>(0)?) | |
| 69 | + let keys: Vec<SubjectId> = ids.iter().map(|id| **id).collect(); | |
| 70 | + let rows = sqlx::query_as!( | |
| 71 | + Subject, | |
| 72 | + r#"SELECT id, course, name, description FROM subject WHERE id = ANY($1)"#, | |
| 73 | + keys, | |
| 74 | + ) | |
| 75 | + .fetch_all(db) | |
| 76 | + .await?; | |
| 77 | + Ok(rows) | |
| 62 | 78 | } |
| 63 | 79 | |
| 64 | -/// The course's subjects in curriculum order (ULID ids sort by creation). | |
| 80 | +/// The course's subjects in curriculum order (uuid ids sort by creation). | |
| 65 | 81 | pub async fn list_for_course( |
| 66 | 82 | db: &Database, |
| 67 | 83 | course: &CourseId, |
| 68 | 84 | limit: Option<i64>, |
| 69 | 85 | offset: i64, |
| 70 | 86 | ) -> Result<(Vec<Subject>, i64), AppError> { |
| 71 | - PagedList::new("subject WHERE course = $course", "ORDER BY id ASC") | |
| 72 | - .bind("course", course.record()) | |
| 73 | - .run(limit, offset, db) | |
| 87 | + PagedList::new("subject WHERE course = $1", "ORDER BY id ASC") | |
| 88 | + .bind(course.uuid()) | |
| 89 | + .run::<Subject>(limit, offset, db) | |
| 74 | 90 | .await |
| 75 | 91 | } |
| 76 | 92 | |
| @@ -86,9 +102,9 @@ pub async fn update( | ||
| 86 | 102 | name: Option<SubjectName>, |
| 87 | 103 | description: Option<SubjectDescription>, |
| 88 | 104 | ) -> Result<Subject, AppError> { |
| 89 | - FieldUpdate::new(subject.id.record()) | |
| 90 | - .set("name", name) | |
| 91 | - .set("description", description) | |
| 105 | + FieldUpdate::new(SUBJECT_TABLE, subject.id.uuid()) | |
| 106 | + .set("name", name.map(|name| name.as_str().to_owned())) | |
| 107 | + .set("description", description.map(|d| d.as_str().to_owned())) | |
| 92 | 108 | .run::<Subject>(db) |
| 93 | 109 | .await |
| 94 | 110 | } |
| @@ -99,72 +115,63 @@ pub async fn update( | ||
| 99 | 115 | /// |
| 100 | 116 | /// Exam questions and homework are *not* cascaded: their `subject` is a |
| 101 | 117 | /// required field that may not be orphaned, so either one still refuses the |
| 102 | -/// delete with a 409. That refusal is the delete's own `WHERE`, read off the | |
| 103 | -/// two reference counters this row carries | |
| 104 | -/// ([`crate::constant::SUBJECT_QUESTION_COUNT_FIELD`] and its homework | |
| 105 | -/// twin), which is what makes it hold against a question created by a | |
| 106 | -/// request racing this one — the cross-table `SELECT … LIMIT 1` it replaces | |
| 107 | -/// was a count-then-delete no transaction serializes, pinned by three | |
| 108 | -/// process-wide locks that could not cover the round trip between them. | |
| 118 | +/// delete with a 409. That refusal is this transaction's own check, read | |
| 119 | +/// off the two reference counters this row carries | |
| 120 | +/// (`exam_question_count` and its homework twin) under a `FOR UPDATE` row | |
| 121 | +/// lock — the lock *is* the guard, because every counter writer must | |
| 122 | +/// update this same row, so no question can land between the check and the | |
| 123 | +/// delete. The old store could not conflict-check the cross-table shape, | |
| 124 | +/// which is why the check had to be the `DELETE`'s own `WHERE`; here the | |
| 125 | +/// row lock does it and the `WHERE` reads off the same columns. | |
| 109 | 126 | /// |
| 110 | 127 | /// Which of the two blocked is read off the counters *before* the delete, |
| 111 | 128 | /// purely to pick the message; the decision itself was already made by the |
| 112 | -/// `WHERE`. | |
| 129 | +/// check. | |
| 113 | 130 | /// |
| 114 | 131 | /// The bank's subject is optional metadata, and blocking on it was a dead |
| 115 | 132 | /// end — only the template's owner may re-tag it, so a manager could never |
| 116 | 133 | /// clear their own 409, and a private template raising it leaked its |
| 117 | -/// existence. Its cascade runs *after* the conditional delete, so a refused | |
| 134 | +/// existence. Its cascade runs *after* the guarded delete, so a refused | |
| 118 | 135 | /// delete leaves every template's subject where it was. |
| 119 | 136 | pub async fn delete(db: &Database, subject: Subject) -> Result<Subject, AppError> { |
| 120 | - let (mut result, mut errors) = transaction_with_retry( | |
| 121 | - db, | |
| 122 | - "BEGIN TRANSACTION; | |
| 123 | - LET $held = (SELECT exam_question_count AS q, homework_count AS h FROM $sub); | |
| 124 | - LET $before = (DELETE $sub | |
| 125 | - WHERE (exam_question_count ?? 0) = 0 AND (homework_count ?? 0) = 0 | |
| 126 | - RETURN BEFORE); | |
| 127 | - IF array::len($before) = 0 { | |
| 128 | - THROW IF array::len($held) = 0 { 'subject_missing' } | |
| 129 | - ELSE IF ($held[0].q ?? 0) > 0 { 'subject_questions' } | |
| 130 | - ELSE { 'subject_homework' } | |
| 131 | - }; | |
| 132 | - UPDATE bank_question SET subject = NONE WHERE subject = $sub; | |
| 133 | - RETURN $before; | |
| 134 | - COMMIT TRANSACTION;", | |
| 135 | - &[("sub".into(), subject.id.record().into_value())], | |
| 136 | - &["subject_missing", "subject_questions", "subject_homework"], | |
| 137 | - ) | |
| 138 | - .await?; | |
| 139 | - // An aborted transaction errors every slot; only the THROW's own slot | |
| 140 | - // names the marker (the [`crate::db::appointment_slot`] treatment), | |
| 141 | - // and a lost round is re-sent rather than reported. | |
| 142 | - let thrown = |marker: &str| { | |
| 143 | - errors | |
| 144 | - .values() | |
| 145 | - .any(|error| error.to_string().contains(marker)) | |
| 146 | - }; | |
| 147 | - if thrown("subject_questions") { | |
| 148 | - return Err(AppError::Conflict( | |
| 149 | - "exam questions still reference this subject — re-tag or delete them first", | |
| 150 | - )); | |
| 151 | - } | |
| 152 | - if thrown("subject_homework") { | |
| 153 | - return Err(AppError::Conflict( | |
| 154 | - "homework still references this subject — re-tag or delete it first", | |
| 155 | - )); | |
| 156 | - } | |
| 157 | - if thrown("subject_missing") { | |
| 158 | - return Err(AppError::NotFound); | |
| 159 | - } | |
| 160 | - if let Some(error) = errors.drain().map(|(_, error)| error).next() { | |
| 161 | - return Err(error.into()); | |
| 162 | - } | |
| 163 | - // Read through the trailing `RETURN`, not a counted slot — see | |
| 164 | - // [`crate::db::exam::delete`]. | |
| 165 | - let slot = result.num_statements().saturating_sub(2); | |
| 166 | - let deleted: Option<Subject> = result.take::<Vec<Subject>>(slot)?.into_iter().next(); | |
| 167 | - deleted.ok_or(AppError::NotFound) | |
| 137 | + crate::database::tx_with_retry(db, false, async |tx| { | |
| 138 | + // The pre-image read and the guard in one locked statement: the row | |
| 139 | + // (and its counters) cannot change under this transaction. | |
| 140 | + let held = sqlx::query!( | |
| 141 | + r#"SELECT exam_question_count, homework_count | |
| 142 | + FROM subject WHERE id = $1 FOR UPDATE"#, | |
| 143 | + subject.id, | |
| 144 | + ) | |
| 145 | + .fetch_optional(&mut *tx) | |
| 146 | + .await?; | |
| 147 | + let held = held.ok_or(AppError::NotFound)?; | |
| 148 | + if held.exam_question_count > 0 { | |
| 149 | + return Err(AppError::Conflict( | |
| 150 | + "exam questions still reference this subject — re-tag or delete them first", | |
| 151 | + )); | |
| 152 | + } | |
| 153 | + if held.homework_count > 0 { | |
| 154 | + return Err(AppError::Conflict( | |
| 155 | + "homework still references this subject — re-tag or delete it first", | |
| 156 | + )); | |
| 157 | + } | |
| 158 | + let deleted = sqlx::query_as!( | |
| 159 | + Subject, | |
| 160 | + r#"DELETE FROM subject WHERE id = $1 | |
| 161 | + RETURNING id, course, name, description"#, | |
| 162 | + subject.id, | |
| 163 | + ) | |
| 164 | + .fetch_one(&mut *tx) | |
| 165 | + .await?; | |
| 166 | + sqlx::query!( | |
| 167 | + r#"UPDATE bank_question SET subject = NULL WHERE subject = $1"#, | |
| 168 | + subject.id, | |
| 169 | + ) | |
| 170 | + .execute(&mut *tx) | |
| 171 | + .await?; | |
| 172 | + Ok(deleted) | |
| 173 | + }) | |
| 174 | + .await | |
| 168 | 175 | } |
| 169 | 176 | |
| 170 | 177 | #[cfg(test)] |
src/db/term.rsmodified+66 -56
| @@ -4,10 +4,8 @@ | ||
| 4 | 4 | //! stamps. The type and the errors its guards hand out live in |
| 5 | 5 | //! [`crate::domain::term`]. |
| 6 | 6 | |
| 7 | -use surrealdb::types::SurrealValue; | |
| 8 | - | |
| 9 | -use crate::constant::{COURSE_COUNT_FIELD, TERM_CLASS_COUNT_FIELD}; | |
| 10 | -use crate::database::{Database, write_with_retry}; | |
| 7 | +use crate::constant::TERM_TABLE; | |
| 8 | +use crate::database::Database; | |
| 11 | 9 | use crate::db::field_update::FieldUpdate; |
| 12 | 10 | use crate::db::page::PagedList; |
| 13 | 11 | use crate::domain::term::{Term, TermId, TermName}; |
| @@ -27,12 +25,30 @@ pub async fn create( | ||
| 27 | 25 | ends_at, |
| 28 | 26 | archived_at: None, |
| 29 | 27 | }; |
| 30 | - let created: Option<Term> = db.create(term.id.record()).content(term).await?; | |
| 31 | - created.ok_or_else(|| AppError::Internal("failed to create term".into())) | |
| 28 | + let created = sqlx::query_as!( | |
| 29 | + Term, | |
| 30 | + r#"INSERT INTO term (id, name, starts_at, ends_at, archived_at) | |
| 31 | + VALUES ($1, $2, $3, $4, NULL) | |
| 32 | + RETURNING id, name, starts_at, ends_at, archived_at"#, | |
| 33 | + term.id, | |
| 34 | + term.name, | |
| 35 | + term.starts_at, | |
| 36 | + term.ends_at, | |
| 37 | + ) | |
| 38 | + .fetch_one(db) | |
| 39 | + .await?; | |
| 40 | + Ok(created) | |
| 32 | 41 | } |
| 33 | 42 | |
| 34 | 43 | pub async fn read(db: &Database, id: &TermId) -> Result<Option<Term>, AppError> { |
| 35 | - Ok(db.select(id.record()).await?) | |
| 44 | + let term = sqlx::query_as!( | |
| 45 | + Term, | |
| 46 | + r#"SELECT id, name, starts_at, ends_at, archived_at FROM term WHERE id = $1"#, | |
| 47 | + id, | |
| 48 | + ) | |
| 49 | + .fetch_optional(db) | |
| 50 | + .await?; | |
| 51 | + Ok(term) | |
| 36 | 52 | } |
| 37 | 53 | |
| 38 | 54 | /// Every term, newest first — the school calendar is small by nature. |
| @@ -42,7 +58,7 @@ pub async fn list_all( | ||
| 42 | 58 | offset: i64, |
| 43 | 59 | ) -> Result<(Vec<Term>, i64), AppError> { |
| 44 | 60 | PagedList::new("term", "ORDER BY starts_at DESC, id DESC") |
| 45 | - .run(limit, offset, db) | |
| 61 | + .run::<Term>(limit, offset, db) | |
| 46 | 62 | .await |
| 47 | 63 | } |
| 48 | 64 | |
| @@ -57,10 +73,10 @@ pub async fn update( | ||
| 57 | 73 | starts_at: Option<Timestamp>, |
| 58 | 74 | ends_at: Option<Timestamp>, |
| 59 | 75 | ) -> Result<Term, AppError> { |
| 60 | - FieldUpdate::new(term.id.record()) | |
| 61 | - .set("name", name) | |
| 62 | - .set("starts_at", starts_at) | |
| 63 | - .set("ends_at", ends_at) | |
| 76 | + FieldUpdate::new(TERM_TABLE, term.id.uuid()) | |
| 77 | + .set("name", name.map(|name| name.as_str().to_owned())) | |
| 78 | + .set("starts_at", starts_at.map(|at| at.as_millis())) | |
| 79 | + .set("ends_at", ends_at.map(|at| at.as_millis())) | |
| 64 | 80 | .ordered("starts_at", "ends_at", range_error()) |
| 65 | 81 | .run::<Term>(db) |
| 66 | 82 | .await |
| @@ -77,20 +93,19 @@ pub async fn update( | ||
| 77 | 93 | /// refused itself, with the same 400 the lookup gives). `Err(NotFound)` |
| 78 | 94 | /// keeps the answer a concurrent *delete* used to get. |
| 79 | 95 | /// |
| 80 | -/// Classes ([`crate::domain::class_group::ClassGroup`]) link a term the same | |
| 81 | -/// way and count on `class_count` — a column of their own, because | |
| 82 | -/// `course_count` is seeded at boot from the course rows alone. | |
| 96 | +/// The referencing foreign keys (`course.term`, `class_group.term`, both | |
| 97 | +/// `NO ACTION`) are the backstop behind the counters, not a second guard: | |
| 98 | +/// while the counts are honest the `WHERE` decides every race on its own, | |
| 99 | +/// because both claim paths lock this very row before they write a link. | |
| 83 | 100 | pub async fn delete(db: &Database, term: Term) -> Result<bool, AppError> { |
| 84 | - let sql = format!( | |
| 85 | - "DELETE $term WHERE ({COURSE_COUNT_FIELD} ?? 0) = 0 \ | |
| 86 | - AND ({TERM_CLASS_COUNT_FIELD} ?? 0) = 0 RETURN BEFORE" | |
| 87 | - ); | |
| 88 | - // Through the retry, because the guard reads the very column a course | |
| 89 | - // create claims: a lost round writes nothing, and re-sending it is what | |
| 90 | - // keeps the answer the 404 or 409 it owes instead of a 500. | |
| 91 | - let gone: Vec<Term> = | |
| 92 | - write_with_retry(db, &sql, &[("term".into(), term.id.record().into_value())]).await?; | |
| 93 | - if !gone.is_empty() { | |
| 101 | + let gone = sqlx::query!( | |
| 102 | + r#"DELETE FROM term | |
| 103 | + WHERE id = $1 AND course_count = 0 AND class_count = 0"#, | |
| 104 | + term.id, | |
| 105 | + ) | |
| 106 | + .execute(db) | |
| 107 | + .await?; | |
| 108 | + if gone.rows_affected() > 0 { | |
| 94 | 109 | return Ok(true); |
| 95 | 110 | } |
| 96 | 111 | // Still linked or already gone: the one statement cannot tell those |
| @@ -105,44 +120,39 @@ pub async fn delete(db: &Database, term: Term) -> Result<bool, AppError> { | ||
| 105 | 120 | /// open row, so a repeat archive writes nothing and answers with the |
| 106 | 121 | /// *original* stamp — the year is not re-dated by a double click. |
| 107 | 122 | pub async fn archive(db: &Database, term: Term) -> Result<Term, AppError> { |
| 108 | - stamp( | |
| 109 | - db, | |
| 110 | - term, | |
| 111 | - "UPDATE $term SET archived_at = $now WHERE archived_at = NONE RETURN AFTER", | |
| 112 | - Some(Timestamp::now()), | |
| 123 | + let written = sqlx::query_as!( | |
| 124 | + Term, | |
| 125 | + r#"UPDATE term SET archived_at = $2 | |
| 126 | + WHERE id = $1 AND archived_at IS NULL | |
| 127 | + RETURNING id, name, starts_at, ends_at, archived_at"#, | |
| 128 | + term.id, | |
| 129 | + Timestamp::now(), | |
| 113 | 130 | ) |
| 114 | - .await | |
| 131 | + .fetch_optional(db) | |
| 132 | + .await?; | |
| 133 | + match written { | |
| 134 | + Some(written) => Ok(written), | |
| 135 | + // A repeat archive (or a term deleted under the request): the stored | |
| 136 | + // row is the answer, and only the no-op path pays for the read. | |
| 137 | + None => read(db, &term.id).await?.ok_or(AppError::NotFound), | |
| 138 | + } | |
| 115 | 139 | } |
| 116 | 140 | |
| 117 | 141 | /// Re-open the term; idempotent the same way. |
| 118 | 142 | pub async fn unarchive(db: &Database, term: Term) -> Result<Term, AppError> { |
| 119 | - stamp( | |
| 120 | - db, | |
| 121 | - term, | |
| 122 | - "UPDATE $term SET archived_at = NONE WHERE archived_at != NONE RETURN AFTER", | |
| 123 | - None, | |
| 143 | + let written = sqlx::query_as!( | |
| 144 | + Term, | |
| 145 | + r#"UPDATE term SET archived_at = NULL | |
| 146 | + WHERE id = $1 AND archived_at IS NOT NULL | |
| 147 | + RETURNING id, name, starts_at, ends_at, archived_at"#, | |
| 148 | + term.id, | |
| 124 | 149 | ) |
| 125 | - .await | |
| 126 | -} | |
| 127 | - | |
| 128 | -/// One conditional write, through [`write_with_retry`] like every other | |
| 129 | -/// guarded single statement here; an empty result is the no-op case, and | |
| 130 | -/// only that path pays for the read that reports the stored row. | |
| 131 | -async fn stamp( | |
| 132 | - db: &Database, | |
| 133 | - term: Term, | |
| 134 | - sql: &str, | |
| 135 | - now: Option<Timestamp>, | |
| 136 | -) -> Result<Term, AppError> { | |
| 137 | - let mut bindings = vec![("term".into(), term.id.record().into_value())]; | |
| 138 | - if let Some(now) = now { | |
| 139 | - bindings.push(("now".into(), now.as_millis().into_value())); | |
| 140 | - } | |
| 141 | - let written: Vec<Term> = write_with_retry(db, sql, &bindings).await?; | |
| 142 | - if let Some(written) = written.into_iter().next() { | |
| 143 | - return Ok(written); | |
| 150 | + .fetch_optional(db) | |
| 151 | + .await?; | |
| 152 | + match written { | |
| 153 | + Some(written) => Ok(written), | |
| 154 | + None => read(db, &term.id).await?.ok_or(AppError::NotFound), | |
| 144 | 155 | } |
| 145 | - read(db, &term.id).await?.ok_or(AppError::NotFound) | |
| 146 | 156 | } |
| 147 | 157 | |
| 148 | 158 | #[cfg(test)] |
src/domain/course.rsmodified+6 -0
| @@ -27,6 +27,12 @@ impl CourseId { | ||
| 27 | 27 | pub fn key(&self) -> String { |
| 28 | 28 | self.0.to_string() |
| 29 | 29 | } |
| 30 | + | |
| 31 | + /// The inner uuid, for binding the id through the runtime-checked | |
| 32 | + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. | |
| 33 | + pub(crate) fn uuid(&self) -> uuid::Uuid { | |
| 34 | + self.0 | |
| 35 | + } | |
| 30 | 36 | } |
| 31 | 37 | |
| 32 | 38 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
src/domain/course_session.rsmodified+6 -0
| @@ -28,6 +28,12 @@ impl CourseSessionId { | ||
| 28 | 28 | pub fn key(&self) -> String { |
| 29 | 29 | self.0.to_string() |
| 30 | 30 | } |
| 31 | + | |
| 32 | + /// The inner uuid, for binding the id through the runtime-checked | |
| 33 | + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. | |
| 34 | + pub(crate) fn uuid(&self) -> uuid::Uuid { | |
| 35 | + self.0 | |
| 36 | + } | |
| 31 | 37 | } |
| 32 | 38 | |
| 33 | 39 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
src/domain/subject.rsmodified+6 -0
| @@ -34,6 +34,12 @@ impl SubjectId { | ||
| 34 | 34 | pub fn key(&self) -> String { |
| 35 | 35 | self.0.to_string() |
| 36 | 36 | } |
| 37 | + | |
| 38 | + /// The inner uuid, for binding the id through the runtime-checked | |
| 39 | + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. | |
| 40 | + pub(crate) fn uuid(&self) -> Uuid { | |
| 41 | + self.0 | |
| 42 | + } | |
| 37 | 43 | } |
| 38 | 44 | |
| 39 | 45 | #[derive(Debug, Clone, PartialEq, Eq, Type)] |
src/domain/term.rsmodified+6 -0
| @@ -34,6 +34,12 @@ impl TermId { | ||
| 34 | 34 | pub fn key(&self) -> String { |
| 35 | 35 | self.0.to_string() |
| 36 | 36 | } |
| 37 | + | |
| 38 | + /// The inner uuid, for binding the id through the runtime-checked | |
| 39 | + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. | |
| 40 | + pub(crate) fn uuid(&self) -> Uuid { | |
| 41 | + self.0 | |
| 42 | + } | |
| 37 | 43 | } |
| 38 | 44 | |
| 39 | 45 | /// The answer every link to a term that is not there gets — the claim is a |