From 2ce829cc01c6606c014194259fe6ba1e60e6b241 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 10 Jun 2020 14:03:20 +0800 Subject: [PATCH] DEV: Allow multisite apps to boot with reading connection handler. --- Gemfile.lock | 2 +- config/initializers/002-rails_failover.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 189cfed62ac..2870d86e7b8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -288,7 +288,7 @@ GEM nokogiri (>= 1.6) rails-html-sanitizer (1.3.0) loofah (~> 2.3) - rails_multisite (2.2.2) + rails_multisite (2.3.0) activerecord (> 5.0, < 7) railties (> 5.0, < 7) railties (6.0.3.1) diff --git a/config/initializers/002-rails_failover.rb b/config/initializers/002-rails_failover.rb index d53451bc4ce..54692b7f822 100644 --- a/config/initializers/002-rails_failover.rb +++ b/config/initializers/002-rails_failover.rb @@ -17,6 +17,13 @@ if ENV["REDIS_RAILS_FAILOVER"] end if ENV["ACTIVE_RECORD_RAILS_FAILOVER"] + if Rails.configuration.multisite + if ActiveRecord::Base.current_role == ActiveRecord::Base.reading_role + RailsMultisite::ConnectionManagement.default_connection_handler = + ActiveRecord::Base.connection_handlers[ActiveRecord::Base.reading_role] + end + end + RailsFailover::ActiveRecord.on_failover do RailsMultisite::ConnectionManagement.each_connection do Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY) @@ -29,6 +36,11 @@ if ENV["ACTIVE_RECORD_RAILS_FAILOVER"] Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY) Sidekiq.unpause! if Sidekiq.paused? end + + if Rails.configuration.multisite + RailsMultisite::ConnectionManagement.default_connection_handler = + ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role] + end end module Discourse