# Generated by Django 5.1.5 on 2025-01-30 11:57

from django.db import migrations, models
from django.db.models import Count


def initialize_num_purchases(apps, schema_editor):
    Purchase = apps.get_model("games", "Purchase")
    purchases = Purchase.objects.annotate(num_games=Count("games"))

    for purchase in purchases:
        purchase.num_purchases = purchase.num_games
        purchase.save(update_fields=["num_purchases"])


class Migration(migrations.Migration):
    dependencies = [
        ("games", "0003_purchase_updated_at"),
    ]

    operations = [
        migrations.AddField(
            model_name="purchase",
            name="num_purchases",
            field=models.IntegerField(default=0),
        ),
        migrations.RunPython(initialize_num_purchases),
    ]
