# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2018-03-12 21:00


from django.db import migrations, models
import django.db.models.deletion


def add_mupit_structures(apps, schema_editor):
    MupitStructure = apps.get_model("data", "MupitStructure")
    MupitStructure.objects.create(name="1t15")
    MupitStructure.objects.create(name="1jm7")
    MupitStructure.objects.create(name="4igk")
    MupitStructure.objects.create(name="fENSP00000380152_7")


class Migration(migrations.Migration):

    dependencies = [
        ('data', '0027_reportdiff'),
    ]

    operations = [
        migrations.CreateModel(
            name='MupitStructure',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.TextField()),
            ],
        ),
        migrations.RunPython(add_mupit_structures),
        migrations.AddField(
            model_name='variant',
            name='Mupit_Structure',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='data.MupitStructure'),
        ),
        migrations.RunSQL(
            """
            DROP MATERIALIZED VIEW IF EXISTS currentvariant;
            CREATE MATERIALIZED VIEW currentvariant AS (
                SELECT * FROM "variant" WHERE (
                    "id" IN ( SELECT DISTINCT ON ("Genomic_Coordinate_hg38") "id" FROM "variant" ORDER BY "Genomic_Coordinate_hg38" ASC, "Data_Release_id" DESC )
                )
            );
            """
        ),
    ]
