class ActiveRecord::Middleware::ShardSelector

}
tenant.shard
tenant = Tenant.find_by_subdomain!(subdomain)
subdomain = request.subdomain
config.active_record.shard_resolver = ->(request) {
specific models. An example resolver would look like this:
Applications must also provide the code for the resolver as it depends on application
config.active_record.shard_selector = { lock: true }
Options can be set in the config:
code from mistakenly switching between tenants.
For tenant based sharding, lock should always be true to prevent application
inside the block. If lock is false, then shard swapping will be allowed.
true by default and will prohibit the request from switching shards once
that can be used by the middleware to alter behavior. lock is
The ShardSelector takes a set of options (currently only lock is supported)
for swapping if needed.
shard to switch to and allows for applications to write custom strategies
swapping shards. Rails provides a basic framework to determine which
The ShardSelector Middleware provides a framework for automatically

def call(env)

def call(env)
  request = ActionDispatch::Request.new(env)
  shard = selected_shard(request)
  set_shard(shard) do
    @app.call(env)
  end
end

def initialize(app, resolver, options = {})

def initialize(app, resolver, options = {})
  @app = app
  @resolver = resolver
  @options = options
end

def selected_shard(request)

def selected_shard(request)
  resolver.call(request)
end

def set_shard(shard, &block)

def set_shard(shard, &block)
  ActiveRecord::Base.connected_to(shard: shard.to_sym) do
    ActiveRecord::Base.prohibit_shard_swapping(options.fetch(:lock, true), &block)
  end
end