Module: DbCharmer::Sharding

Defined in:
lib/db_charmer/sharding.rb,
lib/db_charmer/sharding/method.rb,
lib/db_charmer/sharding/connection.rb,
lib/db_charmer/sharding/method/range.rb,
lib/db_charmer/sharding/method/hash_map.rb,
lib/db_charmer/sharding/stub_connection.rb,
lib/db_charmer/sharding/method/db_block_map.rb,
lib/db_charmer/sharding/method/db_block_group_map.rb,
lib/db_charmer/sharding/method/db_block_schema_map.rb,
lib/db_charmer/sharding/method/db_block_group_map_base.rb

Defined Under Namespace

Modules: Method Classes: Connection, StubConnection

Constant Summary collapse

@@sharded_connections =
{}

Class Method Summary collapse

Class Method Details

.register_connection(config) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/db_charmer/sharding.rb', line 9

def self.register_connection(config)
  name = config[:name] or raise ArgumentError, "No :name in connection!"
  @@sharded_connections[name] = DbCharmer::Sharding::Connection.new(config)

  # Enable multi-db migrations
  if @@sharded_connections[name].sharder.is_a?(DbCharmer::Sharding::Method::DbBlockSchemaMap)
    ::ActiveRecord::Base.extend(DbCharmer::Sharding::Method::SchemaTableNamePrefix)
  end
end

.reset_connectionsObject



46
47
48
# File 'lib/db_charmer/sharding.rb', line 46

def self.reset_connections
  @@sharded_connections = {}
end

.shard_for_connection_name(connection_config) ⇒ Object

Return shard record for the given config hash. connection_config is the config hash taken from a PostgreSQLAdapter instance.



40
41
42
43
44
# File 'lib/db_charmer/sharding.rb', line 40

def self.shard_for_connection_name(connection_config)
  sharder = sharder_for_connection_name(connection_config[:sharder_name])
  return unless sharder
  sharder.shard_class.where(:id => connection_config[:shard_id]).first
end

.sharded_connection(name) ⇒ Object

name is the is the sharded_connection name passed to db_magic in the model



20
21
22
# File 'lib/db_charmer/sharding.rb', line 20

def self.sharded_connection(name)
  @@sharded_connections[name.to_sym] or raise ArgumentError, "Invalid sharded connection name!"
end

.sharded_connection_by_connection_name(name) ⇒ Object

Return the DbCharmer::Sharding::Connection that matches name name is the the connection name calculated in the sharder for a specific database



26
27
28
29
30
31
# File 'lib/db_charmer/sharding.rb', line 26

def self.sharded_connection_by_connection_name(name)
  connection = @@sharded_connections.detect do |c|
    c[1].config[:connection] == name.to_sym
  end
  connection[1] if connection
end

.sharder_for_connection_name(sharder_name) ⇒ Object



33
34
35
36
# File 'lib/db_charmer/sharding.rb', line 33

def self.sharder_for_connection_name(sharder_name)
  connection = sharded_connection(sharder_name)
  return connection.sharder if connection
end