# Generated by Django 2.0.8 on 2018-10-15 22:54

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


def denormalize_study_id(apps, schema_editor):
    """Copies study ID to child objects for easier permissions checking."""
    # load migration models
    Assay = apps.get_model("main", "Assay")
    Measurement = apps.get_model("main", "Measurement")
    MeasurementValue = apps.get_model("main", "MeasurementValue")
    # update models with parent study_id
    sub_qs = Assay.objects.filter(pk=OuterRef("pk"))
    Assay.objects.update(study_id=Subquery(sub_qs.values("line__study_id")[:1]))
    sub_qs = Measurement.objects.filter(pk=OuterRef("pk"))
    Measurement.objects.update(study_id=Subquery(sub_qs.values("assay__study_id")[:1]))
    sub_qs = MeasurementValue.objects.filter(pk=OuterRef("pk"))
    MeasurementValue.objects.update(
        study_id=Subquery(sub_qs.values("measurement__study_id")[:1])
    )


class Migration(migrations.Migration):

    dependencies = [("main", "0013_remove-hstore")]

    operations = [
        migrations.AddField(
            model_name="assay",
            name="study",
            field=models.ForeignKey(
                blank=True,
                help_text="The Study containing this Assay.",
                null=True,
                on_delete=deletion.CASCADE,
                to="main.Study",
                verbose_name="Study",
            ),
        ),
        migrations.AddField(
            model_name="measurement",
            name="study",
            field=models.ForeignKey(
                blank=True,
                help_text="The Study containing this Measurement.",
                null=True,
                on_delete=deletion.CASCADE,
                to="main.Study",
                verbose_name="Study",
            ),
        ),
        migrations.AddField(
            model_name="measurementvalue",
            name="study",
            field=models.ForeignKey(
                blank=True,
                help_text="The Study containing this Value.",
                null=True,
                on_delete=deletion.CASCADE,
                to="main.Study",
                verbose_name="Study",
            ),
        ),
        migrations.RunPython(
            code=denormalize_study_id, reverse_code=migrations.RunPython.noop
        ),
        migrations.AlterField(
            model_name="assay",
            name="study",
            field=models.ForeignKey(
                help_text="The Study containing this Assay.",
                on_delete=deletion.CASCADE,
                to="main.Study",
                verbose_name="Study",
            ),
        ),
        migrations.AlterField(
            model_name="measurement",
            name="study",
            field=models.ForeignKey(
                help_text="The Study containing this Measurement.",
                on_delete=deletion.CASCADE,
                to="main.Study",
                verbose_name="Study",
            ),
        ),
        migrations.AlterField(
            model_name="measurementvalue",
            name="study",
            field=models.ForeignKey(
                help_text="The Study containing this Value.",
                on_delete=deletion.CASCADE,
                to="main.Study",
                verbose_name="Study",
            ),
        ),
    ]
