Fix #4651: Favorites table contains duplicate favorites
Add fix script to remove duplicate favorites. When a user has duplicate favorites on the same post, the earliest favorite will be kept and the rest will be removed.
This commit is contained in:
22
script/fixes/080_delete_duplicate_favorites.rb
Executable file
22
script/fixes/080_delete_duplicate_favorites.rb
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require_relative "../../config/environment"
|
||||
|
||||
Favorite.transaction do
|
||||
0.upto(99) do |i|
|
||||
Favorite.where("user_id % 100 = #{i}").group(:post_id, :user_id).having("count(*) > 1").count.each do |(post_id, user_id), count|
|
||||
favs = Favorite.where(post_id: post_id, user_id: user_id).order(:id)
|
||||
|
||||
# Remove all duplicates, leaving the oldest favorite.
|
||||
dupe_favs = favs.drop(1)
|
||||
puts "user_id=#{user_id} post_id=#{post_id} count=#{count} keep=#{favs.first.id} drop=#{dupe_favs.map(&:id).join(",")}"
|
||||
dupe_favs.each(&:delete)
|
||||
|
||||
post = Post.find(post_id)
|
||||
post.update_columns(fav_count: post.favorites.count)
|
||||
|
||||
user = User.find(user_id)
|
||||
user.update_columns(favorite_count: user.favorites.count)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user