This commit is contained in:
Raphael Michel
2026-05-22 16:33:48 +02:00
parent eba9ab4430
commit 62396c02d7
4 changed files with 50 additions and 63 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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,
),
)

View File

@@ -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)