removing duplicate tag followings
This commit is contained in:
parent
162d3ccd10
commit
3b1f3f90d1
1 changed files with 21 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue