In short: With the help of the friendly folks in #rails-contrib, I've been working on a patch that provides Ruby configuration of your database connections. I'm looking for people to test the patch and post +1s if it works for them. You can find the patch in Lighthouse: #312 Database config in Ruby - Ruby on Rails - rails (its the last attachment, dry_database_config.diff)
## What's it look like?
In config/environment.rb:
config.active_record.connection.configure do |db| db.adapter = 'mysql' db.encoding = 'utf8' end
In config/development.rb:
config.active_record.connection.configure do |db| db.database = 'test_app_development' db.socket = '/tmp/mysql.sock' db.username = 'root' db.password = '' end
So the short of it is that you configure global stuff in environment.rb and environment-specific stuff in the respective files. This generates the connection-specification hashes that ActiveRecord::Base.establish_connection expects.
## Why change?
database.yml is workable, but its a little weird at this point. There is no other instance of YAML configuration in one's app when it runs in production. This approach also lets you significantly DRY up your configuration. Its even possible to specify the entire configuration in environment.rb.
Idatabase.yml is still loaded if you don't specify the database connection in your environment files. I wouldn't up and take it away from you like that.
## Securing your database credentials
We all know its a good idea to keep your database credentials (username/password) out of source control. This patch supports that raising an exception if you try to set your username or password in a production setting. Instead, you specify a credentials file like so:
config.active_record.configure do |db| db.database = 'test_app_production' db.socket = '/var/run/mysql.sock' db.credentials = "#{RAILS_ROOT}/config/credentials.rb" end
The preferred credential format is Ruby, like so:
username = 'root' password = ''
YAML is also supported:
username: root password:
## Other bits and bobs
I also patched the application generator to produce an app with no database.yml. Database config bits are added to environment.rb. The adapter-specific comments get added in environment.rb.
## Did I mention I'm looking for +1s
Yes, the pandering is strong with this one. Grab dry_database_config.diff from #312 Database config in Ruby - Ruby on Rails - rails and give it a spin.
Thanks in advance!