activerecord_partitioning 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +6 -0
- data/lib/activerecord_partitioning/connection_pools.rb +39 -0
- data/lib/activerecord_partitioning.rb +28 -0
- metadata +113 -0
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module ActiveRecordPartitioning
|
2
|
+
class NoActiveConnectionPoolError < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class ConnectionPools
|
6
|
+
attr_reader :store
|
7
|
+
|
8
|
+
def initialize(store={})
|
9
|
+
@store = store
|
10
|
+
end
|
11
|
+
|
12
|
+
def [](key)
|
13
|
+
config = ActiveRecordPartitioning.current_connection_pool_config
|
14
|
+
raise NoActiveConnectionPoolError if config.nil?
|
15
|
+
@store[connection_pool_key(config)]
|
16
|
+
end
|
17
|
+
|
18
|
+
def []=(key, pool)
|
19
|
+
@store[connection_pool_key(pool.spec.config)] = pool
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete_if(&block)
|
23
|
+
@store.delete_if(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def each_value(&block)
|
27
|
+
@store.each_value(&block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def size
|
31
|
+
@store.size
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def connection_pool_key(config)
|
36
|
+
config[:url]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'activerecord_partitioning/connection_pools'
|
3
|
+
|
4
|
+
module ActiveRecordPartitioning
|
5
|
+
module_function
|
6
|
+
def setup(base_config = {}, pools = {})
|
7
|
+
@base_config = base_config.symbolize_keys
|
8
|
+
ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new(ConnectionPools.new(pools))
|
9
|
+
end
|
10
|
+
|
11
|
+
def with_connection_pool(config, &block)
|
12
|
+
self.current_connection_pool_config = config = config.symbolize_keys
|
13
|
+
if ActiveRecord::Base.connection_pool.nil?
|
14
|
+
ActiveRecord::Base.establish_connection(@base_config.merge(config))
|
15
|
+
end
|
16
|
+
yield if block_given?
|
17
|
+
ensure
|
18
|
+
self.current_connection_pool_config = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def current_connection_pool_config
|
22
|
+
Thread.current[:current_connection_pool_config]
|
23
|
+
end
|
24
|
+
|
25
|
+
def current_connection_pool_config=(config)
|
26
|
+
Thread.current[:current_connection_pool_config] = config
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_partitioning
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Xiao Li
|
14
|
+
- sdqali
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2013-10-10 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 39
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 3
|
33
|
+
- 18
|
34
|
+
version: 2.3.18
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rake
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: sqlite3
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description: An ActiveRecord ConnectionPools class supports switching connection pool by database config instead of ActiveRecord model class name
|
66
|
+
email:
|
67
|
+
- swing1979@gmail.com
|
68
|
+
- sadiqalikm@gmail.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files: []
|
74
|
+
|
75
|
+
files:
|
76
|
+
- README.md
|
77
|
+
- lib/activerecord_partitioning/connection_pools.rb
|
78
|
+
- lib/activerecord_partitioning.rb
|
79
|
+
homepage: https://github.com/ThoughtWorksStudios/activerecord_partitioning
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.8.24
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: An ActiveRecord ConnectionPools class supports switching connection pool by database config instead of ActiveRecord model class name
|
112
|
+
test_files: []
|
113
|
+
|