Search code examples
ruby-on-railsrubyruby-on-rails-3capistranoseed

How to seed the production database using the Capistrano gem?


I am using Ruby on Rails 3.0.9 and I would like to seed the production database in order to add some record without re-building all the database (that is, without delete all existing records but just adding some of those not existing yet). I would like to do that because the new data is needed to make the application to work.

So, since I am using the Capistrano gem, I run the cap -T command in the console in order to list all available commands and to know how I can accomplish what I aim:

$ cap -T
=> ...
=> cap deploy:seed          # Reload the database with seed data.
=> ...

I am not sure on the word "Reload" present in the "Reload the database with seed data." sentence. So, my question is: if I run the cap deploy:seed command in the console on my local machine will the seeding process delete all existing data in the production database and then populate it or will that command just add the new data in that database as I aim to do?


Solution

  • If you are using bundler, then the capistrano task should be:

    namespace :deploy do
      desc "reload the database with seed data"
      task :seed do
        run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
      end
    end
    

    and it might be placed in a separate file, such as lib/deploy/seed.rb and included in your deploy.rb file using following command:

    load 'lib/deploy/seed'