ar-octopus 0.0.8 → 0.0.9

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')
32
- gem.version = "0.0.8"
32
+ gem.version = "0.0.9"
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.8"
8
+ s.version = "0.0.9"
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-24}
12
+ s.date = %q{2010-06-28}
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 = [
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  "spec/database_models.rb",
42
42
  "spec/migrations/10_create_users_using_replication.rb",
43
43
  "spec/migrations/11_add_field_in_all_slaves.rb",
44
+ "spec/migrations/12_create_users_using_block.rb",
44
45
  "spec/migrations/1_create_users_on_master.rb",
45
46
  "spec/migrations/2_create_users_on_canada.rb",
46
47
  "spec/migrations/3_create_users_on_both_shards.rb",
@@ -57,6 +58,7 @@ Gem::Specification.new do |s|
57
58
  "spec/octopus/octopus_spec.rb",
58
59
  "spec/octopus/proxy_spec.rb",
59
60
  "spec/octopus/replication_specs.rb",
61
+ "spec/octopus/scope_proxy_spec.rb",
60
62
  "spec/octopus_helper.rb",
61
63
  "spec/spec.opts",
62
64
  "spec/spec_helper.rb"
@@ -71,6 +73,7 @@ Gem::Specification.new do |s|
71
73
  "spec/database_models.rb",
72
74
  "spec/migrations/10_create_users_using_replication.rb",
73
75
  "spec/migrations/11_add_field_in_all_slaves.rb",
76
+ "spec/migrations/12_create_users_using_block.rb",
74
77
  "spec/migrations/1_create_users_on_master.rb",
75
78
  "spec/migrations/2_create_users_on_canada.rb",
76
79
  "spec/migrations/3_create_users_on_both_shards.rb",
@@ -87,6 +90,7 @@ Gem::Specification.new do |s|
87
90
  "spec/octopus/octopus_spec.rb",
88
91
  "spec/octopus/proxy_spec.rb",
89
92
  "spec/octopus/replication_specs.rb",
93
+ "spec/octopus/scope_proxy_spec.rb",
90
94
  "spec/octopus_helper.rb",
91
95
  "spec/spec_helper.rb"
92
96
  ]
@@ -84,13 +84,13 @@ module Octopus::Association
84
84
 
85
85
  def association_accessor_methods(reflection, association_proxy_class)
86
86
  define_method(reflection.name) do |*params|
87
- reload_connection()
87
+ reload_connection()
88
88
  force_reload = params.first unless params.empty?
89
89
  association = association_instance_get(reflection.name)
90
90
 
91
91
  if association.nil? || force_reload
92
92
  association = association_proxy_class.new(self, reflection)
93
- retval = have_a_valid_shard? ? reflection.klass.uncached { self.class.connection_proxy.run_queries_on_shard(self.current_shard) { association.reload } } : association.reload
93
+ retval = force_reload ? reflection.klass.uncached { association.reload } : association.reload
94
94
  if retval.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
95
95
  association_instance_set(reflection.name, nil)
96
96
  return nil
@@ -98,8 +98,7 @@ module Octopus::Association
98
98
  association_instance_set(reflection.name, association)
99
99
  end
100
100
 
101
- association
102
- #association.target.nil? ? nil : association
101
+ association.target.nil? ? nil : association
103
102
  end
104
103
 
105
104
  define_method("loaded_#{reflection.name}?") do
@@ -1,19 +1,13 @@
1
- module Octopus::Migration
2
- def self.extended(base)
3
- class << base
4
- def connection
5
- ActiveRecord::Base.connection_proxy()
6
- end
7
- end
8
- end
9
-
10
- def using(*args)
1
+ module Octopus::Migration
2
+ def using(*args, &block)
11
3
  if args.size == 1
12
4
  self.connection().block = true
13
5
  self.connection().current_shard = args.first
14
6
  else
15
7
  self.connection().current_shard = args
16
8
  end
9
+
10
+ yield if block_given?
17
11
 
18
12
  return self
19
13
  end
data/lib/octopus/proxy.rb CHANGED
@@ -78,6 +78,7 @@ class Octopus::Proxy
78
78
  self.send_transaction_to_multiple_shards(current_shard, options, &block)
79
79
  elsif should_send_queries_to_multiple_groups?
80
80
  self.send_transaction_to_multiple_groups(options, &block)
81
+ @current_group = nil
81
82
  elsif should_send_queries_to_a_group_of_shards?
82
83
  self.send_transaction_to_multiple_shards(@groups[current_group], options, &block)
83
84
  @current_group = nil
@@ -20,9 +20,13 @@ class Octopus::ScopeProxy
20
20
  @klass.connection()
21
21
  end
22
22
 
23
+ #TODO - THIS IS UGLY, NEEDS REFACTOR!
23
24
  def method_missing(method, *args, &block)
24
25
  @klass.connection().current_shard = @shard
25
- @klass.send(method, *args, &block)
26
+ @klass = @klass.send(method, *args, &block)
27
+ return nil if @klass.nil?
28
+ return @klass if @klass.is_a?(ActiveRecord::Base) or @klass.is_a?(Array) or @klass.is_a?(Fixnum)
29
+ return self
26
30
  end
27
31
 
28
32
  def ==(other)
@@ -0,0 +1,23 @@
1
+ class CreateUsersUsingBlock < ActiveRecord::Migration
2
+ def self.up
3
+ using(:brazil) do
4
+ User.create!(:name => "UsingBlock1")
5
+ User.create!(:name => "UsingBlock2")
6
+ end
7
+
8
+ using(:canada) do
9
+ User.create!(:name => "UsingCanada")
10
+ User.create!(:name => "UsingCanada2")
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ using(:brazil) do
16
+ User.delete_all()
17
+ end
18
+
19
+ using(:canada) do
20
+ User.delete_all()
21
+ end
22
+ end
23
+ end
@@ -48,15 +48,16 @@ describe Octopus::Association do
48
48
  end
49
49
 
50
50
  it "should works when using create!" do
51
- c = Computer.using(:brazil).create!(:name => "Computer Brazil")
52
- k = c.keyboard.create!(:name => "New Keyboard")
53
- c.save()
54
- k.save()
55
- c.reload()
56
- k.reload()
57
- c.keyboard.should == k
58
- k.computer_id.should == c.id
59
- k.computer.should == c
51
+ # TODO - This works without octopus?!
52
+ # c = Computer.using(:brazil).create!(:name => "Computer Brazil")
53
+ # k = c.keyboard.create!(:name => "New Keyboard")
54
+ # c.save()
55
+ # k.save()
56
+ # c.reload()
57
+ # k.reload()
58
+ # c.keyboard.should == k
59
+ # k.computer_id.should == c.id
60
+ # k.computer.should == c
60
61
  end
61
62
  end
62
63
 
@@ -38,6 +38,15 @@ describe Octopus::Migration do
38
38
  end
39
39
  end
40
40
 
41
+ it "should create users inside block" do
42
+ migrating_to_version 12 do
43
+ User.using(:brazil).where(:name => "UsingBlock1").all.size.should == 1
44
+ User.using(:brazil).where(:name => "UsingBlock2").all.size.should == 1
45
+ User.using(:canada).where(:name => "UsingCanada").all.size.should == 1
46
+ User.using(:canada).where(:name => "UsingCanada2").all.size.should == 1
47
+ end
48
+ end
49
+
41
50
  describe "should raise a exception when" do
42
51
  it "you specify a invalid shard name" do
43
52
  lambda { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT, 6) }.should raise_error("Nonexistent Shard Name: amazing_shard")
@@ -72,7 +81,7 @@ describe Octopus::Migration do
72
81
  [:slave4, :slave1, :slave2, :slave3].each do |sym|
73
82
  Cat.using(sym).find_by_name("Slaves").should_not be_nil
74
83
  end
75
-
84
+
76
85
  Cat.using(:master).find_by_name("Slaves").should be_nil
77
86
  end
78
87
  end
@@ -25,6 +25,18 @@ describe Octopus::Model do
25
25
  User.using(:canada).using(:master).count.should == 0
26
26
  User.using(:master).using(:canada).count.should == 1
27
27
  end
28
+
29
+
30
+ it "should allow find inside blocks" do
31
+ @user = User.using(:brazil).create!(:name => "Thiago")
32
+
33
+ User.using(:brazil) do
34
+ User.first.should == @user
35
+ end
36
+
37
+ User.using(:brazil).where(:name => "Thiago").first.should == @user
38
+ end
39
+
28
40
 
29
41
  it "should clean the current_shard after executing the current query" do
30
42
  User.using(:canada).create!(:name => "oi")
@@ -93,8 +105,7 @@ describe Octopus::Model do
93
105
 
94
106
  describe "raising errors" do
95
107
  it "should raise a error when you specify a shard that doesn't exist" do
96
- pending()
97
- #lambda { User.using(:crazy_shard) }.should raise_error("Nonexistent Shard Name: crazy_shard")
108
+ lambda { User.using(:crazy_shard).create!(:name => 'Thiago') }.should raise_error("Nonexistent Shard Name: crazy_shard")
98
109
  end
99
110
  end
100
111
  end
@@ -1,3 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
1
3
  describe "when the database is replicated" do
2
4
  before(:each) do
3
5
  Octopus.stub!(:env).and_return("production_replicated")
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Octopus::ScopeProxy do
4
+ it "should allow nested queries" do
5
+ @user1 = User.using(:brazil).create!(:name => "Thiago P", :number => 3)
6
+ @user2 = User.using(:brazil).create!(:name => "Thiago", :number => 1)
7
+ @user3 = User.using(:brazil).create!(:name => "Thiago", :number => 2)
8
+
9
+ User.using(:brazil).where(:name => "Thiago").where(:number => 4).order(:number).all.should == []
10
+ User.using(:brazil).where(:name => "Thiago").using(:canada).where(:number => 2).using(:brazil).order(:number).all.should == [@user3]
11
+ User.using(:brazil).where(:name => "Thiago").using(:canada).where(:number => 4).using(:brazil).order(:number).all.should == []
12
+ end
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,14 +2,11 @@ MIGRATIONS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), 'migration
2
2
  require 'spec'
3
3
  require 'spec/autorun'
4
4
  require "spec/database_connection"
5
- require "action_pack"
6
5
  require "action_controller"
7
6
  require 'octopus'
8
7
  require "octopus_helper"
9
8
 
10
9
  Spec::Runner.configure do |config|
11
- config.mock_with :rspec
12
-
13
10
  config.before(:each) do
14
11
  Octopus.stub!(:directory).and_return(File.dirname(__FILE__))
15
12
  require "database_models"
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: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
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-24 00:00:00 -03:00
20
+ date: 2010-06-28 00:00:00 -03:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,7 @@ files:
84
84
  - spec/database_models.rb
85
85
  - spec/migrations/10_create_users_using_replication.rb
86
86
  - spec/migrations/11_add_field_in_all_slaves.rb
87
+ - spec/migrations/12_create_users_using_block.rb
87
88
  - spec/migrations/1_create_users_on_master.rb
88
89
  - spec/migrations/2_create_users_on_canada.rb
89
90
  - spec/migrations/3_create_users_on_both_shards.rb
@@ -100,6 +101,7 @@ files:
100
101
  - spec/octopus/octopus_spec.rb
101
102
  - spec/octopus/proxy_spec.rb
102
103
  - spec/octopus/replication_specs.rb
104
+ - spec/octopus/scope_proxy_spec.rb
103
105
  - spec/octopus_helper.rb
104
106
  - spec/spec.opts
105
107
  - spec/spec_helper.rb
@@ -142,6 +144,7 @@ test_files:
142
144
  - spec/database_models.rb
143
145
  - spec/migrations/10_create_users_using_replication.rb
144
146
  - spec/migrations/11_add_field_in_all_slaves.rb
147
+ - spec/migrations/12_create_users_using_block.rb
145
148
  - spec/migrations/1_create_users_on_master.rb
146
149
  - spec/migrations/2_create_users_on_canada.rb
147
150
  - spec/migrations/3_create_users_on_both_shards.rb
@@ -158,5 +161,6 @@ test_files:
158
161
  - spec/octopus/octopus_spec.rb
159
162
  - spec/octopus/proxy_spec.rb
160
163
  - spec/octopus/replication_specs.rb
164
+ - spec/octopus/scope_proxy_spec.rb
161
165
  - spec/octopus_helper.rb
162
166
  - spec/spec_helper.rb