-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Description
Hello,
It is my understanding that db:seed
should be idempotent and should be able to be run at any time. As such, its seems like it should be run once during each invocation of db:prepare
after the db:migrate
step.
Example scenario: You have an existing app that already has a DB set up. You then create a model and migration called GlobalSettings
. This model must always have exactly one row, which is added in seeds.rb
with GlobalSettings.create() if GlobalSettings.count == 0
. You run the migration and then the seeds to update the app.
I ran into this when I had the above scenario and deployed my app via the scaffolded Docker container and entrypoints. The scaffolded entrypoint runs db:prepare
(which handles initial setup and all migrations), but this does not handle migrations requiring db:seed
to be run again.
Related issues/pulls:
- Add db:prepare rake task. #35768
- Seed task runs multiple times and breaks with multi-database setup #44957
- Fix
db:prepare
to run seeds once #44958 - [ci-skip][Docs]Add description for
db:prepare
command #49480
Related code (see that db:setup
runs db:seed
but only if the DB does not exist, db:seed
is not run after db:migrate
):
The template for seeds.rb
says:
# This file should ensure the existence of records required to run the application in every environment (production,
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).