text: # Generated by Django 2.1 on 2018-10-10 16:11
  # from json import dump
  
  from django.db import migrations, models
  
  TMP_FILE = '/tmp/migrate_ip.json'
  
  DUMP = []
  
  
  def backup_info(apps, _):
      Abon = apps.get_model('abonapp', 'Abon')
      abons = Abon.objects.annotate(
          addr_count=models.Count('ip_addresses')
      ).filter(addr_count__gt=0).only('ip_addresses').iterator()
      global DUMP
      for abon in abons:
          ip_addr = abon.ip_addresses.first()
          DUMP.append({
              'pk': abon.pk,
              'addr': ip_addr.ip
          })
      # with open(TMP_FILE, 'w') as f:
      #    dump(r, f, indent=2)
  
  
  def restore_ips(apps, _):
      Abon = apps.get_model('abonapp', 'Abon')
      for abon in DUMP:
          Abon.objects.filter(pk=abon.get('pk')).update(ip_address=abon.get('addr'))
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('abonapp', '0005_current_tariff'),
      ]
  
      operations = [
          migrations.RunPython(backup_info),
          migrations.RemoveField(
              model_name='abon',
              name='ip_addresses',
          ),
          migrations.AddField(
              model_name='abon',
              name='ip_address',
              field=models.GenericIPAddressField(blank=True, null=True, unique=True, verbose_name='Ip address'),
          ),
          migrations.RunPython(restore_ips)
      ]
  