From c9570e698bb230f4dc972920c7dfe4c6a9f49071 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 21 Jan 2021 02:37:16 -0600 Subject: [PATCH] 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. --- script/fixes/074_delete_duplicate_comment_votes.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 script/fixes/074_delete_duplicate_comment_votes.rb diff --git a/script/fixes/074_delete_duplicate_comment_votes.rb b/script/fixes/074_delete_duplicate_comment_votes.rb new file mode 100755 index 000000000..b97b299d1 --- /dev/null +++ b/script/fixes/074_delete_duplicate_comment_votes.rb @@ -0,0 +1,14 @@ +#!/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