# Generated by Django 5.2.3 on 2025-08-22 08:28

from django.db import migrations, transaction
from bookmarks.utils import normalize_url


def populate_url_normalized(apps, schema_editor):
    Bookmark = apps.get_model("bookmarks", "Bookmark")

    batch_size = 500
    with transaction.atomic():
        qs = Bookmark.objects.all()
        for start in range(0, qs.count(), batch_size):
            batch = list(qs[start : start + batch_size])
            for bookmark in batch:
                bookmark.url_normalized = normalize_url(bookmark.url)
            Bookmark.objects.bulk_update(
                batch, ["url_normalized"], batch_size=batch_size
            )


def reverse_populate_url_normalized(apps, schema_editor):
    Bookmark = apps.get_model("bookmarks", "Bookmark")
    Bookmark.objects.all().update(url_normalized="")


class Migration(migrations.Migration):

    dependencies = [
        ("bookmarks", "0046_add_url_normalized_field"),
    ]

    operations = [
        migrations.RunPython(
            populate_url_normalized,
            reverse_populate_url_normalized,
        ),
    ]
