<reponame>gravitationalwavedc/gwcloud_job_server<gh_stars>0
# Generated by Django 3.0.4 on 2020-05-17 22:34

from django.db import migrations, models
from django.utils import timezone


def migrate_job_completion(apps, schema_editor):
    Job = apps.get_model("jobserver", "Job")
    JobHistory = apps.get_model("jobserver", "JobHistory")

    # Iterate over all jobs
    for job in Job.objects.all():
        # Check if there are any _job_completion_ job history instances for this job
        histories = JobHistory.objects.filter(job=job)
        if histories.filter(what='_job_completion_').exists():
            # Nothing more to do for this job
            continue

        job_error = False
        # Check that all job steps exist with a success value
        job_steps = histories.values_list('what').distinct()
        for step in job_steps:
            if not histories.filter(what=step, state=500).exists():
                job_error = True

        if job_error:
            JobHistory.objects.create(
                job=job,
                what="_job_completion_",
                state=400,  # ERROR
                timestamp=timezone.now(),
                details="Error state migrated forwards"
            )
        else:
            JobHistory.objects.create(
                job=job,
                what="_job_completion_",
                state=500,  # ERROR
                timestamp=timezone.now(),
                details="Success state migrated forwards"
            )


class Migration(migrations.Migration):
    dependencies = [
        ('jobserver', '0012_auto_20200517_2234'),
    ]

    operations = [
        migrations.RunPython(migrate_job_completion),
    ]

# Generated by Django 3.0.4 on 2020-05-17 22:34

from django.db import migrations, models
from django.utils import timezone


def migrate_job_completion(apps, schema_editor):
    Job = apps.get_model("jobserver", "Job")
    JobHistory = apps.get_model("jobserver", "JobHistory")

    # Iterate over all jobs
    for job in Job.objects.all():
        # Check if there are any _job_completion_ job history instances for this job
        histories = JobHistory.objects.filter(job=job)
        if histories.filter(what='_job_completion_').exists():
            # Nothing more to do for this job
            continue

        job_error = False
        # Check that all job steps exist with a success value
        job_steps = histories.values_list('what').distinct()
        for step in job_steps:
            if not histories.filter(what=step, state=500).exists():
                job_error = True

        if job_error:
            JobHistory.objects.create(
                job=job,
                what="_job_completion_",
                state=400,  # ERROR
                timestamp=timezone.now(),
                details="Error state migrated forwards"
            )
        else:
            JobHistory.objects.create(
                job=job,
                what="_job_completion_",
                state=500,  # ERROR
                timestamp=timezone.now(),
                details="Success state migrated forwards"
            )


class Migration(migrations.Migration):
    dependencies = [
        ('jobserver', '0012_auto_20200517_2234'),
    ]

    operations = [
        migrations.RunPython(migrate_job_completion),
    ]
# Generated by Django 3.0.4 on 2020-05-17 22:34 # Iterate over all jobs # Check if there are any _job_completion_ job history instances for this job # Nothing more to do for this job # Check that all job steps exist with a success value # ERROR # ERROR