Fix migration 0202 to prevent duplicate Platform Auditor assignments#5
Draft
ambient-code[bot] wants to merge 1 commit into
Draft
Fix migration 0202 to prevent duplicate Platform Auditor assignments#5ambient-code[bot] wants to merge 1 commit into
ambient-code[bot] wants to merge 1 commit into
Conversation
The migration converts users from Controller System Auditor to Platform Auditor using create() without checking for pre-existing Platform Auditor assignments. When a user already has a Platform Auditor assignment (e.g., from the gateway or earlier migration 0192), the migration creates a duplicate row. Because PostgreSQL does not enforce unique_together when one column (object_role) is NULL, the duplicate insert succeeds silently. On subsequent JWT authentication, give_or_remove_global_permission calls get_or_create which internally does get() and finds 2 matching rows, raising MultipleObjectsReturned and causing a 401 error. Add a filter().exists() check before create() to skip users who already have a Platform Auditor assignment. Fixes: AAP-72477 Assisted-by: Debuggernaut (claude-opus-4-6) <[email protected]> Co-Authored-By: Claude <[email protected]>
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.
Summary
0202_convert_controller_role_definitionsto check for existing Platform AuditorRoleUserAssignmentbefore creating a new onecreate()without checking for pre-existing onesunique_togetherwhenobject_roleis NULL, so the duplicate succeeds silentlygive_or_remove_global_permissioncallsget_or_createwhich finds 2 matching rows and raisesMultipleObjectsReturned, causing a 401 errorfilter().exists()check beforecreate()to skip users who already have a Platform Auditor assignmenttest_migrations.pyCompanion PR in django-ansible-base adds defensive handling for duplicates at runtime.
Fixes: AAP-72477
Test plan
test_no_duplicate_auditor_assignmentspasses in CIAssisted-by: Debuggernaut (claude-opus-4-6) [email protected]