11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsのDB接続先をbranch名に従って動的に変更する

Posted at

複数のバージョンの並行開発でDBスキーマが異なる場合、DBの接続先切り替えが非常に厄介。

そこでdatabase.yml内では式展開できることを利用し、
以下のようにするとgitブランチ名に応じて接続先を変更することが可能になる。

database.yml
default: &login
  username: root
  password: hogehoge
  adapter:  mysql
  host:     localhost
  encoding: utf8
  reconnect: true
  pool: 5

development:
  database: db_<%=
    branch = `git symbolic-ref --short HEAD`.chomp
    if branch == 'master'
      'master'
    elsif branch.start_with?('fix_')
      'fix'
    elsif branch.start_with?('pre_')
      'pre'
    else
      'development'
    end
  %>
  <<: *login

上記例

  • master:db_master
  • fix_で始まる:db_fix
  • pre_で始まる:db_pre
  • その他:db_development
11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?