ar-octopus 0.0.10 → 0.0.11
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/Rakefile +1 -1
- data/ar-octopus.gemspec +1 -1
- data/lib/octopus/proxy.rb +7 -2
- data/spec/octopus/proxy_spec.rb +48 -17
- data/spec/octopus/replication_specs.rb +0 -9
- metadata +3 -3
data/Rakefile
CHANGED
@@ -29,7 +29,7 @@ begin
|
|
29
29
|
gem.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
|
30
30
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
31
31
|
gem.add_dependency('activerecord')
|
32
|
-
gem.version = "0.0.
|
32
|
+
gem.version = "0.0.11"
|
33
33
|
end
|
34
34
|
Jeweler::GemcutterTasks.new
|
35
35
|
rescue LoadError
|
data/ar-octopus.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ar-octopus}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.11"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
|
data/lib/octopus/proxy.rb
CHANGED
@@ -3,7 +3,7 @@ class Octopus::Proxy
|
|
3
3
|
|
4
4
|
def initialize(config)
|
5
5
|
initialize_shards(config)
|
6
|
-
initialize_replication() if config
|
6
|
+
initialize_replication() if have_a_valid_configuration?(config) && config[Octopus.env()]["replicated"]
|
7
7
|
end
|
8
8
|
|
9
9
|
def initialize_shards(config)
|
@@ -11,7 +11,8 @@ class Octopus::Proxy
|
|
11
11
|
@groups = {}
|
12
12
|
@shards[:master] = ActiveRecord::Base.connection_pool()
|
13
13
|
@current_shard = :master
|
14
|
-
shards_config = config[Octopus.env()]["shards"]
|
14
|
+
shards_config = config[Octopus.env()]["shards"] if have_a_valid_configuration?(config)
|
15
|
+
shards_config ||= []
|
15
16
|
|
16
17
|
shards_config.each do |key, value|
|
17
18
|
if value.has_key?("adapter")
|
@@ -67,6 +68,10 @@ class Octopus::Proxy
|
|
67
68
|
def shard_name
|
68
69
|
current_shard.is_a?(Array) ? current_shard.first : current_shard
|
69
70
|
end
|
71
|
+
|
72
|
+
def have_a_valid_configuration?(config)
|
73
|
+
!config[Octopus.env()].nil?
|
74
|
+
end
|
70
75
|
|
71
76
|
def add_transaction_record(record)
|
72
77
|
if !select_connection().instance_variable_get(:@_current_transaction_records).nil?
|
data/spec/octopus/proxy_spec.rb
CHANGED
@@ -1,21 +1,20 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
3
|
describe Octopus::Proxy do
|
4
|
-
|
5
|
-
|
6
|
-
end
|
7
|
-
|
4
|
+
let(:proxy) { Octopus::Proxy.new(Octopus.config()) }
|
5
|
+
|
8
6
|
describe "creating a new instance" do
|
9
7
|
it "should initialize all shards and groups" do
|
10
|
-
|
11
|
-
|
8
|
+
proxy.instance_variable_get(:@shards).keys.to_set.should == [:postgresql_shard, :alone_shard, :aug2011, :canada, :brazil, :aug2009, :russia, :aug2010, :master].to_set
|
9
|
+
proxy.instance_variable_get(:@groups).should == {:country_shards=>[:canada, :brazil, :russia], :history_shards=>[:aug2009, :aug2010, :aug2011]}
|
12
10
|
end
|
13
11
|
|
14
12
|
it "should initialize the block attribute as false" do
|
15
|
-
|
13
|
+
proxy.block.should be_false
|
16
14
|
end
|
15
|
+
|
17
16
|
it "should initialize replicated attribute as false" do
|
18
|
-
|
17
|
+
proxy.instance_variable_get(:@replicated).should be_false
|
19
18
|
end
|
20
19
|
|
21
20
|
describe "should raise error if you have duplicated shard names" do
|
@@ -24,36 +23,68 @@ describe Octopus::Proxy do
|
|
24
23
|
end
|
25
24
|
|
26
25
|
it "should raise the error" do
|
27
|
-
lambda {
|
26
|
+
lambda { proxy }.should raise_error("You have duplicated shard names!")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "should initialize just the master when you don't have a shards.yml file" do
|
31
|
+
before(:each) do
|
32
|
+
Octopus.stub!(:env).and_return("crazy_enviroment")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should initialize just the master shard" do
|
36
|
+
proxy.instance_variable_get(:@shards).keys.should == [:master]
|
28
37
|
end
|
38
|
+
|
39
|
+
it "should not initialize the groups variable" do
|
40
|
+
proxy.instance_variable_get(:@groups).should == {}
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not initialize replication" do
|
44
|
+
proxy.instance_variable_get(:@replicated).should be_nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "when you have a replicated enviroment" do
|
50
|
+
before(:each) do
|
51
|
+
Octopus.stub!(:env).and_return("production_replicated")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should have the replicated attribute as true" do
|
55
|
+
proxy.instance_variable_get(:@replicated).should be_true
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should initialize the list of shards" do
|
59
|
+
proxy.instance_variable_get(:@slaves_list).should == ["slave1", "slave2", "slave3", "slave4"]
|
29
60
|
end
|
30
61
|
end
|
31
62
|
|
32
63
|
describe "returning the correct connection" do
|
33
64
|
describe "should return the shard name" do
|
34
65
|
it "when current_shard is empty" do
|
35
|
-
|
66
|
+
proxy.shard_name.should == :master
|
36
67
|
end
|
37
68
|
|
38
69
|
it "when current_shard is a single shard" do
|
39
|
-
|
40
|
-
|
70
|
+
proxy.current_shard = :canada
|
71
|
+
proxy.shard_name.should == :canada
|
41
72
|
end
|
42
73
|
|
43
74
|
it "when current_shard is more than one shard" do
|
44
|
-
|
45
|
-
|
75
|
+
proxy.current_shard = [:russia, :brazil]
|
76
|
+
proxy.shard_name.should == :russia
|
46
77
|
end
|
47
78
|
end
|
48
79
|
|
49
80
|
describe "should return the connection based on shard_name" do
|
50
81
|
it "when current_shard is empty" do
|
51
|
-
|
82
|
+
proxy.select_connection().should == proxy.instance_variable_get(:@shards)[:master].connection()
|
52
83
|
end
|
53
84
|
|
54
85
|
it "when current_shard is a single shard" do
|
55
|
-
|
56
|
-
|
86
|
+
proxy.current_shard = :canada
|
87
|
+
proxy.select_connection().should == proxy.instance_variable_get(:@shards)[:canada].connection()
|
57
88
|
end
|
58
89
|
end
|
59
90
|
end
|
@@ -3,17 +3,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
3
3
|
describe "when the database is replicated" do
|
4
4
|
before(:each) do
|
5
5
|
Octopus.stub!(:env).and_return("production_replicated")
|
6
|
-
@proxy = Octopus::Proxy.new(Octopus.config())
|
7
6
|
clean_connection_proxy()
|
8
7
|
end
|
9
|
-
|
10
|
-
it "should have the replicated attribute as true" do
|
11
|
-
@proxy.replicated.should be_true
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should initialize the list of shards" do
|
15
|
-
@proxy.instance_variable_get(:@slaves_list).should == ["slave1", "slave2", "slave3", "slave4"]
|
16
|
-
end
|
17
8
|
|
18
9
|
it "should send all writes/reads queries to master when you have a replicated model" do
|
19
10
|
u = User.create!(:name => "Replicated")
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-octopus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 11
|
10
|
+
version: 0.0.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thiago Pradi
|