about summary refs log tree commit diff stats
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20171003191205_remove_boxes.rb106
1 files changed, 106 insertions, 0 deletions
diff --git a/db/migrate/20171003191205_remove_boxes.rb b/db/migrate/20171003191205_remove_boxes.rb new file mode 100644 index 0000000..476b98a --- /dev/null +++ b/db/migrate/20171003191205_remove_boxes.rb
@@ -0,0 +1,106 @@
1class RemoveBoxes < ActiveRecord::Migration[5.1]
2 def up
3 change_table :pokeviewer_trainers do |t|
4 t.string :box_1_name, null: false, default: ""
5 t.string :box_2_name, null: false, default: ""
6 t.string :box_3_name, null: false, default: ""
7 t.string :box_4_name, null: false, default: ""
8 t.string :box_5_name, null: false, default: ""
9 t.string :box_6_name, null: false, default: ""
10 t.string :box_7_name, null: false, default: ""
11 t.string :box_8_name, null: false, default: ""
12 t.string :box_9_name, null: false, default: ""
13 t.string :box_10_name, null: false, default: ""
14 t.string :box_11_name, null: false, default: ""
15 t.string :box_12_name, null: false, default: ""
16 t.string :box_13_name, null: false, default: ""
17 t.string :box_14_name, null: false, default: ""
18 end
19
20 change_column_default :pokeviewer_trainers, :box_1_name, nil
21 change_column_default :pokeviewer_trainers, :box_2_name, nil
22 change_column_default :pokeviewer_trainers, :box_3_name, nil
23 change_column_default :pokeviewer_trainers, :box_4_name, nil
24 change_column_default :pokeviewer_trainers, :box_5_name, nil
25 change_column_default :pokeviewer_trainers, :box_6_name, nil
26 change_column_default :pokeviewer_trainers, :box_7_name, nil
27 change_column_default :pokeviewer_trainers, :box_8_name, nil
28 change_column_default :pokeviewer_trainers, :box_9_name, nil
29 change_column_default :pokeviewer_trainers, :box_10_name, nil
30 change_column_default :pokeviewer_trainers, :box_11_name, nil
31 change_column_default :pokeviewer_trainers, :box_12_name, nil
32 change_column_default :pokeviewer_trainers, :box_13_name, nil
33 change_column_default :pokeviewer_trainers, :box_14_name, nil
34
35 Pokeviewer::Trainer.all.each do |t|
36 boxes = ActiveRecord::Base.connection.select_all(
37 "SELECT * FROM pokeviewer_boxes WHERE trainer_id = ? ORDER BY number ASC",
38 t.trainer_id).map { |b| b["name"] }
39
40 t.box_1_name = boxes.shift
41 t.box_2_name = boxes.shift
42 t.box_3_name = boxes.shift
43 t.box_4_name = boxes.shift
44 t.box_5_name = boxes.shift
45 t.box_6_name = boxes.shift
46 t.box_7_name = boxes.shift
47 t.box_8_name = boxes.shift
48 t.box_9_name = boxes.shift
49 t.box_10_name = boxes.shift
50 t.box_11_name = boxes.shift
51 t.box_12_name = boxes.shift
52 t.box_13_name = boxes.shift
53 t.box_14_name = boxes.shift
54
55 t.save
56 end
57
58 drop_table :pokeviewer_boxes
59 end
60
61 def down
62 create_table :pokeviewer_boxes do |t|
63 t.integer "trainer_id", null: false
64 t.integer "number", null: false
65 t.string "name", null: false
66
67 t.timestamps
68 end
69
70 add_index :pokeviewer_boxes, [:trainer_id, :number], unique: true
71
72 add_foreign_key :pokeviewer_boxes, :pokeviewer_trainers, column: :trainer_id
73
74 Pokeviewer::Trainer.all.each do |t|
75 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 1, name: t.box_1_name)
76 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 2, name: t.box_2_name)
77 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 3, name: t.box_3_name)
78 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 4, name: t.box_4_name)
79 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 5, name: t.box_5_name)
80 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 6, name: t.box_6_name)
81 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 7, name: t.box_7_name)
82 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 8, name: t.box_8_name)
83 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 9, name: t.box_9_name)
84 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 10, name: t.box_10_name)
85 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 11, name: t.box_11_name)
86 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 12, name: t.box_12_name)
87 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 13, name: t.box_13_name)
88 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 14, name: t.box_14_name)
89 end
90
91 remove_column :pokeviewer_trainers, :box_1_name
92 remove_column :pokeviewer_trainers, :box_2_name
93 remove_column :pokeviewer_trainers, :box_3_name
94 remove_column :pokeviewer_trainers, :box_4_name
95 remove_column :pokeviewer_trainers, :box_5_name
96 remove_column :pokeviewer_trainers, :box_6_name
97 remove_column :pokeviewer_trainers, :box_7_name
98 remove_column :pokeviewer_trainers, :box_8_name
99 remove_column :pokeviewer_trainers, :box_9_name
100 remove_column :pokeviewer_trainers, :box_10_name
101 remove_column :pokeviewer_trainers, :box_11_name
102 remove_column :pokeviewer_trainers, :box_12_name
103 remove_column :pokeviewer_trainers, :box_13_name
104 remove_column :pokeviewer_trainers, :box_14_name
105 end
106end