-
-
Notifications
You must be signed in to change notification settings - Fork 123
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
Make user not nullable in DailyXFormSubmissionCounter #900
Changes from 3 commits
b2bb881
f0da3c3
675a8e9
ba90d45
fb4ff4a
7abba8d
a82302e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,20 @@ def populate_missing_monthly_counters(apps, schema_editor): | |
if not DailyXFormSubmissionCounter.objects.all().exists(): | ||
return | ||
|
||
# Associate each daily counter with user=None with a user based on its xform | ||
for counter in DailyXFormSubmissionCounter.objects.filter(user=None).iterator: | ||
if counter.xform and counter.xform.user: | ||
has_duplicate = DailyXFormSubmissionCounter.objects.filter( | ||
date=counter.date, xform=counter.xform | ||
).exclude(user=None).exists() | ||
# don't add a user to duplicate counters, so they get deleted in the next step | ||
if not has_duplicate: | ||
counter.user = counter.xform.user | ||
counter.save() | ||
|
||
# Delete daily counters without a user to avoid creating invalid monthly counters | ||
DailyXFormSubmissionCounter.objects.filter(user=None).delete() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if those lines should be present in the Example:
What about adding adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a very good point. I refactored This covers the edge case where a user upgraded to |
||
previous_migration = MigrationRecorder.Migration.objects.filter( | ||
app='logger', name='0029_populate_daily_xform_counters_for_year' | ||
).first() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('logger', '0030_backfill_lost_monthly_counters'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='dailyxformsubmissioncounter', | ||
name='user', | ||
field=models.ForeignKey('auth.User', related_name='daily_users', null=False, on_delete=models.CASCADE), | ||
), | ||
] |
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 would prefer to use
bulk_update()
to be more efficient.Something like that:
What do you think?