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`
This commit is contained in:
David Taylor 2025-02-03 15:51:52 +00:00 committed by GitHub
parent 8d810f9271
commit c8718a64dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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|