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

This commit is contained in:
Guo Xiang Tan 2020-06-04 17:13:59 +08:00
parent 54f79ea3ba
commit aaece34e8b
3 changed files with 43 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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