diff --git a/pyproject.toml b/pyproject.toml index 1c98b4313..85a73927e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -127,5 +127,6 @@ max-complexity = 16 [tool.ruff.lint.per-file-ignores] "scripts/*" = ["T201"] "weblate_web/migrations/0031_fill_in_customer.py" = ["T201"] +"weblate_web/migrations/0038_migrate_payments.py" = ["T201"] "weblate_web/payments/backends.py" = ["T201"] "weblate_web/payments/migrations/0040_fill_in_users.py" = ["T201"] diff --git a/weblate_web/migrations/0038_migrate_payments.py b/weblate_web/migrations/0038_migrate_payments.py new file mode 100644 index 000000000..e6e282498 --- /dev/null +++ b/weblate_web/migrations/0038_migrate_payments.py @@ -0,0 +1,31 @@ +# Generated by Django 5.1.3 on 2024-12-18 11:45 + +from django.db import migrations + + +def migrate_payments(payments, customer): + print(f"Moving {len(payments)} payments to {customer.name}") + payments.update(customer=customer) + + +def update_payments(apps, schema_editor): + Subscription = apps.get_model("weblate_web", "Subscription") + Payment = apps.get_model("payments", "Payment") + + for subscription in Subscription.objects.prefetch_related("pastpayments_set"): + payment_ids = {subscription.payment} + payment_ids.update(past.payment for past in subscription.pastpayments_set.all()) + migrate_payments( + Payment.objects.filter(pk__in=payment_ids), subscription.service.customer + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("weblate_web", "0037_package_hidden"), + ("payments", "0037_alter_customer_vat"), + ] + + operations = [ + migrations.RunPython(update_payments, migrations.RunPython.noop, elidable=True), + ]