# Generated by Django 2.2.11 on 2020-03-09 15:50
import json

from django.db import migrations, models


def data_migration(apps, schema_editor):
    MLPProcessor = apps.get_model('mlp', 'MLPProcessor')
    for mlp in MLPProcessor.objects.all():
        indices = list(mlp.indices)
        mlp.keeper = json.dumps(indices)
        mlp.save()


def final_solution(apps, schema_editor):
    MLPProcessor = apps.get_model('mlp', 'MLPProcessor')
    Index = apps.get_model('elastic', 'Index')

    for mlp in MLPProcessor.objects.all():
        indices = json.loads(mlp.keeper)
        for index_name in indices:
            i, is_created = Index.objects.get_or_create(name=index_name)
            mlp.indices.add(i)
        mlp.save()



class Migration(migrations.Migration):

    dependencies = [
        ('mlp', '0001_initial'),
    ]


    operations = [
        migrations.AddField(
            model_name='mlpprocessor',
            name='keeper',
            field=models.TextField(default="[]"),
        ),

        migrations.RunPython(data_migration),

        migrations.RemoveField(
            model_name='mlpprocessor',
            name='indices',
        ),
        migrations.AddField(
            model_name='mlpprocessor',
            name='indices',
            field=models.ManyToManyField(default=None, to='elastic.Index'),
        ),

        migrations.RunPython(final_solution),

        migrations.RemoveField(
            model_name='mlpprocessor',
            name='keeper',
        ),
    ]
