Fix: Prevent duplicate active alerts with unique index - #2144
Closed
sentry[bot] wants to merge 1 commit into
Closed
Conversation
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This PR addresses the
ActiveRecord::RecordInvalid: Validation failed: Address You already have an alert for that addresserror occurring inCuttlefishController#event.Root Cause:
The issue stemmed from a race condition during alert creation, which allowed multiple active alerts for the same user and address to be saved. The Rails-level uniqueness validation (
validates :address, uniqueness: { scope: %i[user_id unsubscribed] }) was insufficient to prevent this under concurrent requests, as there was no corresponding database-level unique index.When Cuttlefish attempted to update an alert (e.g.,
last_delivered_at), thealert.update!call would trigger the Rails validation, find the other duplicate active alert, and raiseActiveRecord::RecordInvalid.Solution:
20260813000000_add_unique_index_to_alerts.rb) has been added to create a partial unique index onalerts(address, user_id)whereunsubscribed = false. This enforces uniqueness at the database level, preventing new duplicate active alerts from being created.(address, user_id)pair (whereunsubscribed = false) remains before the index is added.Alertmodel'svalidates :address, uniqueness:scope has been adjusted to{ scope: :user_id }to align with the new partial database index. Theunless: :unsubscribed?condition already handles the partial nature of the validation.rescueblock has been added aroundalert.update!inCuttlefishController#eventto catchActiveRecord::RecordNotUniqueandActiveRecord::RecordInvalidexceptions. This ensures that if any residual duplicate active alerts cause a validation error during an update, the webhook call will still return a 200 OK, preventing Cuttlefish from retrying and potentially exacerbating the issue.Fixes PLANNING-ALERTS-2