ar-octopus 0.4.0 → 0.5.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.
- data/.gitignore +11 -0
- data/.travis.yml +22 -0
- data/Appraisals +18 -0
- data/Gemfile +3 -12
- data/README.mkdn +63 -24
- data/Rakefile +70 -92
- data/ar-octopus.gemspec +25 -198
- data/lib/ar-octopus.rb +1 -0
- data/lib/octopus.rb +73 -25
- data/lib/octopus/association.rb +6 -5
- data/lib/octopus/association_collection.rb +58 -4
- data/lib/octopus/has_and_belongs_to_many_association.rb +4 -4
- data/lib/octopus/logger.rb +9 -4
- data/lib/octopus/migration.rb +155 -50
- data/lib/octopus/model.rb +98 -34
- data/lib/octopus/proxy.rb +124 -53
- data/lib/octopus/rails2/association.rb +46 -93
- data/lib/octopus/rails2/persistence.rb +1 -1
- data/lib/octopus/rails2/scope.rb +17 -0
- data/lib/octopus/rails3.1/singular_association.rb +34 -0
- data/lib/octopus/rails3.2/persistence.rb +12 -0
- data/lib/octopus/rails3/abstract_adapter.rb +39 -0
- data/lib/octopus/rails3/arel.rb +5 -5
- data/lib/octopus/rails3/log_subscriber.rb +22 -0
- data/lib/octopus/rails3/persistence.rb +10 -5
- data/lib/octopus/railtie.rb +13 -0
- data/lib/octopus/scope_proxy.rb +22 -16
- data/lib/octopus/version.rb +3 -0
- data/lib/tasks/octopus.rake +20 -0
- data/sample_app/Gemfile +2 -2
- data/sample_app/config/initializers/inflections.rb +1 -1
- data/sample_app/config/initializers/secret_token.rb +1 -1
- data/sample_app/db/migrate/20100720172730_create_items.rb +1 -1
- data/sample_app/db/migrate/20100720210335_create_sample_users.rb +1 -1
- data/sample_app/db/seeds.rb +1 -1
- data/sample_app/features/migrate.feature +12 -12
- data/sample_app/features/seed.feature +3 -3
- data/sample_app/features/step_definitions/web_steps.rb +5 -5
- data/sample_app/features/support/env.rb +8 -8
- data/sample_app/lib/tasks/cucumber.rake +2 -2
- data/sample_app/public/javascripts/effects.js +1 -1
- data/spec/config/shards.yml +38 -28
- data/spec/migrations/11_add_field_in_all_slaves.rb +1 -1
- data/spec/migrations/12_create_users_using_block.rb +2 -2
- data/spec/migrations/13_create_users_using_block_and_using.rb +2 -2
- data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +11 -0
- data/spec/migrations/1_create_users_on_master.rb +1 -1
- data/spec/migrations/2_create_users_on_canada.rb +1 -1
- data/spec/migrations/3_create_users_on_both_shards.rb +1 -1
- data/spec/migrations/4_create_users_on_shards_of_a_group.rb +1 -1
- data/spec/migrations/5_create_users_on_multiples_groups.rb +1 -1
- data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +1 -1
- data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +1 -1
- data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +1 -1
- data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +1 -1
- data/spec/octopus/association_spec.rb +88 -70
- data/spec/octopus/log_subscriber_spec.rb +22 -0
- data/spec/octopus/logger_spec.rb +28 -15
- data/spec/octopus/migration_spec.rb +47 -43
- data/spec/octopus/model_spec.rb +179 -13
- data/spec/octopus/octopus_spec.rb +26 -4
- data/spec/octopus/proxy_spec.rb +61 -23
- data/spec/octopus/{replication_specs.rb → replication_spec.rb} +33 -26
- data/spec/octopus/scope_proxy_spec.rb +3 -3
- data/spec/octopus/sharded_spec.rb +9 -9
- data/spec/spec_helper.rb +10 -12
- data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +17 -0
- data/spec/support/database_connection.rb +2 -0
- data/spec/{database_models.rb → support/database_models.rb} +27 -2
- data/spec/support/octopus_helper.rb +50 -0
- data/spec/tasks/octopus.rake_spec.rb +36 -0
- metadata +188 -169
- data/Gemfile.lock +0 -68
- data/lib/octopus/rails3/association.rb +0 -112
- data/spec/database_connection.rb +0 -4
- data/spec/octopus/controller_spec.rb +0 -34
- data/spec/octopus_helper.rb +0 -37
@@ -0,0 +1,22 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
if Octopus.rails3?
|
4
|
+
describe Octopus::LogSubscriber, :shards => [:canada] do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@out = StringIO.new
|
8
|
+
@log = Logger.new(@out)
|
9
|
+
ActiveRecord::Base.logger = @log
|
10
|
+
ActiveRecord::Base.logger.level = Logger::DEBUG
|
11
|
+
end
|
12
|
+
|
13
|
+
after :each do
|
14
|
+
ActiveRecord::Base.logger = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should add to the default logger the shard name the query was sent to" do
|
18
|
+
User.using(:canada).create!(:name => "test")
|
19
|
+
@out.string.should =~ /Shard: canada/
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/spec/octopus/logger_spec.rb
CHANGED
@@ -1,19 +1,32 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
|
-
describe Octopus::Logger do
|
3
|
+
describe Octopus::Logger, :shards => [:canada] do
|
4
|
+
before :each do
|
5
|
+
@out = StringIO.new
|
6
|
+
@log = Octopus::Logger.new(@out)
|
7
|
+
ActiveRecord::Base.logger = @log
|
8
|
+
end
|
9
|
+
|
10
|
+
after :each do
|
11
|
+
ActiveRecord::Base.logger = nil
|
12
|
+
end
|
4
13
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
after :each do
|
12
|
-
ActiveRecord::Base.logger = nil
|
14
|
+
if Octopus.rails3?
|
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
|
13
19
|
end
|
14
20
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
21
|
+
it "should be deprecated" do
|
22
|
+
@last_message = nil
|
23
|
+
ActiveSupport::Deprecation.behavior = Proc.new { |message| @last_message = message }
|
24
|
+
@log = Octopus::Logger.new(@out)
|
25
|
+
|
26
|
+
if @last_message.is_a?(Array)
|
27
|
+
@last_message.first.should =~ /DEPRECATION WARNING: Octopus::Logger is deprecated and will be removed in Octopus 0\.6\.x\./
|
28
|
+
else
|
29
|
+
@last_message.should =~ /DEPRECATION WARNING: Octopus::Logger is deprecated and will be removed in Octopus 0\.6\.x\./
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,91 +1,73 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
|
-
describe Octopus::Migration do
|
3
|
+
describe Octopus::Migration do
|
4
4
|
it "should run just in the master shard" do
|
5
|
-
migrating_to_version 1 do
|
6
|
-
User.using(:master).find_by_name("Master").should_not be_nil
|
5
|
+
OctopusHelper.migrating_to_version 1 do
|
6
|
+
User.using(:master).find_by_name("Master").should_not be_nil
|
7
7
|
User.using(:canada).find_by_name("Master").should be_nil
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should run on specific shard" do
|
12
|
-
migrating_to_version 2 do
|
13
|
-
User.using(:master).find_by_name("Sharding").should be_nil
|
12
|
+
OctopusHelper.migrating_to_version 2 do
|
13
|
+
User.using(:master).find_by_name("Sharding").should be_nil
|
14
14
|
User.using(:canada).find_by_name("Sharding").should_not be_nil
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should run on specifieds shards" do
|
19
|
-
migrating_to_version 3 do
|
20
|
-
User.using(:brazil).find_by_name("Both").should_not be_nil
|
19
|
+
OctopusHelper.migrating_to_version 3 do
|
20
|
+
User.using(:brazil).find_by_name("Both").should_not be_nil
|
21
21
|
User.using(:canada).find_by_name("Both").should_not be_nil
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should run on specified group" do
|
26
|
-
migrating_to_version 4 do
|
26
|
+
OctopusHelper.migrating_to_version 4 do
|
27
27
|
User.using(:canada).find_by_name("Group").should_not be_nil
|
28
|
-
User.using(:brazil).find_by_name("Group").should_not be_nil
|
28
|
+
User.using(:brazil).find_by_name("Group").should_not be_nil
|
29
29
|
User.using(:russia).find_by_name("Group").should_not be_nil
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
it "should run
|
34
|
-
migrating_to_version 5 do
|
35
|
-
User.using(:canada).find(:all, {:conditions => {:name => "MultipleGroup"}}).size.should ==
|
36
|
-
User.using(:brazil).find(:all, {:conditions => {:name => "MultipleGroup"}}).size.should ==
|
37
|
-
User.using(:russia).find(:all, {:conditions => {:name => "MultipleGroup"}}).size.should ==
|
33
|
+
it "should run once per shard" do
|
34
|
+
OctopusHelper.migrating_to_version 5 do
|
35
|
+
User.using(:canada).find(:all, {:conditions => {:name => "MultipleGroup"}}).size.should == 1
|
36
|
+
User.using(:brazil).find(:all, {:conditions => {:name => "MultipleGroup"}}).size.should == 1
|
37
|
+
User.using(:russia).find(:all, {:conditions => {:name => "MultipleGroup"}}).size.should == 1
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should create users inside block" do
|
42
|
-
migrating_to_version 12 do
|
42
|
+
OctopusHelper.migrating_to_version 12 do
|
43
43
|
User.using(:brazil).find(:all, :conditions => {:name => "UsingBlock1"}).size.should == 1
|
44
44
|
User.using(:brazil).find(:all, :conditions => {:name => "UsingBlock2"}).size.should == 1
|
45
45
|
User.using(:canada).find(:all, :conditions => {:name => "UsingCanada"}).size.should == 1
|
46
46
|
User.using(:canada).find(:all, :conditions => {:name => "UsingCanada2"}).size.should == 1
|
47
47
|
end
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
it "should send the query to the correct shard" do
|
51
|
-
migrating_to_version 13 do
|
51
|
+
OctopusHelper.migrating_to_version 13 do
|
52
52
|
User.using(:brazil).find(:all, :conditions => {:name => "Brazil"}).size.should == 1
|
53
53
|
User.using(:brazil).find(:all, :conditions => {:name => "Canada"}).size.should == 0
|
54
54
|
User.using(:canada).find(:all, :conditions => {:name => "Brazil"}).size.should == 0
|
55
55
|
User.using(:canada).find(:all, :conditions => {:name => "Canada"}).size.should == 1
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe "should raise a exception when" do
|
60
|
-
it "you specify a invalid shard name" do
|
61
|
-
lambda { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT, 6) }.should raise_error("Nonexistent Shard Name: amazing_shard")
|
62
|
-
end
|
63
|
-
|
64
|
-
it "you specify a invalid shard name, even if you have multiple shards, and one of them are right" do
|
65
|
-
lambda { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT, 7) }.should raise_error("Nonexistent Shard Name: invalid_shard")
|
66
56
|
end
|
57
|
+
end
|
67
58
|
|
68
|
-
|
69
|
-
lambda { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT, 8) }.should raise_error("Nonexistent Group Name: invalid_group")
|
70
|
-
end
|
71
|
-
|
72
|
-
it "you specify a invalid group name, even if you have multiple groups, and one of them are right" do
|
73
|
-
lambda { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT, 9) }.should raise_error("Nonexistent Group Name: invalid_group")
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
describe "when using replication" do
|
59
|
+
describe "when using replication" do
|
78
60
|
it "should run writes on master when you use replication" do
|
79
|
-
using_environment :production_replicated do
|
80
|
-
migrating_to_version 10 do
|
61
|
+
OctopusHelper.using_environment :production_replicated do
|
62
|
+
OctopusHelper.migrating_to_version 10 do
|
81
63
|
Cat.find_by_name("Replication").should be_nil
|
82
64
|
end
|
83
65
|
end
|
84
66
|
end
|
85
67
|
|
86
68
|
it "should run in all shards, master or another shards" do
|
87
|
-
using_environment :production_replicated do
|
88
|
-
migrating_to_version 11 do
|
69
|
+
OctopusHelper.using_environment :production_replicated do
|
70
|
+
OctopusHelper.migrating_to_version 11 do
|
89
71
|
[:slave4, :slave1, :slave2, :slave3].each do |sym|
|
90
72
|
Cat.find_by_name("Slaves").should_not be_nil
|
91
73
|
end
|
@@ -93,5 +75,27 @@ describe Octopus::Migration do
|
|
93
75
|
end
|
94
76
|
end
|
95
77
|
end
|
96
|
-
end
|
97
78
|
|
79
|
+
it "should store the migration versions in each shard" do
|
80
|
+
class SchemaMigration < ActiveRecord::Base; end
|
81
|
+
|
82
|
+
OctopusHelper.migrating_to_version 14 do
|
83
|
+
Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }.should include(14)
|
84
|
+
Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }.should include(14)
|
85
|
+
Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }.should include(14)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should run the migrations on shards that are missing them" do
|
90
|
+
class SchemaMigration < ActiveRecord::Base; end
|
91
|
+
|
92
|
+
Octopus.using(:master) { SchemaMigration.create(:version => 14) }
|
93
|
+
Octopus.using(:canada) { SchemaMigration.create(:version => 14) }
|
94
|
+
|
95
|
+
OctopusHelper.migrating_to_version 14 do
|
96
|
+
Octopus.using(:canada) { ActiveRecord::Migrator.get_all_versions }.should include(14)
|
97
|
+
Octopus.using(:brazil) { ActiveRecord::Migrator.get_all_versions }.should include(14)
|
98
|
+
Octopus.using(:russia) { ActiveRecord::Migrator.get_all_versions }.should include(14)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/spec/octopus/model_spec.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe Octopus::Model do
|
4
4
|
describe "#using method" do
|
5
5
|
it "should return self after calling the #using method" do
|
6
|
-
User.using(:canada).should
|
6
|
+
User.using(:canada).should be_a(Octopus::ScopeProxy)
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should allow to send a block to the master shard" do
|
@@ -53,6 +53,21 @@ describe Octopus::Model do
|
|
53
53
|
User.all.should == [u1]
|
54
54
|
end
|
55
55
|
|
56
|
+
describe "multiple calls to the same scope" do
|
57
|
+
it "works with nil response" do
|
58
|
+
scope = User.using(:canada)
|
59
|
+
scope.count.should == 0
|
60
|
+
scope.first.should be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it "works with non-nil response" do
|
64
|
+
user = User.using(:canada).create!(:name => 'oi')
|
65
|
+
scope = User.using(:canada)
|
66
|
+
scope.count.should == 1
|
67
|
+
scope.first.should == user
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
56
71
|
it "should select the correct shard" do
|
57
72
|
User.using(:canada)
|
58
73
|
User.create!(:name => 'oi')
|
@@ -85,8 +100,8 @@ describe Octopus::Model do
|
|
85
100
|
end
|
86
101
|
|
87
102
|
it "should clean #current_shard from proxy when using execute" do
|
88
|
-
|
89
|
-
|
103
|
+
User.using(:canada).connection().execute("select * from users limit 1;")
|
104
|
+
User.connection.current_shard.should == :master
|
90
105
|
end
|
91
106
|
|
92
107
|
it "should allow scoping dynamically" do
|
@@ -118,6 +133,17 @@ describe Octopus::Model do
|
|
118
133
|
User.count.should == 0
|
119
134
|
end
|
120
135
|
|
136
|
+
it "should work with named scopes" do
|
137
|
+
u = User.using(:brazil).create!(:name => "Thiago")
|
138
|
+
|
139
|
+
User.thiago.using(:brazil).first.should eq(u)
|
140
|
+
User.using(:brazil).thiago.first.should eq(u)
|
141
|
+
|
142
|
+
Octopus.using(:brazil) do
|
143
|
+
User.thiago.first.should eq(u)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
121
147
|
describe "#current_shard attribute" do
|
122
148
|
it "should store the attribute when you create or find an object" do
|
123
149
|
u = User.using(:alone_shard).create!(:name => "Alone")
|
@@ -183,13 +209,54 @@ describe Octopus::Model do
|
|
183
209
|
lambda { User.using(:crazy_shard).create!(:name => 'Thiago') }.should raise_error("Nonexistent Shard Name: crazy_shard")
|
184
210
|
end
|
185
211
|
end
|
212
|
+
|
213
|
+
describe "equality" do
|
214
|
+
let(:canada1) do
|
215
|
+
u = User.new
|
216
|
+
u.id = 1
|
217
|
+
u.current_shard = :canada
|
218
|
+
u
|
219
|
+
end
|
220
|
+
|
221
|
+
let(:canada1_dup) do
|
222
|
+
u = User.new
|
223
|
+
u.id = 1
|
224
|
+
u.current_shard = :canada
|
225
|
+
u
|
226
|
+
end
|
227
|
+
|
228
|
+
let(:brazil1) do
|
229
|
+
u = User.new
|
230
|
+
u.id = 1
|
231
|
+
u.current_shard = :brazil
|
232
|
+
u
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should work with persisted objects" do
|
236
|
+
u = User.using(:brazil).create(:name => "Mike")
|
237
|
+
User.using(:brazil).find_by_name("Mike").should == u
|
238
|
+
end
|
239
|
+
|
240
|
+
if Octopus.rails31? || Octopus.rails32?
|
241
|
+
# Rails <= 3.0 doesn't support equality checks on non-persisted objects
|
242
|
+
it "should check current_shard when determining equality" do
|
243
|
+
canada1.should_not == brazil1
|
244
|
+
canada1.should == canada1_dup
|
245
|
+
end
|
246
|
+
|
247
|
+
it "delegates equality check on scopes" do
|
248
|
+
u = User.using(:brazil).create!(:name => "Mike")
|
249
|
+
User.using(:brazil).where(:name => "Mike").should == [u]
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
186
253
|
end
|
187
254
|
|
188
255
|
describe "using a postgresql shard" do
|
189
256
|
it "should update the Arel Engine" do
|
190
257
|
if ActiveRecord::VERSION::STRING > '2.4.0'
|
191
258
|
User.using(:postgresql_shard).arel_engine.connection.adapter_name.should == "PostgreSQL"
|
192
|
-
User.using(:alone_shard).arel_engine.connection.adapter_name.should == "
|
259
|
+
User.using(:alone_shard).arel_engine.connection.adapter_name.should == "MySQL"
|
193
260
|
end
|
194
261
|
end
|
195
262
|
|
@@ -202,8 +269,13 @@ describe Octopus::Model do
|
|
202
269
|
end
|
203
270
|
|
204
271
|
describe "AR basic methods" do
|
205
|
-
it "
|
206
|
-
CustomConnection.connection.current_database.should == "
|
272
|
+
it "establish_connection" do
|
273
|
+
CustomConnection.connection.current_database.should == "octopus_shard_2"
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should not mess with custom connection table names" do
|
277
|
+
Advert.connection.current_database.should == "octopus_shard_1"
|
278
|
+
Advert.create!(:name => "Teste")
|
207
279
|
end
|
208
280
|
|
209
281
|
it "increment" do
|
@@ -221,6 +293,21 @@ describe Octopus::Model do
|
|
221
293
|
u = User.using(:brazil).find_by_number(11).should_not be_nil
|
222
294
|
end
|
223
295
|
|
296
|
+
it "decrement" do
|
297
|
+
u = User.using(:brazil).create!(:name => "Teste", :number => 10)
|
298
|
+
u = User.using(:brazil).find_by_number(10)
|
299
|
+
u.decrement(:number)
|
300
|
+
u.save()
|
301
|
+
u = User.using(:brazil).find_by_number(9).should_not be_nil
|
302
|
+
end
|
303
|
+
|
304
|
+
it "decrement!" do
|
305
|
+
u = User.using(:brazil).create!(:name => "Teste", :number => 10)
|
306
|
+
u = User.using(:brazil).find_by_number(10)
|
307
|
+
u.decrement!(:number)
|
308
|
+
u = User.using(:brazil).find_by_number(9).should_not be_nil
|
309
|
+
end
|
310
|
+
|
224
311
|
it "toggle" do
|
225
312
|
u = User.using(:brazil).create!(:name => "Teste", :admin => false)
|
226
313
|
u = User.using(:brazil).find_by_name('Teste')
|
@@ -243,6 +330,73 @@ describe Octopus::Model do
|
|
243
330
|
User.using(:brazil).find(:all, :conditions => {:name => "User2"}).count.should == 1
|
244
331
|
end
|
245
332
|
|
333
|
+
it "maximum" do
|
334
|
+
u1 = User.using(:brazil).create!(:name => "Teste", :number => 11)
|
335
|
+
u2 = User.using(:master).create!(:name => "Teste", :number => 12)
|
336
|
+
|
337
|
+
User.using(:brazil).maximum(:number).should == 11
|
338
|
+
User.using(:master).maximum(:number).should == 12
|
339
|
+
end
|
340
|
+
|
341
|
+
if Octopus.rails3?
|
342
|
+
describe "any?" do
|
343
|
+
before { User.using(:brazil).create!(:name => "User1") }
|
344
|
+
|
345
|
+
it "works when true" do
|
346
|
+
scope = User.using(:brazil).where(:name => "User1")
|
347
|
+
scope.any?.should be_true
|
348
|
+
end
|
349
|
+
|
350
|
+
it "works when false" do
|
351
|
+
scope = User.using(:brazil).where(:name => "User2")
|
352
|
+
scope.any?.should be_false
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
it "exists?" do
|
357
|
+
@user = User.using(:brazil).create!(:name => "User1")
|
358
|
+
|
359
|
+
User.using(:brazil).where(:name => "User1").exists?.should be_true
|
360
|
+
User.using(:brazil).where(:name => "User2").exists?.should be_false
|
361
|
+
end
|
362
|
+
|
363
|
+
describe "touch" do
|
364
|
+
it "updates updated_at by default" do
|
365
|
+
@user = User.using(:brazil).create!(:name => "User1")
|
366
|
+
User.using(:brazil).update_all({:updated_at => Time.now - 3.months}, {:id => @user.id})
|
367
|
+
@user.touch
|
368
|
+
@user.reload.updated_at.to_date.should eq(Date.today)
|
369
|
+
end
|
370
|
+
|
371
|
+
it "updates passed in attribute name" do
|
372
|
+
@user = User.using(:brazil).create!(:name => "User1")
|
373
|
+
User.using(:brazil).update_all({:created_at => Time.now - 3.months}, {:id => @user.id})
|
374
|
+
@user.touch(:created_at)
|
375
|
+
@user.reload.created_at.to_date.should eq(Date.today)
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
if Octopus.rails32?
|
382
|
+
describe "#pluck" do
|
383
|
+
before { User.using(:brazil).create!(:name => "User1") }
|
384
|
+
|
385
|
+
it "should works from scope proxy" do
|
386
|
+
names = User.using(:brazil).pluck(:name)
|
387
|
+
names.should eq(["User1"])
|
388
|
+
User.using(:master).pluck(:name).should eq([])
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
it "update_column" do
|
393
|
+
@user = User.using(:brazil).create!(:name => "User1")
|
394
|
+
@user2 = User.using(:brazil).find(@user.id)
|
395
|
+
@user2.update_column(:name, "Joaquim Shard Brazil")
|
396
|
+
User.using(:brazil).find_by_name("Joaquim Shard Brazil").should_not be_nil
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
246
400
|
it "update_attributes" do
|
247
401
|
@user = User.using(:brazil).create!(:name => "User1")
|
248
402
|
@user2 = User.using(:brazil).find(@user.id)
|
@@ -325,13 +479,25 @@ describe Octopus::Model do
|
|
325
479
|
it 'should work correctly' do
|
326
480
|
Bacon.using(:brazil).create!(:name => "YUMMMYYYY")
|
327
481
|
end
|
482
|
+
|
483
|
+
it 'should work correctly with a block' do
|
484
|
+
Cheese.using(:brazil).create!(:name => "YUMMMYYYY")
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
if Octopus.rails32?
|
489
|
+
describe "when using table_name=" do
|
490
|
+
it 'should work correctly' do
|
491
|
+
Ham.using(:brazil).create!(:name => "YUMMMYYYY")
|
492
|
+
end
|
493
|
+
end
|
328
494
|
end
|
329
495
|
|
330
496
|
describe "when using a environment with a single adapter" do
|
331
497
|
it 'should not clean the table name' do
|
332
|
-
using_environment :production_fully_replicated do
|
498
|
+
OctopusHelper.using_environment :production_fully_replicated do
|
333
499
|
Keyboard.should_not_receive(:reset_table_name)
|
334
|
-
Keyboard.using(:master).create!("Master Cat")
|
500
|
+
Keyboard.using(:master).create!(:name => "Master Cat")
|
335
501
|
end
|
336
502
|
end
|
337
503
|
end
|
@@ -407,15 +573,15 @@ describe Octopus::Model do
|
|
407
573
|
|
408
574
|
describe "#replicated_model method" do
|
409
575
|
it "should be replicated" do
|
410
|
-
using_environment :production_replicated do
|
576
|
+
OctopusHelper.using_environment :production_replicated do
|
411
577
|
ActiveRecord::Base.connection_proxy.instance_variable_get(:@replicated).should be_true
|
412
578
|
end
|
413
579
|
end
|
414
580
|
|
415
581
|
it "should mark the Cat model as replicated" do
|
416
|
-
using_environment :production_replicated do
|
417
|
-
User.
|
418
|
-
Cat.
|
582
|
+
OctopusHelper.using_environment :production_replicated do
|
583
|
+
User.replicated.should be_false
|
584
|
+
Cat.replicated.should be_true
|
419
585
|
end
|
420
586
|
end
|
421
587
|
end
|