New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the purpose of db/seeds.rb clearer through a better description and sample code. #47222
Conversation
Hmm... I've seen seeds used to good effect for [consciously idempotent] definition of development sample data. (And on the other hand, there are few first-look experiences as painful as cloning a nontrivial new-to-me application, starting it up, and staring at a completely empty UI.) |
@matthewd It is possible to make good use through environment checks, but that should be obvious once you know the correct purpose of db seeds. The point is: it should be ok to run In my opinion, the current description is almost there when it says "to seed the database with its default values" -- it's clearly not environment-specific. But, it would be better to emphasize that just a bit more, which is what I'm trying to do here. An example of possible acceptable use IMO. zoo_foo_type = FooType.find_or_create_by(name: 'zoo')
boo_foo_type = FooType.find_or_create_by(name: 'boo')
if Rails.env.development?
Foo.find_or_create_by(slug: 'bar', type: zoo_foo_type)
end Knowing that it should be ok to run seed on any environment will make the dev realize that if they want env-specific logic, they need to run that section with a conditional. |
Perhaps a cleaner and flexible option would be to offer seed files per environment: # Always loaded:
db/seed/shared.rb
# Loaded depending on Rails.env
db/seed/development.rb
db/seed/test.rb
db/seed/production.rb |
railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt
Outdated
Show resolved
Hide resolved
I do not think people should be running seeds in production except for maybe in the very beginning of application development. Even then, I'd probably create separate demo data if I really wanted seed-like data in production. I think encouraging their use in production without sufficient warning is a recipe for a production incident and lost data. IMO seeds are only for dev environments, I would never use them elsewhere. |
Agree with Eileen here, and if they are split by environment (e.g. I think the more conservative approach is to raise a warning when trying to run seeds in production mode. |
@eileencodes @zzak Read DHH's comment when seeds was introduced:
Other comments on that commit worth reading: @dhh perhaps you can chime in here to help us with some verbiage that clarifies the purpose of seeds.rb. Its purpose was not fake or test data. Many applications require some records to exist in every environment for its basic functionality. The example in this PR is movie genres. Some other cases that I've seen:
|
db/seeds.rb is indeed intended to work for production data. When the app requires the database to have some basic seeding in place. I do agree with Elaine that this seeding is meant for the INITIAL setup of the database, and we should either make that clear, or make it clear that seeds.rb needs to be idempotent. |
@dhh If the seeds.rb isn't idempotent, then you would need to write migrations to add more seed data as the application evolves - invalidating the initial purpose of seeds.rb. For that reason, I've always written idempotent seeds.rb -- and also the reason I used
If that was the case, I'm afraid that seeds.rb would become a legacy file too early in the development of an app. |
@rafaelsales Can you squash your commits please? |
@zzak done. |
Hmm, I think this got merged a bit too quickly. I think the name
https://guides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data @rafaelsales Do you always run the seeds automatically for each deployment? I'm also afraid someone might run The example of movie genres doesn't imply a seed file that could be run anytime on production. |
Motivation / Background
Many developers assume that db/seeds.rb is a good place to put sample development data or test fixtures. As most rails projects evolve with time, devs realize that there's a reason this file is simply called "seeds.rb" -- nothing indicates that it's exclusive to an environment, which means that it should be good to run in every environment.
Detail
This Pull Request changes the
db/seeds.rb
template file to make the purpose of that file clearer.Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]