Skip to content

Instantly share code, notes, and snippets.

@kwatch
Last active August 29, 2015 14:02
Show Gist options
  • Save kwatch/20baef0af58f8de4c694 to your computer and use it in GitHub Desktop.
Save kwatch/20baef0af58f8de4c694 to your computer and use it in GitHub Desktop.
他の人のブランチをマージしたら、マイグレーションがコンフリクトしちゃった!どうしたらいいの? (Migr8.rb編)
## たとえば history.txt の内容がこういう履歴になっていたとして、
bcgf7647 # [john] create 'users' table
fdon4243 # [john] add 'birthday' column
## alice が 2 件のマイグレーションを追加したとします
bcgf7647 # [john] create 'users' table
fdon4243 # [john] add 'birthday' column
ycii2472 # [alice] change 'birthday' column as not-null
xggc5677 # [alice] add index to 'users.brthday'
## ここで git merge すると、bob の作業と conflict しちゃいました。さあどうしよう?
bcgf7647 # [john] create 'users' table
fdon4243 # [john] add 'birthday' column
>>>>>
hcei2053 # [bob] create 'groups' table
---
ycii2472 # [alice] change 'birthday' column as not-null
xggc5677 # [alice] add index to 'users.brthday'
<<<<<
## この場合、どの行も消すことなく、すべて残したまま commit → merge します
## (通常は、先に commit&merge されたブランチの内容(ここでは bob の作業)を
## 先に、自分の作業分をあとになるように書き換えます)
bcgf7647 # [john] create 'users' table
fdon4243 # [john] add 'birthday' column
hcei2053 # [bob] create 'groups' table
ycii2472 # [alice] change 'birthday' column as not-null
xggc5677 # [alice] add index to 'users.brthday'
## この状態でコマンドラインから migr8.rb を起動すると、bob の作業分で
## ある「hcei2053」が適用されてないことがわかります ←ココ重要!
$ ./migr8.rb
## Status: there are 1 migrations to apply
## Recent history:
bcgf7647 2014-05-30 01:11:11 # [john] create 'users' table
fdon4243 2014-05-30 02:22:22 # [john] add 'birthday' column
hcei2053 (not applied) # [bob] create 'groups' table
ycii2472 2014-06-01 03:33:33 # [alice] change 'birthday' column as not-null
xggc5677 2014-06-01 04:04:04 # [alice] add index to 'users.brthday'
## そこで、hcei2053 の内容を読んで、自分のマイグレーションと
## かぶらないことを確認してから、手動で適用します。
## (git cherry-pick みたいなものだと思ってください。)
$ ./migr8.rb show hcei2053 | less
$ ./migr8.rb apply hcei2053
## ただこれだと、history.txt に書かれたバージョンの順番と、
## 実際にDBに適用した順番とが異なることになります。
## 作業内容がかぶってない限りは気にする必要はありませんが、
## 気になる場合は、自分のマイグレーションを unapply してから
## 改めてbobと自分のマイグレーションを apply します。
$ ./migr8.rb unapply xggc5677
$ ./migr8.rb unapply ycii2472
$ ./migr8.rb up -a # 未適用のマイグレーションがすべて適用される
## 以上です
@kwatch
Copy link
Author

kwatch commented Jun 19, 2014

この状態でコマンドラインから migr8.rb を起動すると、bob の作業分で
ある「hcei2053」が適用されてないことがわかります ←ココ重要!

ここが重要なところでして、Ruby on Rails だと「DBに提供した最新のバージョン番号」しかわからないのでこういうことができない。Migr8.rb だと適用したマイグレーションの情報をすべて DB に記録しているため、抜けてある(=適用されてない)バージョンがわかる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment