Class: ShardHandler::Model
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ShardHandler::Model
- 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.
Class Method Summary collapse
-
.connection_handler ⇒ ActiveRecord::ConnectionAdapters::ConnectionHandler
private
Overrides ActiveRecord::Core#connection_handler method to return the appropriate ConnectionHandler for the current shard.
-
.current_shard ⇒ Symbol
Returns the current shard name for the current Thread.
-
.current_shard=(name) ⇒ Object
Sets the current shard name for the current Thread.
- .handler ⇒ Handler private
-
.setup(config) ⇒ Object
This method creates an instance of Handler for this class.
-
.using(shard) { ... } ⇒ Object
This method will switch to the passed shard making all queries be executed using the shard connection.
Class Method Details
.connection_handler ⇒ ActiveRecord::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.
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_shard ⇒ Symbol
Returns the current shard name for the current Thread.
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.
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 |
.handler ⇒ Handler
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.
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.
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.
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 |