Order claim_seat's lock handshake before the seat bump
Browse files at this commit995dab74159518d1c42cd722fcfa2b760789cb29Parent: 35683ac- fix
- order claim_seat's lock handshake before the seat bump
4 changed files +68 -66
- .sqlx/query-7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08.json → .sqlx/query-1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8.json +2 -2
- .sqlx/query-2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3.json +54 -0
- .sqlx/query-d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd.json +0 -54
- src/db/registration.rs +12 -10
.sqlx/query-7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08.json → .sqlx/query-1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8.jsonrenamed+2 -2
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "db_name": "PostgreSQL", |
| 3 | - "query": "WITH gone AS (\n DELETE FROM pomodoro_session\n WHERE app_user = $1 AND finished_at IS NULL\n RETURNING 1)\n INSERT INTO pomodoro_session (id, app_user, started_at, finished_at, counted, label)\n SELECT $2, $1, $3, NULL, NULL, $4\n RETURNING id AS \"id: PomodoroSessionId\", app_user AS \"user: UserId\", started_at AS \"started_at: Timestamp\", finished_at AS \"finished_at: Timestamp\", counted, label", | |
| 3 | + "query": "INSERT INTO pomodoro_session (id, app_user, started_at, finished_at, counted, label)\n VALUES ($2, $1, $3, NULL, NULL, $4)\n ON CONFLICT (app_user) WHERE finished_at IS NULL DO UPDATE\n SET started_at = EXCLUDED.started_at, finished_at = NULL, counted = NULL,\n label = EXCLUDED.label\n RETURNING id AS \"id: PomodoroSessionId\", app_user AS \"user: UserId\", started_at AS \"started_at: Timestamp\", finished_at AS \"finished_at: Timestamp\", counted, label", | |
| 4 | 4 | "describe": { |
| 5 | 5 | "columns": [ |
| 6 | 6 | { |
| @@ -87,5 +87,5 @@ | ||
| 87 | 87 | true |
| 88 | 88 | ] |
| 89 | 89 | }, |
| 90 | - "hash": "7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08" | |
| 90 | + "hash": "1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8" | |
| 91 | 91 | } |
.sqlx/query-2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3.jsonadded+54 -0
| @@ -0,0 +1,54 @@ | ||
| 1 | +{ | |
| 2 | + "db_name": "PostgreSQL", | |
| 3 | + "query": "WITH person AS (\n -- The lock handshake comes FIRST: FOR NO KEY UPDATE waits out a\n -- racing demotion and re-checks the role against the committed\n -- row. A data-modifying CTE executes even when the INSERT below\n -- matches nothing, so the bump must depend on this CTE's result —\n -- the EXISTS reference in `seat` forces that order.\n SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent'\n FOR NO KEY UPDATE),\n seat AS (\n UPDATE event SET registration_count = registration_count + 1\n WHERE id = $1\n AND (audience_capacity IS NULL OR registration_count < audience_capacity)\n AND EXISTS (SELECT 1 FROM person)\n RETURNING 1)\n INSERT INTO registration (event, app_user, registered_by)\n SELECT $1, $2, $3\n WHERE EXISTS (SELECT 1 FROM seat)\n RETURNING event AS \"event: EventId\", app_user AS \"user: UserId\", registered_by AS \"registered_by: UserId\"", | |
| 4 | + "describe": { | |
| 5 | + "columns": [ | |
| 6 | + { | |
| 7 | + "ordinal": 0, | |
| 8 | + "name": "event: EventId", | |
| 9 | + "type_info": "Uuid", | |
| 10 | + "origin": { | |
| 11 | + "Table": { | |
| 12 | + "table": "registration", | |
| 13 | + "name": "event" | |
| 14 | + } | |
| 15 | + } | |
| 16 | + }, | |
| 17 | + { | |
| 18 | + "ordinal": 1, | |
| 19 | + "name": "user: UserId", | |
| 20 | + "type_info": "Uuid", | |
| 21 | + "origin": { | |
| 22 | + "Table": { | |
| 23 | + "table": "registration", | |
| 24 | + "name": "app_user" | |
| 25 | + } | |
| 26 | + } | |
| 27 | + }, | |
| 28 | + { | |
| 29 | + "ordinal": 2, | |
| 30 | + "name": "registered_by: UserId", | |
| 31 | + "type_info": "Uuid", | |
| 32 | + "origin": { | |
| 33 | + "Table": { | |
| 34 | + "table": "registration", | |
| 35 | + "name": "registered_by" | |
| 36 | + } | |
| 37 | + } | |
| 38 | + } | |
| 39 | + ], | |
| 40 | + "parameters": { | |
| 41 | + "Left": [ | |
| 42 | + "Uuid", | |
| 43 | + "Uuid", | |
| 44 | + "Uuid" | |
| 45 | + ] | |
| 46 | + }, | |
| 47 | + "nullable": [ | |
| 48 | + false, | |
| 49 | + false, | |
| 50 | + false | |
| 51 | + ] | |
| 52 | + }, | |
| 53 | + "hash": "2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3" | |
| 54 | +} |
.sqlx/query-d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd.jsondeleted+0 -54
| @@ -1,54 +0,0 @@ | ||
| 1 | -{ | |
| 2 | - "db_name": "PostgreSQL", | |
| 3 | - "query": "WITH seat AS (\n UPDATE event SET registration_count = registration_count + 1\n WHERE id = $1\n AND (audience_capacity IS NULL OR registration_count < audience_capacity)\n -- The role gate rides the bump's WHERE: a data-modifying CTE\n -- executes even when the INSERT below matches nothing, so the\n -- count must refuse exactly when the seat refuses.\n AND EXISTS (SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent')\n RETURNING 1),\n person AS (\n SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent'\n FOR NO KEY UPDATE)\n INSERT INTO registration (event, app_user, registered_by)\n SELECT $1, $2, $3\n WHERE EXISTS (SELECT 1 FROM seat) AND EXISTS (SELECT 1 FROM person)\n RETURNING event AS \"event: EventId\", app_user AS \"user: UserId\", registered_by AS \"registered_by: UserId\"", | |
| 4 | - "describe": { | |
| 5 | - "columns": [ | |
| 6 | - { | |
| 7 | - "ordinal": 0, | |
| 8 | - "name": "event: EventId", | |
| 9 | - "type_info": "Uuid", | |
| 10 | - "origin": { | |
| 11 | - "Table": { | |
| 12 | - "table": "registration", | |
| 13 | - "name": "event" | |
| 14 | - } | |
| 15 | - } | |
| 16 | - }, | |
| 17 | - { | |
| 18 | - "ordinal": 1, | |
| 19 | - "name": "user: UserId", | |
| 20 | - "type_info": "Uuid", | |
| 21 | - "origin": { | |
| 22 | - "Table": { | |
| 23 | - "table": "registration", | |
| 24 | - "name": "app_user" | |
| 25 | - } | |
| 26 | - } | |
| 27 | - }, | |
| 28 | - { | |
| 29 | - "ordinal": 2, | |
| 30 | - "name": "registered_by: UserId", | |
| 31 | - "type_info": "Uuid", | |
| 32 | - "origin": { | |
| 33 | - "Table": { | |
| 34 | - "table": "registration", | |
| 35 | - "name": "registered_by" | |
| 36 | - } | |
| 37 | - } | |
| 38 | - } | |
| 39 | - ], | |
| 40 | - "parameters": { | |
| 41 | - "Left": [ | |
| 42 | - "Uuid", | |
| 43 | - "Uuid", | |
| 44 | - "Uuid" | |
| 45 | - ] | |
| 46 | - }, | |
| 47 | - "nullable": [ | |
| 48 | - false, | |
| 49 | - false, | |
| 50 | - false | |
| 51 | - ] | |
| 52 | - }, | |
| 53 | - "hash": "d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd" | |
| 54 | -} |
src/db/registration.rsmodified+12 -10
| @@ -69,21 +69,23 @@ pub async fn claim_seat( | ||
| 69 | 69 | ) -> Result<Claimed<Registration>, AppError> { |
| 70 | 70 | match query_as!( |
| 71 | 71 | Registration, |
| 72 | - "WITH seat AS ( | |
| 72 | + "WITH person AS ( | |
| 73 | + -- The lock handshake comes FIRST: FOR NO KEY UPDATE waits out a | |
| 74 | + -- racing demotion and re-checks the role against the committed | |
| 75 | + -- row. A data-modifying CTE executes even when the INSERT below | |
| 76 | + -- matches nothing, so the bump must depend on this CTE's result — | |
| 77 | + -- the EXISTS reference in `seat` forces that order. | |
| 78 | + SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent' | |
| 79 | + FOR NO KEY UPDATE), | |
| 80 | + seat AS ( | |
| 73 | 81 | UPDATE event SET registration_count = registration_count + 1 |
| 74 | 82 | WHERE id = $1 |
| 75 | 83 | AND (audience_capacity IS NULL OR registration_count < audience_capacity) |
| 76 | - -- The role gate rides the bump's WHERE: a data-modifying CTE | |
| 77 | - -- executes even when the INSERT below matches nothing, so the | |
| 78 | - -- count must refuse exactly when the seat refuses. | |
| 79 | - AND EXISTS (SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent') | |
| 80 | - RETURNING 1), | |
| 81 | - person AS ( | |
| 82 | - SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent' | |
| 83 | - FOR NO KEY UPDATE) | |
| 84 | + AND EXISTS (SELECT 1 FROM person) | |
| 85 | + RETURNING 1) | |
| 84 | 86 | INSERT INTO registration (event, app_user, registered_by) |
| 85 | 87 | SELECT $1, $2, $3 |
| 86 | - WHERE EXISTS (SELECT 1 FROM seat) AND EXISTS (SELECT 1 FROM person) | |
| 88 | + WHERE EXISTS (SELECT 1 FROM seat) | |
| 87 | 89 | RETURNING event AS \"event: EventId\", app_user AS \"user: UserId\", registered_by AS \"registered_by: UserId\"", |
| 88 | 90 | event.uuid(), |
| 89 | 91 | user.uuid(), |