A platform for community discussion. Free, open, simple.
DEV: Support per-unicorn-worker DB variable overrides take 2 (#35647)
Alan Guo Xiang Tand4781f0fe276
diff --git a/app/models/global_setting.rb b/app/models/global_setting.rbindex 490c5b9e38e..56454dbad25 100644--- a/app/models/global_setting.rb+++ b/app/models/global_setting.rb@@ -126,7 +126,8 @@ class GlobalSettinghostnamesend- def self.database_config+ def self.database_config(variables_overrides: {})+ variables_overrides = variables_overrides.with_indifferent_accesshash = { "adapter" => "postgresql" }%w[@@ -168,6 +169,11 @@ class GlobalSettingendend+ variables_overrides.each do |key, value|+ hash["variables"] ||= {}+ hash["variables"][key.to_s] = value+ end+{ "production" => hash }end@@ -366,13 +372,14 @@ class GlobalSettingattr_accessor :providerend- def self.configure!- if Rails.env.test?+ def self.configure!(+ path: File.expand_path("../../../config/discourse.conf", __FILE__),+ use_blank_provider: Rails.env.test?+ )+ if use_blank_provider@provider = BlankProvider.newelse- @provider =- FileProvider.from(File.expand_path("../../../config/discourse.conf", __FILE__)) ||- EnvProvider.new+ @provider = FileProvider.from(path) || EnvProvider.newendenddiff --git a/config/application.rb b/config/application.rbindex 24ea33f68cc..356b1fb3b70 100644--- a/config/application.rb+++ b/config/application.rb@@ -59,11 +59,11 @@ require_relative "../lib/require_dependency_backward_compatibility"module Discourseclass Application < Rails::Application- def config.database_configuration+ def config.database_configuration(variables_overrides: {})if Rails.env.production?- GlobalSetting.database_config+ GlobalSetting.database_config(variables_overrides:)else- super+ super()endenddiff --git a/lib/discourse.rb b/lib/discourse.rbindex 27abbd1815c..db174bec6d7 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -935,20 +935,20 @@ module Discourseenddef self.after_unicorn_worker_fork- unicorn_worker_db_variables_prefix = "DISCOURSE_UNICORN_WORKER_DB_VARIABLES_"- db_variables_overrides = {}+ variables_overrides = {}+ unicorn_worker_db_variables_prefix = "unicorn_worker_db_variables_"- ENV.filter do |key, value|+ GlobalSetting.provider.keys.each do |key|if key.start_with?(unicorn_worker_db_variables_prefix)- db_variables_overrides[- "DISCOURSE_DB_VARIABLES_#{key.sub(unicorn_worker_db_variables_prefix, "")}"- ] = value+ variables_overrides[+ key.to_s.sub(unicorn_worker_db_variables_prefix, "").downcase.to_sym+ ] = GlobalSetting.public_send(key)endend- if db_variables_overrides.any?- db_variables_overrides.each { |key, value| ENV[key] = value }- ActiveRecord::Base.configurations = Rails.application.config.database_configuration+ if variables_overrides.any?+ ActiveRecord::Base.configurations =+ Rails.application.config.database_configuration(variables_overrides:)ActiveRecord::Base.connection_handler.clear_all_connections!(:all)ActiveRecord::Base.establish_connectionenddiff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rbindex 17d16744372..f33c3608215 100644--- a/spec/lib/discourse_spec.rb+++ b/spec/lib/discourse_spec.rb@@ -81,6 +81,8 @@ RSpec.describe Discourse doActiveRecord::Base.configurations = original_configActiveRecord::Base.connection_handler.clear_all_connections!(:all)ActiveRecord::Base.establish_connection+ GlobalSetting.configure!+ GlobalSetting.load_defaultsexpect(ActiveRecord::Base.connection.execute("SHOW statement_timeout").first[@@ -90,15 +92,38 @@ RSpec.describe Discourse doendend- it "applies worker-specific database variable overrides" do- ENV["DISCOURSE_DB_VARIABLES_STATEMENT_TIMEOUT"] = "5s"- ENV["DISCOURSE_UNICORN_WORKER_DB_VARIABLES_STATEMENT_TIMEOUT"] = "100s"+ it "applies worker-specific database variable overrides in a production environment" do+ test_database_config = Rails.application.config.database_configuration["test"]++ # In the production environment, `DISCOURSE_` ENV variables are written to the `discourse.conf` file so we need+ # to simulate that here in the test environment.+ temp_discourse_conf = Tempfile.new("discourse.conf")+ temp_discourse_conf.write <<~TEXT+ db_name = #{test_database_config["database"]}+ db_username = ""+ db_variables_statement_timeout = 10s+ unicorn_worker_db_variables_statement_timeout = 100s+ TEXT+ temp_discourse_conf.rewind++ Rails.stubs(:env).returns(ActiveSupport::StringInquirer.new("production"))+ GlobalSetting.configure!(path: temp_discourse_conf.path, use_blank_provider: false)+ GlobalSetting.load_defaultsDiscourse.after_unicorn_worker_forkexpect(ActiveRecord::Base.connection.execute("SHOW statement_timeout").first["statement_timeout"],).to eq("100s")+ ensure+ %i[+ db_variables_statement_timeout+ unicorn_worker_db_variables_statement_timeout+ ].each { |method| GlobalSetting.singleton_class.remove_method(method) }++ allow(Rails).to receive(:env).and_call_original+ temp_discourse_conf&.close+ temp_discourse_conf&.unlinkendend