Give back the seats the boot sweep has been eating
Browse files at this commit628b5e8e11018ec3d653447d89edca5876c6cb04Parent: 70591d4- fix
- give back the seats the boot sweep has been eating
2 changed files +197 -2
- src/migration_sql.rs +63 -2
- tests/persistence.rs +134 -0
src/migration_sql.rsmodified+63 -2
| @@ -964,10 +964,65 @@ pub const BACKFILL: &str = " | |||
| 964 | completed_at = time::unix(time::now()) * 1000 | 964 | completed_at = time::unix(time::now()) * 1000 |
| 965 | WHERE status = 'pending' AND created_at < time::unix(time::now()) * 1000 - $stale_ms; | 965 | WHERE status = 'pending' AND created_at < time::unix(time::now()) * 1000 - $stale_ms; |
| 966 | 966 | ||
| 967 | + -- The seats the sweep below spent every past boot *not* giving back: until | ||
| 968 | + -- today it deleted those rows without touching the course's counter, so | ||
| 969 | + -- every course it ever swept carries a phantom seat — one that denies a real | ||
| 970 | + -- student a place, and that `Course::delete` refuses to let anyone past, | ||
| 971 | + -- forever (its guard is `(enrollment_count ?? 0) = 0` on the course row). | ||
| 972 | + -- This recomputes the counter for every course whose stored number disagrees | ||
| 973 | + -- with the enrollment rows it actually has. | ||
| 974 | + -- | ||
| 975 | + -- Counting the rows cannot read *low*. The counter has exactly one meaning — | ||
| 976 | + -- the live enrollment rows of this course — and every writer moves it with | ||
| 977 | + -- the row in one transaction (`Enrollment::enroll`/`remove`, `set_role`'s | ||
| 978 | + -- demotion sweep, the class pump's attach and detach); `Course::delete` takes | ||
| 979 | + -- the rows *and* the course together, so no cascade leaves counted rows | ||
| 980 | + -- behind. That is what separates it from the lifetime tallies below, which | ||
| 981 | + -- are seeded once precisely because their rows do die under them. The claims | ||
| 982 | + -- that borrow this column as a collision device (`cap::touch_and_create`, | ||
| 983 | + -- `Axis::pivot_claim`) bump and restore it inside their own transaction, so | ||
| 984 | + -- no other reader ever sees the raised value — and `migrate` runs to | ||
| 985 | + -- completion before the router is bound, one process per volume, so there is | ||
| 986 | + -- no concurrent writer to see it from anyway. | ||
| 987 | + -- | ||
| 988 | + -- Neither `= NONE`-guarded nor marked, for the board-stroke repair's reason: | ||
| 989 | + -- this is not an opinion the live system maintains, it is the course's row | ||
| 990 | + -- count, so recomputing converges instead of overwriting and the `!=` guard | ||
| 991 | + -- writes nothing on the next boot. It runs *before* the sweep, so the two | ||
| 992 | + -- own separate halves of the same invariant — this one clears the drift the | ||
| 993 | + -- volume arrived with, the sweep's own decrement leaves none behind — and it | ||
| 994 | + -- does not fight the `= NONE` seeding further down either: a pre-counter | ||
| 995 | + -- course with rows gets the same number here that the seeding would have | ||
| 996 | + -- given it, and one with no rows stays absent for the zero pass to fill. | ||
| 997 | + -- | ||
| 998 | + -- Per course rather than `GROUP BY course`, because the case that matters | ||
| 999 | + -- most forms no group: a course whose last enrollment the sweep took away has | ||
| 1000 | + -- zero rows and a counter above zero, and a grouped pass would never visit | ||
| 1001 | + -- it. `array::len` over ids rather than `count()`, because an aggregate over | ||
| 1002 | + -- an indexed field (`enrollment` is indexed on `course`) can come back as | ||
| 1003 | + -- `{ count: n }` from the server — a shape the in-memory engine never | ||
| 1004 | + -- produces and `option<int>` would refuse. | ||
| 1005 | + FOR $c IN ((SELECT VALUE id FROM course) ?? []) { | ||
| 1006 | + LET $live = array::len((SELECT VALUE id FROM enrollment WHERE course = $c)); | ||
| 1007 | + UPDATE $c SET enrollment_count = $live WHERE (enrollment_count ?? 0) != $live; | ||
| 1008 | + }; | ||
| 1009 | + | ||
| 967 | -- Promotion out of student now deletes the user's enrollments (2026-07-18); | 1010 | -- Promotion out of student now deletes the user's enrollments (2026-07-18); |
| 968 | -- this sweeps rows promoted before that fix. A deleted user reads as | 1011 | -- this sweeps rows promoted before that fix. A deleted user reads as |
| 969 | -- `user.role = NONE`, which is also != 'student' — those rows go too. | 1012 | -- `user.role = NONE`, which is also != 'student' — those rows go too. |
| 970 | - DELETE enrollment WHERE user.role != 'student'; | 1013 | + -- |
| 1014 | + -- The course's seat goes back with the row, per course, exactly the way | ||
| 1015 | + -- `User::set_role`'s own demotion sweep releases it: the counter is the | ||
| 1016 | + -- roster's authority, so a swept row that kept its seat denies a real | ||
| 1017 | + -- student a place *and* makes the course undeletable for good | ||
| 1018 | + -- (`Course::delete` refuses on `(enrollment_count ?? 0) = 0`). Several rows | ||
| 1019 | + -- of one course each take their own turn through the loop, so the counter | ||
| 1020 | + -- comes down by exactly as many rows as went. | ||
| 1021 | + LET $swept = (DELETE enrollment WHERE user.role != 'student' RETURN BEFORE); | ||
| 1022 | + FOR $row IN ($swept ?? []) { | ||
| 1023 | + UPDATE $row.course SET enrollment_count = | ||
| 1024 | + math::max([(enrollment_count ?? 0) - 1, 0]); | ||
| 1025 | + }; | ||
| 971 | 1026 | ||
| 972 | -- Choices gained stable ids (2026-07-24): `choices` held bare strings and | 1027 | -- Choices gained stable ids (2026-07-24): `choices` held bare strings and |
| 973 | -- `correct`/`selected`/`slot` held the option's *position*. The DDL above | 1028 | -- `correct`/`selected`/`slot` held the option's *position*. The DDL above |
| @@ -1292,7 +1347,13 @@ pub const BACKFILL: &str = " | |||
| 1292 | UPDATE $row.user SET homework_on_time_total = $row.n; | 1347 | UPDATE $row.user SET homework_on_time_total = $row.n; |
| 1293 | }; | 1348 | }; |
| 1294 | UPDATE homework_submission SET counted_on_time = submitted_at <= homework.due_at; | 1349 | UPDATE homework_submission SET counted_on_time = submitted_at <= homework.due_at; |
| 1295 | - FOR $row IN ((SELECT user, count() AS n FROM exam_attempt GROUP BY user) ?? []) { | 1350 | + -- `(seq ?? 1) = 1` is one row per (exam, student) pair, so this counts |
| 1351 | + -- *exams sat* — what the request path credits (a retake writes a higher | ||
| 1352 | + -- seq and moves nothing). Counting every row instead would seed a | ||
| 1353 | + -- pre-existing student above a new one for the same work, and above | ||
| 1354 | + -- what the live path would ever give them again. | ||
| 1355 | + FOR $row IN ((SELECT user, count() AS n FROM exam_attempt | ||
| 1356 | + WHERE (seq ?? 1) = 1 GROUP BY user) ?? []) { | ||
| 1296 | UPDATE $row.user SET exam_sat_total = $row.n; | 1357 | UPDATE $row.user SET exam_sat_total = $row.n; |
| 1297 | }; | 1358 | }; |
| 1298 | FOR $row IN ((SELECT user, count() AS sessions, | 1359 | FOR $row IN ((SELECT user, count() AS sessions, |
tests/persistence.rsmodified+134 -0
| @@ -506,6 +506,140 @@ async fn stale_staff_enrollments_are_swept_on_boot() { | |||
| 506 | ); | 506 | ); |
| 507 | } | 507 | } |
| 508 | 508 | ||
| 509 | +/// The seat goes back with the swept row. The sweep above deletes enrollments | ||
| 510 | +/// whose holder is no longer a student, and for every boot it ever ran it kept | ||
| 511 | +/// their seats: the course stayed as full as it was, so a real student was | ||
| 512 | +/// refused a place that nobody held. Two rows of the *same* course go here, | ||
| 513 | +/// because the counter has to come down once per row and not once per course. | ||
| 514 | +/// | ||
| 515 | +/// The repair pass runs *before* the sweep, so it cannot stand in for the | ||
| 516 | +/// decrement being asserted here: at that point both rows are still there and | ||
| 517 | +/// the counter already agrees with them. | ||
| 518 | +#[tokio::test] | ||
| 519 | +async fn a_swept_enrollment_gives_the_course_its_seat_back() { | ||
| 520 | + let (app, db) = common::app_and_db().await; | ||
| 521 | + let teacher = common::login_as(&app, &db, "teacher", "teacher").await; | ||
| 522 | + let res = send( | ||
| 523 | + &app, | ||
| 524 | + "POST", | ||
| 525 | + "/courses", | ||
| 526 | + Some(&teacher), | ||
| 527 | + Some(json!({ "title": "small", "description": "", "capacity": 2 })), | ||
| 528 | + ) | ||
| 529 | + .await; | ||
| 530 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 531 | + let course = common::id_of(&res.body); | ||
| 532 | + | ||
| 533 | + for name in ["veli", "ayse"] { | ||
| 534 | + let cookie = common::login(&app, name).await; | ||
| 535 | + let id = me_id(&app, &cookie).await; | ||
| 536 | + enroll(&app, &teacher, &course, &id).await; | ||
| 537 | + } | ||
| 538 | + // Aged the way a pre-2026-07-18 promotion left them: the role moved on the | ||
| 539 | + // row itself, so no endpoint ever released the seat. | ||
| 540 | + set_role(&db, "veli", "teacher").await; | ||
| 541 | + set_role(&db, "ayse", "manager").await; | ||
| 542 | + | ||
| 543 | + let app = reboot(&db).await; | ||
| 544 | + let mut counter = db | ||
| 545 | + .query("SELECT VALUE enrollment_count FROM course") | ||
| 546 | + .await | ||
| 547 | + .expect("read the counter") | ||
| 548 | + .check() | ||
| 549 | + .expect("read the counter"); | ||
| 550 | + assert_eq!( | ||
| 551 | + counter.take::<Vec<i64>>(0).expect("enrollment_count"), | ||
| 552 | + vec![0], | ||
| 553 | + "both swept rows must hand their seats back" | ||
| 554 | + ); | ||
| 555 | + | ||
| 556 | + // The user-visible half: the capacity is free again, so a real student gets | ||
| 557 | + // the place the phantom seats were denying them. | ||
| 558 | + let can = common::login(&app, "can").await; | ||
| 559 | + let can_id = me_id(&app, &can).await; | ||
| 560 | + let res = send( | ||
| 561 | + &app, | ||
| 562 | + "POST", | ||
| 563 | + &format!("/courses/{course}/enrollments"), | ||
| 564 | + Some(&teacher), | ||
| 565 | + Some(json!({ "user_id": can_id })), | ||
| 566 | + ) | ||
| 567 | + .await; | ||
| 568 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 569 | +} | ||
| 570 | + | ||
| 571 | +/// The repair for the drift past boots already left behind: a course whose | ||
| 572 | +/// `enrollment_count` outran its roster is recounted from the rows it actually | ||
| 573 | +/// has. The symptom that makes it worth a boot pass is the delete guard — | ||
| 574 | +/// `Course::delete` refuses on `(enrollment_count ?? 0) = 0`, so a course | ||
| 575 | +/// carrying a phantom seat can never be deleted by anyone — so that is what is | ||
| 576 | +/// asserted, not just the number. | ||
| 577 | +/// | ||
| 578 | +/// The second course pins the other direction: the repair recounts to the | ||
| 579 | +/// roster, never to zero, so a course with real students keeps its seats. | ||
| 580 | +#[tokio::test] | ||
| 581 | +async fn a_course_whose_count_outran_its_roster_is_repaired_and_deletable() { | ||
| 582 | + let (app, db) = common::app_and_db().await; | ||
| 583 | + let teacher = common::login_as(&app, &db, "teacher", "teacher").await; | ||
| 584 | + let stranded = create_course(&app, &teacher, "stranded").await; | ||
| 585 | + let live = create_course(&app, &teacher, "live").await; | ||
| 586 | + let student = common::login(&app, "veli").await; | ||
| 587 | + let student_id = me_id(&app, &student).await; | ||
| 588 | + enroll(&app, &teacher, &live, &student_id).await; | ||
| 589 | + | ||
| 590 | + // The state every past run of the sweep left: rows gone, seats not. | ||
| 591 | + db.query( | ||
| 592 | + "UPDATE type::record('course', $stranded) SET enrollment_count = 3; | ||
| 593 | + UPDATE type::record('course', $live) SET enrollment_count = 5;", | ||
| 594 | + ) | ||
| 595 | + .bind(("stranded", stranded.clone())) | ||
| 596 | + .bind(("live", live.clone())) | ||
| 597 | + .await | ||
| 598 | + .expect("age the counters") | ||
| 599 | + .check() | ||
| 600 | + .expect("age the counters"); | ||
| 601 | + | ||
| 602 | + let app = reboot(&db).await; | ||
| 603 | + let mut counters = db | ||
| 604 | + .query("SELECT VALUE enrollment_count FROM course ORDER BY id") | ||
| 605 | + .await | ||
| 606 | + .expect("read the counters") | ||
| 607 | + .check() | ||
| 608 | + .expect("read the counters"); | ||
| 609 | + let mut stored = counters.take::<Vec<i64>>(0).expect("enrollment_count"); | ||
| 610 | + stored.sort_unstable(); | ||
| 611 | + assert_eq!( | ||
| 612 | + stored, | ||
| 613 | + vec![0, 1], | ||
| 614 | + "each course is recounted from its own rows" | ||
| 615 | + ); | ||
| 616 | + | ||
| 617 | + let res = send( | ||
| 618 | + &app, | ||
| 619 | + "DELETE", | ||
| 620 | + &format!("/courses/{stranded}"), | ||
| 621 | + Some(&teacher), | ||
| 622 | + None, | ||
| 623 | + ) | ||
| 624 | + .await; | ||
| 625 | + assert_eq!( | ||
| 626 | + res.status, | ||
| 627 | + StatusCode::NO_CONTENT, | ||
| 628 | + "a repaired course is deletable again: {}", | ||
| 629 | + res.body | ||
| 630 | + ); | ||
| 631 | + // And the one with a real student on it is still guarded. | ||
| 632 | + let res = send( | ||
| 633 | + &app, | ||
| 634 | + "DELETE", | ||
| 635 | + &format!("/courses/{live}"), | ||
| 636 | + Some(&teacher), | ||
| 637 | + None, | ||
| 638 | + ) | ||
| 639 | + .await; | ||
| 640 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | ||
| 641 | +} | ||
| 642 | + | ||
| 509 | /// Exam rows written before the draft flag existed (2026-07-19) backfill to | 643 | /// Exam rows written before the draft flag existed (2026-07-19) backfill to |
| 510 | /// published (`draft = false`) on the next boot — an old volume's exams stay | 644 | /// published (`draft = false`) on the next boot — an old volume's exams stay |
| 511 | /// exactly as visible as they were. | 645 | /// exactly as visible as they were. |