FIX: site settings loading default values when no db

FIX: site settings loading default values when no db

This fixes a condition where an intermittent db connection could cause invalid site settings to be stored

It also removes a catch all we had.

Somewhere around Rails 5 db:create started wanting full environment this is a problem for Discourse since it needs to boot up data from the db.

This removes the catch all and surgically adds a db / redis bypass to db:create task.

diff --git a/lib/site_settings/db_provider.rb b/lib/site_settings/db_provider.rb
index 40c3f82..6f29199 100644
--- a/lib/site_settings/db_provider.rb
+++ b/lib/site_settings/db_provider.rb
@@ -58,12 +58,7 @@ class SiteSettings::DbProvider
   # table is not in the db yet, initial migration, etc
   def table_exists?
     @table_exists ||= {}
-    begin
-      @table_exists[current_site] ||= ActiveRecord::Base.connection.table_exists?(@model.table_name)
-    rescue
-      STDERR.puts "No connection to db, unable to retrieve site settings! (normal when running db:create)"
-      @table_exists[current_site] = false
-    end
+    @table_exists[current_site] ||= ActiveRecord::Base.connection.table_exists?(@model.table_name)
   end
 
 end
diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake
index b5e4d77..d55ecb6 100644
--- a/lib/tasks/db.rake
+++ b/lib/tasks/db.rake
@@ -21,12 +21,23 @@ task 'db:environment:set' => [:load_config]  do |_, args|
   end
 end
 
-task 'db:create' => [:load_config] do |_, args|
+task 'db:force_skip_persist' do
+  GlobalSetting.skip_db = true
+  GlobalSetting.skip_redis = true
+end
+
+task 'db:create' do |_, args|
   if MultisiteTestHelpers.load_multisite?
     system("RAILS_DB=discourse_test_multisite rake db:create")
   end
 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)
+end
+
 task 'db:drop' => [:load_config] do |_, args|
   if MultisiteTestHelpers.load_multisite?
     system("RAILS_DB=discourse_test_multisite rake db:drop")

GitHub sha: 3af00a65

5 Likes

DEV: improve on rake db:create