activerecord_partitioning 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,19 +3,26 @@ module ActiveRecordPartitioning
3
3
  end
4
4
 
5
5
  class ConnectionPools
6
- attr_reader :store
6
+ attr_reader :key_name, :store
7
7
 
8
- def initialize(store={})
8
+ def initialize(key_name, store={})
9
+ @key_name = key_name
9
10
  @store = store
10
11
  end
11
12
 
12
- def [](key)
13
+ def merge!(pools)
14
+ pools.each do |klass_name, pool|
15
+ self[klass_name] = pool
16
+ end
17
+ end
18
+
19
+ def [](klass_name)
13
20
  config = ActiveRecordPartitioning.current_connection_pool_config
14
21
  raise NoActiveConnectionPoolError if config.nil?
15
22
  @store[connection_pool_key(config)]
16
23
  end
17
24
 
18
- def []=(key, pool)
25
+ def []=(klass_name, pool)
19
26
  @store[connection_pool_key(pool.spec.config)] = pool
20
27
  end
21
28
 
@@ -33,7 +40,7 @@ module ActiveRecordPartitioning
33
40
 
34
41
  private
35
42
  def connection_pool_key(config)
36
- config[:url]
43
+ config[@key_name]
37
44
  end
38
45
  end
39
46
  end
@@ -3,9 +3,15 @@ require 'activerecord_partitioning/connection_pools'
3
3
 
4
4
  module ActiveRecordPartitioning
5
5
  module_function
6
- def setup(base_config = {}, pools = {})
6
+ def setup(key_name, base_config = {}, store = {})
7
7
  @base_config = base_config.symbolize_keys
8
- ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new(ConnectionPools.new(pools))
8
+ new_pools = ConnectionPools.new(key_name, store)
9
+ new_pools.merge!(ActiveRecord::Base.connection_handler.connection_pools)
10
+ ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new(new_pools)
11
+ end
12
+
13
+ def reset_connection_handler
14
+ ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
9
15
  end
10
16
 
11
17
  def with_connection_pool(config, &block)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_partitioning
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Xiao Li