Finish tags migration

This commit is contained in:
Raphael Sofaer 2011-03-30 11:52:36 -07:00
parent 23af0d14b0
commit 2a18ded47c
2 changed files with 4 additions and 3 deletions

View file

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

View file

@ -328,8 +328,8 @@ ActiveRecord::Schema.define(:version => 20110330175950) do
end end
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context", "tag_id"], :name => "index_taggings_uniquely", :unique => true
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", "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| create_table "tags", :force => true do |t|
t.string "name" t.string "name"