Merge branch 'postgres' into pg/l4
Browse files at this commit39 changed files +418 -152
- migrations/school/20260912000004_food_appointments_fees.sql +8 -2
- src/db/page.rs +7 -0
- src/db/subject.rs +98 -91
- src/db/term.rs +66 -56
- src/domain/appointment.rs +14 -3
- src/domain/appointment_slot.rs +6 -0
- src/domain/bank_question.rs +6 -0
- src/domain/board.rs +6 -0
- src/domain/board_stroke.rs +6 -0
- src/domain/builder.rs +6 -0
- src/domain/chatbot_message.rs +6 -0
- src/domain/chatbot_thread.rs +6 -0
- src/domain/class_group.rs +6 -0
- src/domain/class_member.rs +3 -0
- src/domain/course.rs +12 -0
- src/domain/course_note.rs +6 -0
- src/domain/course_note_file.rs +6 -0
- src/domain/course_session.rs +12 -0
- src/domain/event.rs +6 -0
- src/domain/exam.rs +6 -0
- src/domain/exam_question.rs +6 -0
- src/domain/fee_plan.rs +6 -0
- src/domain/homework.rs +6 -0
- src/domain/homework_file.rs +6 -0
- src/domain/homework_result.rs +6 -0
- src/domain/homework_submission.rs +6 -0
- src/domain/menu_dish.rs +6 -0
- src/domain/message.rs +6 -0
- src/domain/note.rs +6 -0
- src/domain/note_file.rs +6 -0
- src/domain/pomodoro.rs +6 -0
- src/domain/pool_question.rs +6 -0
- src/domain/rag_output.rs +6 -0
- src/domain/session.rs +6 -0
- src/domain/solution.rs +6 -0
- src/domain/subject.rs +12 -0
- src/domain/term.rs +12 -0
- src/domain/user.rs +6 -0
- src/domain/work_entry.rs +6 -0
migrations/school/20260912000004_food_appointments_fees.sqlmodified+8 -2
| @@ -41,7 +41,9 @@ CREATE INDEX appointment_slot_series ON appointment_slot (series); | |||
| 41 | 41 | ||
| 42 | CREATE TABLE appointment ( | 42 | CREATE TABLE appointment ( |
| 43 | id uuid PRIMARY KEY, | 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 | requester uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, | 47 | requester uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, |
| 46 | status TEXT NOT NULL DEFAULT 'pending' | 48 | status TEXT NOT NULL DEFAULT 'pending' |
| 47 | CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')), | 49 | CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')), |
| @@ -108,7 +110,11 @@ CREATE TABLE dietary_profile ( | |||
| 108 | -- A cancel flips `status` and stamps `cancelled_at`; the row stays so the | 110 | -- A cancel flips `status` and stamps `cancelled_at`; the row stays so the |
| 109 | -- freed seat is still auditable against the ledger line it charged. | 111 | -- freed seat is still auditable against the ledger line it charged. |
| 110 | CREATE TABLE meal_booking ( | 112 | CREATE TABLE meal_booking ( |
| 111 | - menu TEXT NOT NULL REFERENCES menu(id) ON DELETE NO ACTION, | 113 | + -- No FK to menu(id) ON PURPOSE: cancelled booking rows must survive menu |
| 114 | + -- deletion (their attempt counter keeps meal_ledger (booking, attempt) | ||
| 115 | + -- ids unique across a republish — the pinned meal_bookings_round_trip | ||
| 116 | + -- contract). A booking's menu exists at write time by app transaction. | ||
| 117 | + menu TEXT NOT NULL, | ||
| 112 | student uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, | 118 | student uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, |
| 113 | booked_by uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, | 119 | booked_by uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, |
| 114 | status TEXT NOT NULL DEFAULT 'booked' | 120 | status TEXT NOT NULL DEFAULT 'booked' |
src/db/page.rsmodified+7 -0
| @@ -62,6 +62,11 @@ pub(crate) enum Param { | |||
| 62 | /// still named, so `IS NOT DISTINCT FROM $n` compares an absent link | 62 | /// still named, so `IS NOT DISTINCT FROM $n` compares an absent link |
| 63 | /// truthfully. | 63 | /// truthfully. |
| 64 | OptUuid(Option<Uuid>), | 64 | OptUuid(Option<Uuid>), |
| 65 | + /// Nullable TEXT column clear/set — same NULL-with-type rationale as | ||
| 66 | + /// OptUuid. | ||
| 67 | + OptText(Option<String>), | ||
| 68 | + /// Nullable BIGINT column clear/set. | ||
| 69 | + OptI64(Option<i64>), | ||
| 65 | } | 70 | } |
| 66 | 71 | ||
| 67 | impl Param { | 72 | impl Param { |
| @@ -73,6 +78,8 @@ impl Param { | |||
| 73 | Param::Text(value) => args.add(value), | 78 | Param::Text(value) => args.add(value), |
| 74 | Param::Uuid(value) => args.add(value), | 79 | Param::Uuid(value) => args.add(value), |
| 75 | Param::OptUuid(value) => args.add(value), | 80 | Param::OptUuid(value) => args.add(value), |
| 81 | + Param::OptText(value) => args.add(value), | ||
| 82 | + Param::OptI64(value) => args.add(value), | ||
| 76 | }; | 83 | }; |
| 77 | } | 84 | } |
| 78 | } | 85 | } |
src/db/subject.rsmodified+98 -91
| @@ -1,19 +1,17 @@ | |||
| 1 | //! The `subject` table: the count-and-create that pins a topic to a live | 1 | //! The `subject` table: the count-and-create that pins a topic to a live |
| 2 | //! course row, the bulk id listing, the field-scoped PATCH, and the guarded | 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 | //! its validation live in [`crate::domain::subject`]. | 4 | //! its validation live in [`crate::domain::subject`]. |
| 5 | 5 | ||
| 6 | -use surrealdb::types::{RecordId, SurrealValue}; | 6 | +use crate::constant::SUBJECT_TABLE; |
| 7 | - | 7 | +use crate::database::Database; |
| 8 | -use crate::constant::ENROLLMENT_COUNT_FIELD; | ||
| 9 | -use crate::database::{Database, transaction_with_retry}; | ||
| 10 | -use crate::db::cap; | ||
| 11 | use crate::db::field_update::FieldUpdate; | 8 | use crate::db::field_update::FieldUpdate; |
| 12 | use crate::db::page::PagedList; | 9 | use crate::db::page::PagedList; |
| 13 | use crate::domain::course::CourseId; | 10 | use crate::domain::course::CourseId; |
| 14 | use crate::domain::subject::{Subject, SubjectDescription, SubjectId, SubjectName}; | 11 | use crate::domain::subject::{Subject, SubjectDescription, SubjectId, SubjectName}; |
| 15 | use crate::error::AppError; | 12 | use crate::error::AppError; |
| 16 | 13 | ||
| 14 | + | ||
| 17 | pub async fn create( | 15 | pub async fn create( |
| 18 | db: &Database, | 16 | db: &Database, |
| 19 | course: &CourseId, | 17 | course: &CourseId, |
| @@ -26,24 +24,40 @@ pub async fn create( | |||
| 26 | name, | 24 | name, |
| 27 | description, | 25 | description, |
| 28 | }; | 26 | }; |
| 29 | - // The course row is *written* (bumped and put back), not read, so this | 27 | + // The course foreign key is the existence proof the old bump-and-restore |
| 30 | - // collides with `Course::delete`'s cascade: a topic that outlives its | 28 | + // "touch" trick faked: a topic whose course is already gone — or which is |
| 31 | - // course 404s through `subject_with_course` forever, while | 29 | + // deleted while this insert is in flight — is refused here, so a subject |
| 32 | - // `must_exist` still accepts its id — so a bank template can be | 30 | + // can never outlive its course and a bank template can never be tagged |
| 33 | - // tagged with a subject nobody can reach. See [`cap::touch_and_create`]. | 31 | + // with a subject nobody can reach. `23503` is the parent-gone refusal, |
| 34 | - cap::touch_and_create( | 32 | + // the same answer the touch produced. |
| 35 | - &course.record(), | 33 | + let created = sqlx::query_as!( |
| 36 | - ENROLLMENT_COUNT_FIELD, | 34 | + Subject, |
| 37 | - &subject.id.record(), | 35 | + r#"INSERT INTO subject (id, course, name, description, exam_question_count, homework_count) |
| 38 | - &subject, | 36 | + VALUES ($1, $2, $3, $4, 0, 0) |
| 39 | - db, | 37 | + RETURNING id, course, name, description"#, |
| 38 | + subject.id, | ||
| 39 | + subject.course, | ||
| 40 | + subject.name, | ||
| 41 | + subject.description, | ||
| 40 | ) | 42 | ) |
| 41 | - .await? | 43 | + .fetch_one(db) |
| 42 | - .ok_or(AppError::NotFound) | 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 | pub async fn read(db: &Database, id: &SubjectId) -> Result<Option<Subject>, AppError> { | 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 | /// Several subjects in one query — the bulk half of a list endpoint that | 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 | if ids.is_empty() { | 66 | if ids.is_empty() { |
| 53 | return Ok(Vec::new()); | 67 | return Ok(Vec::new()); |
| 54 | } | 68 | } |
| 55 | - let records: Vec<RecordId> = ids.iter().map(|id| id.record()).collect(); | 69 | + let keys: Vec<SubjectId> = ids.iter().map(|id| **id).collect(); |
| 56 | - let mut result = db | 70 | + let rows = sqlx::query_as!( |
| 57 | - .query("SELECT * FROM subject WHERE id IN $ids") | 71 | + Subject, |
| 58 | - .bind(("ids", records)) | 72 | + r#"SELECT id, course, name, description FROM subject WHERE id = ANY($1)"#, |
| 59 | - .await? | 73 | + keys, |
| 60 | - .check()?; | 74 | + ) |
| 61 | - Ok(result.take::<Vec<Subject>>(0)?) | 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 | pub async fn list_for_course( | 81 | pub async fn list_for_course( |
| 66 | db: &Database, | 82 | db: &Database, |
| 67 | course: &CourseId, | 83 | course: &CourseId, |
| 68 | limit: Option<i64>, | 84 | limit: Option<i64>, |
| 69 | offset: i64, | 85 | offset: i64, |
| 70 | ) -> Result<(Vec<Subject>, i64), AppError> { | 86 | ) -> Result<(Vec<Subject>, i64), AppError> { |
| 71 | - PagedList::new("subject WHERE course = $course", "ORDER BY id ASC") | 87 | + PagedList::new("subject WHERE course = $1", "ORDER BY id ASC") |
| 72 | - .bind("course", course.record()) | 88 | + .bind(course.uuid()) |
| 73 | - .run(limit, offset, db) | 89 | + .run::<Subject>(limit, offset, db) |
| 74 | .await | 90 | .await |
| 75 | } | 91 | } |
| 76 | 92 | ||
| @@ -86,9 +102,9 @@ pub async fn update( | |||
| 86 | name: Option<SubjectName>, | 102 | name: Option<SubjectName>, |
| 87 | description: Option<SubjectDescription>, | 103 | description: Option<SubjectDescription>, |
| 88 | ) -> Result<Subject, AppError> { | 104 | ) -> Result<Subject, AppError> { |
| 89 | - FieldUpdate::new(subject.id.record()) | 105 | + FieldUpdate::new(SUBJECT_TABLE, subject.id.uuid()) |
| 90 | - .set("name", name) | 106 | + .set("name", name.map(|name| name.as_str().to_owned())) |
| 91 | - .set("description", description) | 107 | + .set("description", description.map(|d| d.as_str().to_owned())) |
| 92 | .run::<Subject>(db) | 108 | .run::<Subject>(db) |
| 93 | .await | 109 | .await |
| 94 | } | 110 | } |
| @@ -99,72 +115,63 @@ pub async fn update( | |||
| 99 | /// | 115 | /// |
| 100 | /// Exam questions and homework are *not* cascaded: their `subject` is a | 116 | /// Exam questions and homework are *not* cascaded: their `subject` is a |
| 101 | /// required field that may not be orphaned, so either one still refuses the | 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 | 118 | +/// delete with a 409. That refusal is this transaction's own check, read |
| 103 | -/// two reference counters this row carries | 119 | +/// off the two reference counters this row carries |
| 104 | -/// ([`crate::constant::SUBJECT_QUESTION_COUNT_FIELD`] and its homework | 120 | +/// (`exam_question_count` and its homework twin) under a `FOR UPDATE` row |
| 105 | -/// twin), which is what makes it hold against a question created by a | 121 | +/// lock — the lock *is* the guard, because every counter writer must |
| 106 | -/// request racing this one — the cross-table `SELECT … LIMIT 1` it replaces | 122 | +/// update this same row, so no question can land between the check and the |
| 107 | -/// was a count-then-delete no transaction serializes, pinned by three | 123 | +/// delete. The old store could not conflict-check the cross-table shape, |
| 108 | -/// process-wide locks that could not cover the round trip between them. | 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 | /// Which of the two blocked is read off the counters *before* the delete, | 127 | /// Which of the two blocked is read off the counters *before* the delete, |
| 111 | /// purely to pick the message; the decision itself was already made by the | 128 | /// purely to pick the message; the decision itself was already made by the |
| 112 | -/// `WHERE`. | 129 | +/// check. |
| 113 | /// | 130 | /// |
| 114 | /// The bank's subject is optional metadata, and blocking on it was a dead | 131 | /// The bank's subject is optional metadata, and blocking on it was a dead |
| 115 | /// end — only the template's owner may re-tag it, so a manager could never | 132 | /// end — only the template's owner may re-tag it, so a manager could never |
| 116 | /// clear their own 409, and a private template raising it leaked its | 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 | /// delete leaves every template's subject where it was. | 135 | /// delete leaves every template's subject where it was. |
| 119 | pub async fn delete(db: &Database, subject: Subject) -> Result<Subject, AppError> { | 136 | pub async fn delete(db: &Database, subject: Subject) -> Result<Subject, AppError> { |
| 120 | - let (mut result, mut errors) = transaction_with_retry( | 137 | + crate::database::tx_with_retry(db, false, async |tx| { |
| 121 | - db, | 138 | + // The pre-image read and the guard in one locked statement: the row |
| 122 | - "BEGIN TRANSACTION; | 139 | + // (and its counters) cannot change under this transaction. |
| 123 | - LET $held = (SELECT exam_question_count AS q, homework_count AS h FROM $sub); | 140 | + let held = sqlx::query!( |
| 124 | - LET $before = (DELETE $sub | 141 | + r#"SELECT exam_question_count, homework_count |
| 125 | - WHERE (exam_question_count ?? 0) = 0 AND (homework_count ?? 0) = 0 | 142 | + FROM subject WHERE id = $1 FOR UPDATE"#, |
| 126 | - RETURN BEFORE); | 143 | + subject.id, |
| 127 | - IF array::len($before) = 0 { | 144 | + ) |
| 128 | - THROW IF array::len($held) = 0 { 'subject_missing' } | 145 | + .fetch_optional(&mut *tx) |
| 129 | - ELSE IF ($held[0].q ?? 0) > 0 { 'subject_questions' } | 146 | + .await?; |
| 130 | - ELSE { 'subject_homework' } | 147 | + let held = held.ok_or(AppError::NotFound)?; |
| 131 | - }; | 148 | + if held.exam_question_count > 0 { |
| 132 | - UPDATE bank_question SET subject = NONE WHERE subject = $sub; | 149 | + return Err(AppError::Conflict( |
| 133 | - RETURN $before; | 150 | + "exam questions still reference this subject — re-tag or delete them first", |
| 134 | - COMMIT TRANSACTION;", | 151 | + )); |
| 135 | - &[("sub".into(), subject.id.record().into_value())], | 152 | + } |
| 136 | - &["subject_missing", "subject_questions", "subject_homework"], | 153 | + if held.homework_count > 0 { |
| 137 | - ) | 154 | + return Err(AppError::Conflict( |
| 138 | - .await?; | 155 | + "homework still references this subject — re-tag or delete it first", |
| 139 | - // An aborted transaction errors every slot; only the THROW's own slot | 156 | + )); |
| 140 | - // names the marker (the [`crate::db::appointment_slot`] treatment), | 157 | + } |
| 141 | - // and a lost round is re-sent rather than reported. | 158 | + let deleted = sqlx::query_as!( |
| 142 | - let thrown = |marker: &str| { | 159 | + Subject, |
| 143 | - errors | 160 | + r#"DELETE FROM subject WHERE id = $1 |
| 144 | - .values() | 161 | + RETURNING id, course, name, description"#, |
| 145 | - .any(|error| error.to_string().contains(marker)) | 162 | + subject.id, |
| 146 | - }; | 163 | + ) |
| 147 | - if thrown("subject_questions") { | 164 | + .fetch_one(&mut *tx) |
| 148 | - return Err(AppError::Conflict( | 165 | + .await?; |
| 149 | - "exam questions still reference this subject — re-tag or delete them first", | 166 | + sqlx::query!( |
| 150 | - )); | 167 | + r#"UPDATE bank_question SET subject = NULL WHERE subject = $1"#, |
| 151 | - } | 168 | + subject.id, |
| 152 | - if thrown("subject_homework") { | 169 | + ) |
| 153 | - return Err(AppError::Conflict( | 170 | + .execute(&mut *tx) |
| 154 | - "homework still references this subject — re-tag or delete it first", | 171 | + .await?; |
| 155 | - )); | 172 | + Ok(deleted) |
| 156 | - } | 173 | + }) |
| 157 | - if thrown("subject_missing") { | 174 | + .await |
| 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) | ||
| 168 | } | 175 | } |
| 169 | 176 | ||
| 170 | #[cfg(test)] | 177 | #[cfg(test)] |
src/db/term.rsmodified+66 -56
| @@ -4,10 +4,8 @@ | |||
| 4 | //! stamps. The type and the errors its guards hand out live in | 4 | //! stamps. The type and the errors its guards hand out live in |
| 5 | //! [`crate::domain::term`]. | 5 | //! [`crate::domain::term`]. |
| 6 | 6 | ||
| 7 | -use surrealdb::types::SurrealValue; | 7 | +use crate::constant::TERM_TABLE; |
| 8 | - | 8 | +use crate::database::Database; |
| 9 | -use crate::constant::{COURSE_COUNT_FIELD, TERM_CLASS_COUNT_FIELD}; | ||
| 10 | -use crate::database::{Database, write_with_retry}; | ||
| 11 | use crate::db::field_update::FieldUpdate; | 9 | use crate::db::field_update::FieldUpdate; |
| 12 | use crate::db::page::PagedList; | 10 | use crate::db::page::PagedList; |
| 13 | use crate::domain::term::{Term, TermId, TermName}; | 11 | use crate::domain::term::{Term, TermId, TermName}; |
| @@ -27,12 +25,30 @@ pub async fn create( | |||
| 27 | ends_at, | 25 | ends_at, |
| 28 | archived_at: None, | 26 | archived_at: None, |
| 29 | }; | 27 | }; |
| 30 | - let created: Option<Term> = db.create(term.id.record()).content(term).await?; | 28 | + let created = sqlx::query_as!( |
| 31 | - created.ok_or_else(|| AppError::Internal("failed to create term".into())) | 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 | pub async fn read(db: &Database, id: &TermId) -> Result<Option<Term>, AppError> { | 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 | /// Every term, newest first — the school calendar is small by nature. | 54 | /// Every term, newest first — the school calendar is small by nature. |
| @@ -42,7 +58,7 @@ pub async fn list_all( | |||
| 42 | offset: i64, | 58 | offset: i64, |
| 43 | ) -> Result<(Vec<Term>, i64), AppError> { | 59 | ) -> Result<(Vec<Term>, i64), AppError> { |
| 44 | PagedList::new("term", "ORDER BY starts_at DESC, id DESC") | 60 | PagedList::new("term", "ORDER BY starts_at DESC, id DESC") |
| 45 | - .run(limit, offset, db) | 61 | + .run::<Term>(limit, offset, db) |
| 46 | .await | 62 | .await |
| 47 | } | 63 | } |
| 48 | 64 | ||
| @@ -57,10 +73,10 @@ pub async fn update( | |||
| 57 | starts_at: Option<Timestamp>, | 73 | starts_at: Option<Timestamp>, |
| 58 | ends_at: Option<Timestamp>, | 74 | ends_at: Option<Timestamp>, |
| 59 | ) -> Result<Term, AppError> { | 75 | ) -> Result<Term, AppError> { |
| 60 | - FieldUpdate::new(term.id.record()) | 76 | + FieldUpdate::new(TERM_TABLE, term.id.uuid()) |
| 61 | - .set("name", name) | 77 | + .set("name", name.map(|name| name.as_str().to_owned())) |
| 62 | - .set("starts_at", starts_at) | 78 | + .set("starts_at", starts_at.map(|at| at.as_millis())) |
| 63 | - .set("ends_at", ends_at) | 79 | + .set("ends_at", ends_at.map(|at| at.as_millis())) |
| 64 | .ordered("starts_at", "ends_at", range_error()) | 80 | .ordered("starts_at", "ends_at", range_error()) |
| 65 | .run::<Term>(db) | 81 | .run::<Term>(db) |
| 66 | .await | 82 | .await |
| @@ -77,20 +93,19 @@ pub async fn update( | |||
| 77 | /// refused itself, with the same 400 the lookup gives). `Err(NotFound)` | 93 | /// refused itself, with the same 400 the lookup gives). `Err(NotFound)` |
| 78 | /// keeps the answer a concurrent *delete* used to get. | 94 | /// keeps the answer a concurrent *delete* used to get. |
| 79 | /// | 95 | /// |
| 80 | -/// Classes ([`crate::domain::class_group::ClassGroup`]) link a term the same | 96 | +/// The referencing foreign keys (`course.term`, `class_group.term`, both |
| 81 | -/// way and count on `class_count` — a column of their own, because | 97 | +/// `NO ACTION`) are the backstop behind the counters, not a second guard: |
| 82 | -/// `course_count` is seeded at boot from the course rows alone. | 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 | pub async fn delete(db: &Database, term: Term) -> Result<bool, AppError> { | 100 | pub async fn delete(db: &Database, term: Term) -> Result<bool, AppError> { |
| 84 | - let sql = format!( | 101 | + let gone = sqlx::query!( |
| 85 | - "DELETE $term WHERE ({COURSE_COUNT_FIELD} ?? 0) = 0 \ | 102 | + r#"DELETE FROM term |
| 86 | - AND ({TERM_CLASS_COUNT_FIELD} ?? 0) = 0 RETURN BEFORE" | 103 | + WHERE id = $1 AND course_count = 0 AND class_count = 0"#, |
| 87 | - ); | 104 | + term.id, |
| 88 | - // Through the retry, because the guard reads the very column a course | 105 | + ) |
| 89 | - // create claims: a lost round writes nothing, and re-sending it is what | 106 | + .execute(db) |
| 90 | - // keeps the answer the 404 or 409 it owes instead of a 500. | 107 | + .await?; |
| 91 | - let gone: Vec<Term> = | 108 | + if gone.rows_affected() > 0 { |
| 92 | - write_with_retry(db, &sql, &[("term".into(), term.id.record().into_value())]).await?; | ||
| 93 | - if !gone.is_empty() { | ||
| 94 | return Ok(true); | 109 | return Ok(true); |
| 95 | } | 110 | } |
| 96 | // Still linked or already gone: the one statement cannot tell those | 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 | /// open row, so a repeat archive writes nothing and answers with the | 120 | /// open row, so a repeat archive writes nothing and answers with the |
| 106 | /// *original* stamp — the year is not re-dated by a double click. | 121 | /// *original* stamp — the year is not re-dated by a double click. |
| 107 | pub async fn archive(db: &Database, term: Term) -> Result<Term, AppError> { | 122 | pub async fn archive(db: &Database, term: Term) -> Result<Term, AppError> { |
| 108 | - stamp( | 123 | + let written = sqlx::query_as!( |
| 109 | - db, | 124 | + Term, |
| 110 | - term, | 125 | + r#"UPDATE term SET archived_at = $2 |
| 111 | - "UPDATE $term SET archived_at = $now WHERE archived_at = NONE RETURN AFTER", | 126 | + WHERE id = $1 AND archived_at IS NULL |
| 112 | - Some(Timestamp::now()), | 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 | /// Re-open the term; idempotent the same way. | 141 | /// Re-open the term; idempotent the same way. |
| 118 | pub async fn unarchive(db: &Database, term: Term) -> Result<Term, AppError> { | 142 | pub async fn unarchive(db: &Database, term: Term) -> Result<Term, AppError> { |
| 119 | - stamp( | 143 | + let written = sqlx::query_as!( |
| 120 | - db, | 144 | + Term, |
| 121 | - term, | 145 | + r#"UPDATE term SET archived_at = NULL |
| 122 | - "UPDATE $term SET archived_at = NONE WHERE archived_at != NONE RETURN AFTER", | 146 | + WHERE id = $1 AND archived_at IS NOT NULL |
| 123 | - None, | 147 | + RETURNING id, name, starts_at, ends_at, archived_at"#, |
| 148 | + term.id, | ||
| 124 | ) | 149 | ) |
| 125 | - .await | 150 | + .fetch_optional(db) |
| 126 | -} | 151 | + .await?; |
| 127 | - | 152 | + match written { |
| 128 | -/// One conditional write, through [`write_with_retry`] like every other | 153 | + Some(written) => Ok(written), |
| 129 | -/// guarded single statement here; an empty result is the no-op case, and | 154 | + None => read(db, &term.id).await?.ok_or(AppError::NotFound), |
| 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); | ||
| 144 | } | 155 | } |
| 145 | - read(db, &term.id).await?.ok_or(AppError::NotFound) | ||
| 146 | } | 156 | } |
| 147 | 157 | ||
| 148 | #[cfg(test)] | 158 | #[cfg(test)] |
src/domain/appointment.rsmodified+14 -3
| @@ -45,6 +45,12 @@ impl AppointmentId { | |||
| 45 | Self(next_uuid()) | 45 | Self(next_uuid()) |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 49 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 50 | + pub fn uuid(&self) -> Uuid { | ||
| 51 | + self.0 | ||
| 52 | + } | ||
| 53 | + | ||
| 48 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 54 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 49 | /// which matches no row — a malformed path param stays a 404, exactly | 55 | /// which matches no row — a malformed path param stays a 404, exactly |
| 50 | /// like a well-formed one that names nothing. | 56 | /// like a well-formed one that names nothing. |
| @@ -113,7 +119,12 @@ impl AppointmentReason { | |||
| 113 | #[derive(Debug, Clone, sqlx::FromRow)] | 119 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 114 | pub struct Appointment { | 120 | pub struct Appointment { |
| 115 | pub(crate) id: AppointmentId, | 121 | pub(crate) id: AppointmentId, |
| 116 | - pub(crate) slot: AppointmentSlotId, | 122 | + /// The booked slot. `NULL` only on a settled row whose slot was later |
| 123 | + /// withdrawn (a demotion sweep or a delete after the seat came back): | ||
| 124 | + /// the booking still renders, without a window — the dangling reference | ||
| 125 | + /// the old store permitted, now an explicit NULL. A live booking's slot | ||
| 126 | + /// is pinned by the `occupied` counter and can never be NULL. | ||
| 127 | + pub(crate) slot: Option<AppointmentSlotId>, | ||
| 117 | pub(crate) requester: UserId, | 128 | pub(crate) requester: UserId, |
| 118 | pub(crate) status: AppointmentStatus, | 129 | pub(crate) status: AppointmentStatus, |
| 119 | pub(crate) reason: AppointmentReason, | 130 | pub(crate) reason: AppointmentReason, |
| @@ -137,8 +148,8 @@ impl Appointment { | |||
| 137 | &self.id | 148 | &self.id |
| 138 | } | 149 | } |
| 139 | 150 | ||
| 140 | - pub fn get_slot(&self) -> &AppointmentSlotId { | 151 | + pub fn get_slot(&self) -> Option<&AppointmentSlotId> { |
| 141 | - &self.slot | 152 | + self.slot.as_ref() |
| 142 | } | 153 | } |
| 143 | 154 | ||
| 144 | pub fn get_requester(&self) -> &UserId { | 155 | pub fn get_requester(&self) -> &UserId { |
src/domain/appointment_slot.rsmodified+6 -0
| @@ -42,6 +42,12 @@ impl AppointmentSlotId { | |||
| 42 | Self(next_uuid()) | 42 | Self(next_uuid()) |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 46 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 47 | + pub fn uuid(&self) -> Uuid { | ||
| 48 | + self.0 | ||
| 49 | + } | ||
| 50 | + | ||
| 45 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 51 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 46 | /// which matches no row — a malformed path param stays a 404, exactly | 52 | /// which matches no row — a malformed path param stays a 404, exactly |
| 47 | /// like a well-formed one that names nothing. | 53 | /// like a well-formed one that names nothing. |
src/domain/bank_question.rsmodified+6 -0
| @@ -75,6 +75,12 @@ impl BankQuestionId { | |||
| 75 | Self(next_uuid()) | 75 | Self(next_uuid()) |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 79 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 80 | + pub fn uuid(&self) -> Uuid { | ||
| 81 | + self.0 | ||
| 82 | + } | ||
| 83 | + | ||
| 78 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 84 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 79 | /// which matches no row — a malformed path param stays a 404, exactly | 85 | /// which matches no row — a malformed path param stays a 404, exactly |
| 80 | /// like a well-formed one that names nothing. | 86 | /// like a well-formed one that names nothing. |
src/domain/board.rsmodified+6 -0
| @@ -30,6 +30,12 @@ impl BoardId { | |||
| 30 | Self(next_uuid()) | 30 | Self(next_uuid()) |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 34 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 35 | + pub fn uuid(&self) -> Uuid { | ||
| 36 | + self.0 | ||
| 37 | + } | ||
| 38 | + | ||
| 33 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 39 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 34 | /// which matches no row — a malformed path param stays a 404, exactly | 40 | /// which matches no row — a malformed path param stays a 404, exactly |
| 35 | /// like a well-formed one that names nothing. | 41 | /// like a well-formed one that names nothing. |
src/domain/board_stroke.rsmodified+6 -0
| @@ -67,6 +67,12 @@ impl BoardStrokeId { | |||
| 67 | Self(next_uuid()) | 67 | Self(next_uuid()) |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 71 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 72 | + pub fn uuid(&self) -> Uuid { | ||
| 73 | + self.0 | ||
| 74 | + } | ||
| 75 | + | ||
| 70 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 76 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 71 | /// which matches no row. | 77 | /// which matches no row. |
| 72 | pub fn from_key(key: &str) -> Self { | 78 | pub fn from_key(key: &str) -> Self { |
src/domain/builder.rsmodified+6 -0
| @@ -30,6 +30,12 @@ impl BuilderId { | |||
| 30 | Self(next_uuid()) | 30 | Self(next_uuid()) |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 34 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 35 | + pub fn uuid(&self) -> Uuid { | ||
| 36 | + self.0 | ||
| 37 | + } | ||
| 38 | + | ||
| 33 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 39 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 34 | /// which matches no row. | 40 | /// which matches no row. |
| 35 | pub fn from_key(key: &str) -> Self { | 41 | pub fn from_key(key: &str) -> Self { |
src/domain/chatbot_message.rsmodified+6 -0
| @@ -39,6 +39,12 @@ impl ChatbotMessageId { | |||
| 39 | Self(next_uuid()) | 39 | Self(next_uuid()) |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 43 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 44 | + pub fn uuid(&self) -> Uuid { | ||
| 45 | + self.0 | ||
| 46 | + } | ||
| 47 | + | ||
| 42 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 48 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 43 | /// which matches no row. | 49 | /// which matches no row. |
| 44 | pub fn from_key(key: &str) -> Self { | 50 | pub fn from_key(key: &str) -> Self { |
src/domain/chatbot_thread.rsmodified+6 -0
| @@ -24,6 +24,12 @@ impl ChatbotThreadId { | |||
| 24 | Self(next_uuid()) | 24 | Self(next_uuid()) |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 28 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 29 | + pub fn uuid(&self) -> Uuid { | ||
| 30 | + self.0 | ||
| 31 | + } | ||
| 32 | + | ||
| 27 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 33 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 28 | /// which matches no row. | 34 | /// which matches no row. |
| 29 | pub fn from_key(key: &str) -> Self { | 35 | pub fn from_key(key: &str) -> Self { |
src/domain/class_group.rsmodified+6 -0
| @@ -30,6 +30,12 @@ impl ClassGroupId { | |||
| 30 | Self(next_uuid()) | 30 | Self(next_uuid()) |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 34 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 35 | + pub fn uuid(&self) -> Uuid { | ||
| 36 | + self.0 | ||
| 37 | + } | ||
| 38 | + | ||
| 33 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 39 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 34 | /// which matches no row. | 40 | /// which matches no row. |
| 35 | pub fn from_key(key: &str) -> Self { | 41 | pub fn from_key(key: &str) -> Self { |
src/domain/class_member.rsmodified+3 -0
| @@ -39,6 +39,9 @@ impl ClassMemberId { | |||
| 39 | #[derive(Debug, Clone, sqlx::FromRow)] | 39 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 40 | pub struct ClassMember { | 40 | pub struct ClassMember { |
| 41 | pub(crate) class: ClassGroupId, | 41 | pub(crate) class: ClassGroupId, |
| 42 | + /// The column is `app_user` (the reserved-word rename); the Rust name is | ||
| 43 | + /// the domain's. | ||
| 44 | + #[sqlx(rename = "app_user")] | ||
| 42 | pub(crate) user: UserId, | 45 | pub(crate) user: UserId, |
| 43 | pub(crate) added_by: UserId, | 46 | pub(crate) added_by: UserId, |
| 44 | /// When they were added, and the *only* thing "newest first" can mean | 47 | /// When they were added, and the *only* thing "newest first" can mean |
src/domain/course.rsmodified+12 -0
| @@ -18,6 +18,12 @@ impl CourseId { | |||
| 18 | Self(next_uuid()) | 18 | Self(next_uuid()) |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 22 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 23 | + pub fn uuid(&self) -> Uuid { | ||
| 24 | + self.0 | ||
| 25 | + } | ||
| 26 | + | ||
| 21 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 27 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 22 | /// which matches no row. | 28 | /// which matches no row. |
| 23 | pub fn from_key(key: &str) -> Self { | 29 | pub fn from_key(key: &str) -> Self { |
| @@ -27,6 +33,12 @@ impl CourseId { | |||
| 27 | pub fn key(&self) -> String { | 33 | pub fn key(&self) -> String { |
| 28 | self.0.to_string() | 34 | self.0.to_string() |
| 29 | } | 35 | } |
| 36 | + | ||
| 37 | + /// The inner uuid, for binding the id through the runtime-checked | ||
| 38 | + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. | ||
| 39 | + pub(crate) fn uuid(&self) -> uuid::Uuid { | ||
| 40 | + self.0 | ||
| 41 | + } | ||
| 30 | } | 42 | } |
| 31 | 43 | ||
| 32 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] | 44 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
src/domain/course_note.rsmodified+6 -0
| @@ -18,6 +18,12 @@ impl CourseNoteId { | |||
| 18 | Self(next_uuid()) | 18 | Self(next_uuid()) |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 22 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 23 | + pub fn uuid(&self) -> Uuid { | ||
| 24 | + self.0 | ||
| 25 | + } | ||
| 26 | + | ||
| 21 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 27 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 22 | /// which matches no row. | 28 | /// which matches no row. |
| 23 | pub fn from_key(key: &str) -> Self { | 29 | pub fn from_key(key: &str) -> Self { |
src/domain/course_note_file.rsmodified+6 -0
| @@ -26,6 +26,12 @@ impl CourseNoteFileId { | |||
| 26 | Self(next_uuid()) | 26 | Self(next_uuid()) |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 30 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 31 | + pub fn uuid(&self) -> Uuid { | ||
| 32 | + self.0 | ||
| 33 | + } | ||
| 34 | + | ||
| 29 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 35 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 30 | /// which matches no row. | 36 | /// which matches no row. |
| 31 | pub fn from_key(key: &str) -> Self { | 37 | pub fn from_key(key: &str) -> Self { |
src/domain/course_session.rsmodified+12 -0
| @@ -19,6 +19,12 @@ impl CourseSessionId { | |||
| 19 | Self(next_uuid()) | 19 | Self(next_uuid()) |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 23 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 24 | + pub fn uuid(&self) -> Uuid { | ||
| 25 | + self.0 | ||
| 26 | + } | ||
| 27 | + | ||
| 22 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 28 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 23 | /// which matches no row. | 29 | /// which matches no row. |
| 24 | pub fn from_key(key: &str) -> Self { | 30 | pub fn from_key(key: &str) -> Self { |
| @@ -28,6 +34,12 @@ impl CourseSessionId { | |||
| 28 | pub fn key(&self) -> String { | 34 | pub fn key(&self) -> String { |
| 29 | self.0.to_string() | 35 | self.0.to_string() |
| 30 | } | 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::Uuid { | ||
| 41 | + self.0 | ||
| 42 | + } | ||
| 31 | } | 43 | } |
| 32 | 44 | ||
| 33 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] | 45 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
src/domain/event.rsmodified+6 -0
| @@ -21,6 +21,12 @@ impl EventId { | |||
| 21 | Self(next_uuid()) | 21 | Self(next_uuid()) |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 25 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 26 | + pub fn uuid(&self) -> Uuid { | ||
| 27 | + self.0 | ||
| 28 | + } | ||
| 29 | + | ||
| 24 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 30 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 25 | /// which matches no row. | 31 | /// which matches no row. |
| 26 | pub fn from_key(key: &str) -> Self { | 32 | pub fn from_key(key: &str) -> Self { |
src/domain/exam.rsmodified+6 -0
| @@ -35,6 +35,12 @@ impl ExamId { | |||
| 35 | Self(next_uuid()) | 35 | Self(next_uuid()) |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 39 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 40 | + pub fn uuid(&self) -> Uuid { | ||
| 41 | + self.0 | ||
| 42 | + } | ||
| 43 | + | ||
| 38 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 44 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 39 | /// which matches no row. | 45 | /// which matches no row. |
| 40 | pub fn from_key(key: &str) -> Self { | 46 | pub fn from_key(key: &str) -> Self { |
src/domain/exam_question.rsmodified+6 -0
| @@ -25,6 +25,12 @@ impl ExamQuestionId { | |||
| 25 | Self(next_uuid()) | 25 | Self(next_uuid()) |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 29 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 30 | + pub fn uuid(&self) -> Uuid { | ||
| 31 | + self.0 | ||
| 32 | + } | ||
| 33 | + | ||
| 28 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 34 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 29 | /// which matches no row. | 35 | /// which matches no row. |
| 30 | pub fn from_key(key: &str) -> Self { | 36 | pub fn from_key(key: &str) -> Self { |
src/domain/fee_plan.rsmodified+6 -0
| @@ -32,6 +32,12 @@ impl FeePlanId { | |||
| 32 | Self(next_uuid()) | 32 | Self(next_uuid()) |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 36 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 37 | + pub fn uuid(&self) -> Uuid { | ||
| 38 | + self.0 | ||
| 39 | + } | ||
| 40 | + | ||
| 35 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 41 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 36 | /// which matches no row. | 42 | /// which matches no row. |
| 37 | pub fn from_key(key: &str) -> Self { | 43 | pub fn from_key(key: &str) -> Self { |
src/domain/homework.rsmodified+6 -0
| @@ -34,6 +34,12 @@ impl HomeworkId { | |||
| 34 | Self(next_uuid()) | 34 | Self(next_uuid()) |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 38 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 39 | + pub fn uuid(&self) -> Uuid { | ||
| 40 | + self.0 | ||
| 41 | + } | ||
| 42 | + | ||
| 37 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 43 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 38 | /// which matches no row. | 44 | /// which matches no row. |
| 39 | pub fn from_key(key: &str) -> Self { | 45 | pub fn from_key(key: &str) -> Self { |
src/domain/homework_file.rsmodified+6 -0
| @@ -27,6 +27,12 @@ impl HomeworkFileId { | |||
| 27 | Self(next_uuid()) | 27 | Self(next_uuid()) |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 31 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 32 | + pub fn uuid(&self) -> Uuid { | ||
| 33 | + self.0 | ||
| 34 | + } | ||
| 35 | + | ||
| 30 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 36 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 31 | /// which matches no row. | 37 | /// which matches no row. |
| 32 | pub fn from_key(key: &str) -> Self { | 38 | pub fn from_key(key: &str) -> Self { |
src/domain/homework_result.rsmodified+6 -0
| @@ -32,6 +32,12 @@ impl HomeworkResultId { | |||
| 32 | Self(next_uuid()) | 32 | Self(next_uuid()) |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 36 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 37 | + pub fn uuid(&self) -> Uuid { | ||
| 38 | + self.0 | ||
| 39 | + } | ||
| 40 | + | ||
| 35 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 41 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 36 | /// which matches no row. | 42 | /// which matches no row. |
| 37 | pub fn from_key(key: &str) -> Self { | 43 | pub fn from_key(key: &str) -> Self { |
src/domain/homework_submission.rsmodified+6 -0
| @@ -32,6 +32,12 @@ impl HomeworkSubmissionId { | |||
| 32 | Self(next_uuid()) | 32 | Self(next_uuid()) |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 36 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 37 | + pub fn uuid(&self) -> Uuid { | ||
| 38 | + self.0 | ||
| 39 | + } | ||
| 40 | + | ||
| 35 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, | 41 | /// Parses a wire key. A key that is not a UUID parses as the nil UUID, |
| 36 | /// which matches no row. | 42 | /// which matches no row. |
| 37 | pub fn from_key(key: &str) -> Self { | 43 | pub fn from_key(key: &str) -> Self { |
src/domain/menu_dish.rsmodified+6 -0
| @@ -34,6 +34,12 @@ impl MenuDishId { | |||
| 34 | Self(next_uuid()) | 34 | Self(next_uuid()) |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 38 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 39 | + pub fn uuid(&self) -> Uuid { | ||
| 40 | + self.0 | ||
| 41 | + } | ||
| 42 | + | ||
| 37 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 43 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 38 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 44 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 39 | /// dangling record key produced under the old store, without turning a | 45 | /// dangling record key produced under the old store, without turning a |
src/domain/message.rsmodified+6 -0
| @@ -104,6 +104,12 @@ impl MessageId { | |||
| 104 | Self(next_uuid()) | 104 | Self(next_uuid()) |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 108 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 109 | + pub fn uuid(&self) -> Uuid { | ||
| 110 | + self.0 | ||
| 111 | + } | ||
| 112 | + | ||
| 107 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 113 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 108 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 114 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 109 | /// dangling record key produced under the old store, without turning a | 115 | /// dangling record key produced under the old store, without turning a |
src/domain/note.rsmodified+6 -0
| @@ -20,6 +20,12 @@ impl NoteId { | |||
| 20 | Self(next_uuid()) | 20 | Self(next_uuid()) |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 24 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 25 | + pub fn uuid(&self) -> Uuid { | ||
| 26 | + self.0 | ||
| 27 | + } | ||
| 28 | + | ||
| 23 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 29 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 24 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 30 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 25 | /// dangling record key produced under the old store, without turning a | 31 | /// dangling record key produced under the old store, without turning a |
src/domain/note_file.rsmodified+6 -0
| @@ -27,6 +27,12 @@ impl NoteFileId { | |||
| 27 | Self(next_uuid()) | 27 | Self(next_uuid()) |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 31 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 32 | + pub fn uuid(&self) -> Uuid { | ||
| 33 | + self.0 | ||
| 34 | + } | ||
| 35 | + | ||
| 30 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 36 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 31 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 37 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 32 | /// dangling record key produced under the old store, without turning a | 38 | /// dangling record key produced under the old store, without turning a |
src/domain/pomodoro.rsmodified+6 -0
| @@ -18,6 +18,12 @@ impl PomodoroSessionId { | |||
| 18 | Self(next_uuid()) | 18 | Self(next_uuid()) |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 22 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 23 | + pub fn uuid(&self) -> Uuid { | ||
| 24 | + self.0 | ||
| 25 | + } | ||
| 26 | + | ||
| 21 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 27 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 22 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 28 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 23 | /// dangling record key produced under the old store, without turning a | 29 | /// dangling record key produced under the old store, without turning a |
src/domain/pool_question.rsmodified+6 -0
| @@ -38,6 +38,12 @@ impl PoolQuestionId { | |||
| 38 | Self(next_uuid()) | 38 | Self(next_uuid()) |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 42 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 43 | + pub fn uuid(&self) -> Uuid { | ||
| 44 | + self.0 | ||
| 45 | + } | ||
| 46 | + | ||
| 41 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 47 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 42 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 48 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 43 | /// dangling record key produced under the old store, without turning a | 49 | /// dangling record key produced under the old store, without turning a |
src/domain/rag_output.rsmodified+6 -0
| @@ -37,6 +37,12 @@ impl RagOutputId { | |||
| 37 | Self(next_uuid()) | 37 | Self(next_uuid()) |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 41 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 42 | + pub fn uuid(&self) -> Uuid { | ||
| 43 | + self.0 | ||
| 44 | + } | ||
| 45 | + | ||
| 40 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 46 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 41 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 47 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 42 | /// dangling record key produced under the old store, without turning a | 48 | /// dangling record key produced under the old store, without turning a |
src/domain/session.rsmodified+6 -0
| @@ -17,6 +17,12 @@ impl SessionId { | |||
| 17 | Self(next_uuid()) | 17 | Self(next_uuid()) |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 21 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 22 | + pub fn uuid(&self) -> Uuid { | ||
| 23 | + self.0 | ||
| 24 | + } | ||
| 25 | + | ||
| 20 | /// The hyphenated wire form. | 26 | /// The hyphenated wire form. |
| 21 | #[allow(dead_code)] | 27 | #[allow(dead_code)] |
| 22 | pub fn key(&self) -> String { | 28 | pub fn key(&self) -> String { |
src/domain/solution.rsmodified+6 -0
| @@ -24,6 +24,12 @@ impl SolutionId { | |||
| 24 | Self(next_uuid()) | 24 | Self(next_uuid()) |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 28 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 29 | + pub fn uuid(&self) -> Uuid { | ||
| 30 | + self.0 | ||
| 31 | + } | ||
| 32 | + | ||
| 27 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 33 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 28 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 34 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 29 | /// dangling record key produced under the old store, without turning a | 35 | /// dangling record key produced under the old store, without turning a |
src/domain/subject.rsmodified+12 -0
| @@ -22,6 +22,12 @@ impl SubjectId { | |||
| 22 | Self(next_uuid()) | 22 | Self(next_uuid()) |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 26 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 27 | + pub fn uuid(&self) -> Uuid { | ||
| 28 | + self.0 | ||
| 29 | + } | ||
| 30 | + | ||
| 25 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 31 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 26 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 32 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 27 | /// dangling record key produced under the old store, without turning a | 33 | /// dangling record key produced under the old store, without turning a |
| @@ -34,6 +40,12 @@ impl SubjectId { | |||
| 34 | pub fn key(&self) -> String { | 40 | pub fn key(&self) -> String { |
| 35 | self.0.to_string() | 41 | self.0.to_string() |
| 36 | } | 42 | } |
| 43 | + | ||
| 44 | + /// The inner uuid, for binding the id through the runtime-checked | ||
| 45 | + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. | ||
| 46 | + pub(crate) fn uuid(&self) -> Uuid { | ||
| 47 | + self.0 | ||
| 48 | + } | ||
| 37 | } | 49 | } |
| 38 | 50 | ||
| 39 | #[derive(Debug, Clone, PartialEq, Eq, Type)] | 51 | #[derive(Debug, Clone, PartialEq, Eq, Type)] |
src/domain/term.rsmodified+12 -0
| @@ -22,6 +22,12 @@ impl TermId { | |||
| 22 | Self(next_uuid()) | 22 | Self(next_uuid()) |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 26 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 27 | + pub fn uuid(&self) -> Uuid { | ||
| 28 | + self.0 | ||
| 29 | + } | ||
| 30 | + | ||
| 25 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 31 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 26 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 32 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 27 | /// dangling record key produced under the old store, without turning a | 33 | /// dangling record key produced under the old store, without turning a |
| @@ -34,6 +40,12 @@ impl TermId { | |||
| 34 | pub fn key(&self) -> String { | 40 | pub fn key(&self) -> String { |
| 35 | self.0.to_string() | 41 | self.0.to_string() |
| 36 | } | 42 | } |
| 43 | + | ||
| 44 | + /// The inner uuid, for binding the id through the runtime-checked | ||
| 45 | + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. | ||
| 46 | + pub(crate) fn uuid(&self) -> Uuid { | ||
| 47 | + self.0 | ||
| 48 | + } | ||
| 37 | } | 49 | } |
| 38 | 50 | ||
| 39 | /// The answer every link to a term that is not there gets — the claim is a | 51 | /// The answer every link to a term that is not there gets — the claim is a |
src/domain/user.rsmodified+6 -0
| @@ -35,6 +35,12 @@ impl UserId { | |||
| 35 | Self(next_uuid()) | 35 | Self(next_uuid()) |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 39 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 40 | + pub fn uuid(&self) -> Uuid { | ||
| 41 | + self.0 | ||
| 42 | + } | ||
| 43 | + | ||
| 38 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 44 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 39 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 45 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 40 | /// dangling record key produced under the old store, without turning a | 46 | /// dangling record key produced under the old store, without turning a |
src/domain/work_entry.rsmodified+6 -0
| @@ -18,6 +18,12 @@ impl WorkEntryId { | |||
| 18 | Self(next_uuid()) | 18 | Self(next_uuid()) |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that | ||
| 22 | + /// cannot take the newtype. Static `query!` binds take `self` directly. | ||
| 23 | + pub fn uuid(&self) -> Uuid { | ||
| 24 | + self.0 | ||
| 25 | + } | ||
| 26 | + | ||
| 21 | /// Parse a wire key. A key that parses as no UUID — a malformed path | 27 | /// Parse a wire key. A key that parses as no UUID — a malformed path |
| 22 | /// segment — reads as the nil id, which matches no row: exactly the 404 a | 28 | /// segment — reads as the nil id, which matches no row: exactly the 404 a |
| 23 | /// dangling record key produced under the old store, without turning a | 29 | /// dangling record key produced under the old store, without turning a |