Take context out of index, delete dup taggings

This commit is contained in:
Raphael Sofaer 2011-03-30 11:40:26 -07:00
parent 6e0315965b
commit 23af0d14b0

View file

@ -1,6 +1,20 @@
class TagUniqueness < ActiveRecord::Migration
def self.delete_duplicate_taggings
duplicate_rows = execute <<SQL
SELECT COUNT(t.taggable_id), t.taggable_id, t.taggable_type, t.tag_id
GROUP BY t.taggable_id, t.taggable_type, t.tag_id
HAVING COUNT(*)>1;
SQL
duplicate_rows.each do |row|
execute <<SQL
DELETE FROM taggings
WHERE taggings.taggable_id = #{row[1]} AND taggings.taggable_type = #{row[2]} AND taggings.tag_id = #{row[3]}
LIMIT #{row[0]-1}
SQL
end
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context, :tag_id], :unique => true, :name => 'index_taggings_uniquely'
delete_duplicate_taggings
add_index :taggings, [:taggable_id, :taggable_type, :tag_id], :unique => true, :name => 'index_taggings_uniquely'
end
def self.down