From 62396c02d75fcc655cb1e471f65632bbf9a622e8 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Fri, 22 May 2026 16:33:48 +0200 Subject: [PATCH] . --- .../management/commands/reindex_orders.py | 72 ++++++------------- src/pretix/base/models/orders.py | 4 +- src/pretix/base/models/search.py | 33 ++++++--- src/pretix/helpers/migration_utils.py | 4 +- 4 files changed, 50 insertions(+), 63 deletions(-) diff --git a/src/pretix/base/management/commands/reindex_orders.py b/src/pretix/base/management/commands/reindex_orders.py index 25a175709..19f5b1f48 100644 --- a/src/pretix/base/management/commands/reindex_orders.py +++ b/src/pretix/base/management/commands/reindex_orders.py @@ -22,12 +22,12 @@ import time from django.core.management.base import BaseCommand -from django.db.models import F, Max, Q -from django.utils.timezone import now from django_scopes import scopes_disabled from tqdm import tqdm -from pretix.base.models import Order +from pretix.base.models import ( + Invoice, InvoiceAddress, Order, OrderPayment, OrderPosition, OrderRefund, +) class Command(BaseCommand): @@ -45,51 +45,25 @@ class Command(BaseCommand): @scopes_disabled() def handle(self, *args, **options): - t = 0 - qs = Order.objects.annotate( - last_transaction=Max('transactions__created') - ).filter( - Q(last_transaction__isnull=True) | Q(last_modified__gt=F('last_transaction')), - require_approval=False, - ).prefetch_related( - 'all_positions', 'all_fees' - ).order_by( - 'pk' - ) - last_pk = 0 - with tqdm(total=qs.count()) as pbar: - while True: - batch = list(qs.filter(pk__gt=last_pk)[:5000]) - if not batch: - break + querysets = [ + Order.objects.all(), + OrderPosition.all.all(), + OrderPayment.objects.all(), + OrderRefund.objects.all(), + InvoiceAddress.objects.filter(order__isnull=False), + Invoice.objects.all(), + ] - for o in batch: - if o.last_transaction is None: - tn = o.create_transactions( - positions=o.all_positions.all(), - fees=o.all_fees.all(), - dt_now=o.datetime, - migrated=True, - is_new=True, - _backfill_before_cancellation=True, - ) - o.create_transactions( - positions=o.all_positions.all(), - fees=o.all_fees.all(), - dt_now=o.cancellation_date or (o.expires if o.status == Order.STATUS_EXPIRED else o.datetime), - migrated=True, - ) - else: - tn = o.create_transactions( - positions=o.all_positions.all(), - fees=o.all_fees.all(), - dt_now=now(), - migrated=True, - ) - if tn: - t += 1 - time.sleep(0) - pbar.update(1) - last_pk = batch[-1].pk + for qs in querysets: + last_pk = 0 + with tqdm(total=qs.count()) as pbar: + while True: + batch = list(qs.filter(pk__gt=last_pk)[:5000]) + if not batch: + break - self.stderr.write(self.style.SUCCESS(f'Created transactions for {t} orders.')) + for o in batch: + o._update_search_index() + time.sleep(0) + pbar.update(1) + last_pk = batch[-1].pk diff --git a/src/pretix/base/models/orders.py b/src/pretix/base/models/orders.py index fa99a5fbb..4c6cf17cc 100644 --- a/src/pretix/base/models/orders.py +++ b/src/pretix/base/models/orders.py @@ -149,7 +149,9 @@ class SearchIndexModelMixin: def get_search_index_content(self): for f in self.search_index_fields: - yield str(getattr(self, f)) + val = getattr(self, f) + if val is not None: + yield str(val) def _update_search_index(self): from .search import OrderSearchIndex diff --git a/src/pretix/base/models/search.py b/src/pretix/base/models/search.py index 243b6a6cf..f8c578514 100644 --- a/src/pretix/base/models/search.py +++ b/src/pretix/base/models/search.py @@ -111,19 +111,30 @@ class OrderSearchIndex(models.Model): order = obj else: order = obj.order - OrderSearchIndex.objects.update_or_create( + + kwargs = dict( order=order, orderpayment=obj if isinstance(obj, OrderPayment) else None, orderrefund=obj if isinstance(obj, OrderRefund) else None, orderposition=obj if isinstance(obj, OrderPosition) else None, - defaults=dict( - search_vector=SearchVector( - Value(index_text), - config="pretix_search" - ), - ), - create_defaults=dict( - event_id=order.event_id, - organizer_id=order.organizer_id, - ), ) + + if not index_text.strip(): + OrderSearchIndex.objects.filter(**kwargs).delete() + else: + OrderSearchIndex.objects.update_or_create( + order=order, + orderpayment=obj if isinstance(obj, OrderPayment) else None, + orderrefund=obj if isinstance(obj, OrderRefund) else None, + orderposition=obj if isinstance(obj, OrderPosition) else None, + defaults=dict( + search_vector=SearchVector( + Value(index_text), + config="pretix_search" + ), + ), + create_defaults=dict( + event_id=order.event_id, + organizer_id=order.organizer_id, + ), + ) diff --git a/src/pretix/helpers/migration_utils.py b/src/pretix/helpers/migration_utils.py index 1f0030638..190a8858c 100644 --- a/src/pretix/helpers/migration_utils.py +++ b/src/pretix/helpers/migration_utils.py @@ -27,11 +27,11 @@ class PostgreSQLAndStateOnly(migrations.SeparateDatabaseAndState): super().__init__(database_operations=operations, state_operations=operations) def database_forwards(self, app_label, schema_editor, from_state, to_state): - if schema_editor.connection != "postgresql": + if schema_editor.connection.vendor != "postgresql": return super().database_forwards(app_label, schema_editor, from_state, to_state) def database_backwards(self, app_label, schema_editor, from_state, to_state): - if schema_editor.connection != "postgresql": + if schema_editor.connection.vendor != "postgresql": return super().database_forwards(app_label, schema_editor, from_state, to_state)