Custom database.yml files for development environments
July 14, 2009 at 03:46 PM · Posted under · Tagged with config, configuration, database, environment, rails
In a previous development role we used to have everyone with their own database.yml file, based on the hostname. However, this was also loaded within the database.yml file itself. It would look for a custom database configuration file then load that in to itself, overloading any previously-defined configuration settings for dev/test. This was great, but was a little messy. An easier way to do this, is to define the configuration file that gets loaded within your environment.rb file:
// this goes within the Rails::Initializer block
require 'socket'
database_file = ['config/', Socket.gethostname, '-database.yml'].join
config.database_configuration_file = database_file if File.exists? database_file
Effectively what we're doing here is looking for a file within the config directory that matches something like: my-hostname-database.yml, and if it finds it - it will load this, otherwise it will retain the default config/database.yml.
Permalink