metamaps--metamaps/db/migrate/20151001024122_mapping_polymorphism.rb
Devin Howard 7d4da81272 Update code style automatically using rubocop gem (#563)
* install rubocop

* 1961 automatic rubocop fixes

* update rubocop.yml to ignore half of the remaining cops

* rubocop lint warnings

* random other warnings fixed
2016-07-26 08:14:23 +08:00

36 lines
1 KiB
Ruby

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? && mapping.topic_id.nil?
puts "Mapping id=#{mapping.id} has no valid id, skipping!"
next
end
if !mapping.synapse_id.nil? && !mapping.topic_id.nil?
puts "Mapping id=#{mapping.id} has both topic and synapse ids, skipping!"
next
end
if mapping.synapse_id.nil?
mapping.mappable = Topic.find_by(id: mapping.topic_id)
else
mapping.mappable = Synapse.find_by(id: mapping.synapse_id)
end
if mapping.mappable.nil?
mapping.delete
else
mapping.save
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