discourse/app
Andrei Prigorshnev ded6aed15e
FIX: avoid race condition when setting user status (#19817) (#20182)
This is a backport of 84e13e9.

We caught it in logs, race condition led to this error:

    ActiveRecord::RecordNotUnique 
    (PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "user_statuses_pkey"
    DETAIL:  Key (user_id)=(15) already exists.)


The reason the problem happened was that we were checking if a user has status and if not inserting status:

    if user_status
      ...
    else
      self.user_status = UserStatus.create!(status)
    end

The problem is that it's possible that another request will insert status just after we check if status exists and just before our request call `UserStatus.create!(status)`. Using `upsert` fixes the problem because under the hood `upsert` generates the only SQL request that uses "INSERT ... ON CONFLICT DO UPDATE". So we do everything in one SQL query, and that query takes care of resolving possible conflicts.
2023-02-06 20:32:45 +04:00
..
assets FIX: Failing system spec for rate limited search (#20046) 2023-02-01 19:05:58 -08:00
controllers FEATURE: rate limit anon searches per second (#19708) 2023-02-01 19:05:58 -08:00
helpers DEV: Apply syntax_tree formatting to app/* 2023-01-09 14:14:59 +00:00
jobs DEV: Fix threading error when running jobs immediately in system tests (#19811) 2023-01-10 13:41:25 +08:00
mailers DEV: Apply syntax_tree formatting to app/* 2023-01-09 14:14:59 +00:00
models FIX: avoid race condition when setting user status (#19817) (#20182) 2023-02-06 20:32:45 +04:00
serializers FIX: Preload user sidebar attrs when ?enable_sidebar=1 (#19843) 2023-01-25 13:48:49 +02:00
services FIX: Do not add empty use/svg tags in ExcerptParser (#19969) 2023-01-25 13:48:49 +02:00
views UX: add btn-primary class on email confirm page (#19803) 2023-01-09 15:26:56 -05:00