hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The pump behind the class layer: one link row changed, and every |
| 2 | //! `enrollment` row that link implies reconciled with it, in one transaction. |
| 3 | //! |
| 4 | //! A class section has two link tables — `class_member` (a student in |
| 5 | //! it) and `class_course` (a course attached to it, as that class×course |
| 6 | //! *instance*) — and the *product* of the two is the roster it owes: every |
| 7 | //! live member is enrolled in every instance the class carries, in real |
| 8 | //! `enrollment` rows tagged [`source`](crate::domain::enrollment) with the |
| 9 | //! class that wrote them. Adding a member and attaching a course are |
| 10 | //! therefore the same operation seen along its two axes, and so are ending |
| 11 | //! one and detaching the other. |
| 12 | //! |
| 13 | //! What is *not* one primitive is attach and detach. They share no statement: |
| 14 | //! one gates on "this pair already holds a row" and claims a counter upwards, |
| 15 | //! the other takes the instance and everything the class taught under it, and |
| 16 | //! the member exits release the enrollment rows the ended stint owned — one |
| 17 | //! row per *instance*, so a section's exit never touches the seat the student |
| 18 | //! holds in another section. |
| 19 | //! |
| 20 | //! Every invariant is one Postgres statement or one row lock, in the shapes |
| 21 | //! [`crate::db::cap`] documents: the class counter is claimed by a conditional |
| 22 | //! `UPDATE` fused with the link's `INSERT` (one CTE — a refused insert takes |
| 23 | //! its own bump back), a duplicate live stint is the partial unique index |
| 24 | //! `class_member_live_pair` answering as `23505` (and enrollment's natural |
| 25 | //! `(class_course, app_user)` key doing the same job through `ON CONFLICT`), |
| 26 | //! a gone parent is a real `FOREIGN KEY` answering as `23503`, and the row |
| 27 | //! locks (`FOR KEY SHARE` / `FOR NO KEY UPDATE` / `FOR UPDATE`) put each |
| 28 | //! transaction on the very record a racing writer must touch. A refusal is an |
| 29 | //! early `Err` (or an `Ok` verdict) from the |
| 30 | //! [`crate::database::tx_with_retry`] closure — a decision, never a retry. |
| 31 | |
| 32 | use PgConnection; |
| 33 | |
| 34 | use crate; |
| 35 | use crate; |
| 36 | use crateClassBlueprintId; |
| 37 | use crate; |
| 38 | use crateClassGroupId; |
| 39 | use crate; |
| 40 | use crateCourseId; |
| 41 | use crateCourseKind; |
| 42 | use crateGradeLevel; |
| 43 | use crateRole; |
| 44 | use crateTimestamp; |
| 45 | use crateUserId; |
| 46 | use crate; |
| 47 | |
| 48 | /// What [`attach_course`]'s attach settled. The caller answers each verdict — |
| 49 | /// a hand attach turns them into this route's errors; a blueprint pump reads |
| 50 | /// them and carries on ([`crate::domain::class_blueprint`]). |
| 51 | |
| 52 | pub |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | /// Which way the pump runs: the loop below needs a `(course, user)` pair per |
| 140 | /// enrollment, and each caller supplies one side as a constant and reads the |
| 141 | /// other off the class's *other* link table. |
| 142 | |
| 143 | pub |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | /// The pivot of an attach: the row the link hangs off on its own axis, which |
| 209 | /// must still be there — and, on the member axis, still name a `student` — |
| 210 | /// when the transaction runs. |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | /// The verdicts the shared prechecks can settle before any counter moves. |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | /// The gates that run before any counter moves, in the order their answers |
| 234 | /// outrank each other: |
| 235 | /// |
| 236 | /// 1. **Duplicate** — "you are already in" outranks every other answer: a |
| 237 | /// caller whose row a rival placed a moment ago must be told they are in, |
| 238 | /// not refused for a class ceiling they are not near. The member axis reads |
| 239 | /// a **live** stint (a left one claims nothing, so rejoining is not a |
| 240 | /// duplicate); the course axis reads the (class, course) pair. (The link |
| 241 | /// tables re-answer this below — `23505` on the live-stint partial index, |
| 242 | /// `ON CONFLICT DO NOTHING` on the enrollment pair — for a rival landing |
| 243 | /// inside the window: same verdict, no second count.) |
| 244 | /// 2. **Source** — the blueprint that asked for this attach is still there. A |
| 245 | /// `FOR KEY SHARE` row lock on the blueprint row: it serializes the attach |
| 246 | /// against the blueprint's own delete — the closure the old process-wide |
| 247 | /// lease provided, now on the row itself. A concurrent blueprint `DELETE` |
| 248 | /// waits behind it and its sweep finds the committed row, while an attach |
| 249 | /// starting after the delete finds no row and writes nothing. `KEY SHARE` |
| 250 | /// is the weakest strength that blocks the delete, so two sourced attaches |
| 251 | /// still run concurrently, exactly as they did under the old shared read |
| 252 | /// lease. |
| 253 | /// |
| 254 | /// The same locked read resolves the surrogate `id` the link row will |
| 255 | /// store: the stored tag is that uuid, not the grade label the API |
| 256 | /// speaks, and it rides out of this gate to the insert below. |
| 257 | /// 3. **Pivot** — the member axis takes the [`cap` role-claim |
| 258 | /// recipe](crate::db::cap)'s `FOR NO KEY UPDATE` handshake on the user row: |
| 259 | /// a role change away from `student` sweeps this membership, and demotion |
| 260 | /// and join now serialize on that row lock in both directions. The course |
| 261 | /// axis takes a `FOR KEY SHARE` on the course row, which a concurrent |
| 262 | /// course delete cannot take — the old bump-and-restore existence proof, |
| 263 | /// now a plain lock. |
| 264 | /// 4. **Overloaded** — the *other* axis standing above its own ceiling, which |
| 265 | /// no claim on this axis can see, and which is what bounds this |
| 266 | /// transaction's write loop. |
| 267 | async |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | /// Write the member stint and enroll the student into every instance the |
| 368 | /// class already carries, or write nothing at all. |
| 369 | /// |
| 370 | /// A student already enrolled in one of those instances keeps the row they |
| 371 | /// have — no count charged, and the existing row's `source` is left exactly |
| 372 | /// as it was, so a hand-placed student is never quietly adopted by a class. |
| 373 | /// |
| 374 | /// The class counter is claimed by the conditional half of the CTE below |
| 375 | /// rather than a bare increment, so a class deleted out from under this run |
| 376 | /// matches nothing and the whole cascade aborts: [`Attached::Gone`] instead of |
| 377 | /// a counter on a row that no longer exists. The same write carries |
| 378 | /// [`Axis::Member`]'s cap, which is what keeps the pair loop — and therefore |
| 379 | /// this transaction — finite: [`Attached::ClassFull`] once the class is at its |
| 380 | /// ceiling. Claim and insert are one statement, so a refused insert takes its |
| 381 | /// own seat bump back. |
| 382 | /// |
| 383 | /// The stint is live (`left_at` NULL) by construction, which is the row the |
| 384 | /// partial unique index `class_member_live_pair` guards: a rival landing in |
| 385 | /// the window is answered as [`Attached::Duplicate`] rather than a second |
| 386 | /// live stint for the same pair. A student who left earlier keeps their |
| 387 | /// history row — it claims nothing. |
| 388 | pub async |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | /// [`add_member`] carrying the stint's *provenance*: the section the student was |
| 398 | /// copied from, written into `source_class_group` in the same statement as |
| 399 | /// the row. `None` is the hand add — a student someone put here, who came |
| 400 | /// from nowhere (the pump is the only writer of this column, and a rollover |
| 401 | /// is the only caller that has a source to name). |
| 402 | /// |
| 403 | /// Everything else — the claim, the cap, the duplicate verdict, the pair loop |
| 404 | /// — is identical, and deliberately so: the tag rides the member axis's own |
| 405 | /// single statement rather than a follow-up write, so a crash cannot leave a |
| 406 | /// copied stint that lost its provenance. |
| 407 | pub async |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | /// Write the instance and enroll the class's whole live roster into it, or |
| 503 | /// write nothing at all. |
| 504 | /// |
| 505 | /// Students already enrolled in it keep the rows they have — no count charged, |
| 506 | /// `source` untouched. There is no roster-size gate any more (D5: the |
| 507 | /// counter is a count, not a capacity), so the only refusals left are the |
| 508 | /// class's own ceilings, a vanished pivot, and a course kind that is not |
| 509 | /// class-delivered (a 400 straight out of the transaction). |
| 510 | /// |
| 511 | /// `source` is the blueprint whose behalf this attach runs on, and supplying |
| 512 | /// it adds one more claim: that it is still there when the transaction runs |
| 513 | /// ([`Attached::SourceGone`]) — the `FOR KEY SHARE` lock that serializes this |
| 514 | /// transaction against the blueprint's own delete. A hand attach owns itself |
| 515 | /// and passes `None`. |
| 516 | /// |
| 517 | /// The instance's own id is minted here (v7, so a class's instances list in |
| 518 | /// creation order) and its content columns are born `NULL` — the *inherit* |
| 519 | /// state: what the section teaches is whatever its grade-level |
| 520 | /// [`crate::domain::course_offering::CourseOffering`] says, and through it |
| 521 | /// the catalog course. That template is resolved (auto-created empty, with |
| 522 | /// `created_by` naming the attaching actor, when the (course, grade_level) |
| 523 | /// pair has none yet) in this same transaction — the class row is read under |
| 524 | /// `FOR KEY SHARE`, so the offering the insert names is the one for the |
| 525 | /// grade the class carries *at the attach*, not at the caller's read. A |
| 526 | /// PATCH on `/instances` overrides the content afterwards. |
| 527 | /// |
| 528 | /// Only a **class-delivered** course |
| 529 | /// ([`CourseKind::is_class_delivered`], `kind = 'course'`) becomes an |
| 530 | /// instance: a club or an etüt is joined individually and is refused with a |
| 531 | /// 400, whatever axis asked for the attach. |
| 532 | /// |
| 533 | /// Two counters ride the write, both in this one transaction: the class's |
| 534 | /// `class_course_count` (claimed by the conditional CTE, so a full or gone |
| 535 | /// class writes nothing) and the *catalog's* `class_course_count` — how many |
| 536 | /// sections teach this course, which is what [`crate::db::course::delete`]'s |
| 537 | /// guard reads. This is the only insert path into `class_course`; every layer |
| 538 | /// above it (hand attach, blueprint pump, rollover copy) reaches the counter |
| 539 | /// through here. |
| 540 | pub async |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | /// Abort markers the pair loop raises where the old transaction `THROW` did, |
| 705 | /// in the [`crate::db::field_update`] style: a refusal found *after* earlier |
| 706 | /// pairs wrote must take the whole run down with it — returning it as `Ok` |
| 707 | /// would COMMIT the partial enrollment. The marker carries the instance's id |
| 708 | /// and is mapped back to [`Attached`] right after [`tx_with_retry`] returns |
| 709 | /// ([`map_sweep_abort`]); it never reaches the wire. |
| 710 | const COURSE_GONE_MARK: &str = "class_pump_course_gone:"; |
| 711 | |
| 712 | /// The pair loop's abort markers back into their [`Attached`] refusals — |
| 713 | /// every other outcome passes through untouched. |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | /// Enroll every `(instance, user)` pair that has no row yet, and charge the |
| 724 | /// instance's roster counter only for the rows actually written. |
| 725 | /// |
| 726 | /// Per pair, in the order the answers outrank each other: the pair's own row |
| 727 | /// answers "already enrolled" (skip, no count moved); the counter claim is a |
| 728 | /// plain increment on the instance row, so it doubles as the existence check — |
| 729 | /// a claim matching nothing is an instance a concurrent detach took, which is |
| 730 | /// the abort marker rather than a refusal of this pair alone; and then the |
| 731 | /// row is inserted with `ON CONFLICT DO NOTHING`, so a rival landing between |
| 732 | /// the gate and the insert leaves *their* row standing and this run gives its |
| 733 | /// own claim back before the skip. |
| 734 | /// |
| 735 | /// Every abort stops the loop inside the caller's transaction: not one of the |
| 736 | /// earlier rows in the same run survives, which is the whole point of doing |
| 737 | /// this in a transaction. |
| 738 | async |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | // The sweep a role change *off` `student` owes — every class membership (each |
| 808 | // class getting its member count back) and every enrollment row (each course |
| 809 | // getting its seat back) — is two of the arms of |
| 810 | // [`crate::service::user::set_role`], because it belongs in the same |
| 811 | // transaction as the role write that invalidates them. It stays one fact with |
| 812 | // the counters either way: released separately, a failure between the halves |
| 813 | // left enrollment rows tagged `source = class_group:X` while the class had its |
| 814 | // counters back, so the class passed its zero-zero delete guard and the rows |
| 815 | // were left pointing at a class no sweep could ever reach again. |
| 816 | |
| 817 | /// Take a student out of a class and sweep the enrollments the class pumped |
| 818 | /// for them. Answers how many stints went (0 or 1), so the caller can turn |
| 819 | /// zero into a 404. |
| 820 | /// |
| 821 | /// This is the hard form — the row is deleted — and it is kept only for the |
| 822 | /// transfer rollback path, where the stint being undone must leave no trace. |
| 823 | /// The route uses [`leave_member`], which stamps `left_at` and keeps the |
| 824 | /// history. |
| 825 | pub async |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | /// The same exit, soft: `left_at` is stamped and the row stays as history. |
| 834 | /// The roster read and the cap both filter on `left_at IS NULL`, so the |
| 835 | /// student holds no seat from this moment — and a rejoin later inserts a |
| 836 | /// fresh live row beside this one (the partial unique index only guards the |
| 837 | /// live stint), which is how "left in October, came back in January" is |
| 838 | /// represented. |
| 839 | /// |
| 840 | /// Both exits take the user row `FOR NO KEY UPDATE` first — the same lock the |
| 841 | /// pump's pivot claim and a role-change sweep take — so a demotion cannot |
| 842 | /// interleave with this write in either direction. |
| 843 | pub async |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | /// The one body both exits run: find the live stint, end it, give the class |
| 852 | /// its seat back, and release the enrollments the class pumped for the |
| 853 | /// student. |
| 854 | /// |
| 855 | /// The sweep's rule, and it is the whole rule: **every row the ended stint |
| 856 | /// owned goes, and each instance gets its roster count back**. An enrollment |
| 857 | /// is keyed `(class_course, app_user)` — one row per *instance* — so a student |
| 858 | /// in two sections that teach the same course holds two rows, one on each |
| 859 | /// section's instance, and the seat they keep is the other section's own row, |
| 860 | /// written when they joined it. Handing this row over to a rival class (the |
| 861 | /// pre-remodel rule, when one school-wide course carried one roster) would |
| 862 | /// leave the leaving section's roster, exams and roll-call still listing a |
| 863 | /// student who left it, with its `enrollment_count` inflated on top — a |
| 864 | /// "repair" that repairs nothing, because there was never a shortage of rows |
| 865 | /// to repair. |
| 866 | /// |
| 867 | /// Only the rows *this class* wrote are in the list ([`drop_member`] reads |
| 868 | /// `source = <class>`), so a hand-placed enrollment — the operator's own, or |
| 869 | /// one a section handed over before this rule — is never touched. |
| 870 | /// |
| 871 | /// Sweeps tolerate rows that are already gone: an instance detach wipes its |
| 872 | /// own enrollments wholesale while the `class_member` rows survive it, so |
| 873 | /// "this class has a member" and "that member has a live pumped row" are |
| 874 | /// independent facts and the loop simply finds nothing to sweep (and, because |
| 875 | /// [`release`] only gives a count back for a row it actually deleted, takes |
| 876 | /// nothing back twice). |
| 877 | async |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | /// Detach one instance from its class and sweep what hangs off it. `source`, |
| 956 | /// when given, re-asserts the provenance tag on the link's own delete — a |
| 957 | /// blueprint sweep may only take back rows its own tag owns. Answers the blob |
| 958 | /// keys of the image and homework-file rows the sweep removed, or `None` when |
| 959 | /// the pair held no instance at all (nothing written), so a caller whose link |
| 960 | /// was a single pair can turn `None` into a 404 while a detach that removed an |
| 961 | /// instance carrying no uploads still reads as one that went. |
| 962 | /// |
| 963 | /// The instance is the anchor now, so detaching it takes everything the class |
| 964 | /// taught under it: its exams (with their results and questions), its sessions |
| 965 | /// with their roll call, its homework with its submissions, its roster and its |
| 966 | /// teacher links. That is the same cascade a course delete runs |
| 967 | /// ([`crate::db::course::sweep_instance_subtree`]), scoped to one instance — |
| 968 | /// and it has to be, because every one of those rows names the instance as its |
| 969 | /// parent key (`ON DELETE NO ACTION`), so deleting the instance while they |
| 970 | /// stand is refused by the store itself. |
| 971 | /// |
| 972 | /// The sweep runs behind a `FOR UPDATE` claim on the instance row: a rival |
| 973 | /// detach waits here and finds nothing left to sweep, so exactly one |
| 974 | /// transaction unlinks the pair and gives the class its counter back — and |
| 975 | /// with it the catalog's `class_course_count`, the count |
| 976 | /// [`crate::db::course::delete`]'s guard reads (`course::delete` needs no |
| 977 | /// release of its own: it deletes the course row the counter lives on). |
| 978 | /// This is the only delete path for an instance that does *not* remove the |
| 979 | /// course: the hand detach and the blueprint sweep both come through it. |
| 980 | /// |
| 981 | /// The image and homework-file *blobs* the cascade removes come back as keys |
| 982 | /// and are unlinked by the caller, after the commit — the same shape |
| 983 | /// [`crate::service::course::delete`] has. The rows are gone the moment this |
| 984 | /// returns, so a caller that drops the list strands the bytes on disk with |
| 985 | /// nothing left pointing at them. |
| 986 | pub async |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | /// The row goes, and the instance's roster count with it. It is the *only* |
| 1057 | /// fate for an enrollment a section's exit owns: the roster is per instance |
| 1058 | /// (D4), so a student who is also in another section teaching the same course |
| 1059 | /// keeps their seat there through that section's own row — see |
| 1060 | /// [`drop_member`]. |
| 1061 | /// |
| 1062 | /// The count is only given back when the delete actually took the row — an |
| 1063 | /// instance detach's wholesale sweep may have taken both while this |
| 1064 | /// transaction read them, and both halves vanish together there. |
| 1065 | async |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 |