ar-octopus 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
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', '>= 3.0.0beta')
32
- gem.version = "0.0.12"
32
+ gem.version = "0.0.13"
33
33
  end
34
34
  Jeweler::GemcutterTasks.new
35
35
  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.12"
8
+ s.version = "0.0.13"
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"]
12
- s.date = %q{2010-06-29}
12
+ s.date = %q{2010-07-01}
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
@@ -2,7 +2,7 @@ require "yaml"
2
2
 
3
3
  module Octopus
4
4
  def self.env()
5
- @env ||= defined?(Rails) ? Rails.env.to_s : "production"
5
+ @env ||= 'octopus'
6
6
  end
7
7
 
8
8
  def self.config()
data/lib/octopus/model.rb CHANGED
@@ -2,15 +2,16 @@ module Octopus::Model
2
2
  def self.extended(base)
3
3
  base.send(:include, InstanceMethods)
4
4
  base.extend(ClassMethods)
5
- base.hijack_connection()
5
+ base.hijack_connection() unless defined?(::Rails) && (Rails.env == 'test' || Rails.env == 'development')
6
6
  end
7
7
 
8
8
  module SharedMethods
9
9
  def clean_table_name
10
10
  self.reset_table_name() if self != ActiveRecord::Base && self.respond_to?(:reset_table_name)
11
11
  end
12
-
12
+
13
13
  def using(shard, &block)
14
+ return if defined?(::Rails) && (Rails.env == 'test' || Rails.env == 'development')
14
15
  hijack_connection()
15
16
  clean_table_name()
16
17
 
@@ -23,7 +24,7 @@ module Octopus::Model
23
24
  return Octopus::ScopeProxy.new(shard, self)
24
25
  end
25
26
  end
26
-
27
+
27
28
  def hijack_initializer()
28
29
  attr_accessor :current_shard
29
30
  after_initialize :set_current_shard
@@ -54,7 +55,7 @@ module Octopus::Model
54
55
 
55
56
  module InstanceMethods
56
57
  include SharedMethods
57
-
58
+
58
59
  def set_connection(*args)
59
60
  if(args.size == 1)
60
61
  arg = args.first
@@ -63,7 +64,7 @@ module Octopus::Model
63
64
 
64
65
  self.connection.current_shard = self.current_shard if have_a_valid_shard?
65
66
  end
66
-
67
+
67
68
  def have_a_valid_shard?
68
69
  self.respond_to?(:current_shard) && self.current_shard != nil
69
70
  end
data/lib/octopus/proxy.rb CHANGED
@@ -13,7 +13,7 @@ class Octopus::Proxy
13
13
  @current_shard = :master
14
14
  shards_config = config[Octopus.env()]["shards"] if have_a_valid_configuration?(config)
15
15
  shards_config ||= []
16
-
16
+
17
17
  shards_config.each do |key, value|
18
18
  if value.has_key?("adapter")
19
19
  initialize_adapter(value['adapter'])
@@ -69,7 +69,7 @@ class Octopus::Proxy
69
69
  def shard_name
70
70
  current_shard.is_a?(Array) ? current_shard.first : current_shard
71
71
  end
72
-
72
+
73
73
  def have_a_valid_configuration?(config)
74
74
  !config[Octopus.env()].nil?
75
75
  end
@@ -15,6 +15,18 @@ class Octopus::ScopeProxy
15
15
  return self
16
16
  end
17
17
 
18
+ # Transaction Method send all queries to a specified shard.
19
+ def transaction(options = {}, &block)
20
+ @klass.connection().current_shard = @shard
21
+ @klass.connection().block = true
22
+
23
+ begin
24
+ @klass.connection().transaction(options, &block)
25
+ ensure
26
+ @klass.connection().block = false
27
+ end
28
+ end
29
+
18
30
  def connection
19
31
  @klass.connection().current_shard = @shard
20
32
  @klass.connection()
@@ -1,4 +1,4 @@
1
- production:
1
+ octopus:
2
2
  shards:
3
3
  alone_shard:
4
4
  adapter: mysql
@@ -11,6 +11,25 @@ describe Octopus::Model do
11
11
  User.using(:canada).count.should == 1
12
12
  User.count.should == 0
13
13
  end
14
+
15
+ it "should allow selecting the shard using #new" do
16
+ u = User.using(:canada).new
17
+ u.name = "Thiago"
18
+ u.save
19
+
20
+ User.using(:canada).count.should == 1
21
+ User.using(:brazil).count.should == 0
22
+
23
+ u1 = User.new
24
+ u1.name = "Joaquim"
25
+ u2 = User.using(:canada).new
26
+ u2.name = "Manuel"
27
+ u1.save()
28
+ u2.save()
29
+
30
+ User.using(:canada).all.should == [u, u2]
31
+ User.all.should == [u1]
32
+ end
14
33
 
15
34
  it "should select the correct shard" do
16
35
  #TODO - Investigate this - why we need to set to master!?
@@ -184,6 +203,21 @@ describe Octopus::Model do
184
203
  @user2.update_attributes(:name => "Joaquim")
185
204
  User.using(:brazil).where(:name => "Joaquim").first.should_not be_nil
186
205
  end
206
+
207
+ it "transaction" do
208
+ u = User.create!(:name => "Thiago")
209
+
210
+ User.using(:brazil).count.should == 0
211
+ User.using(:master).count.should == 1
212
+
213
+ User.using(:brazil).transaction do
214
+ User.where(:name => "Thiago").first.should be_nil
215
+ User.create!(:name => "Brazil")
216
+ end
217
+
218
+ User.using(:brazil).count.should == 1
219
+ User.using(:master).count.should == 1
220
+ end
187
221
 
188
222
  describe "deleting a record" do
189
223
  before(:each) do
@@ -12,4 +12,10 @@ describe Octopus do
12
12
  Octopus.directory().should == File.expand_path(File.dirname(__FILE__) + "/../")
13
13
  end
14
14
  end
15
+
16
+ describe "#env method" do
17
+ it "should return 'production' when is outside of a rails application" do
18
+ Octopus.env().should == 'octopus'
19
+ end
20
+ end
15
21
  end
@@ -25,7 +25,7 @@ def using_enviroment(enviroment, &block)
25
25
  clean_connection_proxy()
26
26
  yield
27
27
  ensure
28
- Octopus.instance_variable_set(:@env, 'production')
28
+ Octopus.instance_variable_set(:@env, 'octopus')
29
29
  clean_connection_proxy()
30
30
  end
31
31
  end
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: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 12
10
- version: 0.0.12
9
+ - 13
10
+ version: 0.0.13
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thiago Pradi
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-06-29 00:00:00 -03:00
20
+ date: 2010-07-01 00:00:00 -03:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency