How to seed the production database using the Capistrano gem?
Solution 1
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'
Solution 2
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
Solution 3
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! :)
Solution 4
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
Solution 5
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
Backo
Updated on May 23, 2020Comments
-
Backo almost 4 years
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? -
Ain Tohvri over 12 yearscap deploy:seed throws "the task `deploy:seed' does not exist"
-
Backo about 12 yearsWhat is the reason why it might be placed in a separate file?
-
Geekygecko about 12 yearsSometimes 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'
-
Alex Bush almost 12 yearsThanks a lot!!! run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}" worked lika charm :D
-
Peter Lee over 11 years
rails$ cap -e deploy:seed
The task 'deploy:seed' does not exist.
-
Evan over 11 yearsActually, 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 -
PeppyHeppy about 11 yearsdon'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 ...
-
notapatch almost 10 yearsFollowing 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
-
Simon Hürlimann almost 10 yearsPut that one into lib/capistrano/tasks and call it XXX.rake
-
lucke84 almost 10 years+1 for a nice and clean usage of
with rails_env: fetch(:stage)
-
Cyril Duchon-Doris almost 9 yearsThe answer is not below anymore, but above :P
-
Troy about 8 yearsI think it needs to be named
seed.rake
to get it to autoload using the default Capfile. -
phillyslick almost 8 yearsCapistrano 3: See @JoannaK's answer below!