Class: Sequel::SchemaSharding::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/schema-sharding/connection_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager



8
9
10
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 8

def initialize
  @connections = {}
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



6
7
8
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 6

def connections
  @connections
end

Instance Method Details

#[](name) ⇒ Object



12
13
14
15
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 12

def [](name)
  config = db_config_for(name)
  @connections[name.to_s] ||= Sequel.postgres(master_config_for(config).merge!(replica_hash_for(config)))
end

#default_dataset_for(table_name) ⇒ Object



32
33
34
35
36
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 32

def default_dataset_for(table_name)
  shard_number = config.logical_shard_configs(table_name).keys.first
  shard_name = config.logical_shard_configs(table_name)[shard_number]
  self[shard_name][:"#{schema_for(table_name, ENV['RACK_ENV'], shard_number)}__#{table_name}"]
end

#disconnectObject



21
22
23
24
25
26
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 21

def disconnect
  @connections.each_value do |conn|
    conn.disconnect
  end
  @connections = {}
end

#master(name) ⇒ Object



17
18
19
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 17

def master(name)
  @connections["master_#{name}"] ||= Sequel.postgres(master_config_for(db_config_for(name)))
end

#schema_for(table_name, environment, shard_number) ⇒ Object



28
29
30
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 28

def schema_for(table_name, environment, shard_number)
  config.schema_name(table_name).gsub('%e', environment).gsub('%s', shard_number.to_s)
end