2015-10-02 08:04:30 +00:00
|
|
|
class MappingPolymorphism < ActiveRecord::Migration
|
|
|
|
def up
|
|
|
|
add_column :mappings, :mappable_id, :integer
|
|
|
|
add_column :mappings, :mappable_type, :string
|
|
|
|
add_index :mappings, [:mappable_id, :mappable_type]
|
|
|
|
|
|
|
|
Mapping.find_each do |mapping|
|
|
|
|
if mapping.synapse_id.nil? and mapping.topic_id.nil?
|
|
|
|
puts "Mapping id=#{mapping.id} has no valid id, skipping!"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if not mapping.synapse_id.nil? and not mapping.topic_id.nil?
|
|
|
|
puts "Mapping id=#{mapping.id} has both topic and synapse ids, skipping!"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
unless mapping.synapse_id.nil?
|
|
|
|
mapping.mappable = Synapse.find(mapping.synapse_id)
|
|
|
|
else
|
2015-10-23 14:32:09 +00:00
|
|
|
next if mapping.topic_id == 0
|
2015-10-02 08:04:30 +00:00
|
|
|
mapping.mappable = Topic.find(mapping.topic_id)
|
|
|
|
end
|
|
|
|
mapping.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
remove_index :mappings, [:mappable_id, :mappable_type]
|
|
|
|
remove_column :mappings, :mappable_id, :integer
|
|
|
|
remove_column :mappings, :mappable_type, :string
|
|
|
|
end
|
|
|
|
end
|