ruby-on-rails:播种数据策略(或将测试数据加载到开发人员数据库中)

发布于 2024-08-23 19:08:54 字数 129 浏览 4 评论 0原文

我想经常清除并重新加载我的开发人员数据库(Ruby on Rails)。

当然,我可以通过网页手动添加数据,但我想知道是否有人有此类测试的策略。

(我已经有单元、功能和集成测试,仅供参考)

谢谢

I want to clear and re-load my developer database (Ruby on rails) frequently.

Of course, I can manually add the data via the web page but I was wondering if anyone has any strategies for this type of testing.

(I already have unit, functional and integration tests, fyi)

Thanks

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

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

发布评论

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

评论(2

不奢求什么 2024-08-30 19:08:55

db 目录中创建一个 seed.yml 文件。为您要创建的每个模型添加一个 YAML 文档。该文件应包含哈希列表。每个哈希应该包含模型属性。

  users:
      -   login: jake
          password: jake123
          password_confirmation: jake123
          first_name: Jake
          last_name: Driver

      -   login: Jane
          password: jane123
          password_confirmation: jane123
          first_name: Jane
          last_name: McCain

  categories:

  products:

在您的 seed.rb 文件中,

seed_file = File.join(Rails.root, 'db', 'seed.yml')
config = YAML::load_file(seed_file)
User.transaction do 
  config.keys.each{ |key| key.classify.constantize.create(config[key]) }
end

我发现修改 YML 文件中的种子数据更容易。我构建的应用程序由不同的团队部署。他们也喜欢这种方法。

为了清除数据,我在 lib\tasks 目录中有一个 rake 任务。我将 rake 任务运行为 app:flush

namespace :app do
  desc "Flush all the seed data "
  task :flush => :environment do
    config = YAML::load_file(File.join(Rails.root, 'db', 'seed.yml'))
    User.transaction do 
      config.keys.each{ |table| truncate_table(table)}
    end
  end
end

Create a seed.yml file in db directory. Add a YAML document for each model you want to create. This document should contain a list of hash. Each hash should contain model attributes.

  users:
      -   login: jake
          password: jake123
          password_confirmation: jake123
          first_name: Jake
          last_name: Driver

      -   login: Jane
          password: jane123
          password_confirmation: jane123
          first_name: Jane
          last_name: McCain

  categories:

  products:

In your seed.rb file

seed_file = File.join(Rails.root, 'db', 'seed.yml')
config = YAML::load_file(seed_file)
User.transaction do 
  config.keys.each{ |key| key.classify.constantize.create(config[key]) }
end

I find it easier to modify the seed data in the YML file. Application that I have built is deployed by a different team. They like this approach too.

To clear the data I have a rake task in lib\tasks directory. I run the rake task as app:flush.

namespace :app do
  desc "Flush all the seed data "
  task :flush => :environment do
    config = YAML::load_file(File.join(Rails.root, 'db', 'seed.yml'))
    User.transaction do 
      config.keys.each{ |table| truncate_table(table)}
    end
  end
end
爱冒险 2024-08-30 19:08:55

是时候看看“夹具”和“播种数据”了;-)我不足以给你一个清晰的解释,但是谷歌搜索这两个键应该会给你你需要的一切。

查看这些:http://derekdevries.com/2009/04/13 /rails-seed-data/
http://lptf.blogspot.com/2009/ 09/seed-data-in-rails-234.html

Time to look at "fixtures" and "seeding data" ;-) I am not good enough to give you a clear explanation, but googling those two keys should give you all you need.

Check these out: http://derekdevries.com/2009/04/13/rails-seed-data/
http://lptf.blogspot.com/2009/09/seed-data-in-rails-234.html

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