From 09cd3f2dfdce22ae486543e4052dd2e2df5b0a36 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 5 Aug 2026 14:49:50 -0400 Subject: [PATCH] Populate execution_time for existing jobs --- .../migrations/0025_add_job_execution_time.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/netbox/core/migrations/0025_add_job_execution_time.py b/netbox/core/migrations/0025_add_job_execution_time.py index f8c58d9ed..baf4be931 100644 --- a/netbox/core/migrations/0025_add_job_execution_time.py +++ b/netbox/core/migrations/0025_add_job_execution_time.py @@ -1,4 +1,15 @@ from django.db import migrations, models +from django.db.models import DurationField, ExpressionWrapper, F + + +def populate_execution_time(apps, schema_editor): + """ + Populate execution_time for existing jobs which have both a start and completion time recorded. + """ + Job = apps.get_model("core", "Job") + Job.objects.filter(started__isnull=False, completed__isnull=False).update( + execution_time=ExpressionWrapper(F("completed") - F("started"), output_field=DurationField()) + ) class Migration(migrations.Migration): @@ -13,4 +24,8 @@ class Migration(migrations.Migration): name="execution_time", field=models.DurationField(blank=True, editable=False, null=True), ), + migrations.RunPython( + code=populate_execution_time, + reverse_code=migrations.RunPython.noop, + ), ]