Files
linkding/bookmarks/migrations/0047_populate_url_normalized_field.py
Sascha Ißbrücker 723b843c13 Normalize URLs when checking for duplicates (#1169)
* Normalize URLs when checking for duplicates

* Improve migration script
2025-08-22 19:37:28 +02:00

39 lines
1.1 KiB
Python

# 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,
),
]