From 62b84fa66fa0dbf68c483cee972adc4898803a35 Mon Sep 17 00:00:00 2001 From: Andrew Novoselac Date: Fri, 13 Sep 2024 14:23:44 -0400 Subject: [PATCH] Wrap database seeding in a reload 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. --- activerecord/lib/active_record/railties/databases.rake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 5661bde6f2db0..ade1ea4bda1ac 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -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