Normalize URLs when checking for duplicates (#1169)

* Normalize URLs when checking for duplicates

* Improve migration script
This commit is contained in:
Sascha Ißbrücker
2025-08-22 19:37:28 +02:00
committed by GitHub
parent 96176ba50e
commit 723b843c13
11 changed files with 381 additions and 5 deletions

View File

@@ -0,0 +1,38 @@
# 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,
),
]