Skip to content

Commit

Permalink
Merge pull request #899 from kobotoolbox/fix-monthly-counters-deletion
Browse files Browse the repository at this point in the history
Restore missing monthly submission counters data
  • Loading branch information
jnm authored Oct 10, 2023
2 parents f6e77e5 + 7516d70 commit ebfede3
Showing 1 changed file with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db.models import Value, F, DateField
from django.db.models.functions import Cast, Concat
from django.db.models.functions import ExtractYear, ExtractMonth
from django.utils.timezone import now
from django.utils import timezone


def populate_missing_monthly_counters(apps, schema_editor):
Expand Down Expand Up @@ -33,13 +33,12 @@ def populate_missing_monthly_counters(apps, schema_editor):
DailyXFormSubmissionCounter.objects.filter(
date__range=[
previous_migration.applied.date().replace(day=1),
now().date()
timezone.now().date()
]
)
.annotate(year=ExtractYear('date'), month=ExtractMonth('date'))
.values('month', 'year')
.values('user_id', 'xform_id', 'month', 'year')
.annotate(total=Sum('counter'))
.values('user_id', 'xform_id', 'month', 'year', 'total')
).order_by('year', 'month', 'user_id')

# Do not use `ignore_conflicts=True` to ensure all counters are successfully
Expand Down

0 comments on commit ebfede3

Please sign in to comment.