ar-octopus 0.0.18 → 0.0.19

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.18"
32
+ gem.version = "0.0.19"
33
33
  end
34
34
  Jeweler::GemcutterTasks.new
35
35
  rescue LoadError
@@ -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.18"
8
+ s.version = "0.0.19"
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"]
@@ -7,6 +7,12 @@ module Octopus
7
7
 
8
8
  def self.config()
9
9
  @config ||= HashWithIndifferentAccess.new(YAML.load_file(Octopus.directory() + "/config/shards.yml"))
10
+
11
+ if !@config[Octopus.env].nil? && @config[Octopus.env()]['excluded_enviroments']
12
+ self.excluded_enviroments = @config[Octopus.env()]['excluded_enviroments']
13
+ end
14
+
15
+ @config
10
16
  end
11
17
 
12
18
  # Returns the Rails.root_to_s when you are using rails
@@ -74,7 +74,7 @@ module Octopus::Association
74
74
  return_val = association.send(constructor, attributees)
75
75
  end
76
76
 
77
- if have_a_valid_shard?
77
+ if should_set_current_shard?
78
78
  return_val.current_shard = self.current_shard
79
79
  end
80
80
 
@@ -1,6 +1,6 @@
1
1
  module Octopus::Controller
2
2
  def using(shard, &block)
3
- ActiveRecord::Base.connection.run_queries_on_shard(shard, &block)
3
+ ActiveRecord::Base.using(shard, &block)
4
4
  end
5
5
  end
6
6
 
@@ -1,5 +1,7 @@
1
1
  module Octopus::Migration
2
2
  def using(*args, &block)
3
+ Octopus.config()
4
+
3
5
  args.each do |shard|
4
6
  if !ActiveRecord::Base.using(shard).connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
5
7
  ActiveRecord::Base.using(shard).connection.initialize_schema_migrations_table
@@ -19,6 +21,18 @@ module Octopus::Migration
19
21
  end
20
22
 
21
23
  def using_group(*args)
24
+ Octopus.config()
25
+
26
+ args.each do |group_shard|
27
+ shards = self.connection().instance_variable_get(:@groups)[group_shard] || []
28
+
29
+ shards.each do |shard|
30
+ if !ActiveRecord::Base.using(shard).connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
31
+ ActiveRecord::Base.using(shard).connection.initialize_schema_migrations_table
32
+ end
33
+ end
34
+ end
35
+
22
36
  if args.size == 1
23
37
  self.connection().block = true
24
38
  self.connection().current_group = args.first
@@ -60,13 +60,13 @@ module Octopus::Model
60
60
  def set_connection(*args)
61
61
  if(args.size == 1)
62
62
  arg = args.first
63
- arg.current_shard = self.current_shard if arg.respond_to?(:current_shard) && have_a_valid_shard?
63
+ arg.current_shard = self.current_shard if arg.respond_to?(:current_shard) && should_set_current_shard?
64
64
  end
65
65
 
66
- self.connection.current_shard = self.current_shard if have_a_valid_shard?
66
+ self.connection.current_shard = self.current_shard if should_set_current_shard?
67
67
  end
68
68
 
69
- def have_a_valid_shard?
69
+ def should_set_current_shard?
70
70
  self.respond_to?(:current_shard) && self.current_shard != nil
71
71
  end
72
72
  end
@@ -1,6 +1,6 @@
1
1
  module Octopus::Persistence
2
2
  def reload_connection()
3
- set_connection() if have_a_valid_shard?
3
+ set_connection() if should_set_current_shard?
4
4
  end
5
5
 
6
6
  def update_attribute(name, value)
@@ -3,7 +3,7 @@ class Octopus::Proxy
3
3
 
4
4
  def initialize(config)
5
5
  initialize_shards(config)
6
- initialize_replication(config) if have_a_valid_configuration?(config) && config[Octopus.env()]["replicated"]
6
+ initialize_replication(config) if have_config_for_enviroment?(config) && config[Octopus.env()]["replicated"]
7
7
  end
8
8
 
9
9
  def initialize_shards(config)
@@ -12,11 +12,7 @@ class Octopus::Proxy
12
12
  @shards[:master] = ActiveRecord::Base.connection_pool()
13
13
  @current_shard = :master
14
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
-
19
- shards_config = config[Octopus.env()]["shards"] if have_a_valid_configuration?(config)
15
+ shards_config = config[Octopus.env()]["shards"] if have_config_for_enviroment?(config)
20
16
  shards_config ||= []
21
17
 
22
18
  shards_config.each do |key, value|
@@ -75,10 +71,6 @@ class Octopus::Proxy
75
71
  current_shard.is_a?(Array) ? current_shard.first : current_shard
76
72
  end
77
73
 
78
- def have_a_valid_configuration?(config)
79
- !config[Octopus.env()].nil?
80
- end
81
-
82
74
  def add_transaction_record(record)
83
75
  if !select_connection().instance_variable_get(:@_current_transaction_records).nil?
84
76
  select_connection().add_transaction_record(record)
@@ -169,6 +161,10 @@ class Octopus::Proxy
169
161
  def should_send_queries_to_replicated_databases?(method)
170
162
  @replicated && method.to_s =~ /select/
171
163
  end
164
+
165
+ def have_config_for_enviroment?(config)
166
+ !config[Octopus.env()].nil?
167
+ end
172
168
 
173
169
  def send_queries_to_multiple_groups(method, *args, &block)
174
170
  method_return = nil
@@ -36,6 +36,11 @@ describe Octopus::Model do
36
36
  User.create!(:name => 'oi')
37
37
  User.count.should == 1
38
38
  end
39
+
40
+ it "should clean #current_shard from proxy when using execute" do
41
+ ActiveRecord::Base.using(:canada).connection().execute("select * from users limit 1;")
42
+ ActiveRecord::Base.connection.current_shard.should == :master
43
+ end
39
44
 
40
45
  it "should allow scoping dynamically" do
41
46
  User.using(:canada).using(:master).using(:canada).create!(:name => 'oi')
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: 59
4
+ hash: 57
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 18
10
- version: 0.0.18
9
+ - 19
10
+ version: 0.0.19
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thiago Pradi