Update migration to handle duplicate likes
This commit is contained in:
parent
67882c775e
commit
a7d8535d36
1 changed files with 11 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
class LikesOnComments < ActiveRecord::Migration
|
class LikesOnComments < ActiveRecord::Migration
|
||||||
|
class Likes < ActiveRecord::Base; end
|
||||||
def self.up
|
def self.up
|
||||||
remove_foreign_key :likes, :posts
|
remove_foreign_key :likes, :posts
|
||||||
|
|
||||||
|
|
@ -11,7 +12,17 @@ class LikesOnComments < ActiveRecord::Migration
|
||||||
UPDATE likes
|
UPDATE likes
|
||||||
SET target_type = 'Post'
|
SET target_type = 'Post'
|
||||||
SQL
|
SQL
|
||||||
|
execute <<SQL
|
||||||
|
UPDATE posts
|
||||||
|
SET likes_count = (SELECT COUNT(*) FROM likes WHERE likes.target_id = posts.id AND likes.target_type = 'Post')
|
||||||
|
SQL
|
||||||
|
|
||||||
|
#There are some duplicate likes.
|
||||||
|
keeper_likes = Like.group(:target_id, :author_id, :target_type).having('COUNT(*) > 1')
|
||||||
|
keeper_likes.each do |like|
|
||||||
|
l = Like.arel_table
|
||||||
|
Like.where(:target_id => like.target_id, :author_id => like.author_id, :target_type => like.target_type).where(l[:id].not_eq(like.id)).delete_all
|
||||||
|
end
|
||||||
add_index :likes, [:target_id, :author_id, :target_type], :unique => true
|
add_index :likes, [:target_id, :author_id, :target_type], :unique => true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue