diff --git a/migrations/school/20260912000004_food_appointments_fees.sql b/migrations/school/20260912000004_food_appointments_fees.sql index 867c2d2..5edc0c0 100644 --- a/migrations/school/20260912000004_food_appointments_fees.sql +++ b/migrations/school/20260912000004_food_appointments_fees.sql @@ -41,7 +41,9 @@ CREATE INDEX appointment_slot_series ON appointment_slot (series); CREATE TABLE appointment ( id uuid PRIMARY KEY, - slot uuid NOT NULL REFERENCES appointment_slot(id) ON DELETE NO ACTION, + -- Nullable + SET NULL ports the old dangling-ref contract: a settled + -- booking whose slot was deleted still renders, without a window. + slot uuid NULL REFERENCES appointment_slot(id) ON DELETE SET NULL, requester uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')), @@ -108,7 +110,11 @@ CREATE TABLE dietary_profile ( -- A cancel flips `status` and stamps `cancelled_at`; the row stays so the -- freed seat is still auditable against the ledger line it charged. CREATE TABLE meal_booking ( - menu TEXT NOT NULL REFERENCES menu(id) ON DELETE NO ACTION, + -- No FK to menu(id) ON PURPOSE: cancelled booking rows must survive menu + -- deletion (their attempt counter keeps meal_ledger (booking, attempt) + -- ids unique across a republish — the pinned meal_bookings_round_trip + -- contract). A booking's menu exists at write time by app transaction. + menu TEXT NOT NULL, student uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, booked_by uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, status TEXT NOT NULL DEFAULT 'booked' diff --git a/src/db/page.rs b/src/db/page.rs index 26b9d1a..ee4a509 100644 --- a/src/db/page.rs +++ b/src/db/page.rs @@ -62,6 +62,11 @@ pub(crate) enum Param { /// still named, so `IS NOT DISTINCT FROM $n` compares an absent link /// truthfully. OptUuid(Option), + /// Nullable TEXT column clear/set — same NULL-with-type rationale as + /// OptUuid. + OptText(Option), + /// Nullable BIGINT column clear/set. + OptI64(Option), } impl Param { @@ -73,6 +78,8 @@ impl Param { Param::Text(value) => args.add(value), Param::Uuid(value) => args.add(value), Param::OptUuid(value) => args.add(value), + Param::OptText(value) => args.add(value), + Param::OptI64(value) => args.add(value), }; } } diff --git a/src/db/subject.rs b/src/db/subject.rs index 71389d8..69a7ada 100644 --- a/src/db/subject.rs +++ b/src/db/subject.rs @@ -1,19 +1,17 @@ //! The `subject` table: the count-and-create that pins a topic to a live //! course row, the bulk id listing, the field-scoped PATCH, and the guarded -//! delete whose `WHERE` reads the row's own reference counters. The type and +//! delete whose guard reads the row's own reference counters. The type and //! its validation live in [`crate::domain::subject`]. -use surrealdb::types::{RecordId, SurrealValue}; - -use crate::constant::ENROLLMENT_COUNT_FIELD; -use crate::database::{Database, transaction_with_retry}; -use crate::db::cap; +use crate::constant::SUBJECT_TABLE; +use crate::database::Database; use crate::db::field_update::FieldUpdate; use crate::db::page::PagedList; use crate::domain::course::CourseId; use crate::domain::subject::{Subject, SubjectDescription, SubjectId, SubjectName}; use crate::error::AppError; + pub async fn create( db: &Database, course: &CourseId, @@ -26,24 +24,40 @@ pub async fn create( name, description, }; - // The course row is *written* (bumped and put back), not read, so this - // collides with `Course::delete`'s cascade: a topic that outlives its - // course 404s through `subject_with_course` forever, while - // `must_exist` still accepts its id — so a bank template can be - // tagged with a subject nobody can reach. See [`cap::touch_and_create`]. - cap::touch_and_create( - &course.record(), - ENROLLMENT_COUNT_FIELD, - &subject.id.record(), - &subject, - db, + // The course foreign key is the existence proof the old bump-and-restore + // "touch" trick faked: a topic whose course is already gone — or which is + // deleted while this insert is in flight — is refused here, so a subject + // can never outlive its course and a bank template can never be tagged + // with a subject nobody can reach. `23503` is the parent-gone refusal, + // the same answer the touch produced. + let created = sqlx::query_as!( + Subject, + r#"INSERT INTO subject (id, course, name, description, exam_question_count, homework_count) + VALUES ($1, $2, $3, $4, 0, 0) + RETURNING id, course, name, description"#, + subject.id, + subject.course, + subject.name, + subject.description, ) - .await? - .ok_or(AppError::NotFound) + .fetch_one(db) + .await; + match created { + Ok(created) => Ok(created), + Err(err) if crate::database::foreign_key_violation(&err) => Err(AppError::NotFound), + Err(err) => Err(err.into()), + } } pub async fn read(db: &Database, id: &SubjectId) -> Result, AppError> { - Ok(db.select(id.record()).await?) + let subject = sqlx::query_as!( + Subject, + r#"SELECT id, course, name, description FROM subject WHERE id = $1"#, + id, + ) + .fetch_optional(db) + .await?; + Ok(subject) } /// 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 = ids.iter().map(|id| id.record()).collect(); - let mut result = db - .query("SELECT * FROM subject WHERE id IN $ids") - .bind(("ids", records)) - .await? - .check()?; - Ok(result.take::>(0)?) + let keys: Vec = ids.iter().map(|id| **id).collect(); + let rows = sqlx::query_as!( + Subject, + r#"SELECT id, course, name, description FROM subject WHERE id = ANY($1)"#, + keys, + ) + .fetch_all(db) + .await?; + Ok(rows) } -/// The course's subjects in curriculum order (ULID ids sort by creation). +/// The course's subjects in curriculum order (uuid ids sort by creation). pub async fn list_for_course( db: &Database, course: &CourseId, limit: Option, offset: i64, ) -> Result<(Vec, i64), AppError> { - PagedList::new("subject WHERE course = $course", "ORDER BY id ASC") - .bind("course", course.record()) - .run(limit, offset, db) + PagedList::new("subject WHERE course = $1", "ORDER BY id ASC") + .bind(course.uuid()) + .run::(limit, offset, db) .await } @@ -86,9 +102,9 @@ pub async fn update( name: Option, description: Option, ) -> Result { - FieldUpdate::new(subject.id.record()) - .set("name", name) - .set("description", description) + FieldUpdate::new(SUBJECT_TABLE, subject.id.uuid()) + .set("name", name.map(|name| name.as_str().to_owned())) + .set("description", description.map(|d| d.as_str().to_owned())) .run::(db) .await } @@ -99,72 +115,63 @@ pub async fn update( /// /// Exam questions and homework are *not* cascaded: their `subject` is a /// required field that may not be orphaned, so either one still refuses the -/// delete with a 409. That refusal is the delete's own `WHERE`, read off the -/// two reference counters this row carries -/// ([`crate::constant::SUBJECT_QUESTION_COUNT_FIELD`] and its homework -/// twin), which is what makes it hold against a question created by a -/// request racing this one — the cross-table `SELECT … LIMIT 1` it replaces -/// was a count-then-delete no transaction serializes, pinned by three -/// process-wide locks that could not cover the round trip between them. +/// delete with a 409. That refusal is this transaction's own check, read +/// off the two reference counters this row carries +/// (`exam_question_count` and its homework twin) under a `FOR UPDATE` row +/// lock — the lock *is* the guard, because every counter writer must +/// update this same row, so no question can land between the check and the +/// delete. The old store could not conflict-check the cross-table shape, +/// which is why the check had to be the `DELETE`'s own `WHERE`; here the +/// row lock does it and the `WHERE` reads off the same columns. /// /// Which of the two blocked is read off the counters *before* the delete, /// purely to pick the message; the decision itself was already made by the -/// `WHERE`. +/// check. /// /// The bank's subject is optional metadata, and blocking on it was a dead /// end — only the template's owner may re-tag it, so a manager could never /// clear their own 409, and a private template raising it leaked its -/// existence. Its cascade runs *after* the conditional delete, so a refused +/// existence. Its cascade runs *after* the guarded delete, so a refused /// delete leaves every template's subject where it was. pub async fn delete(db: &Database, subject: Subject) -> Result { - let (mut result, mut errors) = transaction_with_retry( - db, - "BEGIN TRANSACTION; - LET $held = (SELECT exam_question_count AS q, homework_count AS h FROM $sub); - LET $before = (DELETE $sub - WHERE (exam_question_count ?? 0) = 0 AND (homework_count ?? 0) = 0 - RETURN BEFORE); - IF array::len($before) = 0 { - THROW IF array::len($held) = 0 { 'subject_missing' } - ELSE IF ($held[0].q ?? 0) > 0 { 'subject_questions' } - ELSE { 'subject_homework' } - }; - UPDATE bank_question SET subject = NONE WHERE subject = $sub; - RETURN $before; - COMMIT TRANSACTION;", - &[("sub".into(), subject.id.record().into_value())], - &["subject_missing", "subject_questions", "subject_homework"], - ) - .await?; - // An aborted transaction errors every slot; only the THROW's own slot - // names the marker (the [`crate::db::appointment_slot`] treatment), - // and a lost round is re-sent rather than reported. - let thrown = |marker: &str| { - errors - .values() - .any(|error| error.to_string().contains(marker)) - }; - if thrown("subject_questions") { - return Err(AppError::Conflict( - "exam questions still reference this subject — re-tag or delete them first", - )); - } - if thrown("subject_homework") { - return Err(AppError::Conflict( - "homework still references this subject — re-tag or delete it first", - )); - } - if thrown("subject_missing") { - return Err(AppError::NotFound); - } - if let Some(error) = errors.drain().map(|(_, error)| error).next() { - return Err(error.into()); - } - // Read through the trailing `RETURN`, not a counted slot — see - // [`crate::db::exam::delete`]. - let slot = result.num_statements().saturating_sub(2); - let deleted: Option = result.take::>(slot)?.into_iter().next(); - deleted.ok_or(AppError::NotFound) + crate::database::tx_with_retry(db, false, async |tx| { + // The pre-image read and the guard in one locked statement: the row + // (and its counters) cannot change under this transaction. + let held = sqlx::query!( + r#"SELECT exam_question_count, homework_count + FROM subject WHERE id = $1 FOR UPDATE"#, + subject.id, + ) + .fetch_optional(&mut *tx) + .await?; + let held = held.ok_or(AppError::NotFound)?; + if held.exam_question_count > 0 { + return Err(AppError::Conflict( + "exam questions still reference this subject — re-tag or delete them first", + )); + } + if held.homework_count > 0 { + return Err(AppError::Conflict( + "homework still references this subject — re-tag or delete it first", + )); + } + let deleted = sqlx::query_as!( + Subject, + r#"DELETE FROM subject WHERE id = $1 + RETURNING id, course, name, description"#, + subject.id, + ) + .fetch_one(&mut *tx) + .await?; + sqlx::query!( + r#"UPDATE bank_question SET subject = NULL WHERE subject = $1"#, + subject.id, + ) + .execute(&mut *tx) + .await?; + Ok(deleted) + }) + .await } #[cfg(test)] diff --git a/src/db/term.rs b/src/db/term.rs index 8b21ac0..6bb8f40 100644 --- a/src/db/term.rs +++ b/src/db/term.rs @@ -4,10 +4,8 @@ //! stamps. The type and the errors its guards hand out live in //! [`crate::domain::term`]. -use surrealdb::types::SurrealValue; - -use crate::constant::{COURSE_COUNT_FIELD, TERM_CLASS_COUNT_FIELD}; -use crate::database::{Database, write_with_retry}; +use crate::constant::TERM_TABLE; +use crate::database::Database; use crate::db::field_update::FieldUpdate; use crate::db::page::PagedList; use crate::domain::term::{Term, TermId, TermName}; @@ -27,12 +25,30 @@ pub async fn create( ends_at, archived_at: None, }; - let created: Option = db.create(term.id.record()).content(term).await?; - created.ok_or_else(|| AppError::Internal("failed to create term".into())) + let created = sqlx::query_as!( + Term, + r#"INSERT INTO term (id, name, starts_at, ends_at, archived_at) + VALUES ($1, $2, $3, $4, NULL) + RETURNING id, name, starts_at, ends_at, archived_at"#, + term.id, + term.name, + term.starts_at, + term.ends_at, + ) + .fetch_one(db) + .await?; + Ok(created) } pub async fn read(db: &Database, id: &TermId) -> Result, AppError> { - Ok(db.select(id.record()).await?) + let term = sqlx::query_as!( + Term, + r#"SELECT id, name, starts_at, ends_at, archived_at FROM term WHERE id = $1"#, + id, + ) + .fetch_optional(db) + .await?; + Ok(term) } /// Every term, newest first — the school calendar is small by nature. @@ -42,7 +58,7 @@ pub async fn list_all( offset: i64, ) -> Result<(Vec, i64), AppError> { PagedList::new("term", "ORDER BY starts_at DESC, id DESC") - .run(limit, offset, db) + .run::(limit, offset, db) .await } @@ -57,10 +73,10 @@ pub async fn update( starts_at: Option, ends_at: Option, ) -> Result { - FieldUpdate::new(term.id.record()) - .set("name", name) - .set("starts_at", starts_at) - .set("ends_at", ends_at) + FieldUpdate::new(TERM_TABLE, term.id.uuid()) + .set("name", name.map(|name| name.as_str().to_owned())) + .set("starts_at", starts_at.map(|at| at.as_millis())) + .set("ends_at", ends_at.map(|at| at.as_millis())) .ordered("starts_at", "ends_at", range_error()) .run::(db) .await @@ -77,20 +93,19 @@ pub async fn update( /// refused itself, with the same 400 the lookup gives). `Err(NotFound)` /// keeps the answer a concurrent *delete* used to get. /// -/// Classes ([`crate::domain::class_group::ClassGroup`]) link a term the same -/// way and count on `class_count` — a column of their own, because -/// `course_count` is seeded at boot from the course rows alone. +/// The referencing foreign keys (`course.term`, `class_group.term`, both +/// `NO ACTION`) are the backstop behind the counters, not a second guard: +/// while the counts are honest the `WHERE` decides every race on its own, +/// because both claim paths lock this very row before they write a link. pub async fn delete(db: &Database, term: Term) -> Result { - let sql = format!( - "DELETE $term WHERE ({COURSE_COUNT_FIELD} ?? 0) = 0 \ - AND ({TERM_CLASS_COUNT_FIELD} ?? 0) = 0 RETURN BEFORE" - ); - // Through the retry, because the guard reads the very column a course - // create claims: a lost round writes nothing, and re-sending it is what - // keeps the answer the 404 or 409 it owes instead of a 500. - let gone: Vec = - write_with_retry(db, &sql, &[("term".into(), term.id.record().into_value())]).await?; - if !gone.is_empty() { + let gone = sqlx::query!( + r#"DELETE FROM term + WHERE id = $1 AND course_count = 0 AND class_count = 0"#, + term.id, + ) + .execute(db) + .await?; + if gone.rows_affected() > 0 { return Ok(true); } // Still linked or already gone: the one statement cannot tell those @@ -105,44 +120,39 @@ pub async fn delete(db: &Database, term: Term) -> Result { /// open row, so a repeat archive writes nothing and answers with the /// *original* stamp — the year is not re-dated by a double click. pub async fn archive(db: &Database, term: Term) -> Result { - stamp( - db, - term, - "UPDATE $term SET archived_at = $now WHERE archived_at = NONE RETURN AFTER", - Some(Timestamp::now()), + let written = sqlx::query_as!( + Term, + r#"UPDATE term SET archived_at = $2 + WHERE id = $1 AND archived_at IS NULL + RETURNING id, name, starts_at, ends_at, archived_at"#, + term.id, + Timestamp::now(), ) - .await + .fetch_optional(db) + .await?; + match written { + Some(written) => Ok(written), + // A repeat archive (or a term deleted under the request): the stored + // row is the answer, and only the no-op path pays for the read. + None => read(db, &term.id).await?.ok_or(AppError::NotFound), + } } /// Re-open the term; idempotent the same way. pub async fn unarchive(db: &Database, term: Term) -> Result { - stamp( - db, - term, - "UPDATE $term SET archived_at = NONE WHERE archived_at != NONE RETURN AFTER", - None, + let written = sqlx::query_as!( + Term, + r#"UPDATE term SET archived_at = NULL + WHERE id = $1 AND archived_at IS NOT NULL + RETURNING id, name, starts_at, ends_at, archived_at"#, + term.id, ) - .await -} - -/// One conditional write, through [`write_with_retry`] like every other -/// guarded single statement here; an empty result is the no-op case, and -/// only that path pays for the read that reports the stored row. -async fn stamp( - db: &Database, - term: Term, - sql: &str, - now: Option, -) -> Result { - let mut bindings = vec![("term".into(), term.id.record().into_value())]; - if let Some(now) = now { - bindings.push(("now".into(), now.as_millis().into_value())); - } - let written: Vec = write_with_retry(db, sql, &bindings).await?; - if let Some(written) = written.into_iter().next() { - return Ok(written); + .fetch_optional(db) + .await?; + match written { + Some(written) => Ok(written), + None => read(db, &term.id).await?.ok_or(AppError::NotFound), } - read(db, &term.id).await?.ok_or(AppError::NotFound) } #[cfg(test)] diff --git a/src/domain/appointment.rs b/src/domain/appointment.rs index 9301baf..287743a 100644 --- a/src/domain/appointment.rs +++ b/src/domain/appointment.rs @@ -45,6 +45,12 @@ impl AppointmentId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row — a malformed path param stays a 404, exactly /// like a well-formed one that names nothing. @@ -113,7 +119,12 @@ impl AppointmentReason { #[derive(Debug, Clone, sqlx::FromRow)] pub struct Appointment { pub(crate) id: AppointmentId, - pub(crate) slot: AppointmentSlotId, + /// The booked slot. `NULL` only on a settled row whose slot was later + /// withdrawn (a demotion sweep or a delete after the seat came back): + /// the booking still renders, without a window — the dangling reference + /// the old store permitted, now an explicit NULL. A live booking's slot + /// is pinned by the `occupied` counter and can never be NULL. + pub(crate) slot: Option, pub(crate) requester: UserId, pub(crate) status: AppointmentStatus, pub(crate) reason: AppointmentReason, @@ -137,8 +148,8 @@ impl Appointment { &self.id } - pub fn get_slot(&self) -> &AppointmentSlotId { - &self.slot + pub fn get_slot(&self) -> Option<&AppointmentSlotId> { + self.slot.as_ref() } pub fn get_requester(&self) -> &UserId { diff --git a/src/domain/appointment_slot.rs b/src/domain/appointment_slot.rs index c220293..c05c758 100644 --- a/src/domain/appointment_slot.rs +++ b/src/domain/appointment_slot.rs @@ -42,6 +42,12 @@ impl AppointmentSlotId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row — a malformed path param stays a 404, exactly /// like a well-formed one that names nothing. diff --git a/src/domain/bank_question.rs b/src/domain/bank_question.rs index 98c1821..8d7c7f9 100644 --- a/src/domain/bank_question.rs +++ b/src/domain/bank_question.rs @@ -75,6 +75,12 @@ impl BankQuestionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row — a malformed path param stays a 404, exactly /// like a well-formed one that names nothing. diff --git a/src/domain/board.rs b/src/domain/board.rs index 671deaf..7b39965 100644 --- a/src/domain/board.rs +++ b/src/domain/board.rs @@ -30,6 +30,12 @@ impl BoardId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row — a malformed path param stays a 404, exactly /// like a well-formed one that names nothing. diff --git a/src/domain/board_stroke.rs b/src/domain/board_stroke.rs index f94186f..1ea4b77 100644 --- a/src/domain/board_stroke.rs +++ b/src/domain/board_stroke.rs @@ -67,6 +67,12 @@ impl BoardStrokeId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/builder.rs b/src/domain/builder.rs index 4c6b94a..bd8d04f 100644 --- a/src/domain/builder.rs +++ b/src/domain/builder.rs @@ -30,6 +30,12 @@ impl BuilderId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/chatbot_message.rs b/src/domain/chatbot_message.rs index df74005..afc6c4d 100644 --- a/src/domain/chatbot_message.rs +++ b/src/domain/chatbot_message.rs @@ -39,6 +39,12 @@ impl ChatbotMessageId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/chatbot_thread.rs b/src/domain/chatbot_thread.rs index c58756c..7bfabac 100644 --- a/src/domain/chatbot_thread.rs +++ b/src/domain/chatbot_thread.rs @@ -24,6 +24,12 @@ impl ChatbotThreadId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/class_group.rs b/src/domain/class_group.rs index 1adbe11..12cc779 100644 --- a/src/domain/class_group.rs +++ b/src/domain/class_group.rs @@ -30,6 +30,12 @@ impl ClassGroupId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/class_member.rs b/src/domain/class_member.rs index fcb623a..9e14e82 100644 --- a/src/domain/class_member.rs +++ b/src/domain/class_member.rs @@ -39,6 +39,9 @@ impl ClassMemberId { #[derive(Debug, Clone, sqlx::FromRow)] pub struct ClassMember { pub(crate) class: ClassGroupId, + /// The column is `app_user` (the reserved-word rename); the Rust name is + /// the domain's. + #[sqlx(rename = "app_user")] pub(crate) user: UserId, pub(crate) added_by: UserId, /// When they were added, and the *only* thing "newest first" can mean diff --git a/src/domain/course.rs b/src/domain/course.rs index 50c69bb..71a2e08 100644 --- a/src/domain/course.rs +++ b/src/domain/course.rs @@ -18,6 +18,12 @@ impl CourseId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { @@ -27,6 +33,12 @@ impl CourseId { pub fn key(&self) -> String { self.0.to_string() } + + /// The inner uuid, for binding the id through the runtime-checked + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. + pub(crate) fn uuid(&self) -> uuid::Uuid { + self.0 + } } #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] diff --git a/src/domain/course_note.rs b/src/domain/course_note.rs index d14fa68..4058db2 100644 --- a/src/domain/course_note.rs +++ b/src/domain/course_note.rs @@ -18,6 +18,12 @@ impl CourseNoteId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/course_note_file.rs b/src/domain/course_note_file.rs index 4366aca..5a1d954 100644 --- a/src/domain/course_note_file.rs +++ b/src/domain/course_note_file.rs @@ -26,6 +26,12 @@ impl CourseNoteFileId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/course_session.rs b/src/domain/course_session.rs index 1d28a16..56df9ff 100644 --- a/src/domain/course_session.rs +++ b/src/domain/course_session.rs @@ -19,6 +19,12 @@ impl CourseSessionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { @@ -28,6 +34,12 @@ impl CourseSessionId { pub fn key(&self) -> String { self.0.to_string() } + + /// The inner uuid, for binding the id through the runtime-checked + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. + pub(crate) fn uuid(&self) -> uuid::Uuid { + self.0 + } } #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] diff --git a/src/domain/event.rs b/src/domain/event.rs index 74a508d..07868bb 100644 --- a/src/domain/event.rs +++ b/src/domain/event.rs @@ -21,6 +21,12 @@ impl EventId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/exam.rs b/src/domain/exam.rs index 3fbaca6..c8a256a 100644 --- a/src/domain/exam.rs +++ b/src/domain/exam.rs @@ -35,6 +35,12 @@ impl ExamId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/exam_question.rs b/src/domain/exam_question.rs index 4b624ef..9ba0d10 100644 --- a/src/domain/exam_question.rs +++ b/src/domain/exam_question.rs @@ -25,6 +25,12 @@ impl ExamQuestionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/fee_plan.rs b/src/domain/fee_plan.rs index c18c073..8c2cb41 100644 --- a/src/domain/fee_plan.rs +++ b/src/domain/fee_plan.rs @@ -32,6 +32,12 @@ impl FeePlanId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/homework.rs b/src/domain/homework.rs index 4d47d5d..7557118 100644 --- a/src/domain/homework.rs +++ b/src/domain/homework.rs @@ -34,6 +34,12 @@ impl HomeworkId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/homework_file.rs b/src/domain/homework_file.rs index c3d9a53..6a83a36 100644 --- a/src/domain/homework_file.rs +++ b/src/domain/homework_file.rs @@ -27,6 +27,12 @@ impl HomeworkFileId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/homework_result.rs b/src/domain/homework_result.rs index 10ac02f..56ee74a 100644 --- a/src/domain/homework_result.rs +++ b/src/domain/homework_result.rs @@ -32,6 +32,12 @@ impl HomeworkResultId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/homework_submission.rs b/src/domain/homework_submission.rs index 9b6773d..143cca9 100644 --- a/src/domain/homework_submission.rs +++ b/src/domain/homework_submission.rs @@ -32,6 +32,12 @@ impl HomeworkSubmissionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parses a wire key. A key that is not a UUID parses as the nil UUID, /// which matches no row. pub fn from_key(key: &str) -> Self { diff --git a/src/domain/menu_dish.rs b/src/domain/menu_dish.rs index f4993d3..224c137 100644 --- a/src/domain/menu_dish.rs +++ b/src/domain/menu_dish.rs @@ -34,6 +34,12 @@ impl MenuDishId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/message.rs b/src/domain/message.rs index 090336f..7c181bf 100644 --- a/src/domain/message.rs +++ b/src/domain/message.rs @@ -104,6 +104,12 @@ impl MessageId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/note.rs b/src/domain/note.rs index f2c5e05..07978a0 100644 --- a/src/domain/note.rs +++ b/src/domain/note.rs @@ -20,6 +20,12 @@ impl NoteId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/note_file.rs b/src/domain/note_file.rs index 5497e36..0ff0865 100644 --- a/src/domain/note_file.rs +++ b/src/domain/note_file.rs @@ -27,6 +27,12 @@ impl NoteFileId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/pomodoro.rs b/src/domain/pomodoro.rs index 24e0e33..3467b1a 100644 --- a/src/domain/pomodoro.rs +++ b/src/domain/pomodoro.rs @@ -18,6 +18,12 @@ impl PomodoroSessionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/pool_question.rs b/src/domain/pool_question.rs index 7261d49..48757b5 100644 --- a/src/domain/pool_question.rs +++ b/src/domain/pool_question.rs @@ -38,6 +38,12 @@ impl PoolQuestionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/rag_output.rs b/src/domain/rag_output.rs index f99e25a..a85b873 100644 --- a/src/domain/rag_output.rs +++ b/src/domain/rag_output.rs @@ -37,6 +37,12 @@ impl RagOutputId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/session.rs b/src/domain/session.rs index fe37ad2..fc2ed4a 100644 --- a/src/domain/session.rs +++ b/src/domain/session.rs @@ -17,6 +17,12 @@ impl SessionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// The hyphenated wire form. #[allow(dead_code)] pub fn key(&self) -> String { diff --git a/src/domain/solution.rs b/src/domain/solution.rs index 56ae2e2..2c559d0 100644 --- a/src/domain/solution.rs +++ b/src/domain/solution.rs @@ -24,6 +24,12 @@ impl SolutionId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/subject.rs b/src/domain/subject.rs index ce3690a..5b553f3 100644 --- a/src/domain/subject.rs +++ b/src/domain/subject.rs @@ -22,6 +22,12 @@ impl SubjectId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a @@ -34,6 +40,12 @@ impl SubjectId { pub fn key(&self) -> String { self.0.to_string() } + + /// The inner uuid, for binding the id through the runtime-checked + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. + pub(crate) fn uuid(&self) -> Uuid { + self.0 + } } #[derive(Debug, Clone, PartialEq, Eq, Type)] diff --git a/src/domain/term.rs b/src/domain/term.rs index 7e1bec8..dbd259b 100644 --- a/src/domain/term.rs +++ b/src/domain/term.rs @@ -22,6 +22,12 @@ impl TermId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a @@ -34,6 +40,12 @@ impl TermId { pub fn key(&self) -> String { self.0.to_string() } + + /// The inner uuid, for binding the id through the runtime-checked + /// builders ([`crate::db::page::Param`]) whose values are raw uuids. + pub(crate) fn uuid(&self) -> Uuid { + self.0 + } } /// The answer every link to a term that is not there gets — the claim is a diff --git a/src/domain/user.rs b/src/domain/user.rs index fa06ee1..28934fb 100644 --- a/src/domain/user.rs +++ b/src/domain/user.rs @@ -35,6 +35,12 @@ impl UserId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a diff --git a/src/domain/work_entry.rs b/src/domain/work_entry.rs index e390eb6..d3385b6 100644 --- a/src/domain/work_entry.rs +++ b/src/domain/work_entry.rs @@ -18,6 +18,12 @@ impl WorkEntryId { Self(next_uuid()) } + /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that + /// cannot take the newtype. Static `query!` binds take `self` directly. + pub fn uuid(&self) -> Uuid { + self.0 + } + /// Parse a wire key. A key that parses as no UUID — a malformed path /// segment — reads as the nil id, which matches no row: exactly the 404 a /// dangling record key produced under the old store, without turning a