fix: add missing WEBHOOK_TORRENT_DETECTED mapping + dedup before unique index in migration 0053
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user