Ruby-on-rails 如何使用seed.rb 有选择地填充开发和/或生产数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8662127/
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 02:32:16  来源:igfitidea点击:

How to use seed.rb to selectively populate development and/or production databases?

ruby-on-railsruby-on-rails-3environment

提问by Darme

I am using seed.rb to populate both my development and production database. I usually populate the first with dummy data and the latter with the real minimal data that my app needs to run (e.g. the first user and so on).

我正在使用 seed.rb 来填充我的开发和生产数据库。我通常用虚拟数据填充第一个,然后用我的应用程序需要运行的真正最小数据(例如第一个用户等)填充后者。

How can I specify in seed.rb for what environment each data is?

如何在seed.rb 中指定每个数据的环境?

Given that I know "group" to be a Gemfile method, I'd like to achieve the same behavior for seed.rb.

鉴于我知道“group”是一种 Gemfile 方法,我想为 seed.rb 实现相同的行为。

E.g. I'd like to write something like this in my seed.rb:

例如,我想在我的 seed.rb 中写这样的东西:

group :development do 
  # development specific seeding code
end

group :production do 
  # production specific seeding code
end

# non-specific seeding code (it always runs) 

This to be able to call both the development-specific and the non-specific code with

这能够调用特定于开发的代码和非特定代码

$ rake db:seed

And to call both the production-specific and the non-specific code with:

并使用以下命令调用特定于生产的代码和非特定代码:

$ rake db:seed RAILS_ENV=production 

Thank you

谢谢

回答by cam

seeds.rbis just a plain ruby file, so there are several ways you could approach this. How about a case statement?

seeds.rb只是一个普通的 ruby​​ 文件,所以有几种方法可以解决这个问题。案例陈述怎么样?

# do common stuff here

case Rails.env
when "development"
   ...
when "production"
   ...
end

回答by fabro

Another approach could be creating:

另一种方法可能是创建:

db/seeds/development.rb
db/seeds/production.rb
db/seeds/any_other_environment.rb

Then in db/seeds.rb:

然后在db/seeds.rb

# Code you want to run in all environments HERE
# ...
load(Rails.root.join( 'db', 'seeds', "#{Rails.env.downcase}.rb"))

Then write the code you want to run for each environment in the respective file.

然后在各自的文件中编写要为每个环境运行的代码。

回答by yossico

another approach, quite similar to @fabro's answer: add a folder seeds to db/with the environment names and another named common.rb, so you get something like:

另一种方法,与@fabro 的答案非常相似:添加一个db/带有环境名称和另一个名为 common.rb的文件夹种子,因此您会得到如下内容:

db/seeds/common.rb
db/seeds/development.rb
db/seeds/staging.rb
db/seeds/production.rb

than, in your seed.rb:

比,在你的seed.rb

ActiveRecord::Base.transaction do
  ['common', Rails.env].each do |seedfile|
    seed_file = "#{Rails.root}/db/seeds/#{seedfile}.rb"
    if File.exists?(seed_file)
      puts "- - Seeding data from file: #{seedfile}"
      require seed_file
    end
  end
end

I perfer running the seed in one transaction

我更喜欢在一笔交易中运行种子