Skip to content

Commit b3d2020

Browse files
committed
Replicate JobResults to new Job model
1 parent 34236ff commit b3d2020

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

netbox/core/migrations/0003_move_jobresult_to_core.py renamed to netbox/core/migrations/0003_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Migration(migrations.Migration):
2222
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
2323
('object_id', models.PositiveBigIntegerField(blank=True, null=True)),
2424
('name', models.CharField(max_length=200)),
25-
('created', models.DateTimeField(auto_now_add=True)),
25+
('created', models.DateTimeField()),
2626
('scheduled', models.DateTimeField(blank=True, null=True)),
2727
('interval', models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1)])),
2828
('started', models.DateTimeField(blank=True, null=True)),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from django.db import migrations
2+
3+
4+
def replicate_jobresults(apps, schema_editor):
5+
"""
6+
Replicate existing JobResults to the new Jobs table before deleting the old JobResults table.
7+
"""
8+
Job = apps.get_model('core', 'Job')
9+
JobResult = apps.get_model('extras', 'JobResult')
10+
11+
jobs = []
12+
for job_result in JobResult.objects.iterator(chunk_size=100):
13+
jobs.append(
14+
Job(
15+
object_type=job_result.obj_type,
16+
name=job_result.name,
17+
created=job_result.created,
18+
scheduled=job_result.scheduled,
19+
interval=job_result.interval,
20+
started=job_result.started,
21+
completed=job_result.completed,
22+
user=job_result.user,
23+
status=job_result.status,
24+
data=job_result.data,
25+
job_id=job_result.job_id,
26+
)
27+
)
28+
if len(jobs) == 100:
29+
Job.objects.bulk_create(jobs)
30+
if jobs:
31+
Job.objects.bulk_create(jobs)
32+
33+
34+
class Migration(migrations.Migration):
35+
36+
dependencies = [
37+
('core', '0003_job'),
38+
]
39+
40+
operations = [
41+
migrations.RunPython(
42+
code=replicate_jobresults,
43+
reverse_code=migrations.RunPython.noop
44+
),
45+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.1.7 on 2023-03-27 17:28
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('core', '0004_replicate_jobresults'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='job',
15+
name='created',
16+
field=models.DateTimeField(auto_now_add=True),
17+
),
18+
]

0 commit comments

Comments
 (0)