ar-octopus 0.3.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  octopus:
2
- enviroments:
2
+ environments:
3
3
  - development
4
4
  - test
5
5
  development:
@@ -39,6 +39,7 @@ Feature: rake db:migrate
39
39
  When I run inside my Rails project "rake db:abort_if_pending_migrations" with enviroment "development"
40
40
  Then the output should contain "pending migrations"
41
41
  When I run inside my Rails project "rake db:migrate" with enviroment "development"
42
- When I run inside my Rails project "rake db:abort_if_pending_migrations" with enviroment "development"
42
+ When I run inside my Rails project "rake db:abort_if_pending_migrations RAILS_ENV=development" with enviroment "development"
43
43
  Then the output should not contain "pending migrations"
44
+
44
45
 
@@ -1,6 +1,4 @@
1
1
  Then /^the "([^"]*)" shard should have one user named "([^"]*)"$/ do |shard_name, user_name|
2
- # TODO - WTF?! - Why we need to instantiate a new proxy?
3
- Thread.current[:connection_proxy] = nil
4
2
  User.using(shard_name.to_sym).where(:name => user_name).count.should == 1
5
3
  end
6
4
 
@@ -11,5 +9,5 @@ Then /^the version of "([^"]*)" shard should be "([^"]*)"$/ do |shard_name, vers
11
9
  end
12
10
 
13
11
  When /^I run inside my Rails project "([^"]*)" with enviroment "([^"]*)"$/ do |command, enviroment|
14
- run(unescape("cd #{Rails.root.to_s} && RAILS_ENV=#{enviroment} #{command}"), false)
12
+ run("cd #{Rails.root.to_s} && RAILS_ENV=#{enviroment} #{command}")
15
13
  end
@@ -17,6 +17,7 @@ require 'capybara/rails'
17
17
  require 'capybara/cucumber'
18
18
  require 'capybara/session'
19
19
  require 'cucumber/rails/capybara_javascript_emulation' # Lets you click links with onclick javascript handlers without using @culerity or @javascript
20
+ require 'aruba/cucumber'
20
21
  # Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
21
22
  # order to ease the transition to Capybara we set the default here. If you'd
22
23
  # prefer to use XPath just remove this line and adjust any selectors in your
@@ -57,6 +58,13 @@ if defined?(ActiveRecord::Base)
57
58
  end
58
59
  end
59
60
 
61
+ After do
62
+ `rm #{Rails.root.to_s}/db/america.sqlite3`
63
+ `rm #{Rails.root.to_s}/db/asia.sqlite3`
64
+ `rm #{Rails.root.to_s}/db/development.sqlite3`
65
+ `rm #{Rails.root.to_s}/db/europe.sqlite3`
66
+ end
67
+
60
68
  Before do
61
69
  `rm #{Rails.root.to_s}/db/america.sqlite3`
62
70
  `rm #{Rails.root.to_s}/db/asia.sqlite3`
@@ -1,5 +1,5 @@
1
1
  mysql: &mysql
2
- adapter: <%= 'mysql' %>
2
+ adapter: <%= 'mysql2' %>
3
3
  host: localhost
4
4
 
5
5
  octopus:
@@ -7,18 +7,18 @@ octopus:
7
7
  alone_shard:
8
8
  database: octopus_shard5
9
9
  <<: *mysql
10
-
10
+
11
11
  postgresql_shard:
12
12
  adapter: postgresql
13
- username: postgres
13
+ username: <%= ENV['POSTGRES_USER'] || 'postgres'%>
14
14
  password:
15
15
  database: octopus_shard1
16
16
  encoding: unicode
17
-
17
+
18
18
  sqlite_shard:
19
19
  adapter: sqlite3
20
20
  database: /tmp/database.sqlite3
21
-
21
+
22
22
  history_shards:
23
23
  aug2009:
24
24
  database: octopus_shard2
@@ -29,7 +29,7 @@ octopus:
29
29
  aug2011:
30
30
  database: octopus_shard4
31
31
  <<: *mysql
32
-
32
+
33
33
  country_shards:
34
34
  canada:
35
35
  database: octopus_shard2
@@ -40,24 +40,24 @@ octopus:
40
40
  russia:
41
41
  database: octopus_shard4
42
42
  <<: *mysql
43
-
44
-
43
+
44
+
45
45
  production_raise_error:
46
46
  shards:
47
47
  history_shards:
48
48
  duplicated_shard_name:
49
49
  database: octopus_shard5
50
50
  <<: *mysql
51
-
51
+
52
52
  country_shards:
53
53
  duplicated_shard_name:
54
54
  database: octopus_shard4
55
55
  <<: *mysql
56
-
56
+
57
57
  production_replicated:
58
58
  replicated: true
59
59
  fully_replicated: false
60
-
60
+
61
61
  shards:
62
62
  slave1:
63
63
  database: octopus_shard2
@@ -88,7 +88,7 @@ production_fully_replicated:
88
88
  slave4:
89
89
  database: octopus_shard5
90
90
  <<: *mysql
91
-
91
+
92
92
  octopus_rails:
93
93
  replicated: true
94
94
  verify_connection: true
@@ -127,5 +127,3 @@ not_entire_sharded:
127
127
  russia:
128
128
  database: octopus_shard5
129
129
  <<: *mysql
130
-
131
-
@@ -1,4 +1,4 @@
1
1
  require 'rubygems'
2
2
  require 'active_record'
3
3
 
4
- ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "octopus_shard1", :username => "root", :password => "")
4
+ ActiveRecord::Base.establish_connection(:adapter => "mysql2", :database => "octopus_shard1", :username => "root", :password => "")
@@ -21,7 +21,7 @@ end
21
21
 
22
22
  #This class sets its own connection
23
23
  class CustomConnection < ActiveRecord::Base
24
- octopus_establish_connection(:adapter => "mysql", :database => "octopus_shard2", :username => "root", :password => "")
24
+ octopus_establish_connection(:adapter => "mysql2", :database => "octopus_shard2", :username => "root", :password => "")
25
25
  end
26
26
 
27
27
  #This items belongs to a client
@@ -58,7 +58,7 @@ class Assignment < ActiveRecord::Base
58
58
  end
59
59
 
60
60
  class Programmer < ActiveRecord::Base
61
- has_many :assignments
61
+ has_many :assignments
62
62
  has_many :projects, :through => :assignments
63
63
  end
64
64
 
@@ -70,3 +70,8 @@ end
70
70
  class Comment < ActiveRecord::Base
71
71
  belongs_to :commentable, :polymorphic => true
72
72
  end
73
+
74
+
75
+ class Bacon < ActiveRecord::Base
76
+ octopus_set_table_name "yummy"
77
+ end
@@ -1,5 +1,5 @@
1
1
  class CreateUsersOnMultiplesGroups < ActiveRecord::Migration
2
- using_group(:country_shards, :history_shards)
2
+ using_group('country_shards', 'history_shards')
3
3
 
4
4
  def self.up
5
5
  User.create!(:name => "MultipleGroup")
@@ -8,4 +8,4 @@ class CreateUsersOnMultiplesGroups < ActiveRecord::Migration
8
8
  def self.down
9
9
  User.delete_all()
10
10
  end
11
- end
11
+ end
@@ -654,4 +654,4 @@ describe Octopus::Association do
654
654
  end
655
655
  end
656
656
  end
657
- end
657
+ end
@@ -22,7 +22,7 @@ describe "Rails Controllers" do
22
22
 
23
23
  if Octopus.rails3?
24
24
  a = UsersControllers.new
25
- a.stub!(:request).and_return(mock({:fullpath => "", :filtered_parameters => {}, :formats => ["xml"], :method => "GET"}))
25
+ a.stub!(:request).and_return(mock({:fullpath => "", :filtered_parameters => {}, :formats => [mock(:to_sym => :xml, :ref => "xml")], :method => "GET"}))
26
26
  a.instance_variable_set(:@_response, mock(:content_type => "xml", :body= => "", :status => 401))
27
27
  a.process(:create)
28
28
  User.using(:brazil).find_by_name("ActionController").should_not be_nil
@@ -31,4 +31,4 @@ describe "Rails Controllers" do
31
31
  pending()
32
32
  end
33
33
  end
34
- end
34
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Octopus::Logger do
4
+
5
+ before :each do
6
+ @out = StringIO.new
7
+ @log = Octopus::Logger.new(@out)
8
+ ActiveRecord::Base.logger = @log
9
+ end
10
+
11
+ after :each do
12
+ ActiveRecord::Base.logger = nil
13
+ end
14
+
15
+ it "should add to the default logger what shard the query was sent" do
16
+ User.using(:canada).create!(:name => "test")
17
+ @out.string.should =~ /Shard: canada/
18
+ end
19
+ end
@@ -5,15 +5,29 @@ describe Octopus::Model do
5
5
  it "should return self after calling the #using method" do
6
6
  User.using(:canada).should == Octopus::ScopeProxy.new(:canada, User)
7
7
  end
8
-
8
+
9
9
  it "should allow to send a block to the master shard" do
10
- Octopus.using(:master) do
10
+ Octopus.using(:master) do
11
11
  User.create!(:name => "Block test")
12
12
  end
13
-
13
+
14
14
  User.using(:master).find_by_name("Block test").should_not be_nil
15
15
  end
16
16
 
17
+ it 'should allow to pass a string as the shard name to a AR subclass' do
18
+ User.using('canada').create!(:name => 'Rafael Pilha')
19
+
20
+ User.using('canada').find_by_name('Rafael Pilha').should_not be_nil
21
+ end
22
+
23
+ it 'should allow to pass a string as the shard name to a block' do
24
+ Octopus.using('canada') do
25
+ User.create!(:name => 'Rafael Pilha')
26
+ end
27
+
28
+ User.using('canada').find_by_name('Rafael Pilha').should_not be_nil
29
+ end
30
+
17
31
  it "should allow selecting the shards on scope" do
18
32
  User.using(:canada).create!(:name => 'oi')
19
33
  User.using(:canada).count.should == 1
@@ -44,11 +58,11 @@ describe Octopus::Model do
44
58
  User.create!(:name => 'oi')
45
59
  User.count.should == 1
46
60
  end
47
-
61
+
48
62
  it "should ensure that the connection will be cleaned" do
49
63
  ActiveRecord::Base.connection.current_shard.should == :master
50
64
  begin
51
- Octopus.using(:canada) do
65
+ Octopus.using(:canada) do
52
66
  raise "Some Exception"
53
67
  end
54
68
  rescue
@@ -61,7 +75,7 @@ describe Octopus::Model do
61
75
  User.using(:canada).create([{ :name => 'America User 1' }, { :name => 'America User 2' }])
62
76
  User.create!(:name => "Thiago")
63
77
  User.using(:canada).find_by_name("America User 1").should_not be_nil
64
- User.using(:canada).find_by_name("America User 2").should_not be_nil
78
+ User.using(:canada).find_by_name("America User 2").should_not be_nil
65
79
  User.using(:master).find_by_name("Thiago").should_not be_nil
66
80
  end
67
81
 
@@ -110,7 +124,7 @@ describe Octopus::Model do
110
124
  u.current_shard.should == :alone_shard
111
125
  User.using(:canada).create!(:name => 'oi')
112
126
  u = User.using(:canada).find_by_name("oi")
113
- u.current_shard.should == :canada
127
+ u.current_shard.should == :canada
114
128
  end
115
129
 
116
130
  it "should store the attribute when you find multiple instances" do
@@ -136,7 +150,7 @@ describe Octopus::Model do
136
150
  u.reload
137
151
  u.name.should == "Alone"
138
152
  end
139
-
153
+
140
154
  it "should work passing some arguments to reload method" do
141
155
  User.using(:alone_shard).create!(:name => "Alone")
142
156
  u = User.using(:alone_shard).find_by_name("Alone")
@@ -153,7 +167,7 @@ describe Octopus::Model do
153
167
 
154
168
  User.using(:canada).count.should == 1
155
169
  User.using(:master).count.should == 0
156
- User.count.should == 0
170
+ User.count.should == 0
157
171
  end
158
172
 
159
173
  it "should allow execute queries inside a model" do
@@ -175,20 +189,21 @@ describe Octopus::Model do
175
189
  it "should update the Arel Engine" do
176
190
  if ActiveRecord::VERSION::STRING > '2.4.0'
177
191
  User.using(:postgresql_shard).arel_engine.connection.adapter_name.should == "PostgreSQL"
178
- User.using(:alone_shard).arel_engine.connection.adapter_name.should == "MySQL"
192
+ User.using(:alone_shard).arel_engine.connection.adapter_name.should == "Mysql2"
179
193
  end
180
194
  end
181
195
 
182
196
  it "should works with writes and reads" do
183
- u = User.using(:postgresql_shard).create!(:name => "PostgreSQL User")
184
- User.using(:postgresql_shard).find(:all).should == [u]
197
+ u = User.using(:postgresql_shard).create!(:name => "PostgreSQL User")
198
+ User.using(:postgresql_shard).find(:all).should == [u]
185
199
  User.using(:alone_shard).find(:all).should == []
200
+ User.connection_handler.connection_pools["ActiveRecord::Base"] = User.connection.instance_variable_get(:@shards)[:master]
186
201
  end
187
202
  end
188
203
 
189
204
  describe "AR basic methods" do
190
- it "establish_connection" do
191
- CustomConnection.connection.current_database.should == "octopus_shard2"
205
+ it "octopus_establish_connection" do
206
+ CustomConnection.connection.current_database.should == "octopus_shard2"
192
207
  end
193
208
 
194
209
  it "increment" do
@@ -196,7 +211,7 @@ describe Octopus::Model do
196
211
  u = User.using(:brazil).find_by_number(10)
197
212
  u.increment(:number)
198
213
  u.save()
199
- u = User.using(:brazil).find_by_number(11).should_not be_nil
214
+ u = User.using(:brazil).find_by_number(11).should_not be_nil
200
215
  end
201
216
 
202
217
  it "increment!" do
@@ -231,15 +246,15 @@ describe Octopus::Model do
231
246
  it "update_attributes" do
232
247
  @user = User.using(:brazil).create!(:name => "User1")
233
248
  @user2 = User.using(:brazil).find(@user.id)
234
- @user2.update_attributes(:name => "Joaquim")
235
- User.using(:brazil).find_by_name("Joaquim").should_not be_nil
249
+ @user2.update_attributes(:name => "Joaquim")
250
+ User.using(:brazil).find_by_name("Joaquim").should_not be_nil
236
251
  end
237
252
 
238
253
  it "using update_attributes inside a block" do
239
254
  Octopus.using(:brazil) do
240
255
  @user = User.create!(:name => "User1")
241
256
  @user2 = User.find(@user.id)
242
- @user2.update_attributes(:name => "Joaquim")
257
+ @user2.update_attributes(:name => "Joaquim")
243
258
  end
244
259
 
245
260
  User.find_by_name("Joaquim").should be_nil
@@ -249,7 +264,7 @@ describe Octopus::Model do
249
264
  it "update_attribute" do
250
265
  @user = User.using(:brazil).create!(:name => "User1")
251
266
  @user2 = User.using(:brazil).find(@user.id)
252
- @user2.update_attribute(:name, "Joaquim")
267
+ @user2.update_attribute(:name, "Joaquim")
253
268
  User.using(:brazil).find_by_name("Joaquim").should_not be_nil
254
269
  end
255
270
 
@@ -278,12 +293,12 @@ describe Octopus::Model do
278
293
  @user2.delete
279
294
  lambda { User.using(:brazil).find(@user2.id) }.should raise_error(ActiveRecord::RecordNotFound)
280
295
  end
281
-
296
+
282
297
  it "delete within block shouldn't lose shard" do
283
298
  Octopus.using(:brazil) do
284
299
  @user2.delete
285
300
  @user3 = User.create(:name => "User3")
286
-
301
+
287
302
  User.connection.current_shard.should == :brazil
288
303
  User.find(@user3.id).should == @user3
289
304
  end
@@ -293,12 +308,12 @@ describe Octopus::Model do
293
308
  @user2.destroy
294
309
  lambda { User.using(:brazil).find(@user2.id) }.should raise_error(ActiveRecord::RecordNotFound)
295
310
  end
296
-
311
+
297
312
  it "destroy within block shouldn't lose shard" do
298
313
  Octopus.using(:brazil) do
299
314
  @user2.destroy
300
315
  @user3 = User.create(:name => "User3")
301
-
316
+
302
317
  User.connection.current_shard.should == :brazil
303
318
  User.find(@user3.id).should == @user3
304
319
  end
@@ -306,11 +321,26 @@ describe Octopus::Model do
306
321
  end
307
322
  end
308
323
 
309
- describe "when you have joins/include" do
324
+ describe "when using set_table_name" do
325
+ it 'should work correctly' do
326
+ Bacon.using(:brazil).create!(:name => "YUMMMYYYY")
327
+ end
328
+ end
329
+
330
+ describe "when using a environment with a single adapter" do
331
+ it 'should not clean the table name' do
332
+ using_environment :production_fully_replicated do
333
+ Keyboard.should_not_receive(:reset_table_name)
334
+ Keyboard.using(:master).create!("Master Cat")
335
+ end
336
+ end
337
+ end
338
+
339
+ describe "when you have joins/include" do
310
340
  before(:each) do
311
341
  @client1 = Client.using(:brazil).create(:name => "Thiago")
312
-
313
- Octopus.using(:canada) do
342
+
343
+ Octopus.using(:canada) do
314
344
  @client2 = Client.create(:name => "Mike")
315
345
  @client3 = Client.create(:name => "Joao")
316
346
  @item1 = Item.create(:client => @client2, :name => "Item 1")
@@ -320,24 +350,24 @@ describe Octopus::Model do
320
350
  @part2 = Part.create(:item => @item1, :name => "Part 2")
321
351
  @part3 = Part.create(:item => @item2, :name => "Part 3")
322
352
  end
323
-
353
+
324
354
  @item4 = Item.using(:brazil).create(:client => @client1, :name => "Item 4")
325
355
  end
326
-
356
+
327
357
  it "should work with the rails 2.x syntax" do
328
- items = Item.using(:canada).find(:all, :joins => :client, :conditions => { :clients => { :id => @client2.id } })
358
+ items = Item.using(:canada).find(:all, :joins => :client, :conditions => { :clients => { :id => @client2.id } })
329
359
  items.should == [@item1, @item2]
330
360
  end
331
-
361
+
332
362
  it "should work using the rails 3.x syntax" do
333
363
  if Octopus.rails3?
334
364
  items = Item.using(:canada).joins(:client).where("clients.id = #{@client2.id}").all
335
- items.should == [@item1, @item2]
365
+ items.should == [@item1, @item2]
336
366
  end
337
367
  end
338
-
368
+
339
369
  it "should work for include also, rails 2.x syntax" do
340
- items = Item.using(:canada).find(:all, :include => :client, :conditions => { :clients => { :id => @client2.id } })
370
+ items = Item.using(:canada).find(:all, :include => :client, :conditions => { :clients => { :id => @client2.id } })
341
371
  items.should == [@item1, @item2]
342
372
  end
343
373
 
@@ -347,13 +377,13 @@ describe Octopus::Model do
347
377
  items.should == [@item1, @item2]
348
378
  end
349
379
  end
350
-
380
+
351
381
  it "should work for multiple includes, with rails 2.x syntax" do
352
382
  parts = Part.using(:canada).find(:all, :include => {:item => :client}, :conditions => {:clients => { :id => @client2.id}})
353
383
  parts.should == [@part1, @part2, @part3]
354
384
  parts.first.item.client.should == @client2
355
385
  end
356
-
386
+
357
387
  it "should work for multiple join, with rails 2.x syntax" do
358
388
  parts = Part.using(:canada).find(:all, :joins => {:item => :client}, :conditions => {:clients => { :id => @client2.id}})
359
389
  parts.should == [@part1, @part2, @part3]
@@ -377,13 +407,13 @@ describe Octopus::Model do
377
407
 
378
408
  describe "#replicated_model method" do
379
409
  it "should be replicated" do
380
- using_environment :production_replicated do
410
+ using_environment :production_replicated do
381
411
  ActiveRecord::Base.connection_proxy.instance_variable_get(:@replicated).should be_true
382
412
  end
383
413
  end
384
414
 
385
415
  it "should mark the Cat model as replicated" do
386
- using_environment :production_replicated do
416
+ using_environment :production_replicated do
387
417
  User.read_inheritable_attribute(:replicated).should be_false
388
418
  Cat.read_inheritable_attribute(:replicated).should be_true
389
419
  end