Managing database.yml with Capistrano 2.0

Posted by shane
on Wednesday, May 30

Jeremy Voorhis posted a really great Capistrano recipe for managing database.yml which dynamically creates a database.yml file in your shared directory on setup, and symlinks your app’s database.yml once it’s deployed. This is great if you don’t version control your database.yml file for security reasons or working with multiple developers.

changes the syntax for task callbacks and gets rid of the useful render method.  However, using ERb, Ruby's built-in templating system, isn't much more difficult than using the old render method.  Here is Jeremy's script updated for Capistrano 2.0 using ERb and the new namespaced callback syntax.
require 'erb'

before "deploy:setup", :db
after "deploy:update_code", "db:symlink" 

namespace :db do
  desc "Create database yaml in shared path" 
  task :default do
    db_config = ERB.new <<-EOF
    base: &base
      adapter: mysql
      socket: /tmp/mysql.sock
      username: #{user}
      password: #{password}

    development:
      database: #{application}_dev
      <<: *base

    test:
      database: #{application}_test
      <<: *base

    production:
      database: #{application}_prod
      <<: *base
    EOF

    run "mkdir -p #{shared_path}/config" 
    put db_config.result, "#{shared_path}/config/database.yml" 
  end

  desc "Make symlink for database yaml" 
  task :symlink do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" 
  end
end

Until I get better syntax highlighting for this blog, check out the Pastie for the color version. For more info on whats new in Capistrano 2.0, check out Jamis’ preview and Geoff’s post. Also, props to Jamis for suggesting I use ERb directly.

Update: Updated code to use its own :db namespace instead of the default one. The database yaml file will be created by the default :db task, and the symlink will be created by the db:symlink task. Note how namespaces in Cap 2.0 allows us to have two symlink tasks, one in the deploy namespace and the other in db.

Comments

Leave a response

  1. joshJune 13, 2007 @ 09:58 AM
    I had some problems with yaml parsing that until I de-indented the ERB so that it matches how the normal database.yml is layed out (double space indents, no tabs). Otherwise, works like a champ. Ahh capistrano, how I love thee ;)
  2. shaneJune 13, 2007 @ 10:23 AM
    Thanks for the tip josh. Probably a copy/paste thing.
  3. Frederico AraujoDecember 03, 2007 @ 06:10 AM

    thanks for the post. helped a lot.

    I modified to my gentoo linux.

    here is my setup:

    namespace :db do desc "Create database yaml in shared path" task :default do database_config = ERB.new <<-EOF base: &base adapter: mysql # for Gentoo linux this is the sock file for mysql socket: /var/run/mysqld/mysqld.sock username: root password: development: database: #{application}_development <<: *base test: database: #{application}_test <<: *base production: database: #{application}_production <<: *base EOF mongrel_cluster_config = ERB.new <<-EOF --- cwd: #{current_path} log_file: #{shared_path}/log/mongrel.log port: "8001" environment: production address: 127.0.0.1 pid_file: #{shared_path}/mongrel.pid servers: 6 EOF run "mkdir -p #{shared_path}/config" put database_config.result, "#{shared_path}/config/database.yml" put mongrel_cluster_config.result, "#{deploy_to}/#{shared_dir}/config/mongrel_cluster.yml" end end
  4. viagraJune 15, 2008 @ 03:56 AM

    Article Opinion

Comment