fix: add missing WEBHOOK_TORRENT_DETECTED mapping + dedup before unique index in migration 0053

This commit is contained in:
c0mrade
2026-04-03 20:50:13 +03:00
parent 819f09a68e
commit 3b5d5a18a1
5 changed files with 28 additions and 2 deletions
@@ -21,6 +21,30 @@ def upgrade() -> None:
# Drop old partial unique index that only covered active/trial
op.execute(sa.text('DROP INDEX IF EXISTS uq_subscriptions_user_tariff_active'))
# Deduplicate: if a user has multiple active/trial/limited subscriptions
# for the same tariff, expire all but the most recent one.
op.execute(
sa.text(
"""
UPDATE subscriptions
SET status = 'expired'
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY user_id, tariff_id
ORDER BY created_at DESC
) AS rn
FROM subscriptions
WHERE tariff_id IS NOT NULL
AND status IN ('active', 'trial', 'limited')
) ranked
WHERE rn > 1
)
"""
)
)
# Recreate with limited status included — a limited subscription (traffic
# exhausted but time remaining) is still "alive" and should prevent
# duplicate subscriptions for the same user+tariff combination.