Keeping uploaded files between deployments

Problem
You are using file_column plugin (or maybe another plugin?), to upload files in your ruby on rails application. Because the files are big you don’t want to have a different copy stored in your subversion repository for each different deployment version. You want to keep a common folder with all your uploaded files, and use it with every different deployment version.

Solution
For the example, we will have a numbers table that has two file_column uploadable columns (intro,voice_mail), with the following migration:

class CreateNumbers < ActiveRecord::Migration
  def self.up
    create_table :numbers do |t|
      t.column :customer_id, :int, :null => false
      t.column :phone_no, :string, :null => false
      t.column :intro, :string
      t.column :vmail, :string
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end

  def self.down
    drop_table :numbers
  end
end
  1. If you already have used cap deploy or cap setup you should have a shared folder in your deployment server.
    You should copy the intro and vmail folders that should be located on your public/number folder on your local development client, on a folder called number in your development server in your shared folder.
  2. Create a file in your local pc in lib/cap_recipes.rb:
    Capistrano.configuration(:must_exist).load do
    
      desc "Keep generated uploaded files between deployments"
        task :after_symlink do
           run "rm -drf #{deploy_to}/#{current_dir}/public/number"
           sudo "ln -nfs #{shared_path}/number #{deploy_to}/#{current_dir}/public"
        end
      end

    Be careful with the naming of the task as (at least for capistrano 1.4 that I’m using), it must have a special name as after_symlink.
    Also be careful that if you try to use before_symlink, it won’t work as the current symlink won’t be setup.

  3. In your config/deploy.rb file add at the top the following:
    require 'lib/cap_recipes'
  4. Now you should be ready to deploy your new version so:
    svn -m "added customised capistrano recipes" commit

    make sure that you check in your version in subversion, and then:

    cap deploy

    you should be able to see a link in your current/public folder called number that points to the shared/number folder that hold all the uploaded files.