Skip to content

Commit

Permalink
Wrap database seeding in a reload
Browse files Browse the repository at this point in the history
It is quite possible that database seeding may run jobs inline. In that case a reload will be attempted. For app that is set to reload_classes_only_on_change this would normally not initiate a reload, since no files would have been changed. However, it is common to run the seeds as part of one command that also sets up the database (eg bin/rails db:prepare db:seed). In that case, the schema.rb file may have changes. By default that file is watched by the reloader. So, the application will be reloaded at the time the first job (or other reload) occurs. Reloading the application during the middle of running seeds can lead to unexpected and buggy behaviour.

Instead, we should wrap the entire seeds task in a reload so that the reload only occurs at the end of seeding.
  • Loading branch information
andrewn617 committed Sep 13, 2024
1 parent 281a00a commit 62b84fa
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,10 @@ db_namespace = namespace :db do

desc "Load the seed data from db/seeds.rb"
task seed: :load_config do
db_namespace["abort_if_pending_migrations"].invoke
ActiveRecord::Tasks::DatabaseTasks.load_seed
Rails.application.reloader.wrap do
db_namespace["abort_if_pending_migrations"].invoke
ActiveRecord::Tasks::DatabaseTasks.load_seed
end
end

namespace :seed do
Expand Down

0 comments on commit 62b84fa

Please sign in to comment.