Merge branch 'tag_uniqueness'

This commit is contained in:
Raphael Sofaer 2011-03-30 11:54:21 -07:00
commit 197b8fe73f
2 changed files with 26 additions and 1 deletions

View file

@ -0,0 +1,24 @@
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 FROM taggings AS t
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
end
def self.up
delete_duplicate_taggings
add_index :taggings, [:taggable_id, :taggable_type, :tag_id], :unique => true, :name => 'index_taggings_uniquely'
end
def self.down
remove_index :taggings, :name => 'index_taggings_uniquely'
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110328202414) do
ActiveRecord::Schema.define(:version => 20110330175950) do
create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id", :null => false
@ -329,6 +329,7 @@ ActiveRecord::Schema.define(:version => 20110328202414) do
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
add_index "taggings", ["taggable_id", "taggable_type", "tag_id"], :name => "index_taggings_uniquely", :unique => true
create_table "tags", :force => true do |t|
t.string "name"