Class: ShardHandler::Model

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/shard_handler/model.rb

Overview

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.

Examples:

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

Class Method Summary collapse

Class Method Details

.connection_handlerActiveRecord::ConnectionAdapters::ConnectionHandler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Returns:

  • (ActiveRecord::ConnectionAdapters::ConnectionHandler)


63
64
65
66
67
68
69
70
71
# 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_shardSymbol

Returns the current shard name for the current Thread.

Returns:

  • (Symbol)


46
47
48
# File 'lib/shard_handler/model.rb', line 46

def current_shard
  ThreadRegistry.current_shard
end

.current_shard=(name) ⇒ Object

Sets the current shard name for the current Thread.

Parameters:

  • name (Symbol, String)

    shard name configured using setup



53
54
55
# File 'lib/shard_handler/model.rb', line 53

def current_shard=(name)
  ThreadRegistry.current_shard = name.nil? ? nil : name.to_sym
end

.handlerHandler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



30
31
32
# File 'lib/shard_handler/model.rb', line 30

def handler
  @@handler
end

.setup(config) ⇒ Object

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

Parameters:

  • config (Hash)

    a hash with database connection settings



38
39
40
41
# File 'lib/shard_handler/model.rb', line 38

def setup(config)
  @@handler = Handler.new(self, config)
  @@handler.setup
end

.using(shard) { ... } ⇒ Object

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

Parameters:

  • shard (Symbol, String)

    shard name configured using .setup

Yields:

  • The block that must be executed using the shard connection



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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