Skip to content

Commit

Permalink
Merge pull request #1580 from lucasmarchd01/issue-1368-3
Browse files Browse the repository at this point in the history
Fix `populate_diff_id_fields` management command
  • Loading branch information
lucasmarchd01 authored Aug 7, 2024
2 parents 540cb3e + 390cb57 commit f0e5ce6
Showing 1 changed file with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,42 @@
from django.core.management.base import BaseCommand
from django.db.models import Q
from typing import Optional
from django.db import transaction


class Command(BaseCommand):
@transaction.atomic
def handle(self, *args, **kwargs):
CHUNK_SIZE = 500
chants = Chant.objects.filter(
Q(differentiae_database__isnull=False) & Q(diff_db__isnull=True)
)
chants_count = chants.count()
start_index = 0
).iterator(chunk_size=500)
chants_total = Chant.objects.filter(
Q(differentiae_database__isnull=False) & Q(diff_db__isnull=True)
).count()
count = 0
while start_index <= chants_count:
self.stdout.write(f"processing chunk with {start_index=}")
chunk = chants[start_index : start_index + CHUNK_SIZE]

for chant in chunk:
try:
differentia_id: Optional[str] = chant.differentiae_database
differentia = Differentia.objects.get(differentia_id=differentia_id)
if differentia:
chant.diff_db = differentia
else:
# If the Differentia doesn't exist, create a new one
differentia = Differentia(
differentia_id=differentia_id,
)
differentia.save()
chant.diff_db = differentia
chant.save()
except Differentia.DoesNotExist:
print(f"Differentia not found for chant: {chant}")
count += 1
if count % 100 == 0:
print(
f"------------------ {count} of {chants_count} chants updated ------------------"
for chant in chants:
try:
differentia_id: Optional[str] = chant.differentiae_database
differentia = Differentia.objects.get(differentia_id=differentia_id)
except Differentia.DoesNotExist:
# If the Differentia doesn't exist, create a new one
differentia = Differentia(differentia_id=differentia_id)
differentia.save()
self.stdout.write(
self.style.WARNING(f"Differentia created for chant: {chant}")
)

chant.diff_db = differentia
chant.save()

count += 1
if count % 100 == 0:
self.stdout.write(
self.style.SUCCESS(
f"------------------ {count} of {chants_total} chants updated ------------------"
)
del chunk # make sure we don't use too much RAM
start_index += CHUNK_SIZE
)

self.stdout.write(
self.style.SUCCESS("Success! Command has run to completion.\n")
Expand Down

0 comments on commit f0e5ce6

Please sign in to comment.