Skip to content
Extraits de code Groupes Projets
Valider 2a18ded4 rédigé par Raphael Sofaer's avatar Raphael Sofaer
Parcourir les fichiers

Finish tags migration

parent 23af0d14
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
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
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]}
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'
......
......@@ -328,8 +328,8 @@ ActiveRecord::Schema.define(:version => 20110330175950) do
end
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", "tag_id"], :name => "index_taggings_uniquely", :unique => true
create_table "tags", :force => true do |t|
t.string "name"
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter