# Generated by Django 2.2.16 on 2020-10-05 06:15

import json
from itertools import islice

from django.db import migrations


def move_data_to_remote_relations(apps, schema_editor):
    RemoteRelation = apps.get_model('oauth', 'RemoteRelation')

    def remote_relations_generator(relations):
        for relation in relations:
            relation.account = relation.remoterepository.account
            relation.active = relation.remoterepository.active
            relation.admin = relation.remoterepository.admin
            relation.pub_date = relation.remoterepository.pub_date
            try:
                relation.json = json.loads(relation.remoterepository.json)
            except json.decoder.JSONDecodeError:
                pass

            yield relation

    relations_queryset = RemoteRelation.objects.all().select_related('remoterepository')
    remote_relations = remote_relations_generator(relations_queryset)
    batch_size = 5000

    while True:
        batch = list(islice(remote_relations, batch_size))

        if not batch:
            break
        RemoteRelation.objects.bulk_update(
            batch,
            ['account', 'active', 'admin', 'pub_date', 'json'],
            batch_size
        )


class Migration(migrations.Migration):

    dependencies = [
        ('oauth', '0012_add_fields_to_remote_relation_model'),
    ]

    operations = [
        migrations.RunPython(move_data_to_remote_relations),
    ]

# Generated by Django 2.2.16 on 2020-10-05 06:15

import json
from itertools import islice

from django.db import migrations


def move_data_to_remote_relations(apps, schema_editor):
    RemoteRelation = apps.get_model('oauth', 'RemoteRelation')

    def remote_relations_generator(relations, batch_size):
        for relation in relations.iterator(chunk_size=batch_size):
            relation.account_id = relation.remoterepository.account_id
            relation.active = relation.remoterepository.active
            relation.admin = relation.remoterepository.admin
            relation.pub_date = relation.remoterepository.pub_date
            try:
                relation.json = json.loads(relation.remoterepository.json)
            except json.decoder.JSONDecodeError:
                pass

            yield relation

    batch_size = 5000
    relations_queryset = RemoteRelation.objects.all().select_related('remoterepository')
    remote_relations = remote_relations_generator(relations_queryset, batch_size)

    while True:
        batch = list(islice(remote_relations, batch_size))

        if not batch:
            break
        RemoteRelation.objects.bulk_update(
            batch,
            ['account_id', 'active', 'admin', 'pub_date', 'json'],
            batch_size
        )


class Migration(migrations.Migration):

    dependencies = [
        ('oauth', '0012_add_fields_to_remote_relation_model'),
    ]

    operations = [
        migrations.RunPython(move_data_to_remote_relations),
    ]

Improve data migration performance