如何使用 Capistrano gem 为生产数据库播种?

发布于 2024-12-05 05:11:49 字数 509 浏览 0 评论 0原文

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

颜漓半夏 2024-12-12 05:11:49

如果您使用捆绑器,那么 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

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

load 'lib/deploy/seed'

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'
小梨窩很甜 2024-12-12 05:11:49

这对我有用:

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

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

莫相离 2024-12-12 05:11:49

使用 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

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

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

妄想挽回 2024-12-12 05:11:49

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

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
撞了怀 2024-12-12 05:11:49

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文