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 `class_blueprint` table: the row keyed by its ladder rung, the |
| 2 | //! compare-and-set writes the edit and the delete go through, and the reads |
| 3 | //! the sweep and the status screen are built on. The workflows that sequence |
| 4 | //! these — and the row lock that serializes a delete against the attaches |
| 5 | //! made on the blueprint's behalf — live in [`crate::service::class_blueprint`]. |
| 6 | |
| 7 | use PgConnection; |
| 8 | |
| 9 | use HashMap; |
| 10 | |
| 11 | use crateCLASS_BLUEPRINT_TABLE; |
| 12 | use crate; |
| 13 | use crateclass_pump; |
| 14 | use cratePagedList; |
| 15 | use crate; |
| 16 | use crateClassGroupId; |
| 17 | use crateGradeLevel; |
| 18 | use crateCourseId; |
| 19 | use cratenext_uuid; |
| 20 | use crateUserId; |
| 21 | use crate; |
| 22 | |
| 23 | /// The stored columns of one `class_blueprint` row. A template's course list |
| 24 | /// is *not* one of them — it lives across the `blueprint_course` junction and |
| 25 | /// is joined on in a batch after the read ([`with_courses`]), because the |
| 26 | /// paged list here reads whole rows (`SELECT *`), a shape no aggregate column |
| 27 | /// can ride. |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | /// The stored course list of one blueprint, sorted by course. Every read goes |
| 51 | /// through this ordering, which is what makes the compare-and-set below a |
| 52 | /// plain vector comparison: both sides of it are junction reads. |
| 53 | async |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | /// Join the stored course lists onto a batch of blueprint rows, one junction |
| 71 | /// read for the lot. |
| 72 | async |
| 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 | /// One `class_course` row, projected down to the pair that answers "does this |
| 104 | /// section carry that course". `source` is deliberately not read: a link of any |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | /// Prove every course a template is about to name is still there, *inside* the |
| 112 | /// transaction that stores the list. |
| 113 | /// |
| 114 | /// The caller's own pre-flight read is not that proof: it is a read of |
| 115 | /// `course` in a request that then writes `class_blueprint`, and two |
| 116 | /// transactions that read and write crossed records interleave freely under |
| 117 | /// READ COMMITTED. `DELETE /courses/{id}` sweeps every template naming the |
| 118 | /// course as part of its cascade, so a delete landing between the read and the |
| 119 | /// write sweeps a row that is not there yet and the list keeps an id nothing |
| 120 | /// points at. A `FOR KEY SHARE` row lock per course is the proof: it is the |
| 121 | /// weakest strength a course `DELETE` cannot take, so the delete waits behind |
| 122 | /// this transaction and finds the list that names the course already stored — |
| 123 | /// and sweeps it. |
| 124 | /// |
| 125 | /// One lock per course, the list being at most `MAX_CLASS_COURSES` long — |
| 126 | /// the same pass-per-course the old in-transaction loop made. |
| 127 | async |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | /// The refusal a locked course read answers when the course is gone: the |
| 144 | /// same `400` the caller's pre-flight read raises, since it is the same fact |
| 145 | /// one instant later. |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | /// Write the blueprint's row — and its `blueprint_course` rows, in the same |
| 154 | /// transaction, under the same per-course proof ([`courses_alive`]). A second |
| 155 | /// blueprint for the same grade is a 409 the store itself decides — the grade |
| 156 | /// carries a UNIQUE constraint, so the duplicate is seen rather than raced |
| 157 | /// (`23505` on `class_blueprint_grade_key`, mapped right here: a duplicate is |
| 158 | /// a decision, never a retry). The surrogate `id` is minted here and never |
| 159 | /// read back: the ladder rung stays the only identity the API speaks. |
| 160 | pub async |
| 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 | pub async |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | /// Every blueprint, by ladder rung — the id *is* the rung, so this is the |
| 230 | /// only ordering that means anything here. |
| 231 | pub async |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | /// Write the course list only while the stored rows still answer to the list |
| 243 | /// this caller read: two managers editing the same grade cannot have one's |
| 244 | /// list silently pump the other's diff. `None` means the claim matched |
| 245 | /// nothing — the row is gone, or its list moved since the caller read it, and |
| 246 | /// only the caller can tell those apart (the workflow re-reads and answers a |
| 247 | /// `409` or a `404`). |
| 248 | /// |
| 249 | /// The write is a claim (`FOR UPDATE` on the row), a comparison (the stored |
| 250 | /// `blueprint_course` rows, read under that lock, against `held` — both |
| 251 | /// sorted the same way, so the comparison is a plain vector equality), then |
| 252 | /// the diff: rows `wanted` dropped are deleted, rows it added are inserted. |
| 253 | /// All of it rides one transaction, and the row lock it holds is the same |
| 254 | /// strength the blueprint delete takes, so a pump's `FOR KEY SHARE` claim on |
| 255 | /// this row waits out the whole write exactly as it waits out a delete. |
| 256 | /// |
| 257 | /// A template naming a course that is gone refuses inside the transaction |
| 258 | /// with the pre-flight `400` ([`courses_alive`]). |
| 259 | pub async |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 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 | /// Delete the row — but only while its stored list is still the one this |
| 317 | /// caller read; the edit race answers the caller, not a silent delete of |
| 318 | /// somebody else's additions. `false` means the claim matched nothing: the |
| 319 | /// row is gone, or its list moved since the caller read it, and only the |
| 320 | /// caller can tell those apart. |
| 321 | /// |
| 322 | /// The `DELETE` is also the whole of the serialization against the attaches |
| 323 | /// made on this blueprint's behalf: a sourced attach holds a `FOR KEY SHARE` |
| 324 | /// on this row across its transaction ([`class_pump::attach_course`]), so its |
| 325 | /// row lands before this delete's sweep runs or not at all — the invariant the |
| 326 | /// old process-wide write lease held, now on the row itself. |
| 327 | pub async |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | /// [`delete_if_unchanged`] inside the caller's transaction — the blueprint |
| 339 | /// delete rides the row lock its claim took, so the sourced links are swept |
| 340 | /// while the window the claim opened is still closed. The claim and the |
| 341 | /// comparison re-run here: in the delete workflow's own transaction the |
| 342 | /// [`held_courses_for_update`] claim has already settled both, so they are a |
| 343 | /// formality; the standalone path above gets its whole CAS from this one |
| 344 | /// body. |
| 345 | pub async |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | /// The delete's core: claim the row, compare the stored junction rows with |
| 354 | /// `held`, and take the junction rows before the blueprint row they |
| 355 | /// reference — the `NO ACTION` foreign key is checked the moment the final |
| 356 | /// `DELETE` runs. |
| 357 | async |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | /// The stored course list of one blueprint, read `FOR UPDATE` — the claim |
| 391 | /// the delete holds across its sweep, so a sourced attach (whose own |
| 392 | /// transaction takes `FOR KEY SHARE` on this row before inserting) either |
| 393 | /// commits before the claim and is swept, or waits past it and finds no |
| 394 | /// row. `None` when the blueprint is gone. |
| 395 | pub async |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | /// Every `class_course` row held by any of `classes`, projected down to the |
| 413 | /// pair — the read [`crate::service::class_blueprint::status`] diffs the |
| 414 | /// template against. |
| 415 | pub async |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | /// Drop a course that no longer exists out of a blueprint's list. |
| 433 | /// |
| 434 | /// The delete does this itself now — twice over: the course's own cascade |
| 435 | /// sweeps `blueprint_course` (the junction row is a foreign key now, so a |
| 436 | /// template physically cannot keep naming a course whose row is gone) in the |
| 437 | /// same transaction that takes the course's `class_course` links. This used |
| 438 | /// to be the *only* thing that could remove such an id, and that was the bug: |
| 439 | /// it fires only while walking a section, so a grade with no sections could |
| 440 | /// never reach it, and the repair every doc surface pointed at (`PATCH` the |
| 441 | /// list back as it stands) is a `400` for naming a course that does not |
| 442 | /// exist. A permanent dangling id, and no call that could clear it. |
| 443 | /// |
| 444 | /// What is left for this to do is the **window** the sweep cannot cover: a |
| 445 | /// pump walks a snapshot of the list it read, so a course deleted after |
| 446 | /// that read is still attempted, and the delete's sweep has already run |
| 447 | /// past a row it will not visit again. The skip is then reported once for |
| 448 | /// the run that found it (later classes in the same |
| 449 | /// [`crate::service::class_blueprint::pump`] skip it by its dead-course set |
| 450 | /// rather than re-attempting and re-pruning it), and this write is a no-op |
| 451 | /// against a list the cascade already trimmed. It also clears ids written |
| 452 | /// before that cascade existed. |
| 453 | /// |
| 454 | /// Not a compare-and-set, unlike |
| 455 | /// [`crate::service::class_blueprint::set_courses`]: "a course that does |
| 456 | /// not exist is not in this list" holds for every version of the list, so |
| 457 | /// there is nothing a concurrent edit could make this write wrong about, |
| 458 | /// and re-running it changes nothing. No sweep follows it either — the |
| 459 | /// attachments it would sweep are exactly the ones the course's own delete |
| 460 | /// already took. |
| 461 | pub async |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | /// A blueprint's attachments that `keep` does not justify: every |
| 479 | /// `class_course` row carrying its tag for a course outside that list. The |
| 480 | /// delete passes `&[]` and so takes all of them. |
| 481 | /// |
| 482 | /// Read off the *stored* list rather than a diff against a handle, which is |
| 483 | /// what makes a repeated call a repair rather than a no-op: whatever a |
| 484 | /// half-finished sweep left behind is exactly what the next one finds. |
| 485 | /// |
| 486 | /// `source` is the whole filter, so a row without the tag — a hand attach — |
| 487 | /// is never matched, and a class that acquired the same course by hand keeps |
| 488 | /// it. The rows are read rather than derived from the classes at this grade: |
| 489 | /// a class whose grade was edited after the pump still carries this |
| 490 | /// blueprint's attachments, and only the tag can find it. |
| 491 | /// |
| 492 | /// The stored tag is the blueprint's surrogate uuid, not the grade label the |
| 493 | /// API speaks — the query translates the label to it. |
| 494 | pub async |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | /// Detach the link rows [`sourced_links`] named and sweep the enrollments |
| 520 | /// they pumped. The sweep underneath is the pump's own, so a student a |
| 521 | /// second class still claims is re-tagged rather than unenrolled. |
| 522 | /// |
| 523 | /// Each detach re-asserts the tag: the link's identity is the (class, course) |
| 524 | /// pair and says nothing about provenance, so a row a *new* blueprint at the |
| 525 | /// same grade attached while this ran would otherwise be swept by the old |
| 526 | /// one's tail. |
| 527 | /// |
| 528 | /// One transaction per *link row*, which is the module note's "one |
| 529 | /// transaction per (class, course)" and the mirror of the pump's |
| 530 | /// best-effort rule. One statement per course would be shorter, but it puts |
| 531 | /// every section at the grade into a single unbounded transaction: its write |
| 532 | /// loop is one enrollment sweep per (class, member) with no ceiling over the |
| 533 | /// class count, and a failure anywhere in it rolls back the whole grade — |
| 534 | /// so one contended section keeps the other eleven attached to a course the |
| 535 | /// template no longer holds. Per row, a failure leaves the pairs already |
| 536 | /// detached detached, each with its counter released and its enrollments |
| 537 | /// swept, and the rest exactly as they were: a partial removal the next call |
| 538 | /// finishes. |
| 539 | /// |
| 540 | /// Answers the **blob keys** every detach's cascade removed — the |
| 541 | /// question/answer images and homework files of each swept instance subtree — |
| 542 | /// for the caller's route to unlink after the commits. Each detach commits its |
| 543 | /// own transaction, so the keys of the rows already swept come back even when |
| 544 | /// a later link refuses ([`crate::db::class_pump::detach_course`] answers |
| 545 | /// `None` for a pair that held no instance, which contributes nothing); a |
| 546 | /// failure before a link's detach returns the Err and drops the keys gathered |
| 547 | /// so far, which is the same accepted window |
| 548 | /// [`crate::service::course::delete`] documents: at worst an unreachable blob. |
| 549 | pub async |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 |