Fix race condition in Card::ActivitySpike::DetectionJob
The check-then-act pattern in `register_activity_spike` has been replaced with `find_or_create_by!` to eliminate the race condition that could lead to creating multiple activity spikes for a card. To support this change, the `card_id` index on `cards_activity_spikes` has been made unique. ref: https://app.fizzy.do/5986089/cards/3063
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
class AddUniqueIndexToCardActivitySpikesOnCardId < ActiveRecord::Migration[8.2]
|
||||
def change
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
execute <<-SQL
|
||||
DELETE s1 FROM card_activity_spikes s1
|
||||
INNER JOIN card_activity_spikes s2
|
||||
WHERE s1.card_id = s2.card_id
|
||||
AND s1.updated_at < s2.updated_at
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
||||
remove_index :card_activity_spikes, :card_id
|
||||
add_index :card_activity_spikes, :card_id, unique: true
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user