ar-octopus 0.0.27 → 0.0.28

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -32,7 +32,7 @@ begin
32
32
  gem.add_development_dependency "pg", ">= 0.9.0"
33
33
  gem.add_development_dependency "sqlite3-ruby", ">= 1.3.1"
34
34
  gem.add_dependency('activerecord', '>= 2.3')
35
- gem.version = "0.0.27"
35
+ gem.version = "0.0.28"
36
36
  end
37
37
  Jeweler::GemcutterTasks.new
38
38
  rescue LoadError
data/ar-octopus.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ar-octopus}
8
- s.version = "0.0.27"
8
+ s.version = "0.0.28"
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"]
12
- s.date = %q{2010-07-23}
12
+ s.date = %q{2010-07-26}
13
13
  s.description = %q{This gem allows you to use sharded databases with ActiveRecord. this also provides a interface for replication and for running migrations with multiples shards.}
14
14
  s.email = %q{tchandy@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/octopus.rb CHANGED
@@ -50,7 +50,12 @@ module Octopus
50
50
 
51
51
  def self.using(shard, &block)
52
52
  ActiveRecord::Base.hijack_initializer()
53
- ActiveRecord::Base.connection.run_queries_on_shard(shard, &block)
53
+
54
+ if ActiveRecord::Base.connection.is_a?(Octopus::Proxy)
55
+ ActiveRecord::Base.connection.run_queries_on_shard(shard, &block)
56
+ else
57
+ yield
58
+ end
54
59
  end
55
60
  end
56
61
 
data/lib/octopus/model.rb CHANGED
@@ -15,8 +15,8 @@ module Octopus::Model
15
15
 
16
16
  clean_table_name()
17
17
  hijack_initializer()
18
-
19
- self.connection.using_enabled = true
18
+
19
+ self.connection_proxy.using_enabled = true
20
20
 
21
21
  return Octopus::ScopeProxy.new(shard, self)
22
22
  end
@@ -27,10 +27,10 @@ module Octopus::Model
27
27
  before_save :reload_connection
28
28
 
29
29
  def set_current_shard
30
- if new_record? || self.connection.block
31
- self.current_shard = self.connection.current_shard
30
+ if new_record? || self.class.connection_proxy.block
31
+ self.current_shard = self.class.connection_proxy.current_shard
32
32
  else
33
- self.current_shard = self.connection.last_current_shard
33
+ self.current_shard = self.class.connection_proxy.last_current_shard
34
34
  end
35
35
  end
36
36
 
@@ -46,14 +46,18 @@ module Octopus::Model
46
46
  Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config())
47
47
  end
48
48
 
49
- def self.connection()
50
- if defined?(::Rails) && Octopus.config() && !Octopus.enviroments.include?(Rails.env.to_s)
51
- return super
49
+ def self.connection_with_octopus()
50
+ if defined?(Rails) && Octopus.config() && !Octopus.enviroments.include?(Rails.env.to_s)
51
+ return connection_without_octopus()
52
52
  end
53
53
 
54
54
  self.connection_proxy().current_model = self
55
55
  self.connection_proxy()
56
56
  end
57
+
58
+ class << self
59
+ alias_method_chain :connection, :octopus
60
+ end
57
61
  end
58
62
  end
59
63
 
@@ -65,7 +69,7 @@ module Octopus::Model
65
69
  end
66
70
 
67
71
  def reload_connection()
68
- self.connection.current_shard = self.current_shard() if should_set_current_shard?
72
+ self.class.connection_proxy.current_shard = self.current_shard() if should_set_current_shard?
69
73
  end
70
74
  end
71
75
 
@@ -65,28 +65,59 @@ describe Octopus::Proxy do
65
65
  Rails = mock()
66
66
  set_octopus_env("octopus_rails")
67
67
  end
68
-
68
+
69
69
  it "should initialize correctly octopus common variables for the enviroments" do
70
70
  Rails.stub!(:env).and_return('staging')
71
71
  Octopus.instance_variable_set(:@rails_env, nil)
72
72
  Octopus.config()
73
-
73
+
74
74
  proxy.instance_variable_get(:@replicated).should be_true
75
75
  Octopus.enviroments.should == ["staging", "production"]
76
76
  end
77
-
77
+
78
78
  it "should initialize correctly the shards for the staging enviroment" do
79
79
  Rails.stub!(:env).and_return('staging')
80
80
 
81
81
  proxy.instance_variable_get(:@shards).keys.to_set.should == Set.new([:slave1, :slave2, :master])
82
82
  end
83
-
83
+
84
84
  it "should initialize correctly the shards for the production enviroment" do
85
85
  Rails.stub!(:env).and_return('production')
86
86
 
87
87
  proxy.instance_variable_get(:@shards).keys.to_set.should == Set.new([:slave3, :slave4, :master])
88
88
  end
89
-
89
+
90
+ describe "using the master connection" do
91
+ before(:each) do
92
+ Rails.stub!(:env).and_return('development')
93
+ end
94
+
95
+ it "should use the master connection" do
96
+ user = User.create!(:name =>"Thiago")
97
+ user.name = "New Thiago"
98
+ user.save()
99
+ User.find_by_name("New Thiago").should_not be_nil
100
+ end
101
+
102
+ it "should work when using using syntax" do
103
+ user = User.using(:russia).create!(:name =>"Thiago")
104
+
105
+ user.name = "New Thiago"
106
+ user.save()
107
+
108
+ User.using(:russia).find_by_name("New Thiago").should == user
109
+ User.find_by_name("New Thiago").should == user
110
+ end
111
+
112
+ it "should work when using blocks" do
113
+ Octopus.using(:russia) do
114
+ @user = User.create!(:name =>"Thiago")
115
+ end
116
+
117
+ User.find_by_name("Thiago").should == @user
118
+ end
119
+ end
120
+
90
121
  after(:each) do
91
122
  Object.send(:remove_const, :Rails)
92
123
  Octopus.instance_variable_set(:@config, nil)
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: 41
4
+ hash: 39
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 27
10
- version: 0.0.27
9
+ - 28
10
+ version: 0.0.28
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thiago Pradi
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-07-23 00:00:00 -03:00
19
+ date: 2010-07-26 00:00:00 -03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency