Class: Sequel::SchemaSharding::ConnectionManager
- Inherits:
-
Object
- Object
- Sequel::SchemaSharding::ConnectionManager
- Defined in:
- lib/sequel/schema-sharding/connection_manager.rb
Overview
Used to manage database connections separately from database shards
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#default_dataset_for(table_name) ⇒ Object
Given
table_name
, return a functional dataset. - #disconnect ⇒ Object
-
#initialize ⇒ ConnectionManager
constructor
A new instance of ConnectionManager.
-
#master(name) ⇒ Object
Used by rake tasks that need to deterministically work against a master database even when read/write splitting is configured.
-
#schema_for(table_name, shard_number) ⇒ Object
Given
table_name
andshard_number
, returns the name of the PostgreSQL schema based on aschema_name
pattern defined in sharding.yml.
Constructor Details
#initialize ⇒ ConnectionManager
Returns a new instance of ConnectionManager.
12 13 14 |
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 12 def initialize @connections = {} end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
10 11 12 |
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 10 def connections @connections end |
Instance Method Details
#[](name) ⇒ Object
16 17 18 19 |
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 16 def [](name) config = db_config_for(name) @connections[name.to_s] ||= Sequel.postgres(sequel_connection_config_for(config).merge!(replica_hash_for(config))) end |
#default_dataset_for(table_name) ⇒ Object
Given table_name
, return a functional dataset. This is used when models are loaded to read table columns and allow for data typecasting. In most cases it should not be used directly in application code.
53 54 55 56 57 |
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 53 def default_dataset_for(table_name) shard_number = config.logical_shard_configs(table_name).keys.sample shard_name = config.logical_shard_configs(table_name)[shard_number] self[shard_name][:"#{schema_for(table_name, shard_number)}__#{table_name}"] end |
#disconnect ⇒ Object
29 30 31 32 33 34 |
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 29 def disconnect @connections.each_value do |conn| conn.disconnect end @connections = {} end |
#master(name) ⇒ Object
Used by rake tasks that need to deterministically work against a master database even when read/write splitting is configured.
25 26 27 |
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 25 def master(name) @connections["master_#{name}"] ||= Sequel.postgres(sequel_connection_config_for(db_config_for(name))) end |
#schema_for(table_name, shard_number) ⇒ Object
Given table_name
and shard_number
, returns the name of the PostgreSQL schema based on a schema_name
pattern defined in sharding.yml. shard_number
is interpolated into schema_name
via sprintf, so schema_name
should include a format specifier with which to interpolate it (ex. %s, %02d).
43 44 45 46 |
# File 'lib/sequel/schema-sharding/connection_manager.rb', line 43 def schema_for(table_name, shard_number) pattern = config.schema_name(table_name) sprintf pattern, shard_number end |