removing duplicate tag followings

This commit is contained in:
Ilya Zhitomirskiy 2011-10-26 14:24:35 -07:00
parent 162d3ccd10
commit 3b1f3f90d1

View file

@ -1,5 +1,26 @@
class AddMissingTagFollowingsIndices < ActiveRecord::Migration
def self.delete_duplicate_tag_followings
duplicate_rows = execute <<SQL
SELECT COUNT(tf.user_id), tf.user_id, tf.tag_id from tag_followings AS tf
GROUP BY tf.user_id, tf.tag_id
HAVING COUNT(*)>1;
SQL
duplicate_rows.each do |row|
count = row.first
user_id = row[1]
tag_id = row.last
execute <<SQL
DELETE FROM tag_followings
WHERE tag_followings.user_id = #{user_id} AND tag_followings.tag_id = #{tag_id}
LIMIT #{count-1}
SQL
end
end
def self.up
delete_duplicate_tag_followings
add_index :tag_followings, :tag_id
add_index :tag_followings, :user_id
add_index :tag_followings, [:tag_id, :user_id], :unique => true