# Generated by Django 3.2.15 on 2022-12-16 15:12
import json

from django.conf import settings
from django.db import migrations

from buildinfo.utils import get_datetime


def restore_timestamps(apps, schema_editor):
    BuildInfo = apps.get_model("buildinfo", "BuildInfo")
    for info in BuildInfo.objects.all():
        build_path = settings.JBI_BASEDIR.joinpath(
            info.jobname, str(info.buildnumber), "build.json"
        )
        if build_path.exists():
            with build_path.open("r") as f:
                build_info = json.load(f)
            info.datetime = get_datetime(build_info["timestamp"])
            info.save()


class Migration(migrations.Migration):

    dependencies = [
        ("buildinfo", "0004_buildinfo_datetime"),
    ]

    operations = [
        migrations.RunPython(restore_timestamps),
    ]
