class ShardHandler::Model

This is an abstract model that adds sharding capabilities on ActiveRecord. When you need to query different shards using the same model, you must inherit from this class and configure it.

@example

class Post < ShardHandler::Model
end

Post.setup({
  'shard1' => {
    'adapter' => 'postgresql',
    'database' => 'shard_handler_development',
    'username' => 'postgres',
    'password' => ''
  }
})

Post.using(:shard1) do
  Post.update_all(title: 'foo')
end

Public Class Methods

connection_handler() click to toggle source

Overrides ActiveRecord::Core#connection_handler method to return the appropriate ConnectionHandler for the current shard. This is the integration point between ActiveRecord and this gem.

@api private @return (see Handler#connection_handler_for)

Calls superclass method
# File lib/shard_handler/model.rb, line 63
def connection_handler
  return super if use_master_connection?

  unless defined?(@@handler)
    fail(SetupError, 'the model was not setup')
  end

  @@handler.connection_handler_for(current_shard)
end
current_shard() click to toggle source

Returns the current shard name for the current Thread.

@return [Symbol]

# File lib/shard_handler/model.rb, line 46
def current_shard
  ThreadRegistry.current_shard
end
current_shard=(name) click to toggle source

Sets the current shard name for the current Thread.

@param name [Symbol, String] shard name configured using {.setup}

# File lib/shard_handler/model.rb, line 53
def current_shard=(name)
  ThreadRegistry.current_shard = name.nil? ? nil : name.to_sym
end
handler() click to toggle source

@api private @return [Handler]

# File lib/shard_handler/model.rb, line 30
def handler
  @@handler
end
setup(config) click to toggle source

This method creates an instance of {Handler} for this class. This method must be called before performing any query on shards.

@param config [Hash] a hash with database connection settings

# File lib/shard_handler/model.rb, line 38
def setup(config)
  @@handler = Handler.new(self, config)
  @@handler.setup
end
using(shard) { || ... } click to toggle source

This method will switch to the passed shard making all queries be executed using the shard connection.

@param shard [Symbol, String] shard name configured using .setup @yield The block that must be executed using the shard connection

# File lib/shard_handler/model.rb, line 78
def using(shard)
  old_shard = current_shard
  self.current_shard = shard
  yield
ensure
  # Returns any connections in use back to the pool. It is executed only
  # if the shard name is different from the old one because one can call
  # .using multiple times in a chain, like this:
  #
  #   using(:shard1) do
  #     using(:shard1) do
  #     end
  #   end
  self.clear_active_connections! if old_shard != current_shard
  self.current_shard = old_shard
end

Protected Class Methods

use_master_connection?() click to toggle source
# File lib/shard_handler/model.rb, line 99
def use_master_connection?
  current_shard.nil?
end