Skip to content

Seeds not always run with db:prepare #50272

@DaAwesomeP

Description

@DaAwesomeP

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:

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

https://github.com/robertomiranda/rails/blob/fe006083ea0843ee37fb0259fb706a9ac5562066/activerecord/lib/active_record/railties/databases.rake#L222-L233

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions