diff --git a/app/grandchallenge/algorithms/migrations/0065_create_interfaces_for_jobs.py b/app/grandchallenge/algorithms/migrations/0065_create_interfaces_for_jobs.py index b2af9447d..8cd37d5f3 100644 --- a/app/grandchallenge/algorithms/migrations/0065_create_interfaces_for_jobs.py +++ b/app/grandchallenge/algorithms/migrations/0065_create_interfaces_for_jobs.py @@ -1,18 +1,29 @@ from django.db import migrations +from grandchallenge.algorithms.models import ( + get_existing_interface_for_inputs_and_outputs, +) + def add_algorithm_interfaces_to_jobs(apps, _schema_editor): - Algorithm = apps.get_model("algorithms", "Algorithm") # noqa: N806 + AlgorithmInterface = apps.get_model( # noqa: N806 + "algorithms", "AlgorithmInterface" + ) Job = apps.get_model("algorithms", "Job") # noqa: N806 - for algorithm in Algorithm.objects.prefetch_related("interfaces").all(): - default_interface = algorithm.interfaces.get( - algorithmalgorithminterface__is_default=True + jobs = Job.objects.select_related("algorithm_image__algorithm").all() + for job in jobs: + interface = get_existing_interface_for_inputs_and_outputs( + inputs=job.inputs.all(), outputs=job.outputs.all() ) - jobs = Job.objects.filter(algorithm_image__algorithm=algorithm) - for job in jobs: - job.algorithm_interface = default_interface - jobs.bulk_update(jobs, ["algorithm_interface"]) + if not interface: + interface = AlgorithmInterface.objects.create() + interface.inputs.set(job.inputs.all()) + interface.outputs.set(job.outputs.all()) + + job.algorithm_interface = interface + job.algorithm_image.algorithm.interfaces.add(interface) + jobs.bulk_update(jobs, ["algorithm_interface"]) class Migration(migrations.Migration):