# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-06-23 03:08
from __future__ import unicode_literals

from django.db import migrations


# noinspection PyUnusedLocal
def add_station_sensor_link_to_reading(apps, schema_editor):
    # noinspection PyPep8Naming
    Reading = apps.get_model('climate_data', 'Reading')
    # noinspection PyPep8Naming
    StationSensorLink = apps.get_model('climate_data', 'StationSensorLink')

    offset = 0
    pagesize = 5000
    count = Reading.objects.all().count()

    while offset < count:
        for reading in Reading.objects.all()[offset:offset+pagesize].iterator():
            reading.station_sensor_link = StationSensorLink.objects.filter(
                station=reading.station,
                sensor=reading.sensor
            ).first()
            reading.save()

        offset += pagesize


class Migration(migrations.Migration):

    dependencies = [
        ('climate_data', '0023_reading_station_sensor_link'),
    ]

    operations = [
        migrations.RunPython(add_station_sensor_link_to_reading),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-06-23 03:08
from __future__ import unicode_literals

from django.db import migrations

import sys


# noinspection PyUnusedLocal
def add_station_sensor_link_to_reading(apps, schema_editor):
    # noinspection PyPep8Naming
    Reading = apps.get_model('climate_data', 'Reading')
    # noinspection PyPep8Naming
    StationSensorLink = apps.get_model('climate_data', 'StationSensorLink')

    offset = 0
    pagesize = 25000
    count = Reading.objects.filter(station_sensor_link=None).count()

    sys.stdout.write("\n")

    sys.stdout.write("\r{}/{}".format(offset, count))
    sys.stdout.flush()

    while offset < count:
        for reading in Reading.objects.filter(station_sensor_link=None)[offset:offset+pagesize].iterator():
            reading.station_sensor_link = StationSensorLink.objects.filter(
                station=reading.station,
                sensor=reading.sensor
            ).first()
            reading.save()

        sys.stdout.write("\r{}/{} ({}%)".format(offset, count, (offset / count) * 100))
        sys.stdout.flush()

        offset += pagesize

    sys.stdout.write("\n")


class Migration(migrations.Migration):

    dependencies = [
        ('climate_data', '0023_reading_station_sensor_link'),
    ]

    operations = [
        migrations.RunPython(add_station_sensor_link_to_reading),
    ]

Increase page size again in station-sensor link / reading migration, and add a percent indicator. Exclude any readings that have previously been fixed up by the migration.
Increase page size again in station-sensor link / reading migration, and add a percent indicator. Exclude any readings that have previously been fixed up by the migration.
