about summary refs log tree commit diff stats
path: root/db/migrate/20171015171555_rename_pokemon_met_location.pokeviewer.rb
blob: afb59872f594ecf1e356b97d7137a1ac014316e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# This migration comes from pokeviewer (originally 20171003154157)
class RenamePokemonMetLocation < ActiveRecord::Migration[5.1]
  def up
    add_column :pokeviewer_pokemon, :location_id, :integer, null: true

    add_foreign_key :pokeviewer_pokemon, :pokeviewer_locations,
      column: :location_id

    Pokeviewer::Pokemon.all.each do |p|
      unless p.met_location.nil?
        p.location_id = p.met_location.to_i
        p.save!
      end
    end

    remove_column :pokeviewer_pokemon, :met_location
  end

  def down
    add_column :pokeviewer_pokemon, :met_location, :string

    Pokeviewer::Pokemon.all.each do |p|
      unless p.location_id.nil?
        p.met_location = p.location_id.to_s
        p.save!
      end
    end

    remove_column :pokeviewer_pokemon, :location_id
  end
end