
from django.db import migrations


def update_versions(versions, current_key, new_key):
    for version in versions:
        for result in version["candidate_results"]:
            try:
                result[new_key] = result.pop(current_key)
            except KeyError:
                continue


def forwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.all():
        update_versions(
            versions=result.versions, current_key="is_winner", new_key="elected"
        )
        result.save()


def backwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.all():
        update_versions(
            versions=result.versions, current_key="elected", new_key="is_winner"
        )
        result.save()


class Migration(migrations.Migration):

    dependencies = [("uk_results", "0053_auto_20210928_1007")]

    operations = [migrations.RunPython(code=forwards, reverse_code=backwards)]


from django.db import migrations


def update_versions(versions, current_key, new_key):
    for version in versions:
        for result in version["candidate_results"]:
            try:
                result[new_key] = result.pop(current_key)
            except KeyError:
                continue


def forwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.all().iterator():
        update_versions(
            versions=result.versions, current_key="is_winner", new_key="elected"
        )
        result.save()


def backwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.iterator():
        update_versions(
            versions=result.versions, current_key="elected", new_key="is_winner"
        )
        result.save()


class Migration(migrations.Migration):

    dependencies = [("uk_results", "0053_auto_20210928_1007")]

    operations = [migrations.RunPython(code=forwards, reverse_code=backwards)]

Use iterator in resultset data migration
Use iterator in resultset data migration

  
  from django.db import migrations
  
  
  def update_versions(versions, current_key, new_key):
      for version in versions:
          for result in version["candidate_results"]:
              try:
                  result[new_key] = result.pop(current_key)
              except KeyError:
                  continue
  
  
  def forwards(apps, schema_editor):
      ResultSet = apps.get_model("uk_results", "ResultSet")
-     for result in ResultSet.objects.all():
+     for result in ResultSet.objects.all().iterator():
          update_versions(
              versions=result.versions, current_key="is_winner", new_key="elected"
          )
          result.save()
  
  
  def backwards(apps, schema_editor):
      ResultSet = apps.get_model("uk_results", "ResultSet")
-     for result in ResultSet.objects.all():
+     for result in ResultSet.objects.iterator():
          update_versions(
              versions=result.versions, current_key="elected", new_key="is_winner"
          )
          result.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [("uk_results", "0053_auto_20210928_1007")]
  
      operations = [migrations.RunPython(code=forwards, reverse_code=backwards)]
  
Use iterator in resultset data migration
## Code Before:

from django.db import migrations


def update_versions(versions, current_key, new_key):
    for version in versions:
        for result in version["candidate_results"]:
            try:
                result[new_key] = result.pop(current_key)
            except KeyError:
                continue


def forwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.all():
        update_versions(
            versions=result.versions, current_key="is_winner", new_key="elected"
        )
        result.save()


def backwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.all():
        update_versions(
            versions=result.versions, current_key="elected", new_key="is_winner"
        )
        result.save()


class Migration(migrations.Migration):

    dependencies = [("uk_results", "0053_auto_20210928_1007")]

    operations = [migrations.RunPython(code=forwards, reverse_code=backwards)]

## Instruction:
Use iterator in resultset data migration
## Code After:

from django.db import migrations


def update_versions(versions, current_key, new_key):
    for version in versions:
        for result in version["candidate_results"]:
            try:
                result[new_key] = result.pop(current_key)
            except KeyError:
                continue


def forwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.all().iterator():
        update_versions(
            versions=result.versions, current_key="is_winner", new_key="elected"
        )
        result.save()


def backwards(apps, schema_editor):
    ResultSet = apps.get_model("uk_results", "ResultSet")
    for result in ResultSet.objects.iterator():
        update_versions(
            versions=result.versions, current_key="elected", new_key="is_winner"
        )
        result.save()


class Migration(migrations.Migration):

    dependencies = [("uk_results", "0053_auto_20210928_1007")]

    operations = [migrations.RunPython(code=forwards, reverse_code=backwards)]
