Re-check homework audience under the upsert row lock (Gamze's find)
Browse files at this commit43ba49561eadbfdec430cb452620e3de1b4f712bParent: 914d095- fix
- re-check homework audience under the upsert row lock (Gamze's find)
2 changed files +38 -2
- .sqlx/query-866efd5fe0b7f2b4524f747d9ef26100e13c48d4cb35d390d29c327180681c60.json +29 -0
- src/db/homework_submission.rs +9 -2
.sqlx/query-866efd5fe0b7f2b4524f747d9ef26100e13c48d4cb35d390d29c327180681c60.jsonadded+29 -0
| @@ -0,0 +1,29 @@ | |||
| 1 | +{ | ||
| 2 | + "db_name": "PostgreSQL", | ||
| 3 | + "query": "SELECT due_at AS \"due_at: Timestamp\" FROM homework\n WHERE id = $1 AND (cardinality(assigned) = 0 OR $2 = ANY(assigned))\n FOR UPDATE", | ||
| 4 | + "describe": { | ||
| 5 | + "columns": [ | ||
| 6 | + { | ||
| 7 | + "ordinal": 0, | ||
| 8 | + "name": "due_at: Timestamp", | ||
| 9 | + "type_info": "Int8", | ||
| 10 | + "origin": { | ||
| 11 | + "Table": { | ||
| 12 | + "table": "homework", | ||
| 13 | + "name": "due_at" | ||
| 14 | + } | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | + ], | ||
| 18 | + "parameters": { | ||
| 19 | + "Left": [ | ||
| 20 | + "Uuid", | ||
| 21 | + "Uuid" | ||
| 22 | + ] | ||
| 23 | + }, | ||
| 24 | + "nullable": [ | ||
| 25 | + false | ||
| 26 | + ] | ||
| 27 | + }, | ||
| 28 | + "hash": "866efd5fe0b7f2b4524f747d9ef26100e13c48d4cb35d390d29c327180681c60" | ||
| 29 | +} | ||
src/db/homework_submission.rsmodified+9 -2
| @@ -74,8 +74,15 @@ pub async fn upsert( | |||
| 74 | // homework is gone, which is the 404 the web layer's own lookup | 74 | // homework is gone, which is the 404 the web layer's own lookup |
| 75 | // would have answered. | 75 | // would have answered. |
| 76 | let was_due = sqlx::query!( | 76 | let was_due = sqlx::query!( |
| 77 | - r#"SELECT due_at AS "due_at: Timestamp" FROM homework WHERE id = $1 FOR UPDATE"#, | 77 | + // The audience re-checks UNDER the lock: a narrowing PATCH commits |
| 78 | - homework_id.uuid() | 78 | + // between the web layer's pre-flight read and this write, and a |
| 79 | + // student the homework no longer names must read as gone — the | ||
| 80 | + // same 404 their own lookup answers. | ||
| 81 | + r#"SELECT due_at AS "due_at: Timestamp" FROM homework | ||
| 82 | + WHERE id = $1 AND (cardinality(assigned) = 0 OR $2 = ANY(assigned)) | ||
| 83 | + FOR UPDATE"#, | ||
| 84 | + homework_id.uuid(), | ||
| 85 | + user.uuid() | ||
| 79 | ) | 86 | ) |
| 80 | .fetch_optional(&mut *tx) | 87 | .fetch_optional(&mut *tx) |
| 81 | .await? | 88 | .await? |