ar-multidb 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README.markdown +19 -17
- data/Rakefile +21 -0
- data/ar-multidb.gemspec +22 -0
- data/init.rb +2 -0
- data/lib/multidb.rb +3 -0
- data/lib/multidb/balancer.rb +19 -17
- data/lib/multidb/configuration.rb +8 -4
- data/lib/multidb/version.rb +3 -0
- metadata +28 -55
- data/VERSION +0 -1
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.markdown
CHANGED
@@ -14,32 +14,31 @@ support in a production environment.
|
|
14
14
|
Randomized balancing of multiple connections within a group is supported. In the
|
15
15
|
future, some kind of automatic balancing of read/write queries might be implemented.
|
16
16
|
|
17
|
-
|
17
|
+
Tested with Rails 2.3.11. No guarantees about Rails 3.
|
18
18
|
|
19
19
|
|
20
20
|
Comparison to other ActiveRecord extensions
|
21
21
|
===========================================
|
22
22
|
|
23
|
-
|
24
|
-
Multidb aims to be:
|
23
|
+
Compared to other, more full-featured extensions such as Octopus and Seamless Database Pool:
|
25
24
|
|
26
|
-
|
27
|
-
monkeypatching magic. The only part of ActiveRecord that is overriden is
|
25
|
+
**Minimal amount of monkeypatching magic**. The only part of ActiveRecord that is overridden is
|
28
26
|
`ActiveRecord::Base#connection`.
|
29
27
|
|
30
|
-
|
28
|
+
**Non-invasive**. Very small amounts of configuration and changes to the client
|
31
29
|
application are required.
|
32
30
|
|
33
|
-
|
31
|
+
**Orthogonal**. Unlike Octopus, for example, connections follow
|
32
|
+
context:
|
34
33
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
34
|
+
Multidb.use(:master) do
|
35
|
+
@post = Post.find(1)
|
36
|
+
Multidb.use(:slave) do
|
37
|
+
@post.authors # This will use the slave
|
40
38
|
end
|
39
|
+
end
|
41
40
|
|
42
|
-
|
41
|
+
**Low-overhead**. Since `connection` is called on every single
|
43
42
|
database operation, it needs to be fast. Which it is: Multidb's implementation of
|
44
43
|
`connection` incurs only a single hash lookup in `Thread.current`.
|
45
44
|
|
@@ -124,12 +123,15 @@ Development mode
|
|
124
123
|
|
125
124
|
In development you will typically want `Multidb.use(:slave)` to still work, but you
|
126
125
|
probably don't want to run multiple databases on your development box. To make `use`
|
127
|
-
silently fall back to using the default connection,
|
128
|
-
|
126
|
+
silently fall back to using the default connection, Multidb can run in fallback
|
127
|
+
mode.
|
129
128
|
|
130
|
-
|
129
|
+
If you are using Rails, this will be automatically enabled in 'development' and
|
130
|
+
'test' environments. Otherwise, simply set `fallback: true` in `database.yml`:
|
131
|
+
|
132
|
+
development:
|
131
133
|
adapter: postgresql
|
132
|
-
database:
|
134
|
+
database: myapp_development
|
133
135
|
username: ohoh
|
134
136
|
password: mymy
|
135
137
|
host: db1
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
desc 'Bump version'
|
4
|
+
task :bump do
|
5
|
+
if `git status -uno -s --porcelain | wc -l`.to_i > 0
|
6
|
+
abort "You have uncommitted changed."
|
7
|
+
end
|
8
|
+
text = File.read('lib/multidb/version.rb')
|
9
|
+
if text =~ /VERSION = '(.*)'/
|
10
|
+
old_version = $1
|
11
|
+
version_parts = old_version.split('.')
|
12
|
+
version_parts[-1] = version_parts[-1].to_i + 1
|
13
|
+
new_version = version_parts.join('.')
|
14
|
+
text.gsub!(/VERSION = '(.*)'/, "VERSION = '#{new_version}'")
|
15
|
+
File.open('lib/multidb/version.rb', 'w') { |f| f << text }
|
16
|
+
(system("git add lib/multidb/version.rb") and
|
17
|
+
system("git commit -m 'Bump to #{new_version}.'")) or abort "Failed to commit."
|
18
|
+
else
|
19
|
+
abort "Could not find version number"
|
20
|
+
end
|
21
|
+
end
|
data/ar-multidb.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "multidb/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ar-multidb"
|
7
|
+
s.version = Multidb::VERSION
|
8
|
+
s.authors = ["Alexander Staubo"]
|
9
|
+
s.email = ["alex@origo.no"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = s.description = %q{Multidb is an ActiveRecord extension for switching between multiple database connections, such as master/slave setups.}
|
12
|
+
|
13
|
+
s.rubyforge_project = "ar-multidb"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_runtime_dependency 'activesupport', '>= 2.3'
|
21
|
+
s.add_runtime_dependency 'activerecord', '>= 2.3'
|
22
|
+
end
|
data/init.rb
ADDED
data/lib/multidb.rb
CHANGED
data/lib/multidb/balancer.rb
CHANGED
@@ -30,24 +30,26 @@ module Multidb
|
|
30
30
|
def initialize(configuration)
|
31
31
|
@candidates = {}.with_indifferent_access
|
32
32
|
@configuration = configuration
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
if @configuration
|
34
|
+
(@configuration.raw_configuration[:databases] || {}).each_pair do |name, config|
|
35
|
+
configs = config.is_a?(Array) ? config : [config]
|
36
|
+
configs.each do |config|
|
37
|
+
candidate = Candidate.new(@configuration.default_adapter.merge(config))
|
38
|
+
@candidates[name] ||= []
|
39
|
+
@candidates[name].push(candidate)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
if @configuration.raw_configuration.include?(:fallback)
|
43
|
+
@fallback = @configuration.raw_configuration[:fallback]
|
44
|
+
elsif defined?(Rails)
|
45
|
+
@fallback = %w(development test).include?(Rails.env)
|
46
|
+
else
|
47
|
+
@fallback = false
|
48
|
+
end
|
49
|
+
@default_candidate = Candidate.new(@configuration.default_pool)
|
50
|
+
unless @candidates.include?(:default)
|
51
|
+
@candidates[:default] = [@default_candidate]
|
39
52
|
end
|
40
|
-
end
|
41
|
-
if @configuration.raw_configuration.include?(:fallback)
|
42
|
-
@fallback = @configuration.raw_configuration[:fallback]
|
43
|
-
elsif defined?(Rails)
|
44
|
-
@fallback = %w(development test).include?(Rails.env)
|
45
|
-
else
|
46
|
-
@fallback = false
|
47
|
-
end
|
48
|
-
@default_candidate = Candidate.new(@configuration.default_pool)
|
49
|
-
unless @candidates.include?(:default)
|
50
|
-
@candidates[:default] = [@default_candidate]
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
@@ -3,10 +3,14 @@ module Multidb
|
|
3
3
|
class << self
|
4
4
|
|
5
5
|
def configure!
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
connection_pool = ActiveRecord::Base.connection_pool
|
7
|
+
if connection_pool
|
8
|
+
connection = connection_pool.connection
|
9
|
+
activerecord_config = connection.instance_variable_get(:@config).dup.with_indifferent_access
|
10
|
+
default_adapter, configuration_hash = activerecord_config, activerecord_config.delete(:multidb)
|
11
|
+
configuration_hash ||= {}
|
12
|
+
@configuration = Configuration.new(default_adapter, configuration_hash)
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
attr_reader :configuration
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-multidb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alexander Staubo
|
@@ -15,89 +15,62 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2012-02-07 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
hash:
|
28
|
+
hash: 5
|
29
29
|
segments:
|
30
30
|
- 2
|
31
|
-
-
|
32
|
-
version: "2.
|
33
|
-
|
31
|
+
- 3
|
32
|
+
version: "2.3"
|
33
|
+
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
|
-
prerelease: false
|
36
35
|
- !ruby/object:Gem::Dependency
|
37
|
-
type: :runtime
|
38
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
hash: 7
|
44
|
-
segments:
|
45
|
-
- 2
|
46
|
-
- 2
|
47
|
-
version: "2.2"
|
48
36
|
name: activerecord
|
49
|
-
version_requirements: *id002
|
50
37
|
prerelease: false
|
51
|
-
|
52
|
-
type: :runtime
|
53
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
54
39
|
none: false
|
55
40
|
requirements:
|
56
41
|
- - ">="
|
57
42
|
- !ruby/object:Gem::Version
|
58
|
-
hash:
|
43
|
+
hash: 5
|
59
44
|
segments:
|
60
45
|
- 2
|
61
|
-
-
|
62
|
-
version: "2.
|
63
|
-
name: activesupport
|
64
|
-
version_requirements: *id003
|
65
|
-
prerelease: false
|
66
|
-
- !ruby/object:Gem::Dependency
|
46
|
+
- 3
|
47
|
+
version: "2.3"
|
67
48
|
type: :runtime
|
68
|
-
|
69
|
-
none: false
|
70
|
-
requirements:
|
71
|
-
- - ">="
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
hash: 7
|
74
|
-
segments:
|
75
|
-
- 2
|
76
|
-
- 2
|
77
|
-
version: "2.2"
|
78
|
-
name: activerecord
|
79
|
-
version_requirements: *id004
|
80
|
-
prerelease: false
|
49
|
+
version_requirements: *id002
|
81
50
|
description: Multidb is an ActiveRecord extension for switching between multiple database connections, such as master/slave setups.
|
82
|
-
email:
|
51
|
+
email:
|
52
|
+
- alex@origo.no
|
83
53
|
executables: []
|
84
54
|
|
85
55
|
extensions: []
|
86
56
|
|
87
|
-
extra_rdoc_files:
|
88
|
-
|
89
|
-
- README.markdown
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
90
59
|
files:
|
60
|
+
- .gitignore
|
61
|
+
- Gemfile
|
91
62
|
- LICENSE
|
92
63
|
- README.markdown
|
93
|
-
-
|
64
|
+
- Rakefile
|
65
|
+
- ar-multidb.gemspec
|
66
|
+
- init.rb
|
94
67
|
- lib/ar-multidb.rb
|
95
68
|
- lib/multidb.rb
|
96
69
|
- lib/multidb/balancer.rb
|
97
70
|
- lib/multidb/configuration.rb
|
98
71
|
- lib/multidb/model_extensions.rb
|
99
|
-
|
100
|
-
homepage:
|
72
|
+
- lib/multidb/version.rb
|
73
|
+
homepage: ""
|
101
74
|
licenses: []
|
102
75
|
|
103
76
|
post_install_message:
|
@@ -125,8 +98,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
98
|
version: "0"
|
126
99
|
requirements: []
|
127
100
|
|
128
|
-
rubyforge_project:
|
129
|
-
rubygems_version: 1.
|
101
|
+
rubyforge_project: ar-multidb
|
102
|
+
rubygems_version: 1.8.6
|
130
103
|
signing_key:
|
131
104
|
specification_version: 3
|
132
105
|
summary: Multidb is an ActiveRecord extension for switching between multiple database connections, such as master/slave setups.
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.4
|