Skip to content

[chores:fix] Prevented dangling relations from breaking radius migrations#726

Merged
nemesifier merged 1 commit into
masterfrom
filter-registered-users
May 27, 2026
Merged

[chores:fix] Prevented dangling relations from breaking radius migrations#726
nemesifier merged 1 commit into
masterfrom
filter-registered-users

Conversation

@pandafy
Copy link
Copy Markdown
Member

@pandafy pandafy commented May 27, 2026

Checklist

  • I have read the OpenWISP Contributing Guidelines.
  • I have manually tested the changes proposed in this pull request.
  • N/A I have written new test cases for new code and/or updated existing tests for changes to existing code.
  • N/A I have updated the documentation.

Description of Changes

Historical databases can contain dangling relations in RegisteredUser and OrganizationUser rows. The RegisteredUser UUID and multitenant migration helpers assumed these foreign keys were always valid and could fail during upgrades when copying or backfilling data.

This bug was observed as a foreign key violation while inserting rows into openwisp_radius_registereduser during the 0045/0046 migration chain because a copied RegisteredUser.user_id no longer existed in openwisp_users_user. The same class of issue could also affect OrganizationUser-based backfills when a membership pointed to a missing user or organization.

This change filters invalid RegisteredUser and OrganizationUser rows before reusing their foreign keys in migration helpers, preserving the existing behavior for valid data while safely skipping broken legacy references. Focused regression tests were added to cover dangling RegisteredUser.user_id, dangling OrganizationUser.user_id and dangling OrganizationUser.organization_id cases.

@pandafy pandafy self-assigned this May 27, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 27, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This pull request adds defensive filtering to migration helpers to exclude dangling foreign-key references during RegisteredUser and PhoneToken data-copy operations. Three new helper functions filter querysets to validate that referenced users and organizations still exist. RegisteredUser forward/reverse migrations and the multitenant path now use these filters to avoid copying orphaned rows, and PhoneToken organization population is updated to derive organization IDs only from valid membership references. Tests validate the new filtering behavior with direct FK corruption via raw SQL.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description covers the core requirements: it explains the problem context (dangling relations in historical databases), the specific observed failure (foreign key violation in 0045/0046 migration), the solution (filtering invalid rows), and confirms regression tests were added.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Bug Fixes ✅ Passed PR fixes root cause via filters validating foreign keys before migrations. Regression tests simulate dangling relations and verify filtering behavior. Tests are deterministic without flakiness.
Title check ✅ Passed The title clearly summarizes the main change: preventing dangling relations from breaking radius migrations. It uses the required [type] prefix format with 'chores:fix' and is directly related to the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch filter-registered-users

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot]
coderabbitai Bot previously approved these changes May 27, 2026
@openwisp-companion
Copy link
Copy Markdown

Flake8 E501: Line too long

Hello @pandafy,
(Analysis for commit 5c06a8f)

The Flake8 linter found lines that exceed the maximum line length of 88 characters.

Fix:
Please manually shorten the lines that violate the E501 rule in the file openwisp_radius/tests/test_migrations.py at line 436.

Historical databases can contain dangling relations in RegisteredUser
and OrganizationUser rows. The RegisteredUser UUID and multitenant
migration helpers assumed these foreign keys were always valid and
could fail during upgrades when copying or backfilling data.

This bug was observed as a foreign key violation while inserting rows
into openwisp_radius_registereduser during the 0045/0046 migration
chain because a copied RegisteredUser.user_id no longer existed in
openwisp_users_user. The same class of issue could also affect
OrganizationUser-based backfills when a membership pointed to a missing
user or organization.

This change filters invalid RegisteredUser and OrganizationUser rows
before reusing their foreign keys in migration helpers, preserving the
existing behavior for valid data while safely skipping broken legacy
references. Focused regression tests were added to cover dangling
RegisteredUser.user_id, dangling OrganizationUser.user_id and dangling
OrganizationUser.organization_id cases.
@kilo-code-bot
Copy link
Copy Markdown

kilo-code-bot Bot commented May 27, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • openwisp_radius/migrations/__init__.py
  • openwisp_radius/tests/test_migrations.py

Reviewed by gpt-5.4-20260305 · 238,778 tokens

@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 97.42%. remained the same — filter-registered-users into master

@nemesifier nemesifier changed the title [fix] Prevented dangling relations from breaking radius migrations [chores:fix] Prevented dangling relations from breaking radius migrations May 27, 2026
@nemesifier nemesifier merged commit 40509d3 into master May 27, 2026
22 checks passed
@nemesifier nemesifier deleted the filter-registered-users branch May 27, 2026 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants