Class: Rails::Sharding::Core
- Inherits:
-
Object
- Object
- Rails::Sharding::Core
- Defined in:
- lib/rails/sharding/core.rb
Class Method Summary collapse
- .configurations(environment = Rails.env) ⇒ Object
-
.for_each_shard(shard_group_filter = nil, shard_name_filter = nil) ⇒ Object
yields a block for each shard in each shard group, with its configurations shard_group_filter: if passed yields only shards of this group shard_name_filter: if passed yields only shards with this name.
- .reset_configurations_cache ⇒ Object
-
.setup ⇒ Object
Method that should be called on a rails initializer.
- .shard_groups ⇒ Object
- .shard_names(shard_group) ⇒ Object
- .test_configurations ⇒ Object
-
.using_shard(shard_group, shard_name) ⇒ Object
Opens a block where all queries will be directed to the selected shard.
Class Method Details
.configurations(environment = Rails.env) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/rails/sharding/core.rb', line 30 def self.configurations(environment=Rails.env) @@db_configs ||= YAML.load_file(Config.shards_config_file) environment_config = @@db_configs[environment] return environment_config if environment_config raise Errors::ConfigNotFoundError, 'Found no shard configurations for enviroment "' + environment + '" in ' + Config.shards_config_file.to_s + ' file was not found' rescue Errno::ENOENT raise Errors::ConfigNotFoundError, Config.shards_config_file.to_s + ' file was not found' end |
.for_each_shard(shard_group_filter = nil, shard_name_filter = nil) ⇒ Object
yields a block for each shard in each shard group, with its configurations shard_group_filter: if passed yields only shards of this group shard_name_filter: if passed yields only shards with this name
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rails/sharding/core.rb', line 59 def self.for_each_shard(shard_group_filter=nil, shard_name_filter=nil) shard_group_filter.to_s if shard_group_filter shard_name_filter.to_s if shard_name_filter configurations.each do |shard_group, shards_configurations| next if shard_group_filter && shard_group_filter != shard_group.to_s shards_configurations.each do |shard, configuration| next if shard_name_filter && shard_name_filter != shard.to_s yield shard_group, shard, configuration end end end |
.reset_configurations_cache ⇒ Object
40 41 42 |
# File 'lib/rails/sharding/core.rb', line 40 def self.reset_configurations_cache @@db_configs = nil end |
.setup ⇒ Object
Method that should be called on a rails initializer
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rails/sharding/core.rb', line 74 def self.setup if block_given? yield Config end if Config.establish_all_connections_on_setup # Establishes connections with all shards specified in config/shards.yml ConnectionHandler.establish_all_connections end if Config.extend_active_record_scope # includes the #using_shard method to all AR scopes ActiveRecordExtensions.extend_active_record_scope end end |
.shard_groups ⇒ Object
48 49 50 |
# File 'lib/rails/sharding/core.rb', line 48 def self.shard_groups self.configurations.keys end |
.shard_names(shard_group) ⇒ Object
52 53 54 |
# File 'lib/rails/sharding/core.rb', line 52 def self.shard_names(shard_group) self.configurations[shard_group.to_s].keys end |
.test_configurations ⇒ Object
44 45 46 |
# File 'lib/rails/sharding/core.rb', line 44 def self.test_configurations self.configurations('test') end |
.using_shard(shard_group, shard_name) ⇒ Object
Opens a block where all queries will be directed to the selected shard
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rails/sharding/core.rb', line 12 def self.using_shard(shard_group, shard_name) ShardThreadRegistry.push_current_shard(shard_group, shard_name) yield ensure shard_group, shard_name, connection_used = ShardThreadRegistry.pop_current_shard # shows warning to user if Config.no_connection_retrieved_warning && !connection_used puts "Warning: no connection to shard '#{shard_group}:#{shard_name}' was retrieved inside the using_shard block. Make sure you don't forget to include Rails::Sharding::ShardableModel to the models you want to be sharded. Disable this warning with Rails::Sharding::Config.no_connection_retrieved_warning = false." end # Releases connections in case user left some connection in the reserved state # (by calling retrieve_connection instead of with_connection). Also, using # normal activerecord queries leaves a connection in the reserved state # Obs: don't do this with a master database connection ConnectionHandler.connection_pool(shard_group, shard_name).release_connection if shard_group && shard_name end |