Managing database.yml with Capistrano
Posted by Jeremy Voorhis Thu, 06 Jul 2006 21:43:00 GMT
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 writes database.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
database_configuration = render :template => <<-EOF
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
EOF
run "mkdir -p #{deploy_to}/#{shared_dir}/config"
put database_configuration, "#{deploy_to}/#{shared_dir}/config/database.yml"
end
desc "Link in the production database.yml"
task :after_update_code do
run "ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml"
end
UPDATE
I’ve revised the code on 10 June. Use this version.

Wow, thanks for sharing this, Jeremy!
It’s a lot better than what I had been doing. I have a database.yml.deploy that gets copied into place by my deployment recipe, but that’s obviously no good! I’ll be using your method from here on :)
wow, this is great, just to clarrify, will capistrano only run this once after the initial deployment setup, or is this run on every deployment??? cheers dazza
It will write
database.ymlafter running thesetuptask, and it will create the symlink whenever the code is updated.