Posts Tagged Rails

Rails deploy using sqlite3

I had a hard time figuring out why, when reading up on capistrano, I couldn’t find any info on how to deal with the database file. That was until I realized most people don’t deploy on sqlite3. With mysql and other databases you have a server and it’s automatically “shared” then.

The only information I found on deployment on sqlite3 was an excellent deploy script in this blog article. Basically, using sqlite3 you have to make sure the database is in a shared directory across releases. But the sqlite3 parts of that deploy script didn’t work for me as they were. I had to make sure I referenced the shared database path all the way or I risked overwriting my database with a symlink. Below are the sqlite3 parts of the resulting capistrano script.

Setting up the shared database path. NB: Lazy binding. Important if you’re using multistaging from capistrano-ext.

set(:shared_database_path) {"#{shared_path}/databases"}

Sqlite3 tasks.

namespace :sqlite3 do
  desc "Generate a database configuration file"
  task :build_configuration, :roles => :db do
    db_options = {
      "adapter"  => "sqlite3",
      "database" => "#{shared_database_path}/production.sqlite3"
    }
    config_options = {"production" => db_options}.to_yaml
    put config_options, "#{shared_config_path}/sqlite_config.yml"
  end
 
  desc "Links the configuration file"
  task :link_configuration_file, :roles => :db do
    run "ln -nsf #{shared_config_path}/sqlite_config.yml #{release_path}/config/database.yml"
  end
 
  desc "Make a shared database folder"
  task :make_shared_folder, :roles => :db do
    run "mkdir -p #{shared_database_path}"
  end
end

Hooks (or whatever they’re called, I’m new to all this).

after "deploy:setup", "sqlite3:make_shared_folder"
after "deploy:setup", "sqlite3:build_configuration"
 
before "deploy:migrate", "sqlite3:link_configuration_file"

Hope this helps someone. Please don’t hesitate to ask should something need more explaining. And of course suggest improvements. This is all the makings of a capistrano newbie.

, , ,

1 Comment