From c8718a64dd7a26165efe91706ce5507e6999044a Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 3 Feb 2025 15:51:52 +0000 Subject: [PATCH] DEV: Re-exec rake after creating database (#31120) By design, db:create initializes the Rails app with SKIP_DB=true. That means that SiteSettings get set up with the LocalProcessProvider instead of the DBProvider. In other words: any calls to site settings will return the default, rather then the actual value in the database. Running db:migrate in the same rake invocation means that rails will not be re-initialized, and so skip_db will remain true. Site settings accessed during migrations and fixtures will therefore return incorrect values. One example of this is that running bin/rake db:create db:migrate repeatedly in a development environment will cause the FAQ topic to be seeded repeatedly, because the seed logic does not have access to the site setting which stores the already-seeded topic id. This commit will automatically re-exec the Rake command if any tasks are specified after `db:create` --- lib/tasks/db.rake | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 3abc3e5a520..d393f392e50 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -54,7 +54,14 @@ end begin reqs = Rake::Task["db:create"].prerequisites.map(&:to_sym) Rake::Task["db:create"].clear_prerequisites - Rake::Task["db:create"].enhance(["db:force_skip_persist"] + reqs) + Rake::Task["db:create"].enhance(["db:force_skip_persist"] + reqs) do + # after creating the db, we need to fully reboot the Rails app to make sure + # things like SiteSetting work correctly for future rake tasks. + db_create_index = ARGV.find_index("db:create") + if db_create_index < ARGV.length - 1 + exec "#{Rails.root}/bin/rake", *ARGV[db_create_index + 1..-1] + end + end end task "db:drop" => [:load_config] do |_, args|