# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-09-26 15:18
from __future__ import unicode_literals

import logging

from django.db import migrations, connection
from django.core.paginator import Paginator

from osf.models.base import generate_object_id

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

def yield_next_id():
    yield generate_object_id()


def add_records_to_files(state, schema):
    FileMetadataSchema = state.get_model('osf', 'filemetadataschema')
    FileMetadataRecord = state.get_model('osf', 'filemetadatarecord')
    OsfStorageFile = state.get_model('osf', 'OsfStorageFile')
    datacite_schema = FileMetadataSchema.objects.get(_id='datacite')

    ids_without_records = list(OsfStorageFile.objects.filter(records__isnull=True).values_list('id', flat=True))
    osf_files_without_records = OsfStorageFile.objects.filter(id__in=ids_without_records).order_by('id')

    total_records_to_create = osf_files_without_records.count()
    logger.info('About to add a FileMetadataRecord for {} files.'.format(total_records_to_create))

    paginated_files = Paginator(osf_files_without_records, 1000)

    total_records_created = 0
    for page_num in paginated_files.page_range:
        records_to_create = []
        for osf_file in paginated_files.page(page_num).object_list:
            records_to_create.append(
                FileMetadataRecord(
                    file=osf_file,
                    schema=datacite_schema
                )
            )
            total_records_created += 1

        FileMetadataRecord.objects.bulk_create(records_to_create)
        logger.info('Created {}/{} FileMetadataRecords'.format(total_records_created, total_records_to_create))


def remove_records_from_files(state, schema):
    FileMetadatRecord = state.get_model('osf', 'filemetadatarecord')
    FileMetadatRecord.objects.all().delete()

def add_records_to_files_sql(state, schema):
    FileMetadataSchema = state.get_model('osf', 'filemetadataschema')
    datacite_schema_id = FileMetadataSchema.objects.filter(_id='datacite').values_list('id', flat=True)[0]
    # TODO -- this is unused but it would be awesome to use this as the output of the SEQUENCE below...
    # id_generator = yield_next_id()
    sql = """
    CREATE TEMPORARY SEQUENCE objectid_seq;
    INSERT INTO osf_filemetadatarecord (created, modified, _id, metadata, id, file_id, schema_id)
    SELECT  NOW(), NOW(), nextval('objectid_seq'), '{}', nextval('osf_filemetadatarecord_id_seq'), OSF_FILE.id, %d
        FROM osf_basefilenode OSF_FILE
            LEFT OUTER JOIN osf_filemetadatarecord ON
                (OSF_FILE.id = osf_filemetadatarecord.file_id)
            WHERE (OSF_FILE.type = 'osf.osfstoragefile'
                   AND OSF_FILE.provider = 'osfstorage'
                   AND osf_filemetadatarecord.id IS NULL
            );
    """ % (datacite_schema_id)

    with connection.cursor() as cursor:
        cursor.execute(sql)


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0133_merge_20180919_1852'),
    ]

    operations = [
        # migrations.RunPython(add_records_to_files, remove_records_from_files),
        migrations.RunPython(add_records_to_files_sql, remove_records_from_files),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-09-26 15:18
from __future__ import unicode_literals

from django.db import migrations, connection


def remove_records_from_files(state, schema):
    FileMetadatRecord = state.get_model('osf', 'filemetadatarecord')
    FileMetadatRecord.objects.all().delete()

def add_records_to_files_sql(state, schema):
    FileMetadataSchema = state.get_model('osf', 'filemetadataschema')
    datacite_schema_id = FileMetadataSchema.objects.filter(_id='datacite').values_list('id', flat=True)[0]
    sql = """
    INSERT INTO osf_filemetadatarecord (created, modified, _id, metadata, id, file_id, schema_id)
    SELECT  NOW(), NOW(), generate_object_id(), '{}', nextval('osf_filemetadatarecord_id_seq'), OSF_FILE.id, %d
        FROM osf_basefilenode OSF_FILE
            LEFT OUTER JOIN osf_filemetadatarecord ON
                (OSF_FILE.id = osf_filemetadatarecord.file_id)
            WHERE (OSF_FILE.type = 'osf.osfstoragefile'
                   AND OSF_FILE.provider = 'osfstorage'
                   AND osf_filemetadatarecord.id IS NULL
            );
    """ % (datacite_schema_id)

    with connection.cursor() as cursor:
        cursor.execute(sql)


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0133_merge_20180919_1852'),
    ]

    operations = [
        migrations.RunPython(add_records_to_files_sql, remove_records_from_files),
    ]

Use generate_object_id SQL function created by wiki migration