From 995dab74159518d1c42cd722fcfa2b760789cb29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 12 Sep 2026 18:35:38 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20order=20claim=5Fseat's=20?= =?UTF-8?q?lock=20handshake=20before=20the=20seat=20bump?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...781a841bb664b14ec5adaca33f1cac0c05e8.json} | 4 +- ...18ae35e1969cbe33e21781907c572bf2a66e3.json | 54 +++++++++++++++++++ ...30b67c83f94001ffe99b28fafbf2a137e31cd.json | 54 ------------------- src/db/registration.rs | 22 ++++---- 4 files changed, 68 insertions(+), 66 deletions(-) rename .sqlx/{query-7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08.json => query-1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8.json} (74%) create mode 100644 .sqlx/query-2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3.json delete mode 100644 .sqlx/query-d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd.json diff --git a/.sqlx/query-7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08.json b/.sqlx/query-1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8.json similarity index 74% rename from .sqlx/query-7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08.json rename to .sqlx/query-1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8.json index 779576b..99fb698 100644 --- a/.sqlx/query-7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08.json +++ b/.sqlx/query-1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "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", + "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", "describe": { "columns": [ { @@ -87,5 +87,5 @@ true ] }, - "hash": "7baced8897e05cb3b277caa9debc4e9b995552d82a210994c46c2e4c520d5e08" + "hash": "1070ebb26873befdc19bf655f37b781a841bb664b14ec5adaca33f1cac0c05e8" } diff --git a/.sqlx/query-2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3.json b/.sqlx/query-2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3.json new file mode 100644 index 0000000..75b7cbf --- /dev/null +++ b/.sqlx/query-2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3.json @@ -0,0 +1,54 @@ +{ + "db_name": "PostgreSQL", + "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\"", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "event: EventId", + "type_info": "Uuid", + "origin": { + "Table": { + "table": "registration", + "name": "event" + } + } + }, + { + "ordinal": 1, + "name": "user: UserId", + "type_info": "Uuid", + "origin": { + "Table": { + "table": "registration", + "name": "app_user" + } + } + }, + { + "ordinal": 2, + "name": "registered_by: UserId", + "type_info": "Uuid", + "origin": { + "Table": { + "table": "registration", + "name": "registered_by" + } + } + } + ], + "parameters": { + "Left": [ + "Uuid", + "Uuid", + "Uuid" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "2c7c0898e7f4d8131d5683d70e718ae35e1969cbe33e21781907c572bf2a66e3" +} diff --git a/.sqlx/query-d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd.json b/.sqlx/query-d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd.json deleted file mode 100644 index 2c829cc..0000000 --- a/.sqlx/query-d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "db_name": "PostgreSQL", - "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\"", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "event: EventId", - "type_info": "Uuid", - "origin": { - "Table": { - "table": "registration", - "name": "event" - } - } - }, - { - "ordinal": 1, - "name": "user: UserId", - "type_info": "Uuid", - "origin": { - "Table": { - "table": "registration", - "name": "app_user" - } - } - }, - { - "ordinal": 2, - "name": "registered_by: UserId", - "type_info": "Uuid", - "origin": { - "Table": { - "table": "registration", - "name": "registered_by" - } - } - } - ], - "parameters": { - "Left": [ - "Uuid", - "Uuid", - "Uuid" - ] - }, - "nullable": [ - false, - false, - false - ] - }, - "hash": "d12a287b9327e990350afa3555430b67c83f94001ffe99b28fafbf2a137e31cd" -} diff --git a/src/db/registration.rs b/src/db/registration.rs index 028c728..eb0a06d 100644 --- a/src/db/registration.rs +++ b/src/db/registration.rs @@ -69,21 +69,23 @@ pub async fn claim_seat( ) -> Result, AppError> { match query_as!( Registration, - "WITH seat AS ( + "WITH person AS ( + -- The lock handshake comes FIRST: FOR NO KEY UPDATE waits out a + -- racing demotion and re-checks the role against the committed + -- row. A data-modifying CTE executes even when the INSERT below + -- matches nothing, so the bump must depend on this CTE's result — + -- the EXISTS reference in `seat` forces that order. + SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent' + FOR NO KEY UPDATE), + seat AS ( UPDATE event SET registration_count = registration_count + 1 WHERE id = $1 AND (audience_capacity IS NULL OR registration_count < audience_capacity) - -- The role gate rides the bump's WHERE: a data-modifying CTE - -- executes even when the INSERT below matches nothing, so the - -- count must refuse exactly when the seat refuses. - AND EXISTS (SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent') - RETURNING 1), - person AS ( - SELECT 1 FROM app_user WHERE id = $2 AND role IS DISTINCT FROM 'parent' - FOR NO KEY UPDATE) + AND EXISTS (SELECT 1 FROM person) + RETURNING 1) INSERT INTO registration (event, app_user, registered_by) SELECT $1, $2, $3 - WHERE EXISTS (SELECT 1 FROM seat) AND EXISTS (SELECT 1 FROM person) + WHERE EXISTS (SELECT 1 FROM seat) RETURNING event AS \"event: EventId\", app_user AS \"user: UserId\", registered_by AS \"registered_by: UserId\"", event.uuid(), user.uuid(),