-
Notifications
You must be signed in to change notification settings - Fork 2
Backfill missing LegalAddress/UserProfile for migrated users; guard should_skip_onboarding #3845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1682ef
fix(users): backfill LegalAddress/UserProfile for migrated users, gua…
shaidar f50c5f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3b50729
fix(courses): move migrate_edx_data test file out of management/commands
shaidar 2f94174
fix(auth): stop GatewayLoginView from crashing on a missing UserProfile
shaidar 5a8208c
perf(courses): only fetch id/email for the affected-users repair query
shaidar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| """Tests for migrate_edx_data management command's repair_migrated_profiles type""" | ||
|
|
||
| import pytest | ||
|
|
||
| from courses.management.commands.migrate_edx_data import Command | ||
| from users.factories import UserFactory | ||
| from users.models import LegalAddress, User, UserProfile | ||
|
|
||
| pytestmark = pytest.mark.django_db | ||
|
|
||
|
|
||
| class FakeCursor: | ||
| """Minimal stand-in for a Trino DB-API cursor.""" | ||
|
|
||
| def __init__(self, columns, rows): | ||
| self.description = [(col,) for col in columns] | ||
| self._rows = rows | ||
| self._offset = 0 | ||
|
|
||
| def execute(self, query): | ||
| """No-op - the fake cursor already has its rows in memory.""" | ||
|
|
||
| def fetchmany(self, size): | ||
| """Return the next slice of rows, matching the DB-API contract.""" | ||
| batch = self._rows[self._offset : self._offset + size] | ||
| self._offset += size | ||
| return batch | ||
|
|
||
|
|
||
| class FakeConnection: | ||
| """Minimal stand-in for a Trino DB-API connection.""" | ||
|
|
||
| def __init__(self, columns, rows): | ||
| self._columns = columns | ||
| self._rows = rows | ||
|
|
||
| def cursor(self): | ||
| """Return a fresh fake cursor over the same fixed rows.""" | ||
| return FakeCursor(self._columns, self._rows) | ||
|
|
||
|
|
||
| USER_COLUMNS = [ | ||
| "user_email", | ||
| "user_full_name", | ||
| "user_address_country", | ||
| "user_address_state", | ||
| "user_address_postal_code", | ||
| "user_address_street_1", | ||
| "user_address_street_2", | ||
| "user_address_city", | ||
| "user_gender", | ||
| "user_birth_year", | ||
| ] | ||
|
|
||
|
|
||
| def _user_row(email, name, country="US"): | ||
| return ( | ||
| email, | ||
| name, | ||
| country, | ||
| "MA", | ||
| "02139", | ||
| "1 Main St", | ||
| "", | ||
| "Cambridge", | ||
| None, | ||
| None, | ||
| ) | ||
|
|
||
|
|
||
| def _break_user(user): | ||
| """Simulate the historical bug's effect: a User row with no | ||
| LegalAddress or UserProfile at all. | ||
| """ | ||
| LegalAddress.objects.filter(user=user).delete() | ||
| UserProfile.objects.filter(user=user).delete() | ||
|
|
||
|
|
||
| def test_repair_creates_missing_legal_address_and_profile(): | ||
| """A user missing both rows gets both created from the matching edX row""" | ||
| user = UserFactory.create(email="broken@example.com") | ||
| _break_user(user) | ||
|
|
||
| conn = FakeConnection( | ||
| columns=USER_COLUMNS, | ||
| rows=[_user_row(user.email, "Broken User", country="US")], | ||
| ) | ||
|
|
||
| Command()._repair_migrated_user_profiles(conn, {}) # noqa: SLF001 | ||
|
|
||
| user.refresh_from_db() | ||
| assert user.legal_address.country == "US" | ||
| assert UserProfile.objects.filter(user=user).exists() | ||
|
|
||
|
|
||
| def test_repair_only_backfills_missing_piece(): | ||
| """A user missing only UserProfile (LegalAddress already exists) only | ||
| gets UserProfile created - the existing helpers must not touch or | ||
| duplicate the LegalAddress that's already there | ||
| """ | ||
| user = UserFactory.create(email="half-broken@example.com") | ||
| user.legal_address.country = "CA" | ||
| user.legal_address.save() | ||
| UserProfile.objects.filter(user=user).delete() | ||
|
|
||
| conn = FakeConnection( | ||
| columns=USER_COLUMNS, | ||
| rows=[_user_row(user.email, "Half Broken", country="US")], | ||
| ) | ||
|
|
||
| Command()._repair_migrated_user_profiles(conn, {}) # noqa: SLF001 | ||
|
|
||
| user.refresh_from_db() | ||
| assert user.legal_address.country == "CA" | ||
| assert UserProfile.objects.filter(user=user).exists() | ||
|
|
||
|
|
||
| def test_repair_skips_users_with_no_missing_rows(): | ||
| """A user who already has both rows is left untouched, even though a | ||
| matching edX row exists for their email | ||
| """ | ||
| user = UserFactory.create(email="fine@example.com") | ||
| user.legal_address.country = "MX" | ||
| user.legal_address.save() | ||
|
|
||
| conn = FakeConnection( | ||
| columns=USER_COLUMNS, | ||
| rows=[_user_row(user.email, "Fine User", country="US")], | ||
| ) | ||
|
|
||
| Command()._repair_migrated_user_profiles(conn, {}) # noqa: SLF001 | ||
|
|
||
| user.refresh_from_db() | ||
| assert user.legal_address.country == "MX" | ||
|
|
||
|
|
||
| def test_repair_leaves_unmatched_user_broken_without_crashing(): | ||
| """An affected user with no corresponding row in edxorg_to_mitxonline_users | ||
| (e.g. their edX data was never in that table) is reported as still | ||
| missing, not silently invented from nothing | ||
| """ | ||
| user = UserFactory.create(email="no-edx-row@example.com") | ||
| _break_user(user) | ||
|
|
||
| conn = FakeConnection(columns=USER_COLUMNS, rows=[]) | ||
|
|
||
| Command()._repair_migrated_user_profiles(conn, {}) # noqa: SLF001 | ||
|
|
||
| assert not LegalAddress.objects.filter(user=user).exists() | ||
| assert not UserProfile.objects.filter(user=user).exists() | ||
|
|
||
|
|
||
| def test_dry_run_does_not_write(): | ||
| """--dry-run must not create any rows, even for a clearly matched user""" | ||
| user = UserFactory.create(email="dry-run@example.com") | ||
| _break_user(user) | ||
|
|
||
| conn = FakeConnection( | ||
| columns=USER_COLUMNS, | ||
| rows=[_user_row(user.email, "Dry Run User", country="US")], | ||
| ) | ||
|
|
||
| Command()._repair_migrated_user_profiles(conn, {"dry_run": True}) # noqa: SLF001 | ||
|
|
||
| assert not LegalAddress.objects.filter(user=user).exists() | ||
| assert not UserProfile.objects.filter(user=user).exists() | ||
|
|
||
|
|
||
| def test_limit_caps_number_of_users_repaired(): | ||
| """--limit caps how many affected users are considered at all, leaving | ||
| the rest broken for a later run | ||
| """ | ||
| users = [UserFactory.create(email=f"limited-{i}@example.com") for i in range(3)] | ||
| for user in users: | ||
| _break_user(user) | ||
|
|
||
| conn = FakeConnection( | ||
| columns=USER_COLUMNS, | ||
| rows=[_user_row(user.email, "Limited User", country="US") for user in users], | ||
| ) | ||
|
|
||
| Command()._repair_migrated_user_profiles(conn, {"limit": 1}) # noqa: SLF001 | ||
|
|
||
| repaired = User.objects.filter(legal_address__isnull=False).count() | ||
| assert repaired == 1 |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The repair query for migrated users is too broad, potentially corrupting data for non-migrated users with missing profiles and matching emails.
Severity: LOW
Suggested Fix
Filter the initial query in
_repair_migrated_user_profilesto only include users who were part of the migration. This can be done by checking for a migration-specific marker, such as ascim_external_id, instead of querying for all users missing aLegalAddressorUserProfile.Prompt for AI Agent
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shaidar if the plan is to use
edxorg_to_mitxonline_usersto fix the migrated users who are missing a LegalAddress/UserProfile, then this is the right place. ButQ(legal_address__isnull=True) | Q(user_profile__isnull=True)matches every user in the database missing one of these rows, including users who were not part of the edX users we migrated, and we won't have the data to populate theirs.The simple fix might be to create a blank LegalAddress/UserProfile @rhysyngsun any thoughts on whether we scope this to migrated users only, or backfill blanks for everyone missing a row?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think because this command is scoped to repair migrated users, let's just leave the case as-is for now and we'll look into repairing everyone else separately. I mentioned yesterday that users somehow not having a legaladdress/profile seems to be happening through other means so we need some kind of self-healing mechanism that repairs these users on an ongoing basis but that should be it's own PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good - leaving the query as-is here. Happy to help scope the self-healing follow-up once we've got a sense of how it's happening outside this migration path.