# Generated by Django 2.2.8 on 2019-12-09 21:03

from django.db import migrations

BATCH_SIZE = 2500


def populate_is_squashed(apps, schema_editor):
    DailyCount = apps.get_model("statistics", "DailyCount")
    DailySecondTotalCount = apps.get_model("statistics", "DailySecondTotalCount")
    TotalCount = apps.get_model("statistics", "TotalCount")

    populate_for_model(DailyCount)
    populate_for_model(DailySecondTotalCount)
    populate_for_model(TotalCount)


def populate_for_model(model):
    max_id = 0
    num_updated = 0
    while True:
        id_batch = list(model.objects.filter(id__gt=max_id, is_squashed=None).values_list("id", flat=True).order_by("id")[:BATCH_SIZE])
        if not id_batch:
            break

        model.objects.filter(id__in=id_batch).update(is_squashed=True)
        max_id = id_batch[-1]
        num_updated += len(id_batch)
        
        print(f" > Updated {num_updated} instances of {model.name}")


class Migration(migrations.Migration):

    dependencies = [
        ('statistics', '0014_auto_20191209_1933'),
    ]

    operations = [
        migrations.RunPython(populate_is_squashed)
    ]

Add migration to backfill is_squashed on squashable models