# Generated by Django 2.2 on 2019-07-03 09:36

from django.contrib.postgres.functions import TransactionNow
from django.db import migrations


def fill_device_saved_at(apps, schema_editor):
    Device = apps.get_model("mds", "Device")

    # We don't have a sequence field to split the table in batches
    start_id = "0" * 32
    for end_id in "123456789abcdef":
        end_id *= 32
        qs = Device.objects.filter(id__gte=start_id, id__lt=end_id)
        qs.update(saved_at=TransactionNow())
        start_id = end_id
    # Then in theory "ffffffff..." itself


class Migration(migrations.Migration):
    atomic = False

    dependencies = [("mds", "0032_pre_device_saved_at")]

    operations = [migrations.RunPython(fill_device_saved_at, migrations.RunPython.noop)]
