Ruby in the config file.

Since the database.yml file is processed by eRB, here’s what I do to keep connection and password information specific to the machine:

==== config/database.yml ====

vvv Copy the following two YAML maps to a file called config/mydatabase.yml

login: &login

username: sample

password: sample

connection: &connection

host: 127.0.0.1

port: 3306

^^^ Copy the previous two YAML maps to a file called config/mydatabase.yml

<%= file = File.join(RAILS_ROOT, “config”, “mydatabase.yml”)

IO.read(file) if File.exist?(file) %>

development:

adapter: mysql

database: top_development

<<: *login

<<: *connection

same for test and production

#END

==== config/mydatabase.yml ====

“rob-biedenharns-computer.local”

login: &login

username: sample

password: sample

connection: &connection

host: localhost

socket: /tmp/mysql.sock

#END

Note that the YAML maps stay in config/database.yml as the defaults. If the file config/mydatabase.yml exists, it can redefine either or both the ‘login’ or ‘connection’ maps. In this case, my own local one uses the same default username and password for the login, so it’s not redefined. The connection, however, is via a socket on my Mac and there are similar config/mydatabase.yml files on the other development machines and on the staging and production servers.

The config/database.yml file is kept in version control, but the config/mydatabase.yml file is NOT. I have manual backup copies of a TxD-mydatabase.yml and so on for other machines, but the local login and connection information stays only on the machine it is set for.

-Rob

Rob Biedenharn http://agileconsultingllc.com

Rob@AgileConsultingLLC.com