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|
|
2016-07-26 00:14:23 +00:00
|
|
|
if mapping.synapse_id.nil? && mapping.topic_id.nil?
|
2015-10-02 08:04:30 +00:00
|
|
|
puts "Mapping id=#{mapping.id} has no valid id, skipping!"
|
|
|
|
next
|
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
if !mapping.synapse_id.nil? && !mapping.topic_id.nil?
|
2015-10-02 08:04:30 +00:00
|
|
|
puts "Mapping id=#{mapping.id} has both topic and synapse ids, skipping!"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
if mapping.synapse_id.nil?
|
2016-01-06 02:23:01 +00:00
|
|
|
mapping.mappable = Topic.find_by(id: mapping.topic_id)
|
2016-07-26 00:14:23 +00:00
|
|
|
else
|
|
|
|
mapping.mappable = Synapse.find_by(id: mapping.synapse_id)
|
2016-01-06 02:23:01 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2016-01-06 02:23:01 +00:00
|
|
|
if mapping.mappable.nil?
|
|
|
|
mapping.delete
|
|
|
|
else
|
|
|
|
mapping.save
|
2015-10-02 08:04:30 +00:00
|
|
|
end
|
|
|
|
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
|