From fa1529a905be335e22ed09e281ad5f45df9450a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 14 Sep 2026 18:01:42 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20junction-era=20test=20pin?= =?UTF-8?q?s=20and=20homework=20audience=20recheck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit class_course.source and board_participant are uuids, not grade text / participants[]. Submission upsert locks homework then re-reads assignment, so a narrowing PATCH cannot land a 201 for a dropped student. Local: cargo nextest run --no-fail-fast → 1373 passed. --- ...b32451c0d228cc05490df6c64e245f453a6d7.json | 24 ++++++ ...a50688cc8fafd1d56ffba546dd2360eb01d3.json} | 5 +- src/db/homework_submission.rs | 37 +++++---- tests/regress_blueprints.rs | 83 +++++++++---------- tests/regress_classes.rs | 2 +- tests/regress_roles.rs | 66 ++++++++++++--- 6 files changed, 143 insertions(+), 74 deletions(-) create mode 100644 .sqlx/query-3490bf0f42b3e2c64443add6058b32451c0d228cc05490df6c64e245f453a6d7.json rename .sqlx/{query-c3232a6de8535065f4fc7e1a19494dd17acb47f65cca634b739f9ff6cd0a6894.json => query-9bb1a80e986bf6126b5d34d03027a50688cc8fafd1d56ffba546dd2360eb01d3.json} (51%) diff --git a/.sqlx/query-3490bf0f42b3e2c64443add6058b32451c0d228cc05490df6c64e245f453a6d7.json b/.sqlx/query-3490bf0f42b3e2c64443add6058b32451c0d228cc05490df6c64e245f453a6d7.json new file mode 100644 index 0000000..3da2627 --- /dev/null +++ b/.sqlx/query-3490bf0f42b3e2c64443add6058b32451c0d228cc05490df6c64e245f453a6d7.json @@ -0,0 +1,24 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT (NOT EXISTS (\n SELECT 1 FROM homework_assignment a WHERE a.homework = $1)\n OR EXISTS (\n SELECT 1 FROM homework_assignment a\n WHERE a.homework = $1 AND a.student = $2)) AS \"named!\"", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "named!", + "type_info": "Bool", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Uuid", + "Uuid" + ] + }, + "nullable": [ + null + ] + }, + "hash": "3490bf0f42b3e2c64443add6058b32451c0d228cc05490df6c64e245f453a6d7" +} diff --git a/.sqlx/query-c3232a6de8535065f4fc7e1a19494dd17acb47f65cca634b739f9ff6cd0a6894.json b/.sqlx/query-9bb1a80e986bf6126b5d34d03027a50688cc8fafd1d56ffba546dd2360eb01d3.json similarity index 51% rename from .sqlx/query-c3232a6de8535065f4fc7e1a19494dd17acb47f65cca634b739f9ff6cd0a6894.json rename to .sqlx/query-9bb1a80e986bf6126b5d34d03027a50688cc8fafd1d56ffba546dd2360eb01d3.json index 6537407..2dcdbea 100644 --- a/.sqlx/query-c3232a6de8535065f4fc7e1a19494dd17acb47f65cca634b739f9ff6cd0a6894.json +++ b/.sqlx/query-9bb1a80e986bf6126b5d34d03027a50688cc8fafd1d56ffba546dd2360eb01d3.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT due_at AS \"due_at: Timestamp\" FROM homework\n WHERE id = $1\n AND (NOT EXISTS (SELECT 1 FROM homework_assignment a\n WHERE a.homework = homework.id)\n OR EXISTS (SELECT 1 FROM homework_assignment a\n WHERE a.homework = homework.id AND a.student = $2))\n FOR UPDATE", + "query": "SELECT due_at AS \"due_at: Timestamp\" FROM homework\n WHERE id = $1 FOR UPDATE", "describe": { "columns": [ { @@ -17,7 +17,6 @@ ], "parameters": { "Left": [ - "Uuid", "Uuid" ] }, @@ -25,5 +24,5 @@ false ] }, - "hash": "c3232a6de8535065f4fc7e1a19494dd17acb47f65cca634b739f9ff6cd0a6894" + "hash": "9bb1a80e986bf6126b5d34d03027a50688cc8fafd1d56ffba546dd2360eb01d3" } diff --git a/src/db/homework_submission.rs b/src/db/homework_submission.rs index 77801a5..a81ee6b 100644 --- a/src/db/homework_submission.rs +++ b/src/db/homework_submission.rs @@ -73,25 +73,32 @@ pub async fn upsert( // submission goes through its homework). `Err(NotFound)` means the // homework is gone, which is the 404 the web layer's own lookup // would have answered. - let was_due = sqlx::query!( - // The audience re-checks UNDER the lock: a narrowing PATCH commits - // between the web layer's pre-flight read and this write, and a - // student the homework no longer names must read as gone — the - // same 404 their own lookup answers. + let due = sqlx::query!( r#"SELECT due_at AS "due_at: Timestamp" FROM homework - WHERE id = $1 - AND (NOT EXISTS (SELECT 1 FROM homework_assignment a - WHERE a.homework = homework.id) - OR EXISTS (SELECT 1 FROM homework_assignment a - WHERE a.homework = homework.id AND a.student = $2)) - FOR UPDATE"#, + WHERE id = $1 FOR UPDATE"#, homework_id.uuid(), - user.uuid() ) .fetch_optional(&mut *tx) - .await? - .map(|row| row.due_at); - let Some(was_due) = was_due else { + .await?; + let Some(due) = due else { + return Err(AppError::NotFound); + }; + let was_due = due.due_at; + // Re-read the audience after the lock: a narrowing PATCH that held + // this row may have committed while we waited, and SELECT FOR UPDATE + // does not re-evaluate a subquery on another table. + let named: bool = sqlx::query_scalar!( + r#"SELECT (NOT EXISTS ( + SELECT 1 FROM homework_assignment a WHERE a.homework = $1) + OR EXISTS ( + SELECT 1 FROM homework_assignment a + WHERE a.homework = $1 AND a.student = $2)) AS "named!""#, + homework_id.uuid(), + user.uuid(), + ) + .fetch_one(&mut *tx) + .await?; + if !named { return Err(AppError::NotFound); }; // Equal counts as on time, exactly as the deadline has always been diff --git a/tests/regress_blueprints.rs b/tests/regress_blueprints.rs index b282dc2..caefbee 100644 --- a/tests/regress_blueprints.rs +++ b/tests/regress_blueprints.rs @@ -57,11 +57,11 @@ async fn attached(class: &str, course: &str, db: &Database) -> bool { == 1 } -/// The blueprint key stored on one attachment, or `None` when the row carries +/// The blueprint id stored on one attachment, or `None` when the row carries /// no such key at all — which is the whole provenance rule: absent means a /// human attached it. -async fn source_of(class: &str, course: &str, db: &Database) -> Option { - sqlx::query_scalar::<_, Option>( +async fn source_of(class: &str, course: &str, db: &Database) -> Option { + sqlx::query_scalar::<_, Option>( "SELECT source FROM class_course WHERE class = $1 AND course = $2", ) .bind(uuid::Uuid::parse_str(class).expect("a uuid class id")) @@ -71,6 +71,14 @@ async fn source_of(class: &str, course: &str, db: &Database) -> Option { .unwrap() } +async fn blueprint_id(grade: &str, db: &Database) -> uuid::Uuid { + sqlx::query_scalar::<_, uuid::Uuid>("SELECT id FROM class_blueprint WHERE grade = $1") + .bind(grade) + .fetch_one(db) + .await + .unwrap() +} + /// Does that grade's template still name that course, in the store? async fn templated(grade: &str, course: &str, db: &Database) -> bool { sqlx::query_scalar::<_, i64>( @@ -306,7 +314,7 @@ async fn a_fresh_class_takes_its_grades_blueprint() { assert!(attached(&class, course, &db).await); assert_eq!( source_of(&class, course, &db).await, - Some("9".to_string()), + Some(blueprint_id("9", &db).await), "the blueprint must own what it attached" ); assert_eq!( @@ -323,8 +331,7 @@ async fn a_fresh_class_takes_its_grades_blueprint() { ); } assert_eq!( - count_on("class_course_count", "class_group", &class, &db) - .await, + count_on("class_course_count", "class_group", &class, &db).await, 2 ); @@ -400,13 +407,12 @@ async fn creating_a_class_stocks_it_from_its_grades_blueprint() { assert!(attached(&class, course, &db).await, "{course} is attached"); assert_eq!( source_of(&class, course, &db).await, - Some("9".to_string()), + Some(blueprint_id("9", &db).await), "the blueprint must own what a create attached, exactly as a pump does" ); } assert_eq!( - count_on("class_course_count", "class_group", &class, &db) - .await, + count_on("class_course_count", "class_group", &class, &db).await, 2 ); @@ -445,13 +451,11 @@ async fn creating_a_class_stocks_it_from_its_grades_blueprint() { assert!(skips(&res).is_empty(), "{:?}", res.body); let bare = res.body["class"]["id"].as_str().unwrap(); assert_eq!( - sqlx::query_scalar::<_, i64>( - "SELECT count(*) FROM class_course WHERE class = $1", - ) - .bind(uuid::Uuid::parse_str(bare).expect("a uuid class id")) - .fetch_one(&db) - .await - .unwrap(), + sqlx::query_scalar::<_, i64>("SELECT count(*) FROM class_course WHERE class = $1",) + .bind(uuid::Uuid::parse_str(bare).expect("a uuid class id")) + .fetch_one(&db) + .await + .unwrap(), 0, "nothing may be attached to a section no template reached" ); @@ -608,19 +612,16 @@ async fn an_edit_retro_pumps_every_class_at_the_grade() { // grade. for class in [&a, &b] { assert_eq!( - count_on("class_course_count", "class_group", class, &db) - .await, + count_on("class_course_count", "class_group", class, &db).await, 1 ); } assert_eq!( - count_on("class_course_count", "class_group", &ten, &db) - .await, + count_on("class_course_count", "class_group", &ten, &db).await, 0 ); assert_eq!( - count_on("enrollment_count", "course", &algebra, &db) - .await, + count_on("enrollment_count", "course", &algebra, &db).await, 1, "one student at the grade, one seat" ); @@ -681,13 +682,11 @@ async fn a_class_that_does_not_fit_is_reported_not_aborted() { // Nothing moved on the skipped class: not its attachment counter, and not // one of the seats the refused attach touched on its way to the refusal. assert_eq!( - count_on("class_course_count", "class_group", &full, &db) - .await, + count_on("class_course_count", "class_group", &full, &db).await, 0 ); assert_eq!( - count_on("enrollment_count", "course", &tight, &db) - .await, + count_on("enrollment_count", "course", &tight, &db).await, 0, "the empty class took no seat, and the refused one gave every seat back" ); @@ -814,7 +813,10 @@ async fn a_removal_spares_a_hand_attached_course() { None, "a pump may not adopt an attachment it did not make" ); - assert_eq!(source_of(&pumped, &algebra, &db).await, Some("9".into())); + assert_eq!( + source_of(&pumped, &algebra, &db).await, + Some(blueprint_id("9", &db).await), + ); // Drop it from the template. let patched = send( @@ -836,14 +838,12 @@ async fn a_removal_spares_a_hand_attached_course() { "…and nothing else — a hand attach survives" ); assert_eq!( - count_on("class_course_count", "class_group", &pumped, &db) - .await, + count_on("class_course_count", "class_group", &pumped, &db).await, 0, "the detach gives the class its count back" ); assert_eq!( - count_on("class_course_count", "class_group", &byhand, &db) - .await, + count_on("class_course_count", "class_group", &byhand, &db).await, 1 ); assert_eq!( @@ -856,8 +856,7 @@ async fn a_removal_spares_a_hand_attached_course() { "the enrollments the blueprint pumped go with it" ); assert_eq!( - count_on("enrollment_count", "course", &algebra, &db) - .await, + count_on("enrollment_count", "course", &algebra, &db).await, 1, "the hand-attached class keeps its student's seat" ); @@ -939,8 +938,7 @@ async fn a_row_written_before_the_column_reads_as_hand_attached() { "a pre-migration row is hand-attached and unreachable by any blueprint sweep" ); assert_eq!( - count_on("enrollment_count", "course", &algebra, &db) - .await, + count_on("enrollment_count", "course", &algebra, &db).await, 1 ); } @@ -996,8 +994,10 @@ async fn a_blueprint_lost_mid_attach_strands_a_row_that_stays_detachable() { // The stranded state: the link row committed, the blueprint it names did // not survive. Real FKs refuse that state, so it is written with FK // triggers suspended — the manager id is real, the `source` FK is the one - // the strand consists of. + // the strand consists of. Capture the uuid before the delete: the grade + // label is UNIQUE, not the FK. let manager_id = UserId::from_key(&me_id(&app, &manager).await); + let stranded = blueprint_id("9", &db).await; let mut tx = db.begin().await.unwrap(); sqlx::query("SET LOCAL session_replication_role = replica") .execute(&mut *tx) @@ -1009,12 +1009,13 @@ async fn a_blueprint_lost_mid_attach_strands_a_row_that_stays_detachable() { .unwrap(); sqlx::query( "INSERT INTO class_course (class, course, attached_by, attached_at, source) \ - VALUES ($1, $2, $3, $4, '9')", + VALUES ($1, $2, $3, $4, $5)", ) .bind(uuid::Uuid::parse_str(&class).expect("a uuid class id")) .bind(CourseId::from_key(&algebra)) .bind(manager_id) .bind(Timestamp::now().as_millis()) + .bind(stranded) .execute(&mut *tx) .await .unwrap(); @@ -1035,7 +1036,7 @@ async fn a_blueprint_lost_mid_attach_strands_a_row_that_stays_detachable() { "the link stands after its blueprint was gone: this is the stranded \ row, tagged with a record no sweep can ever reach" ); - assert_eq!(source_of(&class, &algebra, &db).await, Some("9".into())); + assert_eq!(source_of(&class, &algebra, &db).await, Some(stranded)); // The documented recovery: a human detaches it one course at a time, and // the counters come back exact. @@ -1055,8 +1056,7 @@ async fn a_blueprint_lost_mid_attach_strands_a_row_that_stays_detachable() { ); assert!(!attached(&class, &algebra, &db).await); assert_eq!( - count_on("class_course_count", "class_group", &class, &db) - .await, + count_on("class_course_count", "class_group", &class, &db).await, 0, "a stranded row still releases its counter when it is detached" ); @@ -1479,8 +1479,7 @@ async fn a_half_swept_removal_is_finished_by_the_documented_re_patch() { "…enrollments and all" ); assert_eq!( - count_on("enrollment_count", "course", &history, &db) - .await, + count_on("enrollment_count", "course", &history, &db).await, 0, "…with the seat given back" ); diff --git a/tests/regress_classes.rs b/tests/regress_classes.rs index b3a52b5..b177b9a 100644 --- a/tests/regress_classes.rs +++ b/tests/regress_classes.rs @@ -1592,7 +1592,7 @@ async fn a_course_assignment_whose_teacher_is_demoted_mid_write_is_undone() { let teacher_id = me_id(&app, &teacher).await; let course = create_course(&app, &manager, "algebra").await; - demote_during_writes_to("course", "UPDATE", &teacher_id, &db).await; + demote_during_writes_to("course_teacher", "CREATE", &teacher_id, &db).await; let res = send( &app, "POST", diff --git a/tests/regress_roles.rs b/tests/regress_roles.rs index 0eff719..5f42b6f 100644 --- a/tests/regress_roles.rs +++ b/tests/regress_roles.rs @@ -98,7 +98,11 @@ async fn a_demotion_frees_the_event_seat_nothing_else_could_free() { let res = seat(&app, &teacher, &event, &ali_id).await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", &db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + &db + ) + .await, 1 ); @@ -120,7 +124,11 @@ async fn a_demotion_frees_the_event_seat_nothing_else_could_free() { "a non-student may hold no signup row" ); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", &db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + &db + ) + .await, 0, "the seat must be handed back, or the event is full forever" ); @@ -173,7 +181,11 @@ async fn a_promotion_keeps_the_signup_the_promoted_user_can_still_free() { "a promotion must not destroy a signup its holder can still free" ); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", &db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + &db + ) + .await, 1, "…and the seat it holds stays claimed with it" ); @@ -193,7 +205,11 @@ async fn a_promotion_keeps_the_signup_the_promoted_user_can_still_free() { assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); assert_eq!(rows("SELECT count(*) FROM registration", &db).await, 0); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", &db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + &db + ) + .await, 0 ); } @@ -238,7 +254,11 @@ async fn a_demotion_leaves_a_frozen_signup_list_exactly_as_it_stands() { "a closed roster must not be rewritten by a role change" ); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", &db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + &db + ) + .await, 1, "…and its seat stays claimed with it — the row is the seat" ); @@ -303,7 +323,11 @@ async fn the_sweep_deletes_an_orphan_signup_instead_of_stranding_it() { "an orphan signup must go with the rest — nothing else can ever remove it" ); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", &db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + &db + ) + .await, 0, "and the surviving event still gets its seat back" ); @@ -311,7 +335,7 @@ async fn the_sweep_deletes_an_orphan_signup_instead_of_stranding_it() { /// How many boards list `user` as a participant, read out of the store. async fn boards_listing(user: &str, db: &Database) -> i64 { - sqlx::query_scalar("SELECT count(*) FROM board WHERE $1 = ANY(participants)") + sqlx::query_scalar("SELECT count(*) FROM board_participant WHERE participant = $1") .bind(uuid::Uuid::parse_str(user).expect("a uuid user id")) .fetch_one(db) .await @@ -486,7 +510,11 @@ async fn nothing_was_swept(ali_id: &str, db: &Database) { "the enrollment must survive a failed cascade" ); assert_eq!( - counter("SELECT COALESCE(sum(enrollment_count), 0)::bigint FROM course", db).await, + counter( + "SELECT COALESCE(sum(enrollment_count), 0)::bigint FROM course", + db + ) + .await, 1, "…and so must its seat, or the roster and the count disagree forever" ); @@ -501,7 +529,11 @@ async fn nothing_was_swept(ali_id: &str, db: &Database) { "the signup must survive a failed cascade" ); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + db + ) + .await, 1, "…with its seat still claimed" ); @@ -546,11 +578,19 @@ async fn the_retry_takes_everything( } assert_eq!(boards_listing(ali_id, db).await, 0); assert_eq!( - counter("SELECT COALESCE(sum(enrollment_count), 0)::bigint FROM course", db).await, + counter( + "SELECT COALESCE(sum(enrollment_count), 0)::bigint FROM course", + db + ) + .await, 0 ); assert_eq!( - counter("SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", db).await, + counter( + "SELECT COALESCE(sum(registration_count), 0)::bigint FROM event", + db + ) + .await, 0 ); // The seat is usable, which is the whole point of freeing it: the one-seat @@ -585,7 +625,7 @@ async fn a_failure_late_in_the_cascade_leaves_the_old_role_and_every_grant_stand let (app, db) = app_and_db().await; let (admin, teacher, ali_id) = a_student_holding_every_grant(&app, &db).await; - poison("board", "UPDATE", &db).await; + poison("board_participant", "DELETE", &db).await; let res = send( &app, "PATCH", @@ -602,7 +642,7 @@ async fn a_failure_late_in_the_cascade_leaves_the_old_role_and_every_grant_stand ); nothing_was_swept(&ali_id, &db).await; - unpoison("board", &db).await; + unpoison("board_participant", &db).await; the_retry_takes_everything(&app, &db, &admin, &teacher, &ali_id).await; }