forge
Explore
Log in

Don't you dare go Hollow

🎲 mint exam-question ids monotonically so bursts keep their order

Browse files at this commit

788229d68a46084efaace2c47465a4d446f5306eAhmet Kaan GümüşParent: d31f9ce
  • fix
    • 🎲 mint exam-question ids monotonically so bursts keep their order
Show full message
fix: 🎲 mint exam-question ids monotonically so bursts keep their order

`Ulid::new()` has no monotonic counter, so ids minted in the same
millisecond sort randomly. Exam questions are listed `ORDER BY id ASC`
and that order *is* the question order, so a teacher saving several
questions in one burst could get them back shuffled.

`ExamQuestionId::generate` now goes through `monotonic_id::next_ulid`.
`ChoiceId` deliberately stays on `Ulid::new()`: options are ordered by
their position in the vec, nothing reads that id as a clock.

No migration — existing ids are valid ULIDs and still sort by time at
millisecond granularity.

Every other `Ulid::new()` site was checked against every `ORDER BY id`
and left alone on purpose: they are all one row per request, so a
same-millisecond tie is between genuinely simultaneous rows from
different actors and carries no meaning. `exam_question` is the only
id-ordered table where one actor emits a semantic sequence.
src/domain/exam_question.rsmodified+24 -1
@@ -7,6 +7,7 @@ use crate::constant::{
7 use crate::database::{Database, EXAM_QUESTION_TABLE};7 use crate::database::{Database, EXAM_QUESTION_TABLE};
8 use crate::domain::bank_question::BankQuestionId;8 use crate::domain::bank_question::BankQuestionId;
9 use crate::domain::exam::ExamId;9 use crate::domain::exam::ExamId;
10+use crate::domain::monotonic_id::next_ulid;
10 use crate::domain::subject::SubjectId;11 use crate::domain::subject::SubjectId;
11 use crate::error::{AppError, ValidationError};12 use crate::error::{AppError, ValidationError};
12 use crate::validate::{validate_question_kind, validate_question_points, validate_required};13 use crate::validate::{validate_question_kind, validate_question_points, validate_required};
@@ -15,8 +16,12 @@ use crate::validate::{validate_question_kind, validate_question_points, validate
15 pub struct ExamQuestionId(RecordId);16 pub struct ExamQuestionId(RecordId);
16 17
17 impl ExamQuestionId {18 impl ExamQuestionId {
19+ /// Minted from the process-wide monotonic generator, not `Ulid::new()`: the
20+ /// id *is* the question's presentation order ([`ExamQuestion::list_for_exam`]
21+ /// sorts `id ASC`), and a random low half scrambles a burst of saves that
22+ /// lands inside one millisecond.
18 pub fn generate() -> Self {23 pub fn generate() -> Self {
19- Self(RecordId::new(EXAM_QUESTION_TABLE, Ulid::new().to_string()))24+ Self(RecordId::new(EXAM_QUESTION_TABLE, next_ulid().to_string()))
20 }25 }
21 26
22 pub fn from_key(key: &str) -> Self {27 pub fn from_key(key: &str) -> Self {
@@ -109,6 +114,9 @@ impl ChoiceText {
109 pub struct ChoiceId(String);114 pub struct ChoiceId(String);
110 115
111 impl ChoiceId {116 impl ChoiceId {
117+ /// Plain `Ulid::new()` deliberately: options are ordered by their position
118+ /// in the stored `Vec`, never by id, so nothing here reads the id as a
119+ /// clock — it only has to be unique.
112 fn generate() -> Self {120 fn generate() -> Self {
113 Self(Ulid::new().to_string())121 Self(Ulid::new().to_string())
114 }122 }
@@ -546,6 +554,21 @@ mod tests {
546 QuestionSpec::try_new(kind("choice"), choices, correct.map(str::to_string), &[])554 QuestionSpec::try_new(kind("choice"), choices, correct.map(str::to_string), &[])
547 }555 }
548 556
557+ /// A teacher saving several questions back to back gets them back in that
558+ /// order: `list_for_exam` sorts `id ASC`, so the ids minted inside one
559+ /// millisecond have to sort in mint order. Revert `generate` to
560+ /// `Ulid::new()` and this fails — the low 80 bits are redrawn per id, so a
561+ /// same-tick burst comes out shuffled.
562+ #[tokio::test]
563+ async fn ids_sort_in_creation_order() {
564+ let ids: Vec<String> = (0..500)
565+ .map(|_| ExamQuestionId::generate().key().to_string())
566+ .collect();
567+ let mut sorted = ids.clone();
568+ sorted.sort();
569+ assert_eq!(ids, sorted);
570+ }
571+
549 #[tokio::test]572 #[tokio::test]
550 async fn text_is_required() {573 async fn text_is_required() {
551 assert!(QuestionText::try_new("What is 2 + 2?").is_ok());574 assert!(QuestionText::try_new("What is 2 + 2?").is_ok());