Skip to content
Snippets Groups Projects
Commit f3fc6987 authored by Manon Blanco's avatar Manon Blanco
Browse files

Use select_related

parent 29d00c73
No related branches found
No related tags found
No related merge requests found
# Generated by Django 3.2.5 on 2021-11-04 08:14
from collections import defaultdict
from django.db import migrations
from arkindex.dataimport.utils import hash_object
......@@ -9,16 +11,21 @@ def use_worker_configuration(apps, schema_editor):
WorkerRun = apps.get_model('dataimport', 'WorkerRun')
WorkerConfiguration = apps.get_model('dataimport', 'WorkerConfiguration')
for index, worker_run in enumerate(WorkerRun.objects.filter(configuration__isnull=True).exclude(old_configuration={})):
indexes = defaultdict(int)
for worker_run in WorkerRun.objects.filter(configuration__isnull=True) \
.exclude(old_configuration={}) \
.select_related('version__worker'):
worker = worker_run.version.worker
worker_configuration, _ = WorkerConfiguration.objects.get_or_create(
worker_configuration, created = WorkerConfiguration.objects.get_or_create(
worker=worker,
configuration=worker_run.old_configuration,
configuration_hash=hash_object(worker_run.old_configuration),
defaults={
'name': f'config n°{index} - {worker.name}',
'configuration_hash': hash_object(worker_run.old_configuration)
'name': f'config for {worker.name} {indexes[worker.id]+1}',
'configuration': worker_run.old_configuration
}
)
indexes[worker.id] += created
worker_run.configuration = worker_configuration
worker_run.save()
......@@ -26,7 +33,7 @@ def use_worker_configuration(apps, schema_editor):
def use_old_configuration(apps, schema_editor):
WorkerRun = apps.get_model('dataimport', 'WorkerRun')
worker_runs = WorkerRun.objects.filter(configuration__isnull=False)
worker_runs = WorkerRun.objects.filter(configuration__isnull=False).select_related('configuration')
for worker_run in worker_runs:
worker_run.old_configuration = worker_run.configuration.configuration
WorkerRun.objects.bulk_update(worker_runs, ['old_configuration'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment