如何使用 Seed.rb 有选择地填充开发和/或生产数据库?

发布于 2024-12-23 09:01:35 字数 596 浏览 0 评论 0原文

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

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

鉴于我知道“group”是一个 Gemfile 方法,我希望为 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) 

这样就能够使用以下方式调用特定于开发的代码和非特定的代码,

$ rake db:seed

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

$ rake db:seed RAILS_ENV=production 

谢谢

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).

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

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

E.g. I'd like to write something like this in my 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

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

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

发布评论

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

评论(3

<逆流佳人身旁 2024-12-30 09:01:35

seeds.rb 只是一个普通的 ruby​​ 文件,因此有多种方法可以实现此目的。案例陈述怎么样?

# do common stuff here

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

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

# do common stuff here

case Rails.env
when "development"
   ...
when "production"
   ...
end
人│生佛魔见 2024-12-30 09:01:35

另一种方法可以是创建:

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

然后在 db/seeds.rb 中:

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

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

Another approach could be creating:

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

Then in 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.

柏拉图鍀咏恒 2024-12-30 09:01:35

另一种方法,与@fabro的答案非常相似:
将文件夹 seeds 添加到 db/ 中,其中包含环境名称和另一个名为 common.rb 的文件夹,这样您就可以

db/seeds/common.rb
db/seeds/development.rb
db/seeds/staging.rb
db/seeds/production.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一笔交易中的种子

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:

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

than, in your 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

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