DBをブランチ毎に切り替える

config/database.yml でERBを使えば結構簡単。

<% milestone = (`LANG=C; git status` =~ /On branch local_branch_200906/) ? '_200906' : '' %>

common: &common
  adapter: mysql
  encoding: utf8
  username: root
  password:
  socket: /opt/local/var/run/mysql5/mysqld.sock

development:
  database: foo<%= milestone %>_development
  <<: *common

test:
  database: foo<%= milestone %>_test
  <<: *common

production:
  database: foo<%= milestone %>_production
  <<: *common

trunkとブランチを平行して開発していくのは辛いけど、一旦リリースした後に大きな機能を追加・変更する場合は、避けられません。そういう場合に、gitでcheckoutしたときにいちいちconfig/database.ymlを手で直すのはすごくめんどくさい。結構忘れちゃうし。なので、DBを

 アプリ名_ブランチ名_(development/test/production)

という命名規則で使うようにしたら非常にらくちんになりました。

svnだとブランチ毎にプロジェクトをcheckoutするかもしれないけど、もし切り替えて使うなら上記の先頭の部分を

<% milestone = (`LANG=C; svn info` =~ /branches/200906/) ? '_200906' : '' %>

とかにしてあげればオッケーだと思います。