add bulk operations and more logging

This commit is contained in:
FuzzyGrim
2025-06-19 15:10:31 +02:00
parent 11fe0151d2
commit c92e6eda12

View File

@@ -1,39 +1,65 @@
# Generated by Django 5.2 on 2025-05-25 22:01
import django.utils.timezone
from django.db import migrations, models
from itertools import groupby
import logging
logger = logging.getLogger(__name__)
def convert_repeats_to_instances(apps, schema_editor):
Episode = apps.get_model('app', 'Episode')
HistoricalEpisode = apps.get_model('app', 'HistoricalEpisode')
episodes_with_repeats = Episode.objects.filter(repeats__gt=0)
logger.info(f"Found {episodes_with_repeats.count()} episodes with repeats.")
episodes_to_create = []
historical_episodes_to_create = []
episode_ids_to_delete = []
for episode in episodes_with_repeats:
history = HistoricalEpisode.objects.filter(
id=episode.id
).order_by('history_date')
logger.info(f"Processing episode {episode.id} with {len(history)} historical records.")
# Create new episodes for each historical record including the current one
# Prepare new episodes for bulk creation
for historical_record in history:
new_episode = Episode(
item=episode.item,
related_season=episode.related_season,
end_date=historical_record.end_date,
repeats=0
)
new_episode.save()
HistoricalEpisode.objects.create(
id=new_episode.id,
history_date=historical_record.history_date,
history_type='+',
history_user=historical_record.history_user,
item=episode.item,
related_season=episode.related_season,
end_date=historical_record.end_date,
repeats=0,
repeats=0
)
episodes_to_create.append(new_episode)
historical_episodes_to_create.append(
HistoricalEpisode(
id=new_episode.id,
history_date=historical_record.history_date,
history_type='+',
history_user=historical_record.history_user,
end_date=historical_record.end_date,
repeats=0,
)
)
# Delete the original episode
episode.delete()
# Collect IDs for bulk deletion
episode_ids_to_delete.append(episode.id)
# Bulk operations
if episodes_to_create:
logger.info(f"Creating {len(episodes_to_create)} new episode instances.")
Episode.objects.bulk_create(episodes_to_create)
if historical_episodes_to_create:
logger.info(f"Creating {len(historical_episodes_to_create)} historical episode instances.")
HistoricalEpisode.objects.bulk_create(historical_episodes_to_create)
if episode_ids_to_delete:
logger.info(f"Deleting {len(episode_ids_to_delete)} old episode instances.")
Episode.objects.filter(id__in=episode_ids_to_delete).delete()
class Migration(migrations.Migration):