Ruby-on-rails 如何使用 Capistrano gem 为生产数据库做种?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7461985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:59:20  来源:igfitidea点击:

How to seed the production database using the Capistrano gem?

ruby-on-railsrubyruby-on-rails-3capistranoseed

提问by Backo

I am using Ruby on Rails 3.0.9 and I would like to seed the production databasein 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.

我正在使用 Ruby on Rails 3.0.9,我想为生产数据库做种,以便在不重新构建所有数据库的情况下添加一些记录(即,不删除所有现有记录,而只是添加一些尚不存在的记录) . 我想这样做是因为需要新数据才能使应用程序正常工作。

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

因此,由于我使用的是 Capistrano gem,因此我cap -T在控制台中运行该命令以列出所有可用命令并了解如何完成我的目标:

$ 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:seedcommand 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?

我不确定“使用种子数据重新加载数据库”中出现的“重新加载”一词。句子。所以,我的问题是:如果我cap deploy:seed在本地机器上的控制台中运行该命令,播种过程是否会删除生产数据库中的所有现有数据,然后填充它,或者该命令只是按照我的目标在该数据库中添加新数据做?

回答by Javier Vidal

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

如果您使用的是 bundler,那么 capistrano 任务应该是:

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.rband included in your deploy.rb file using following command:

并且可以lib/deploy/seed.rb使用以下命令将其放置在单独的文件中,例如并包含在您的 deploy.rb 文件中:

load 'lib/deploy/seed'

回答by Ioanna

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

卡皮斯特拉诺 3,导轨 4

回答by Martin Sommer

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

使用 Capistrano 3、Rails 4 和SeedMigrations,我在 /lib/capistrano/tasks 下创建了一个 Capistrano seed.rb 任务:

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! :)

我的种子迁移现在与我的架构迁移完全分开,并按照 db:migrate 过程运行。多么快乐!:)

回答by Mark

Try adding something like this in your deploy.rb:

尝试在您的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

回答by nowk

cap deploy:seedshould basically be a reference to rake db:seed. It should not delete existing data, unless you specified it to do so in your seed.rb.

cap deploy:seed基本上应该是对rake db:seed. 它不应删除现有数据,除非您在seed.rb.

Best assumption for the word "Reload" is that :seedis a stateless command, I does not automatically know where it left off, like regular rails migrations. So technically you would always be "reloading" the seed, every time you run it. ...A wild guess, but it sounds good, no?

“重新加载”一词的最佳假设是它:seed是一个无状态命令,我不会自动知道它在哪里停止,就像常规的 rails 迁移一样。因此,从技术上讲,每次运行它时,您总是会“重新加载”种子。......一个疯狂的猜测,但听起来不错,不是吗?



Please view Javier Vidalanswer below

请查看Javier Vidal下面的答案

回答by dei79

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.

在与 capistrano-rails gem 作者讨论后,我决定在一个单独的 gem 中实现这种任务。我认为这有助于遵循 DRY 的想法,而不是一遍又一遍地执行相同的任务。

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

希望对你有帮助:https: //github.com/dei79/capistrano-rails-collection