fix: prevent OrgFeatureFlag IntegrityError on creation#1407
Open
sentry[bot] wants to merge 1 commit into
Open
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 an
IntegrityError: duplicate key value violates unique constraint "ddpui_orgfeatureflag_pkey"that occurred when creatingOrgFeatureFlagentries. The root cause was identified as the PostgreSQL primary key sequence (ddpui_orgfeatureflag_id_seq) being out of sync with the actualMAX(id)in theddpui_orgfeatureflagtable, leading toget_or_createattempting to insert records with already existing primary keys.Changes Implemented:
Data Migration (
ddpui/migrations/0164_reset_orgfeatureflag_pk_sequence.py):ddpui_orgfeatureflag_id_seqsequence. This migration executesSELECT setval('ddpui_orgfeatureflag_id_seq', COALESCE((SELECT MAX(id) FROM ddpui_orgfeatureflag), 1))to synchronize the sequence with the highest existingidin the table. This resolves the immediate data inconsistency.Code Refactoring (
ddpui/utils/feature_flags.py):enable_feature_flaganddisable_feature_flagfunctions have been refactored to use a new internal helper function,_safe_update_or_create._safe_update_or_createutilizesOrgFeatureFlag.objects.update_or_createfor cleaner and more robust handling of existing or new feature flags._safe_update_or_createis wrapped inwith transaction.atomic(). This creates a savepoint, allowing the code to catchIntegrityError(specifically for primary key collisions) without aborting the entire transaction. If anIntegrityErroroccurs, the operation is retried. This retry mechanism is effective because the database sequence advances past the conflicting ID on the first failed attempt, allowing the second attempt to succeed.This solution addresses both the existing data problem (via migration) and provides a resilient, self-healing mechanism for future primary key collisions, improving the robustness of feature flag management.
Fixes DALGO-BACKEND-2AK