-
Notifications
You must be signed in to change notification settings - Fork 184
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
MPP-2503: Change Profile.user to OneToOneField #2749
Conversation
✅ Deploy Preview for fx-relay-demo canceled.
|
426385d
to
a09fd3c
Compare
a09fd3c
to
a100cc2
Compare
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.
Code and spot-checks look good! 1 non-blocking question.
@@ -215,66 +215,3 @@ def markdown_report(self) -> str: | |||
) | |||
|
|||
return "\n".join(lines) | |||
|
|||
|
|||
class ManyProfileDetector(DetectorTask): |
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.
praise: 🪓
@@ -217,21 +217,21 @@ def test_sns_message_with_hard_bounce(self): | |||
|
|||
_sns_notification(BOUNCE_SNS_BODIES["hard"]) | |||
|
|||
profile = self.user.profile_set.get() | |||
assert profile.last_hard_bounce >= pre_request_datetime | |||
self.user.refresh_from_db() |
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.
question (non-blocking): does refresh_from_db()
also refresh related field data? Looks like most of these tests are written such that they might pass even if the data was stale?
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 had to go prove it to myself, but yes it does. This part of the docs wasn't clear to me:
If you need to reload a model’s values from the database, you can use the refresh_from_db() method. When this method is called without arguments the following is done:
- All non-deferred fields of the model are updated to the values currently present in the database.
- Any cached relations are cleared from the reloaded instance.
I had to play around in ./manage.py shell
to see how it worked:
>>> from django.contrib.auth.models import User
>>> from emails.models import Profile
>>> user = User.objects.latest("date_joined")
>>> profile = user.profile
>>> user.profile.num_address_deleted
0
>>> profile.num_address_deleted
0
>>> # The two are now distinct objects
>>> user.profile.num_address_deleted = 10
>>> profile.num_address_deleted
0
>>> user.profile.save()
>>> profile.num_address_deleted
0
>>> profile.refresh_from_db()
>>> profile.num_address_deleted
10
>>> # Going the other way...
>>> profile.num_address_deleted = 20
>>> profile.save()
>>> user.profile.num_address_deleted
10
>>> # This clears user.profile from the cached relations
>>> user.refresh_from_db()
>>> # And this loads the profile from the database
>>> user.profile.num_address_deleted
20
Change Profile.user from ForeignKey to OneToOneField. Make follow-on changes: * Change user.profile_set.get() to user.profile * Change prefetch_related_objects('profile_set') to profile * Remove ManyProfileDetector (no longer possible) * Drop ./manage.py cleanup_data --many-profiles
a100cc2
to
623bfda
Compare
Note: This will fail CircleCI migration tests until
2022.11.02
is in productionChange Profile.user from ForeignKey to OneToOneField, to fix issue #2708 / MPP-2503. This requires some follow-on changes:
user.profile_set.get()
touser.profile
prefetch_related_objects('profile_set')
toprefetch_related_objects('profile')
ManyProfileDetector
(no longer possible)./manage.py cleanup_data --many-profiles
Also makes some changes suggested by
flake8
, such as removing unused imports.