# Generated by Django 2.1 on 2019-11-20 17:54

import base.fields
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion


def copy_reference_points(apps, schema_editor):
    """Copy any reference point into the new Parameter model.

    """
    ReferencePoint = apps.get_model("targets", "ReferencePoint")
    Parameter = apps.get_model("targets", "Parameter")
    for reference_point in ReferencePoint.objects.all():
        Parameter.objects.update_or_create(
            target=reference_point.target,
            canonical_name=reference_point.canonical_name,
            defaults={
                "name": reference_point.name,
                "value": reference_point.coords,
            },
        )


class Migration(migrations.Migration):

    dependencies = [
        ('targets', '0029_auto_20191022_1856'),
    ]

    operations = [
        migrations.CreateModel(
            name='Parameter',
            fields=[
                ('id', models.CharField(default=base.fields.generate_id, editable=False, max_length=22, primary_key=True, serialize=False)),
                ('name', models.CharField(blank=True, max_length=510, null=True)),
                ('canonical_name', models.SlugField(max_length=510)),
                ('value', django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True)),
                ('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='parameters', to='targets.Target')),
            ],
            options={
                'ordering': ['target', 'canonical_name'],
            },
        ),
        migrations.RunPython(copy_reference_points, migrations.RunPython.noop),
        migrations.AlterUniqueTogether(
            name='referencepoint',
            unique_together=set(),
        ),
        migrations.RemoveField(
            model_name='referencepoint',
            name='target',
        ),
        migrations.DeleteModel(
            name='ReferencePoint',
        ),
        migrations.AlterUniqueTogether(
            name='parameter',
            unique_together={('canonical_name', 'target')},
        ),
    ]
