This post originated from an RSS feed registered with Ruby Buzz
by Jeremy Voorhis.
Original Post: Managing database.yml with Capistrano
Feed Title: JVoorhis
Feed URL: http://feeds.feedburner.com/jvoorhis
Feed Description: JVoorhis is a Rubyist in northeast Ohio. He rambles about Ruby on Rails, development practices, other frameworks such as Django, and on other days he is just full of snark.
At PLANET ARGON, we typically do not keep our database.yml files in subversion, leaving behind a database.yml.example file instead. This makes it easy for us to share the repository with multiple developers who work on their own conventions, or sometimes an alternate database engine. We still want to have a one-step automated deployment process, however, and ssh-ing into the server to create database.yml manually just feels a little… uncouth.
The solution? Building upon Tim’s post at “http://toolmantim.com/article/2006/5/26/setting_up_capistrano_on_segpub”, our Capistrano deployment recipe now writesdatabase.yml after we deploy, and symlinks it to the latest release directory after we update the code. Here is the code that lets us do that:
desc "Create database.yml in shared/config"
task :after_setup do
write_database_configuration
end
desc "Link in the production database.yml"
task :after_update_code do
link_database_configuration
end
def write_database_configuration
FileUtils.mkdir_p "#{shared_dir}/config"
File.open("#{shared_dir}/config/database.yml", 'w') do |f|
f.write %Q{login: &login
adapter: postgresql
host: localhost
port: #{postgresql_port}
username: #{user}
password: #{password}
development:
database: #{application}_development
<<: *login
test:
database: #{application}_test
<<: *login
production:
database: #{application}_production
<<: *login
}
end
end
def link_database_configuration
run "ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml"
end