# -*- coding: utf-8 -*-
# Generated by Django 1.9.12 on 2017-03-21 12:15
from __future__ import unicode_literals

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

CURRENT_EMR_RELEASES = ("5.2.1", "5.0.0")


def convert_emr_releases(apps, schema_editor):
    EMRRelease = apps.get_model("clusters", "EMRRelease")
    SparkJob = apps.get_model("jobs", "SparkJob")

    for spark_job in SparkJob.objects.all():
        emr_release, created = EMRRelease.objects.get_or_create(
            version=spark_job.emr_release_version,
            defaults={
                "changelog_url": "https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-%s/emr-release-components.html"
                % spark_job.emr_release_version,
                "is_deprecated": spark_job.emr_release_version
                not in CURRENT_EMR_RELEASES,
            },
        )
        spark_job.emr_release = emr_release
        spark_job.save()


def revert_emr_releases(apps, schema_editor):
    EMRRelease = apps.get_model("clusters", "EMRRelease")
    SparkJob = apps.get_model("jobs", "SparkJob")
    for spark_job in SparkJob.objects.all():
        spark_job.emr_release = None
        spark_job.save()


class Migration(migrations.Migration):

    dependencies = [("jobs", "0018_rename_add_spark_job_emr_release")]

    operations = [migrations.RunPython(convert_emr_releases, revert_emr_releases)]
