teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

詳細手順を追加しました

2017/01/19 01:52

投稿

satoshih
satoshih

スコア864

title CHANGED
File without changes
body CHANGED
@@ -16,8 +16,85 @@
16
16
  => `branch_a` で作成したDBの情報が `config/schema.rb` に反映されてしまうため、
17
17
  不要なdiffができてしまう。
18
18
 
19
+ ### 詳細な手順
20
+ 1. `branch_a` で `rails g migration` を実行し、 `rake db:migrate` で、 DBへ反映
21
+ ```
22
+ $ git branch
23
+ * branch_a
24
+ branch_b
25
+ master
19
26
 
27
+ $ rails g model user name:string
28
+ invoke active_record
29
+ create db/migrate/20170119014258_create_users.rb
30
+ create app/models/user.rb
31
+ invoke test_unit
32
+ create test/models/user_test.rb
33
+ create test/fixtures/users.yml
20
34
 
35
+ $ rake db:migrate
36
+ == 20170119014258 CreateUsers: migrating ======================================
37
+ -- create_table(:users)
38
+ -> 0.0481s
39
+ == 20170119014258 CreateUsers: migrated (0.0482s) =============================
40
+
41
+ $ git status
42
+ On branch branch_a
43
+ Changes not staged for commit:
44
+ (use "git add <file>..." to update what will be committed)
45
+ (use "git checkout -- <file>..." to discard changes in working directory)
46
+
47
+ modified: db/schema.rb
48
+
49
+ Untracked files:
50
+ (use "git add <file>..." to include in what will be committed)
51
+
52
+ app/models/user.rb
53
+ db/migrate/20170119014258_create_users.rb
54
+ test/fixtures/users.yml
55
+ test/models/user_test.rb
56
+
57
+ $ git add .
58
+ $ git commit -m "add user model"
59
+ ```
60
+ 2.`git checkout branch_b` で `branch_b` に切り替え、 `rake db:migrate` を行う
61
+
62
+ ```
63
+ $ git checkout -b branch_b
64
+ $ git branch
65
+ branch_a
66
+ * branch_b
67
+ master
68
+
69
+ $ rake db:migrate #←ここでdiffが発生する
70
+ $ git diff
71
+ diff --git a/db/schema.rb b/db/schema.rb
72
+ index 305532e..1c0d0dc 100644
73
+ --- a/db/schema.rb
74
+ +++ b/db/schema.rb
75
+ @@ -10,7 +10,7 @@
76
+ #
77
+ # It's strongly recommended that you check this file into your version control system.
78
+
79
+ -ActiveRecord::Schema.define(version: 20161230062212) do
80
+ +ActiveRecord::Schema.define(version: 20170119014258) do
81
+
82
+ # These are extensions that must be enabled in order to support this database
83
+ enable_extension "plpgsql"
84
+ @@ -29,4 +29,10 @@ ActiveRecord::Schema.define(version: 20161230062212) do
85
+ t.datetime "updated_at", null: false
86
+ end
87
+
88
+ + create_table "users", force: :cascade do |t|
89
+ + t.string "name"
90
+ + t.datetime "created_at", null: false
91
+ + t.datetime "updated_at", null: false
92
+ + end
93
+ +
94
+ end
95
+ ```
96
+
97
+
21
98
  ### 現在の対応方法
22
99
  `branch_a` で `rake db:migrate:down VERSION=xxx` でDBのschemaを元に戻してから、
23
100
  `branch_b` へ切り替えるようにしています。