discourse/db/migrate/20210323142518_update_invites_redemption_count.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

18 lines
438 B
Ruby
Raw Normal View History

# 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