discourse/db/migrate/20210323142518_update_invites_redemption_count.rb
Dan Ungureanu 2a4ddc621d
FIX: Add migration to set correct redemption_count (#12491)
Redeeming email invites did not increase the redemption_count which let
those invites in a weird state were they were both pending and redeemed.
2021-03-23 18:57:39 +02:00

18 lines
438 B
Ruby

# frozen_string_literal: true
class UpdateInvitesRedemptionCount < ActiveRecord::Migration[6.0]
def change
execute <<~SQL
WITH invite_counts AS (
SELECT invite_id, COUNT(*) count
FROM invited_users
GROUP BY invite_id
)
UPDATE invites
SET redemption_count = GREATEST(redemption_count, count)
FROM invite_counts
WHERE invites.id = invite_counts.invite_id
SQL
end
end