ar-octopus 0.0.20 → 0.0.21

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.20"
32
+ gem.version = "0.0.21"
33
33
  end
34
34
  Jeweler::GemcutterTasks.new
35
35
  rescue LoadError
@@ -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.20"
8
+ s.version = "0.0.21"
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-07-07}
12
+ s.date = %q{2010-07-08}
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 = [
@@ -4,23 +4,16 @@ module Octopus::Migration
4
4
  alias_method_chain :migrate, :octopus
5
5
  end
6
6
  end
7
-
7
+
8
8
  def using(*args, &block)
9
9
  Octopus.config()
10
10
 
11
11
  args.each do |shard|
12
- if !ActiveRecord::Base.using(shard).connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
13
- ActiveRecord::Base.using(shard).connection.initialize_schema_migrations_table
14
- end
15
- end
16
-
17
- if args.size == 1
18
- self.connection().block = true
19
- self.connection().current_shard = args.first
20
- else
21
- self.connection().current_shard = args
12
+ self.connection().check_schema_migrations(shard)
22
13
  end
23
14
 
15
+ self.connection().block = true
16
+ self.connection().current_shard = args
24
17
 
25
18
  yield if block_given?
26
19
 
@@ -29,36 +22,36 @@ module Octopus::Migration
29
22
 
30
23
  def using_group(*args)
31
24
  Octopus.config()
32
-
25
+
33
26
  args.each do |group_shard|
34
27
  shards = self.connection().instance_variable_get(:@groups)[group_shard] || []
35
28
 
36
29
  shards.each do |shard|
37
- if !ActiveRecord::Base.using(shard).connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
38
- ActiveRecord::Base.using(shard).connection.initialize_schema_migrations_table
39
- end
30
+ self.connection().check_schema_migrations(shard)
40
31
  end
41
32
  end
42
33
 
43
- if args.size == 1
44
- self.connection().block = true
45
- self.connection().current_group = args.first
46
- else
47
- self.connection().current_group = args
48
- end
34
+ self.connection().block = true
35
+ self.connection().current_group = args
49
36
 
50
37
  return self
51
38
  end
52
-
39
+
53
40
  def migrate_with_octopus(direction)
54
- ret = migrate_without_octopus(direction)
55
- # This Cleans the proxy
56
- # TODO - REFACTOR!
57
- ActiveRecord::Base.connection.instance_variable_set(:@current_shard, :master)
58
- ActiveRecord::Base.connection.instance_variable_set(:@current_group, nil)
59
- ActiveRecord::Base.connection().block = false
41
+ conn = ActiveRecord::Base.connection
42
+ groups = conn.instance_variable_get(:@groups)
43
+
44
+ if conn.current_group.is_a?(Array)
45
+ conn.current_group.each { |group| conn.send_queries_to_multiple_shards(groups[group]) { migrate_without_octopus(direction) } }
46
+ elsif conn.current_group.is_a?(Symbol)
47
+ conn.send_queries_to_multiple_shards(groups[conn.current_group]) { migrate_without_octopus(direction) }
48
+ elsif conn.current_shard.is_a?(Array)
49
+ conn.send_queries_to_multiple_shards(conn.current_shard) { migrate_without_octopus(direction) }
50
+ else
51
+ migrate_without_octopus(direction)
52
+ end
60
53
 
61
- return ret
54
+ conn.clean_proxy()
62
55
  end
63
56
  end
64
57
 
@@ -70,24 +70,36 @@ class Octopus::Proxy
70
70
  def shard_name
71
71
  current_shard.is_a?(Array) ? current_shard.first : current_shard
72
72
  end
73
+
74
+ def run_queries_on_shard(shard, &block)
75
+ older_shard = self.current_shard
76
+ self.block = true
77
+ self.current_shard = shard
73
78
 
74
- def add_transaction_record(record)
75
- if !select_connection().instance_variable_get(:@_current_transaction_records).nil?
76
- select_connection().add_transaction_record(record)
79
+ begin
80
+ yield
81
+ ensure
82
+ self.block = false
83
+ self.current_shard = older_shard
77
84
  end
78
85
  end
79
-
80
- def transaction(options = {}, &block)
81
- if should_send_queries_to_multiple_shards?
82
- self.send_transaction_to_multiple_shards(current_shard, options, &block)
83
- elsif should_send_queries_to_multiple_groups?
84
- self.send_transaction_to_multiple_groups(options, &block)
85
- @current_group = nil
86
- elsif should_send_queries_to_a_group_of_shards?
87
- self.send_transaction_to_multiple_shards(@groups[current_group], options, &block)
88
- @current_group = nil
89
- else
90
- select_connection.transaction(options, &block)
86
+
87
+ def send_queries_to_multiple_shards(shards, &block)
88
+ shards.each do |shard|
89
+ self.run_queries_on_shard(shard, &block)
90
+ end
91
+ end
92
+
93
+ def clean_proxy()
94
+ @using_enabled = nil
95
+ @current_shard = :master
96
+ @current_group = nil
97
+ @block = false
98
+ end
99
+
100
+ def check_schema_migrations(shard)
101
+ if !ActiveRecord::Base.using(shard).connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
102
+ ActiveRecord::Base.using(shard).connection.initialize_schema_migrations_table
91
103
  end
92
104
  end
93
105
 
@@ -95,35 +107,15 @@ class Octopus::Proxy
95
107
  if should_clean_connection?(method)
96
108
  conn = select_connection()
97
109
  self.last_current_shard = self.current_shard
98
- self.current_shard = :master
99
- @using_enabled = nil
110
+ clean_proxy()
100
111
  conn.send(method, *args, &block)
101
112
  elsif should_send_queries_to_replicated_databases?(method)
102
113
  send_queries_to_selected_slave(method, *args, &block)
103
- elsif should_send_queries_to_multiple_groups?
104
- send_queries_to_multiple_groups(method, *args, &block)
105
- elsif should_send_queries_to_multiple_shards?
106
- send_queries_to_shards(current_shard, method, *args, &block)
107
- elsif should_send_queries_to_a_group_of_shards?
108
- send_queries_to_shards(@groups[current_group], method, *args, &block)
109
114
  else
110
115
  select_connection().send(method, *args, &block)
111
116
  end
112
117
  end
113
118
 
114
- def run_queries_on_shard(shard, &block)
115
- older_shard = self.current_shard
116
- self.block = true
117
- self.current_shard = shard
118
-
119
- begin
120
- yield
121
- ensure
122
- self.block = false
123
- self.current_shard = older_shard
124
- end
125
- end
126
-
127
119
  protected
128
120
  def connection_pool_for(adapter, config)
129
121
  ActiveRecord::ConnectionAdapters::ConnectionPool.new(ActiveRecord::Base::ConnectionSpecification.new(adapter, config))
@@ -143,19 +135,7 @@ class Octopus::Proxy
143
135
  end
144
136
 
145
137
  def should_clean_connection?(method)
146
- method.to_s =~ /insert|select|execute/ && !should_send_queries_to_multiple_shards? && !self.current_group && !@replicated && !self.block
147
- end
148
-
149
- def should_send_queries_to_multiple_shards?
150
- current_shard.is_a?(Array)
151
- end
152
-
153
- def should_send_queries_to_multiple_groups?
154
- current_group.is_a?(Array)
155
- end
156
-
157
- def should_send_queries_to_a_group_of_shards?
158
- !current_group.nil?
138
+ method.to_s =~ /insert|select|execute/ && !self.current_group && !@replicated && !self.block
159
139
  end
160
140
 
161
141
  def should_send_queries_to_replicated_databases?(method)
@@ -166,26 +146,6 @@ class Octopus::Proxy
166
146
  !config[Octopus.env()].nil?
167
147
  end
168
148
 
169
- def send_queries_to_multiple_groups(method, *args, &block)
170
- method_return = nil
171
-
172
- current_group.each do |group_symbol|
173
- method_return = self.send_queries_to_shards(@groups[group_symbol], method, *args, &block)
174
- end
175
-
176
- return method_return
177
- end
178
-
179
- def send_queries_to_shards(shard_array, method, *args, &block)
180
- method_return = nil
181
-
182
- shard_array.each do |shard_symbol|
183
- method_return = @shards[shard_symbol].connection().send(method, *args, &block)
184
- end
185
-
186
- return method_return
187
- end
188
-
189
149
  def send_queries_to_selected_slave(method, *args, &block)
190
150
  old_shard = self.current_shard
191
151
 
@@ -203,16 +163,4 @@ class Octopus::Proxy
203
163
  @using_enabled = nil
204
164
  return sql
205
165
  end
206
-
207
- def send_transaction_to_multiple_shards(shard_array, options, &block)
208
- shard_array.each do |shard_symbol|
209
- @shards[shard_symbol].connection().transaction(options, &block)
210
- end
211
- end
212
-
213
- def send_transaction_to_multiple_groups(options, &block)
214
- current_group.each do |group_symbol|
215
- self.send_transaction_to_multiple_shards(@groups[group_symbol], options, &block)
216
- end
217
- end
218
166
  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: 55
4
+ hash: 53
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 20
10
- version: 0.0.20
9
+ - 21
10
+ version: 0.0.21
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-07-07 00:00:00 -03:00
20
+ date: 2010-07-08 00:00:00 -03:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency