-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #900 from kobotoolbox/daily-counters-user-null
Make user not nullable in DailyXFormSubmissionCounter
- Loading branch information
Showing
5 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
19 changes: 19 additions & 0 deletions
19
onadata/apps/logger/migrations/0031_remove_null_user_daily_counters.py
This file contains 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,19 @@ | ||
from django.conf import settings | ||
from django.db import migrations | ||
|
||
from onadata.apps.logger.utils import delete_null_user_daily_counters | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('logger', '0030_backfill_lost_monthly_counters'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
delete_null_user_daily_counters, | ||
migrations.RunPython.noop, | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
onadata/apps/logger/migrations/0032_alter_daily_submission_counter_user.py
This file contains 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,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', '0031_remove_null_user_daily_counters'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='dailyxformsubmissioncounter', | ||
name='user', | ||
field=models.ForeignKey('auth.User', related_name='daily_users', null=False, on_delete=models.CASCADE), | ||
), | ||
] |
This file contains 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 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,37 @@ | ||
def delete_null_user_daily_counters(apps, *args): | ||
""" | ||
Find any DailyXFormCounters without a user, assign them to a user if we can, otherwise delete them | ||
This function is reused between two migrations, logger.0030 and logger.0031. | ||
If/when those migrations get squashed, please delete this function | ||
""" | ||
DailyXFormSubmissionCounter = apps.get_model('logger', 'DailyXFormSubmissionCounter') # noqa | ||
|
||
counters_without_users = DailyXFormSubmissionCounter.objects.filter(user=None) | ||
|
||
if not counters_without_users.exists(): | ||
return | ||
|
||
# Associate each daily counter with user=None with a user based on its xform | ||
batch = [] | ||
batch_size = 5000 | ||
for counter in ( | ||
counters_without_users | ||
.exclude(xform=None) | ||
.exclude(xform__user=None) | ||
.iterator(chunk_size=batch_size) | ||
): | ||
counter.user = counter.xform.user | ||
# don't add a user to duplicate counters, so they get deleted when we're done looping | ||
if DailyXFormSubmissionCounter.objects.filter( | ||
date=counter.date, xform=counter.xform | ||
).exclude(user=None).exists(): | ||
continue | ||
batch.append(counter) | ||
if len(batch) >= batch_size: | ||
DailyXFormSubmissionCounter.objects.bulk_update(batch, ['user_id']) | ||
batch = [] | ||
if batch: | ||
DailyXFormSubmissionCounter.objects.bulk_update(batch, ['user_id']) | ||
|
||
# Delete daily counters without a user to avoid creating invalid monthly counters | ||
DailyXFormSubmissionCounter.objects.filter(user=None).delete() |