Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 966
Backo Avatar asked Sep 18 '11 14:09

Backo


2 Answers

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'
like image 109
Javier Vidal Avatar answered Oct 19 '22 18:10

Javier Vidal


This worked for me:

task :seed do
 puts "\n=== Seeding Database ===\n"
 on primary :db do
  within current_path do
    with rails_env: fetch(:stage) do
      execute :rake, 'db:seed'
    end
  end
 end
end

capistrano 3, Rails 4

like image 30
Ioanna Avatar answered Oct 19 '22 18:10

Ioanna