Просмотр исходного кода

Replicate JobResults to new Job model

jeremystretch 2 лет назад
Родитель
Сommit
b3d2020045

+ 1 - 1
netbox/core/migrations/0003_move_jobresult_to_core.py → netbox/core/migrations/0003_job.py

@@ -22,7 +22,7 @@ class Migration(migrations.Migration):
                 ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
                 ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
                 ('object_id', models.PositiveBigIntegerField(blank=True, null=True)),
                 ('object_id', models.PositiveBigIntegerField(blank=True, null=True)),
                 ('name', models.CharField(max_length=200)),
                 ('name', models.CharField(max_length=200)),
-                ('created', models.DateTimeField(auto_now_add=True)),
+                ('created', models.DateTimeField()),
                 ('scheduled', models.DateTimeField(blank=True, null=True)),
                 ('scheduled', models.DateTimeField(blank=True, null=True)),
                 ('interval', models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1)])),
                 ('interval', models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1)])),
                 ('started', models.DateTimeField(blank=True, null=True)),
                 ('started', models.DateTimeField(blank=True, null=True)),

+ 45 - 0
netbox/core/migrations/0004_replicate_jobresults.py

@@ -0,0 +1,45 @@
+from django.db import migrations
+
+
+def replicate_jobresults(apps, schema_editor):
+    """
+    Replicate existing JobResults to the new Jobs table before deleting the old JobResults table.
+    """
+    Job = apps.get_model('core', 'Job')
+    JobResult = apps.get_model('extras', 'JobResult')
+
+    jobs = []
+    for job_result in JobResult.objects.iterator(chunk_size=100):
+        jobs.append(
+            Job(
+                object_type=job_result.obj_type,
+                name=job_result.name,
+                created=job_result.created,
+                scheduled=job_result.scheduled,
+                interval=job_result.interval,
+                started=job_result.started,
+                completed=job_result.completed,
+                user=job_result.user,
+                status=job_result.status,
+                data=job_result.data,
+                job_id=job_result.job_id,
+            )
+        )
+        if len(jobs) == 100:
+            Job.objects.bulk_create(jobs)
+    if jobs:
+        Job.objects.bulk_create(jobs)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('core', '0003_job'),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            code=replicate_jobresults,
+            reverse_code=migrations.RunPython.noop
+        ),
+    ]

+ 18 - 0
netbox/core/migrations/0005_job_created_auto_now.py

@@ -0,0 +1,18 @@
+# Generated by Django 4.1.7 on 2023-03-27 17:28
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('core', '0004_replicate_jobresults'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='job',
+            name='created',
+            field=models.DateTimeField(auto_now_add=True),
+        ),
+    ]