DEV: Update rails_failover to avoid monkey patching Rails config.

DEV: Update rails_failover to avoid monkey patching Rails config.

diff --git a/Gemfile.lock b/Gemfile.lock
index 56fa08d..2c64a4b 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,6 +1,6 @@
 GIT
   remote: https://github.com/discourse/rails_failover
-  revision: 19a40ef42cb051c56d2679940ec5f00a436e670b
+  revision: 850d04f4b1c4178862d9674ec0b2a46a2f1c20d9
   specs:
     rails_failover (0.4.0)
       activerecord (~> 6.0)
@@ -155,7 +155,7 @@ GEM
     hkdf (0.3.0)
     htmlentities (4.3.4)
     http_accept_language (2.1.1)
-    i18n (1.8.2)
+    i18n (1.8.3)
       concurrent-ruby (~> 1.0)
     image_size (1.5.0)
     in_threads (1.5.4)
diff --git a/config/application.rb b/config/application.rb
index 07730bc..a93c86c 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -103,6 +103,7 @@ module Discourse
     # wrong position
     config.skip_message_bus_middleware = true
     config.skip_multisite_middleware = true
+    config.skip_rails_failover_active_record_middleware = true
 
     # Disable so this is only run manually
     # we may want to change this later on
diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-first_middlewares.rb
index fc6c661..5fada92 100644
--- a/config/initializers/200-first_middlewares.rb
+++ b/config/initializers/200-first_middlewares.rb
@@ -28,8 +28,8 @@ end
 
 if ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
   if Rails.configuration.multisite
-    Rails.configuration.middleware.move_after(RailsMultisite::Middleware, RailsFailover::ActiveRecord::Middleware)
+    Rails.configuration.middleware.insert_after(RailsMultisite::Middleware, RailsFailover::ActiveRecord::Middleware)
   else
-    Rails.configuration.middleware.move_before(MessageBus::Rack::Middleware, RailsFailover::ActiveRecord::Middleware)
+    Rails.configuration.middleware.insert_before(MessageBus::Rack::Middleware, RailsFailover::ActiveRecord::Middleware)
   end
 end
diff --git a/lib/freedom_patches/middleware_stack.rb b/lib/freedom_patches/middleware_stack.rb
deleted file mode 100644
index 431fef3..0000000
--- a/lib/freedom_patches/middleware_stack.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-# frozen_string_literal: true
-
-# TODO: Remove once we upgrade to Rails 6.1
-# Copied implementation from https://github.com/rails/rails/pull/38169
-module ActionDispatch
-  class MiddlewareStack
-    def move(target, source)
-      source_index = assert_index(source, :before)
-      source_middleware = middlewares.delete_at(source_index)
-
-      target_index = assert_index(target, :before)
-      middlewares.insert(target_index, source_middleware)
-    end
-    alias_method :move_before, :move
-
-    def move_after(target, source)
-      source_index = assert_index(source, :after)
-      source_middleware = middlewares.delete_at(source_index)
-
-      target_index = assert_index(target, :after)
-      middlewares.insert(target_index + 1, source_middleware)
-    end
-  end
-end
-
-module Rails
-  module Configuration
-    class MiddlewareStackProxy
-      def move_before(*args, &block)
-        @delete_operations << -> middleware { middleware.send(__method__, *args, &block) }
-      end
-
-      alias :move :move_before
-
-      def move_after(*args, &block)
-        @delete_operations << -> middleware { middleware.send(__method__, *args, &block) }
-      end
-    end
-  end
-end

GitHub sha: e82d4d8a

1 Like

@SamSaffron I’ve removed the monkey patch in this commit.

2 Likes