ar-octopus 0.0.25 → 0.0.26

Sign up to get free protection for your applications and to get access to all the features.
data/README.mkdn CHANGED
@@ -47,7 +47,7 @@ Octopus adds a method to each AR Class and object. the using method is used to s
47
47
 
48
48
  Octopus also supports queries inside block. When you pass a block to the using method, all queries inside the block will be sent to the specified shard.
49
49
  <pre>
50
- User.using(:slave_two) do
50
+ Octopus.using(:slave_two) do
51
51
  User.create(:name => "Mike")
52
52
  end
53
53
  </pre>
@@ -67,6 +67,8 @@ end
67
67
  @user.save()
68
68
  </pre>
69
69
 
70
+ ### Migrations
71
+
70
72
  In migrations, you also have access to the using method. The syntax is basically the same. This migration will run in brazil and canada shards.
71
73
  <pre>
72
74
  class CreateUsersOnBothShards < ActiveRecord::Migration
@@ -96,6 +98,22 @@ end
96
98
  end
97
99
  </pre>
98
100
 
101
+
102
+ ### Rails Controllers
103
+
104
+ If you want to send a specified action, or all actions from a controller, to a specific shard, use this syntax:
105
+ <pre>
106
+ class ApplicationController < ActionController::Base
107
+ around_filter :select_shard
108
+
109
+ def select_shard
110
+ Octopus.using(:brazil) do
111
+ yield
112
+ end
113
+ end
114
+ end
115
+ </pre>
116
+
99
117
  To see the complete list of features and syntax, please check out our <a href="http://wiki.github.com/tchandy/octopus/"> Wiki</a>
100
118
  Wanna see sample rails applications using octopus features? please check it out: <a href="http://github.com/tchandy/octopus_sharding_example">Sharding Example</a> and <a href="http://github.com/tchandy/octopus_replication_example">Replication Example</a>
101
119
 
data/Rakefile CHANGED
@@ -2,8 +2,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
2
  require 'rubygems'
3
3
  require 'rake'
4
4
  require "yaml"
5
- require "active_support"
6
- require 'active_support/json'
5
+
7
6
  begin
8
7
  require 'metric_fu'
9
8
  MetricFu::Configuration.run do |config|
@@ -32,7 +31,7 @@ begin
32
31
  gem.add_development_dependency "pg", ">= 0.9.0"
33
32
  gem.add_development_dependency "sqlite3-ruby", ">= 1.3.1"
34
33
  gem.add_dependency('activerecord', '>= 3.0.0beta')
35
- gem.version = "0.0.25"
34
+ gem.version = "0.0.26"
36
35
  end
37
36
  Jeweler::GemcutterTasks.new
38
37
  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.25"
8
+ s.version = "0.0.26"
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-16}
12
+ s.date = %q{2010-07-19}
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 = [
@@ -29,7 +29,6 @@ Gem::Specification.new do |s|
29
29
  "lib/octopus.rb",
30
30
  "lib/octopus/association.rb",
31
31
  "lib/octopus/association_collection.rb",
32
- "lib/octopus/controller.rb",
33
32
  "lib/octopus/has_and_belongs_to_many_association.rb",
34
33
  "lib/octopus/migration.rb",
35
34
  "lib/octopus/model.rb",
data/lib/octopus.rb CHANGED
@@ -47,6 +47,11 @@ module Octopus
47
47
  def self.rails?
48
48
  defined?(Rails)
49
49
  end
50
+
51
+ def self.using(shard, &block)
52
+ ActiveRecord::Base.hijack_initializer()
53
+ ActiveRecord::Base.connection.run_queries_on_shard(shard, &block)
54
+ end
50
55
  end
51
56
 
52
57
 
@@ -66,5 +71,4 @@ end
66
71
 
67
72
  require "octopus/proxy"
68
73
  require "octopus/scope_proxy"
69
- require "octopus/controller"
70
74
 
@@ -5,6 +5,10 @@ module Octopus::Association
5
5
 
6
6
  module InstanceMethods
7
7
  def set_connection_on_association(record)
8
+ if !record.current_shard.nil? && !self.current_shard.nil? && record.current_shard != self.current_shard
9
+ raise "Association Error: Records are from different shards"
10
+ end
11
+
8
12
  record.current_shard = self.connection.current_shard = self.current_shard if should_set_current_shard?
9
13
  end
10
14
  end
@@ -5,7 +5,7 @@ module Octopus::AssociationCollection
5
5
 
6
6
  def count(*args)
7
7
  if should_wrap_the_connection?
8
- @owner.using(@owner.current_shard) { super }
8
+ Octopus.using(@owner.current_shard) { super }
9
9
  else
10
10
  super
11
11
  end
@@ -11,7 +11,7 @@ module Octopus::HasAndBelongsToManyAssociation
11
11
 
12
12
  def insert_record_with_octopus(record, force = true, validate = true)
13
13
  if should_wrap_the_connection?
14
- @owner.using(@owner.current_shard) { insert_record_without_octopus(record, force, validate) }
14
+ Octopus.using(@owner.current_shard) { insert_record_without_octopus(record, force, validate) }
15
15
  else
16
16
  insert_record_without_octopus(record, force, validate)
17
17
  end
@@ -11,8 +11,8 @@ module Octopus::Migration
11
11
  end
12
12
  end
13
13
 
14
- def using(*args, &block)
15
- if self.connection().is_a?(Octopus::Proxy) && !block_given?
14
+ def using(*args)
15
+ if self.connection().is_a?(Octopus::Proxy)
16
16
  args.each do |shard|
17
17
  self.connection().check_schema_migrations(shard)
18
18
  end
@@ -21,10 +21,6 @@ module Octopus::Migration
21
21
  self.current_shard = args
22
22
  self.connection().current_shard = args
23
23
  end
24
-
25
- if block_given?
26
- self.connection.run_queries_on_shard(args, &block)
27
- end
28
24
 
29
25
  return self
30
26
  end
data/lib/octopus/model.rb CHANGED
@@ -14,20 +14,15 @@ module Octopus::Model
14
14
  self.reset_table_name() if self != ActiveRecord::Base && self.respond_to?(:reset_table_name)
15
15
  end
16
16
 
17
- def using(shard, &block)
17
+ def using(shard)
18
18
  return self if defined?(::Rails) && !Octopus.enviroments.include?(Rails.env.to_s)
19
19
 
20
- hijack_connection()
21
20
  clean_table_name()
21
+ hijack_initializer()
22
+
23
+ self.connection.using_enabled = true
22
24
 
23
- if block_given?
24
- self.connection.run_queries_on_shard(shard, &block)
25
- else
26
- hijack_initializer()
27
- self.connection.using_enabled = true
28
-
29
- return Octopus::ScopeProxy.new(shard, self)
30
- end
25
+ return Octopus::ScopeProxy.new(shard, self)
31
26
  end
32
27
 
33
28
  def hijack_initializer()
@@ -11,9 +11,7 @@ module Octopus
11
11
 
12
12
  def delete_with_octopus()
13
13
  if should_set_current_shard?
14
- self.using(self.current_shard) do
15
- delete_without_octopus()
16
- end
14
+ Octopus.using(self.current_shard) { delete_without_octopus() }
17
15
  else
18
16
  delete_without_octopus()
19
17
  end
@@ -21,9 +19,7 @@ module Octopus
21
19
 
22
20
  def destroy_with_octopus()
23
21
  if should_set_current_shard?
24
- self.using(self.current_shard) do
25
- destroy_without_octopus()
26
- end
22
+ Octopus.using(self.current_shard) { destroy_without_octopus() }
27
23
  else
28
24
  destroy_without_octopus()
29
25
  end
@@ -31,9 +27,7 @@ module Octopus
31
27
 
32
28
  def reload_with_octopus(options = nil)
33
29
  if should_set_current_shard?
34
- self.using(self.current_shard) do
35
- reload_without_octopus(options)
36
- end
30
+ Octopus.using(self.current_shard) { reload_without_octopus(options) }
37
31
  else
38
32
  reload_without_octopus(options)
39
33
  end
@@ -6,13 +6,8 @@ class Octopus::ScopeProxy
6
6
  @klass = klass
7
7
  end
8
8
 
9
- def using(shard, &block)
9
+ def using(shard)
10
10
  @shard = shard
11
-
12
- if block_given?
13
- @klass.connection.run_queries_on_shard(@shard, &block)
14
- end
15
-
16
11
  return self
17
12
  end
18
13
 
@@ -1,7 +1,7 @@
1
1
  #The user class is just sharded, not replicated
2
2
  class User < ActiveRecord::Base
3
3
  def awesome_queries
4
- using(:canada) do
4
+ Octopus.using(:canada) do
5
5
  User.create(:name => "teste")
6
6
  end
7
7
  end
@@ -1,22 +1,22 @@
1
1
  class CreateUsersUsingBlock < ActiveRecord::Migration
2
2
  def self.up
3
- using(:brazil) do
3
+ Octopus.using(:brazil) do
4
4
  User.create!(:name => "UsingBlock1")
5
5
  User.create!(:name => "UsingBlock2")
6
6
  end
7
7
 
8
- using(:canada) do
8
+ Octopus.using(:canada) do
9
9
  User.create!(:name => "UsingCanada")
10
10
  User.create!(:name => "UsingCanada2")
11
11
  end
12
12
  end
13
13
 
14
14
  def self.down
15
- using(:brazil) do
15
+ Octopus.using(:brazil) do
16
16
  User.delete_all()
17
17
  end
18
18
 
19
- using(:canada) do
19
+ Octopus.using(:canada) do
20
20
  User.delete_all()
21
21
  end
22
22
  end
@@ -2,7 +2,7 @@ class CreateUsersUsingBlockAndUsing < ActiveRecord::Migration
2
2
  using(:brazil)
3
3
 
4
4
  def self.up
5
- using(:canada) do
5
+ Octopus.using(:canada) do
6
6
  User.create!(:name => "Canada")
7
7
  end
8
8
 
@@ -377,6 +377,12 @@ describe Octopus::Association do
377
377
  it "should finds the client that the item belongs" do
378
378
  @item_brazil.client.should == @brazil_client
379
379
  end
380
+
381
+ it "should raise error if you try to add a record from a different shard" do
382
+ lambda do
383
+ @brazil_client.items << Item.using(:canada).create!(:name => "New User")
384
+ end.should raise_error("Association Error: Records are from different shards")
385
+ end
380
386
 
381
387
  it "should update the attribute for the item" do
382
388
  new_brazil_client = Client.using(:brazil).create!(:name => "new Client")
@@ -1,11 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
- describe Octopus::Controller do
4
- it "should have the using method to select the shard" do
5
- a = ActionController::Base.new
6
- a.respond_to?(:using).should be_true
7
- end
8
-
3
+ describe "Rails Controllers" do
9
4
  it "should use #using method to in all requests" do
10
5
  class UsersControllers < ActionController::Base
11
6
  around_filter :select_shard
@@ -15,7 +10,7 @@ describe Octopus::Controller do
15
10
  end
16
11
 
17
12
  def select_shard
18
- using(:brazil) do
13
+ Octopus.using(:brazil) do
19
14
  yield
20
15
  end
21
16
  end
@@ -56,7 +56,7 @@ describe Octopus::Model do
56
56
  it "should allow find inside blocks" do
57
57
  @user = User.using(:brazil).create!(:name => "Thiago")
58
58
 
59
- User.using(:brazil) do
59
+ Octopus.using(:brazil) do
60
60
  User.first.should == @user
61
61
  end
62
62
 
@@ -112,7 +112,7 @@ describe Octopus::Model do
112
112
 
113
113
  describe "passing a block" do
114
114
  it "should allow queries be executed inside the block, ponting to a specific shard" do
115
- User.using(:canada) do
115
+ Octopus.using(:canada) do
116
116
  User.create(:name => "oi")
117
117
  end
118
118
 
@@ -197,7 +197,7 @@ describe Octopus::Model do
197
197
  end
198
198
 
199
199
  it "using update_attributes inside a block" do
200
- ActiveRecord::Base.using(:brazil) do
200
+ Octopus.using(:brazil) do
201
201
  @user = User.create!(:name => "User1")
202
202
  @user2 = User.find(@user.id)
203
203
  @user2.update_attributes(:name => "Joaquim")
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: 45
4
+ hash: 43
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 25
10
- version: 0.0.25
9
+ - 26
10
+ version: 0.0.26
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-16 00:00:00 -03:00
19
+ date: 2010-07-19 00:00:00 -03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -121,7 +121,6 @@ files:
121
121
  - lib/octopus.rb
122
122
  - lib/octopus/association.rb
123
123
  - lib/octopus/association_collection.rb
124
- - lib/octopus/controller.rb
125
124
  - lib/octopus/has_and_belongs_to_many_association.rb
126
125
  - lib/octopus/migration.rb
127
126
  - lib/octopus/model.rb
@@ -1,7 +0,0 @@
1
- module Octopus::Controller
2
- def using(shard, &block)
3
- ActiveRecord::Base.using(shard, &block)
4
- end
5
- end
6
-
7
- ActionController::Base.send(:include, Octopus::Controller) if defined?(ActionController)