about summary refs log tree commit diff stats
path: root/db/migrate/20171003191205_remove_boxes.rb
blob: 476b98a88a8434ba334528051b0109b4593f474a (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class RemoveBoxes < ActiveRecord::Migration[5.1]
  def up
    change_table :pokeviewer_trainers do |t|
      t.string :box_1_name, null: false, default: ""
      t.string :box_2_name, null: false, default: ""
      t.string :box_3_name, null: false, default: ""
      t.string :box_4_name, null: false, default: ""
      t.string :box_5_name, null: false, default: ""
      t.string :box_6_name, null: false, default: ""
      t.string :box_7_name, null: false, default: ""
      t.string :box_8_name, null: false, default: ""
      t.string :box_9_name, null: false, default: ""
      t.string :box_10_name, null: false, default: ""
      t.string :box_11_name, null: false, default: ""
      t.string :box_12_name, null: false, default: ""
      t.string :box_13_name, null: false, default: ""
      t.string :box_14_name, null: false, default: ""
    end

    change_column_default :pokeviewer_trainers, :box_1_name, nil
    change_column_default :pokeviewer_trainers, :box_2_name, nil
    change_column_default :pokeviewer_trainers, :box_3_name, nil
    change_column_default :pokeviewer_trainers, :box_4_name, nil
    change_column_default :pokeviewer_trainers, :box_5_name, nil
    change_column_default :pokeviewer_trainers, :box_6_name, nil
    change_column_default :pokeviewer_trainers, :box_7_name, nil
    change_column_default :pokeviewer_trainers, :box_8_name, nil
    change_column_default :pokeviewer_trainers, :box_9_name, nil
    change_column_default :pokeviewer_trainers, :box_10_name, nil
    change_column_default :pokeviewer_trainers, :box_11_name, nil
    change_column_default :pokeviewer_trainers, :box_12_name, nil
    change_column_default :pokeviewer_trainers, :box_13_name, nil
    change_column_default :pokeviewer_trainers, :box_14_name, nil

    Pokeviewer::Trainer.all.each do |t|
      boxes = ActiveRecord::Base.connection.select_all(
        "SELECT * FROM pokeviewer_boxes WHERE trainer_id = ? ORDER BY number ASC",
        t.trainer_id).map { |b| b["name"] }

      t.box_1_name = boxes.shift
      t.box_2_name = boxes.shift
      t.box_3_name = boxes.shift
      t.box_4_name = boxes.shift
      t.box_5_name = boxes.shift
      t.box_6_name = boxes.shift
      t.box_7_name = boxes.shift
      t.box_8_name = boxes.shift
      t.box_9_name = boxes.shift
      t.box_10_name = boxes.shift
      t.box_11_name = boxes.shift
      t.box_12_name = boxes.shift
      t.box_13_name = boxes.shift
      t.box_14_name = boxes.shift

      t.save
    end

    drop_table :pokeviewer_boxes
  end

  def down
    create_table :pokeviewer_boxes do |t|
      t.integer "trainer_id", null: false
      t.integer "number", null: false
      t.string "name", null: false

      t.timestamps
    end

    add_index :pokeviewer_boxes, [:trainer_id, :number], unique: true

    add_foreign_key :pokeviewer_boxes, :pokeviewer_trainers, column: :trainer_id

    Pokeviewer::Trainer.all.each do |t|
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 1, name: t.box_1_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 2, name: t.box_2_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 3, name: t.box_3_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 4, name: t.box_4_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 5, name: t.box_5_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 6, name: t.box_6_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 7, name: t.box_7_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 8, name: t.box_8_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 9, name: t.box_9_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 10, name: t.box_10_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 11, name: t.box_11_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 12, name: t.box_12_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 13, name: t.box_13_name)
      Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 14, name: t.box_14_name)
    end

    remove_column :pokeviewer_trainers, :box_1_name
    remove_column :pokeviewer_trainers, :box_2_name
    remove_column :pokeviewer_trainers, :box_3_name
    remove_column :pokeviewer_trainers, :box_4_name
    remove_column :pokeviewer_trainers, :box_5_name
    remove_column :pokeviewer_trainers, :box_6_name
    remove_column :pokeviewer_trainers, :box_7_name
    remove_column :pokeviewer_trainers, :box_8_name
    remove_column :pokeviewer_trainers, :box_9_name
    remove_column :pokeviewer_trainers, :box_10_name
    remove_column :pokeviewer_trainers, :box_11_name
    remove_column :pokeviewer_trainers, :box_12_name
    remove_column :pokeviewer_trainers, :box_13_name
    remove_column :pokeviewer_trainers, :box_14_name
  end
end