# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-11-13 16:14
from __future__ import unicode_literals

from django.db import migrations, models, connection
from django.contrib.contenttypes.models import ContentType


def set_basefilenode_target(apps, schema_editor):
    BaseFileNode = apps.get_model('osf', 'basefilenode')
    AbstractNode = apps.get_model('osf', 'abstractnode')
    target_content_type_id = ContentType.objects.get_for_model(AbstractNode).id

    BATCHSIZE = 5000

    max_pk = BaseFileNode.objects.aggregate(models.Max('pk'))['pk__max']
    if max_pk is not None:
        for offset in range(0, max_pk + 1, BATCHSIZE):
            (BaseFileNode.objects
                .filter(pk__gte=offset)
                .filter(pk__lt=offset + BATCHSIZE)
                .filter(target_object_id__isnull=True)
                .filter(target_content_type_id__isnull=True)
                .update(
                    target_content_type_id=target_content_type_id,
                    target_object_id=models.F('node_id')
                )
            )


def reset_basefilenode_target_to_node(*args, **kwargs):
    sql = "UPDATE osf_basefilenode SET node_id = target_object_id;"
    with connection.cursor() as cursor:
        cursor.execute(sql)

class Migration(migrations.Migration):

    # Avoid locking basefilenode
    atomic = False

    dependencies = [
        ('osf', '0083_add_file_fields_for_target'),
    ]

    operations = [
        migrations.RunPython(set_basefilenode_target, reset_basefilenode_target_to_node),
    ]

@@ -105,16 +105,31 @@
 literals
+%0Aimport logging
 %0A%0Afrom d
@@ -235,16 +235,54 @@
 ntType%0A%0A
+logger = logging.getLogger(__name__)%0A%0A
 %0Adef set
@@ -539,9 +539,10 @@
 E = 
-5
+10
 000%0A
@@ -710,16 +710,33 @@
        (
+%0A                
 BaseFile
@@ -1047,16 +1047,16 @@
 ype_id,%0A
-
         
@@ -1136,16 +1136,229 @@
       )%0A
+            logger.info(%0A                'Updated osf_basefilenode %7B%7D-%7B%7D/%7B%7D'.format(%0A                    offset,%0A                    offset + BATCHSIZE,%0A                    max_pk,%0A                )%0A            )%0A
 %0A%0Adef re

Increase batch size and add logging