ar-octopus 0.0.13 → 0.0.14

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.13"
32
+ gem.version = "0.0.14"
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.13"
8
+ s.version = "0.0.14"
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.rb CHANGED
@@ -14,6 +14,21 @@ module Octopus
14
14
  def self.directory()
15
15
  @directory ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd
16
16
  end
17
+
18
+ # This is the default way to do Octopus Setup
19
+ # Available variables:
20
+ # :excluded_enviroments => the enviroments that octopus will not run. default: :development, :cucumber and :test
21
+ def self.setup
22
+ yield self
23
+ end
24
+
25
+ def self.excluded_enviroments=(excluded_enviroments)
26
+ @excluded_enviroments = excluded_enviroments.map {|element| element.to_s }
27
+ end
28
+
29
+ def self.excluded_enviroments
30
+ @excluded_enviroments || ['development',"cucumber", "test"]
31
+ end
17
32
  end
18
33
 
19
34
  require "octopus/proxy"
data/lib/octopus/model.rb CHANGED
@@ -2,7 +2,7 @@ module Octopus::Model
2
2
  def self.extended(base)
3
3
  base.send(:include, InstanceMethods)
4
4
  base.extend(ClassMethods)
5
- base.hijack_connection() unless defined?(::Rails) && (Rails.env == 'test' || Rails.env == 'development')
5
+ base.hijack_connection() unless defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
6
6
  end
7
7
 
8
8
  module SharedMethods
@@ -11,7 +11,8 @@ module Octopus::Model
11
11
  end
12
12
 
13
13
  def using(shard, &block)
14
- return if defined?(::Rails) && (Rails.env == 'test' || Rails.env == 'development')
14
+ return self if defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
15
+
15
16
  hijack_connection()
16
17
  clean_table_name()
17
18
 
data/lib/octopus/proxy.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class Octopus::Proxy
2
2
  attr_accessor :current_model, :current_shard, :current_group, :block, :using_enabled, :last_current_shard
3
3
 
4
- def initialize(config)
4
+ def initialize(config)
5
5
  initialize_shards(config)
6
6
  initialize_replication(config) if have_a_valid_configuration?(config) && config[Octopus.env()]["replicated"]
7
7
  end
@@ -11,6 +11,11 @@ class Octopus::Proxy
11
11
  @groups = {}
12
12
  @shards[:master] = ActiveRecord::Base.connection_pool()
13
13
  @current_shard = :master
14
+
15
+ if have_a_valid_configuration?(config) && config[Octopus.env()]['excluded_enviroments']
16
+ Octopus.excluded_enviroments = config[Octopus.env()]['excluded_enviroments']
17
+ end
18
+
14
19
  shards_config = config[Octopus.env()]["shards"] if have_a_valid_configuration?(config)
15
20
  shards_config ||= []
16
21
 
@@ -8,6 +8,7 @@ class Octopus::ScopeProxy
8
8
 
9
9
  def using(shard, &block)
10
10
  @shard = shard
11
+
11
12
  if block_given?
12
13
  @klass.connection.run_queries_on_shard(@shard, &block)
13
14
  end
@@ -1,4 +1,8 @@
1
1
  octopus:
2
+ excluded_enviroments:
3
+ - cucumber
4
+ - test
5
+ - staging
2
6
  shards:
3
7
  alone_shard:
4
8
  adapter: mysql
@@ -205,6 +205,8 @@ describe Octopus::Model do
205
205
  end
206
206
 
207
207
  it "transaction" do
208
+ #TODO - Investigate this - why we need to set to master!?
209
+ ActiveRecord::Base.connection_proxy.current_shard = :master
208
210
  u = User.create!(:name => "Thiago")
209
211
 
210
212
  User.using(:brazil).count.should == 0
@@ -18,4 +18,27 @@ describe Octopus do
18
18
  Octopus.env().should == 'octopus'
19
19
  end
20
20
  end
21
+
22
+ describe "#setup method" do
23
+ it "should load from YAML" do
24
+ Octopus.excluded_enviroments.should == ["cucumber", "test", "staging"]
25
+ end
26
+
27
+ it "should have the default excluded enviroments" do
28
+ Octopus.instance_variable_set(:@excluded_enviroments, nil)
29
+ Octopus.excluded_enviroments.should == ["development", "cucumber", "test"]
30
+ end
31
+
32
+ it "should configure the excluded enviroments" do
33
+ Octopus.setup do |config|
34
+ config.excluded_enviroments = [:cucumber, :test]
35
+ end
36
+
37
+ Octopus.excluded_enviroments.should == ['cucumber', 'test']
38
+
39
+ Octopus.setup do |config|
40
+ config.excluded_enviroments = [:cucumber, :test, :staging]
41
+ end
42
+ end
43
+ end
21
44
  end
@@ -11,6 +11,14 @@ describe "when the database is replicated" do
11
11
  User.count.should == 1
12
12
  User.find(u.id).should == u
13
13
  end
14
+
15
+ it "should send all writes queries to master" do
16
+ u = Cat.using(:slave4).create!(:name => "Slave Cat")
17
+ u1 = Cat.using(:slave4).first
18
+ u1.name = "Miau"
19
+ u1.save()
20
+ Cat.using(:slave4).first.name.should == "Slave Cat"
21
+ end
14
22
 
15
23
  it "should send read queries to slaves, when you have a replicated model, using a round robin algorithm" do
16
24
  u = Cat.create!(:name => "master")
@@ -44,6 +52,14 @@ describe "when the database is replicated and the entire application is replicat
44
52
  Octopus.stub!(:env).and_return("production_entire_replicated")
45
53
  clean_connection_proxy()
46
54
  end
55
+
56
+ it "should send all writes queries to master" do
57
+ u = Client.using(:slave4).create!(:name => "Slave Client")
58
+ u1 = Client.using(:slave4).first
59
+ u1.name = "Client"
60
+ u1.save()
61
+ Client.using(:slave4).first.name.should == "Slave Client"
62
+ end
47
63
 
48
64
  it "should send read queries to slaves,to all models, using a round robin algorithm" do
49
65
  u = Cat.create!(:name => "master")
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: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 13
10
- version: 0.0.13
9
+ - 14
10
+ version: 0.0.14
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thiago Pradi