Files
danbooru/script/fixes/074_delete_duplicate_comment_votes.rb
evazion c9570e698b comments: add fix script to remove duplicate votes.
There are about 100 duplicate comment votes. This is because there
wasn't a uniqueness constraint in the database to prevent duplicate
votes. This adds a script to remove duplicate votes so that a constraint
can be added later.
2021-01-21 07:58:50 -06:00

15 lines
436 B
Ruby
Executable File

#!/usr/bin/env ruby
require_relative "../../config/environment"
CommentVote.transaction do
CommentVote.group(:comment_id, :user_id).having("count(*) > 1").count.each do |(comment_id, user_id), count|
votes = CommentVote.where(comment_id: comment_id, user_id: user_id).order(:id)
# Remove all but the first duplicate vote.
dupe_votes = votes.drop(1)
dupe_votes.each { p _1 }
dupe_votes.each(&:destroy)
end
end