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, + ), ]