From 0a8fe506b4d404dcdd2876865135558b878c9e6b Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 28 Jan 2021 15:07:07 -0600 Subject: [PATCH] posts: add script to remove duplicate votes. There were 67 duplicate votes in the production database. --- script/fixes/075_delete_duplicate_post_votes.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 script/fixes/075_delete_duplicate_post_votes.rb diff --git a/script/fixes/075_delete_duplicate_post_votes.rb b/script/fixes/075_delete_duplicate_post_votes.rb new file mode 100755 index 000000000..4f7d94917 --- /dev/null +++ b/script/fixes/075_delete_duplicate_post_votes.rb @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require_relative "../../config/environment" + +PostVote.transaction do + PostVote.group(:post_id, :user_id).having("count(*) > 1").count.each do |(post_id, user_id), count| + votes = PostVote.where(post_id: post_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