# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-11-15 22:50
from __future__ import unicode_literals

from django.db import migrations

from ..db.fields import JSONField


class Migration(migrations.Migration):

    dependencies = [
        ('onfido', '0009_add_is_clear_field'),
    ]

    operations = [
        migrations.AlterField(
            model_name='applicant',
            name='raw',
            field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
        ),
        migrations.AlterField(
            model_name='check',
            name='raw',
            field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
        ),
        migrations.AlterField(
            model_name='report',
            name='raw',
            field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
        ),
        migrations.AlterField(
            model_name='event',
            name='raw',
            field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
        ),
    ]

# Below is an example of a full parallel conversion that creates a new field
# copies across the data from the old field, drops the old field and renames
# the new field to the original name. Only required if for some reason a
# straight ALTER statement won't work on existing data. For reference, the
# SQL is: ALTER TABLE "onfido_report" ALTER COLUMN "raw" TYPE jsonb USING "raw"::jsonb;


# def migrate_old_to_new(klass):
#     """Copy the JSON in the TextField across to the new JSONField."""
#     logger.info("Converting {}".format(klass))
#     for obj in klass.objects.order_by('-id'):
#         obj.new_raw = obj.raw
#         logger.info("Saving {}.{}".format(klass, obj.id))
#         obj.save(update_fields=['new_raw'])


# def migrate_applicants(apps, schema_editor):
#     migrate_old_to_new(apps.get_model("onfido", "Applicant"))


# def migrate_checks(apps, schema_editor):
#     migrate_old_to_new(apps.get_model("onfido", "Check"))


# def migrate_reports(apps, schema_editor):
#     migrate_old_to_new(apps.get_model("onfido", "Report"))


# def migrate_events(apps, schema_editor):
#     migrate_old_to_new(apps.get_model("onfido", "Event"))


# class Migration(migrations.Migration):

#     dependencies = [
#         ('onfido', '0009_add_is_clear_field'),
#     ]

#     operations = [
#         # create the new fields
#         migrations.AddField(
#             model_name='applicant',
#             name='new_raw',
#             field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
#         ),
#         migrations.AddField(
#             model_name='check',
#             name='new_raw',
#             field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
#         ),
#         migrations.AddField(
#             model_name='report',
#             name='new_raw',
#             field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
#         ),
#         migrations.AddField(
#             model_name='event',
#             name='new_raw',
#             field=JSONField(blank=True, help_text='The raw JSON returned from the API.', null=True),
#         ),
#         # 2. migrate the data across from the old fields
#         migrations.RunPython(migrate_applicants),
#         migrations.RunPython(migrate_checks),
#         migrations.RunPython(migrate_reports),
#         migrations.RunPython(migrate_events),
#         # 3. remove the old fields
#         migrations.RemoveField(model_name='applicant', name='raw'),
#         migrations.RemoveField(model_name='check', name='raw'),
#         migrations.RemoveField(model_name='report', name='raw'),
#         migrations.RemoveField(model_name='event', name='raw'),
#         # 4. rename the new field to the same name as the old field
#         migrations.RenameField(model_name='applicant', old_name='new_raw', new_name='raw'),
#         migrations.RenameField(model_name='check', old_name='new_raw', new_name='raw'),
#         migrations.RenameField(model_name='report', old_name='new_raw', new_name='raw'),
#         migrations.RenameField(model_name='event', old_name='new_raw', new_name='raw'),
#     ]

