Using interpolated YML and a little logic you can clean up your Rails Database.yml file.

The default Rails Database.yml file.

# SQLite version 3.x
#   gem install sqlite3
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
  adapter: mysql2
  database: appname_development
  username: root
  pool: 5
  timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
  adapter: mysql2
  database: appname_test
  username: root
  pool: 5
  timeout: 5000

Here is a cleaner version

login: &login
  username: root
defaults: &defaults
  pool: 2
  wait_time: 5
  reconnect: true
  encoding: UTF8
  sock = ["/tmp/mysqld.sock", "/tmp/mysql.sock", "/var/run/mysqld/mysqld.sock", "/var/lib/mysql/mysql.sock"].detect do |socket|
<%= Rails.env %>: &<%= Rails.env %>_base
  adapter: mysql
  database: appname_<%= Rails.env %>
  socket: <%= sock %>
  <<: *defaults
  <<: *login

A bit contrived, but as Database.yml becomes more robust and uses mysql you can see how the later will scale. This is especially helpful when you have many environments like a distributed setup on EC2 with many development teams.

Leave a Reply

Your email address will not be published. Required fields are marked *