37

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?

Backo
  • 18,291
  • 27
  • 103
  • 170

5 Answers5

72

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'
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Javier Vidal
  • 1,311
  • 11
  • 8
  • What is the reason why it might be placed in a separate file? – Backo Feb 22 '12 at 19:13
  • 2
    Sometimes it is nice to have your common definition in a separate file so it is easy to port to another project. If you added the lib/deploy/seed.rb file you add the following to the top of your deploy.rb file to include it: load 'lib/deploy/seed' – Geekygecko Apr 27 '12 at 01:58
  • 1
    Thanks a lot!!! run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}" worked lika charm :D – Alex Bush Jun 28 '12 at 00:53
  • 7
    Actually, you want: `run "cd #{release_path} && bundle exec rake db:seed RAILS_ENV=#{rails_env}"` You probably want to be in the version you have deployed (and to seed before you roll your code over). The && also ensure that you can change dirs and will fail if you can't – Evan Dec 11 '12 at 00:40
  • 5
    don't forget to specify the roles that you want to run it. If you have multiple machines you might consider only running it on your db. `task :seed, :roles => :db do ...` – PeppyHeppy Apr 11 '13 at 08:40
  • Capistrano 3: See @JoannaK's answer below! – phillyslick Jun 25 '16 at 03:02
30

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

Ioanna
  • 301
  • 3
  • 5
12

Using Capistrano 3, Rails 4, and SeedMigrations, I created a Capistrano seed.rb task under /lib/capistrano/tasks:

namespace :deploy do
  desc 'Runs rake db:seed for SeedMigrations data'
  task :seed => [:set_rails_env] do
    on primary fetch(:migration_role) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "db:seed"
        end
      end
    end
  end

  after 'deploy:migrate', 'deploy:seed'
end

My seed migrations are now completely separate from my schema migrations, and ran following the db:migrate process. What a joy! :)

Martin Sommer
  • 536
  • 5
  • 14
  • I think it needs to be named `seed.rake` to get it to autoload using the default Capfile. – Troy Mar 29 '16 at 03:09
8

Try adding something like this in your deploy.rb:

namespace :deploy do
  desc "reload the database with seed data"
  task :seed do
    run "cd #{current_path}; rake db:seed RAILS_ENV=#{rails_env}"
  end
end
Mark
  • 7,507
  • 12
  • 52
  • 88
6

After a discussion with capistrano-rails gem authors I decided to implement this kind of tasks in a separate gem. I think this helps to follow the DRY idea and not implementing the same task over and over again.

I hope it helps you: https://github.com/dei79/capistrano-rails-collection

dei79
  • 186
  • 2
  • 5
  • 1
    Following dei79's github page I got a quick result: 1. Gemfile: `gem 'capistrano-rails-collection'` 2. `bundle install` 3. Capfile: `require 'capistrano/rails/collection'` 4. `cap production rails:rake:db:seed` – notapatch May 05 '14 at 13:15