membershipworks: Avoid duplicating Transactions with NULL sid field
Treat NULL in Transaction.sid as non-distinct in the UNIQUE constraint
This commit is contained in:
parent
01c469b0ce
commit
ec7182319d
@ -0,0 +1,44 @@
|
||||
# Generated by Django 5.1.4 on 2024-12-06 19:44
|
||||
|
||||
import itertools
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def remove_duplicates(apps, schema_editor):
|
||||
Transaction = apps.get_model("membershipworks", "Transaction")
|
||||
for _, group in itertools.groupby(
|
||||
Transaction.objects.filter(sid=None), key=lambda t: t.timestamp
|
||||
):
|
||||
dupes = list(group)
|
||||
first_in_group = dupes.pop()
|
||||
for t in dupes:
|
||||
if any(
|
||||
getattr(t, field.name) != getattr(first_in_group, field.name)
|
||||
for field in Transaction._meta.get_fields()
|
||||
if field.name != "id"
|
||||
):
|
||||
raise Exception("Transaction doesn't match!")
|
||||
t.delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("membershipworks", "0005_historicalmember_nfc_card_number_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(remove_duplicates, atomic=True),
|
||||
migrations.RemoveConstraint(
|
||||
model_name="transaction",
|
||||
name="unique_sid_timestamp",
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name="transaction",
|
||||
constraint=models.UniqueConstraint(
|
||||
fields=("sid", "timestamp"),
|
||||
name="unique_sid_timestamp",
|
||||
nulls_distinct=False,
|
||||
),
|
||||
),
|
||||
]
|
@ -377,7 +377,11 @@ class Transaction(BaseModel):
|
||||
class Meta:
|
||||
db_table = "transactions"
|
||||
constraints = [
|
||||
models.UniqueConstraint("sid", "timestamp", name="unique_sid_timestamp")
|
||||
models.UniqueConstraint(
|
||||
fields=["sid", "timestamp"],
|
||||
name="unique_sid_timestamp",
|
||||
nulls_distinct=False,
|
||||
)
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
|
Loading…
Reference in New Issue
Block a user