DEV: Update rails_failover so that we can move middleware up the stack.

DEV: Update rails_failover so that we can move middleware up the stack.

diff --git a/Gemfile.lock b/Gemfile.lock
index baa304a..56fa08d 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,6 +1,6 @@
 GIT
   remote: https://github.com/discourse/rails_failover
-  revision: e9893a3920809c9a1bca4f7ae07b7243b2cd127e
+  revision: 19a40ef42cb051c56d2679940ec5f00a436e670b
   specs:
     rails_failover (0.4.0)
       activerecord (~> 6.0)
diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-first_middlewares.rb
index 14432d8..ab53806 100644
--- a/config/initializers/200-first_middlewares.rb
+++ b/config/initializers/200-first_middlewares.rb
@@ -25,3 +25,7 @@ if Rails.configuration.multisite
   Rails.configuration.middleware.unshift RailsMultisite::Middleware, RailsMultisite::DiscoursePatches.config
   Rails.configuration.middleware.delete ActionDispatch::Executor
 end
+
+if ENV["ACTIVE_RECORD_RAILS_FAILOVER"]
+  Rails.configuration.middleware.move_after(RailsMultisite::Middleware, RailsFailover::ActiveRecord::Middleware)
+end
diff --git a/lib/freedom_patches/middleware_stack.rb b/lib/freedom_patches/middleware_stack.rb
new file mode 100644
index 0000000..bf9d76f
--- /dev/null
+++ b/lib/freedom_patches/middleware_stack.rb
@@ -0,0 +1,38 @@
+# 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: aaece34e

I kind of prefer the pattern rails multisite now uses, you ask the gem not to fuss with middleware and then insert it where you want in an initializer

A lot easier to reason about cause you are not fussing with the position

We still have to do an insert_after or insert_before even if the gem does not insert the middleware. That isn’t a win over move_before or move_after IMO.

I think consistency should win here:

We are using this pattern now in message_bus and multisite, we should simply follow it in this gem as well.

At a minimum it saves us from monkey patching rails.