Skip to content

Instantly share code, notes, and snippets.

@Traxmaxx
Created November 27, 2012 15:59
Show Gist options
  • Save Traxmaxx/4155057 to your computer and use it in GitHub Desktop.
Save Traxmaxx/4155057 to your computer and use it in GitHub Desktop.
Override database config if values are nil and ENV vars are set
module MyApp
class Application < Rails::Application
def config.database_configuration
# detect rails environment, otherwise set it to development
rails_env = ENV['RAILS_ENV'] || 'development'
config = super
# set creds for the different adapters
case config[rails_env]['adapter']
when 'mysql2' # MySQLS and MySQLD
config[rails_env].each do |key, value|
if value === nil # get value from cctrl ENV if nil
if key === 'username' # auto match breaks on username so we need to look for it manually
config[rails_env][key] = ENV["MYSQLD_USER"] || ENV["MYSQLS_USER"] || nil
else
config[rails_env][key] = ENV["MYSQLD_#{key.upcase}"] || ENV["MYSQLS_#{key.upcase}"] || nil
end
end
end
end
return config
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment