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')

    for reading in Reading.objects.all():
        reading.station_sensor_link = StationSensorLink.objects.filter(station=reading.station, sensor=reading.sensor)\
            .first()
        reading.save()


class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(add_station_sensor_link_to_reading),
    ]

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),
    ]

Improve station-sensor link field addition to reading model migration using a paging system to prevent the migration being killed automatically.
Improve station-sensor link field addition to reading model migration using a paging system to prevent the migration being killed automatically.

  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')
  
-     for reading in Reading.objects.all():
-         reading.station_sensor_link = StationSensorLink.objects.filter(station=reading.station, sensor=reading.sensor)\
+     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()
+             ).first()
-         reading.save()
+             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),
      ]
  
Improve station-sensor link field addition to reading model migration using a paging system to prevent the migration being killed automatically.
## Code Before:
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')

    for reading in Reading.objects.all():
        reading.station_sensor_link = StationSensorLink.objects.filter(station=reading.station, sensor=reading.sensor)\
            .first()
        reading.save()


class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(add_station_sensor_link_to_reading),
    ]

## Instruction:
Improve station-sensor link field addition to reading model migration using a paging system to prevent the migration being killed automatically.
## Code After:
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),
    ]
