ar-octopus 0.8.2 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +9 -9
- data/.rspec +1 -1
- data/.rubocop.yml +46 -0
- data/.rubocop_todo.yml +52 -0
- data/.ruby-version +1 -1
- data/.travis.yml +3 -9
- data/Appraisals +4 -0
- data/Rakefile +17 -16
- data/ar-octopus.gemspec +22 -16
- data/gemfiles/rails41.gemfile +7 -0
- data/init.rb +1 -1
- data/lib/ar-octopus.rb +1 -1
- data/lib/octopus.rb +38 -37
- data/lib/octopus/abstract_adapter.rb +0 -2
- data/lib/octopus/association.rb +8 -6
- data/lib/octopus/association_shard_tracking.rb +80 -81
- data/lib/octopus/collection_association.rb +7 -5
- data/lib/octopus/collection_proxy.rb +11 -9
- data/lib/octopus/has_and_belongs_to_many_association.rb +5 -3
- data/lib/octopus/load_balancing.rb +3 -2
- data/lib/octopus/load_balancing/round_robin.rb +12 -8
- data/lib/octopus/migration.rb +117 -108
- data/lib/octopus/model.rb +130 -134
- data/lib/octopus/persistence.rb +1 -1
- data/lib/octopus/proxy.rb +345 -339
- data/lib/octopus/railtie.rb +2 -2
- data/lib/octopus/relation_proxy.rb +6 -1
- data/lib/octopus/scope_proxy.rb +38 -36
- data/lib/octopus/shard_tracking.rb +36 -35
- data/lib/octopus/shard_tracking/attribute.rb +12 -14
- data/lib/octopus/shard_tracking/dynamic.rb +7 -3
- data/lib/octopus/singular_association.rb +5 -3
- data/lib/octopus/slave_group.rb +10 -8
- data/lib/octopus/version.rb +1 -1
- data/rails/init.rb +1 -1
- data/sample_app/autotest/discover.rb +2 -2
- data/sample_app/config/application.rb +1 -1
- data/sample_app/config/boot.rb +1 -1
- data/sample_app/config/environments/test.rb +1 -1
- data/sample_app/config/initializers/session_store.rb +1 -1
- data/sample_app/config/initializers/wrap_parameters.rb +1 -1
- data/sample_app/config/routes.rb +1 -1
- data/sample_app/db/migrate/20100720210335_create_sample_users.rb +2 -2
- data/sample_app/db/schema.rb +10 -10
- data/sample_app/db/seeds.rb +3 -3
- data/sample_app/features/step_definitions/seeds_steps.rb +4 -4
- data/sample_app/features/step_definitions/web_steps.rb +3 -4
- data/sample_app/features/support/env.rb +3 -4
- data/sample_app/features/support/paths.rb +4 -4
- data/sample_app/spec/spec_helper.rb +3 -3
- data/spec/migrations/10_create_users_using_replication.rb +3 -3
- data/spec/migrations/11_add_field_in_all_slaves.rb +3 -3
- data/spec/migrations/12_create_users_using_block.rb +7 -7
- data/spec/migrations/13_create_users_using_block_and_using.rb +4 -4
- data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +2 -2
- data/spec/migrations/15_create_user_on_shards_of_default_group_with_versions.rb +2 -2
- data/spec/migrations/1_create_users_on_master.rb +3 -3
- data/spec/migrations/2_create_users_on_canada.rb +3 -3
- data/spec/migrations/3_create_users_on_both_shards.rb +3 -3
- data/spec/migrations/4_create_users_on_shards_of_a_group.rb +3 -3
- data/spec/migrations/5_create_users_on_multiples_groups.rb +2 -2
- data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +3 -3
- data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +3 -3
- data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +3 -3
- data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +4 -4
- data/spec/octopus/association_shard_tracking_spec.rb +413 -417
- data/spec/octopus/collection_proxy_spec.rb +6 -5
- data/spec/octopus/log_subscriber_spec.rb +4 -4
- data/spec/octopus/migration_spec.rb +48 -48
- data/spec/octopus/model_spec.rb +267 -292
- data/spec/octopus/octopus_spec.rb +40 -41
- data/spec/octopus/proxy_spec.rb +124 -124
- data/spec/octopus/relation_proxy_spec.rb +32 -32
- data/spec/octopus/replicated_slave_grouped_spec.rb +23 -23
- data/spec/octopus/replication_spec.rb +61 -66
- data/spec/octopus/scope_proxy_spec.rb +56 -10
- data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +29 -29
- data/spec/octopus/sharded_spec.rb +10 -10
- data/spec/spec_helper.rb +6 -6
- data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +1 -3
- data/spec/support/database_connection.rb +2 -2
- data/spec/support/database_models.rb +16 -17
- data/spec/support/octopus_helper.rb +19 -21
- data/spec/support/query_count.rb +1 -3
- data/spec/support/shared_contexts.rb +3 -3
- data/spec/tasks/octopus.rake_spec.rb +10 -10
- metadata +43 -26
@@ -1,75 +1,75 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octopus::RelationProxy do
|
4
|
-
describe
|
4
|
+
describe 'shard tracking' do
|
5
5
|
before :each do
|
6
6
|
@client = Client.using(:canada).create!
|
7
7
|
@client.items << Item.using(:canada).create!
|
8
8
|
@relation = @client.items
|
9
9
|
end
|
10
10
|
|
11
|
-
it
|
12
|
-
@relation.current_shard.
|
11
|
+
it 'remembers the shard on which a relation was created' do
|
12
|
+
expect(@relation.current_shard).to eq(:canada)
|
13
13
|
end
|
14
14
|
|
15
|
-
context
|
15
|
+
context 'when comparing to other Relation objects' do
|
16
16
|
before :each do
|
17
17
|
@relation.reset
|
18
18
|
end
|
19
19
|
|
20
|
-
it
|
21
|
-
@relation.
|
20
|
+
it 'is equal to its clone' do
|
21
|
+
expect(@relation).to eq(@relation.clone)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
if Octopus.rails4?
|
26
|
-
context
|
27
|
-
it
|
28
|
-
@relation.class.
|
26
|
+
context 'under Rails 4' do
|
27
|
+
it 'is an Octopus::RelationProxy' do
|
28
|
+
expect(@relation.class).to eq(Octopus::RelationProxy)
|
29
29
|
end
|
30
30
|
|
31
|
-
it
|
32
|
-
@relation.ar_relation.is_a?(ActiveRecord::Relation).
|
31
|
+
it 'should be able to return its ActiveRecord::Relation' do
|
32
|
+
expect(@relation.ar_relation.is_a?(ActiveRecord::Relation)).to be true
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it 'is equal to an identically-defined, but different, RelationProxy' do
|
36
36
|
i = @client.items
|
37
|
-
@relation.
|
38
|
-
@relation.object_id.
|
37
|
+
expect(@relation).to eq(i)
|
38
|
+
expect(@relation.object_id).not_to eq(i.object_id)
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
42
|
-
@relation.
|
43
|
-
@relation.ar_relation.
|
41
|
+
it 'is equal to its own underlying ActiveRecord::Relation' do
|
42
|
+
expect(@relation).to eq(@relation.ar_relation)
|
43
|
+
expect(@relation.ar_relation).to eq(@relation)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
context
|
49
|
-
it
|
50
|
-
@relation.count.
|
48
|
+
context 'when no explicit shard context is provided' do
|
49
|
+
it 'uses the correct shard' do
|
50
|
+
expect(@relation.count).to eq(1)
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
53
|
+
it 'lazily evaluates on the correct shard' do
|
54
54
|
# Do something to force Client.connection_proxy.current_shard to change
|
55
|
-
|
56
|
-
@relation.select(:client_id).count.
|
55
|
+
_some_count = Client.using(:brazil).count
|
56
|
+
expect(@relation.select(:client_id).count).to eq(1)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
context
|
61
|
-
it
|
62
|
-
Item.using(:brazil).count.
|
63
|
-
|
60
|
+
context 'when an explicit, but different, shard context is provided' do
|
61
|
+
it 'uses the correct shard' do
|
62
|
+
expect(Item.using(:brazil).count).to eq(0)
|
63
|
+
_clients_on_brazil = Client.using(:brazil).all
|
64
64
|
Client.using(:brazil) do
|
65
|
-
@relation.count.
|
65
|
+
expect(@relation.count).to eq(1)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
it
|
70
|
-
Item.using(:brazil).count.
|
69
|
+
it 'lazily evaluates on the correct shard' do
|
70
|
+
expect(Item.using(:brazil).count).to eq(0)
|
71
71
|
Client.using(:brazil) do
|
72
|
-
@relation.select(:client_id).count.
|
72
|
+
expect(@relation.select(:client_id).count).to eq(1)
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -1,14 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'when the database is replicated and has slave groups' do
|
4
4
|
|
5
|
-
it
|
5
|
+
it 'should pick the slave group based on current_slave_grup when you have a replicated model' do
|
6
6
|
|
7
7
|
OctopusHelper.using_environment :replicated_slave_grouped do
|
8
8
|
# The following two calls of `create!` both creates cats in :master(The database `octopus_shard_1`)
|
9
9
|
# which is configured through RAILS_ENV and database.yml
|
10
|
-
Cat.create!(:name =>
|
11
|
-
Cat.create!(:name =>
|
10
|
+
Cat.create!(:name => 'Thiago1')
|
11
|
+
Cat.create!(:name => 'Thiago2')
|
12
12
|
|
13
13
|
# See "replicated_slave_grouped" defined in shards.yml
|
14
14
|
# We have:
|
@@ -18,47 +18,47 @@ describe "when the database is replicated and has slave groups" do
|
|
18
18
|
# When a select-count query is sent to `octopus_shard_2`, it should return 0.
|
19
19
|
|
20
20
|
# The query goes to `octopus_shard_1`
|
21
|
-
Cat.using(:master).count.
|
21
|
+
expect(Cat.using(:master).count).to eq(2)
|
22
22
|
# The query goes to `octopus_shard_1`
|
23
|
-
Cat.count.
|
23
|
+
expect(Cat.count).to eq(2)
|
24
24
|
# The query goes to `octopus_shard_2`
|
25
|
-
Cat.using(slave_group
|
25
|
+
expect(Cat.using(:slave_group => :slaves1).count).to eq(0)
|
26
26
|
# The query goes to `octopus_shard_1`
|
27
|
-
Cat.using(slave_group
|
27
|
+
expect(Cat.using(:slave_group => :slaves2).count).to eq(2)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
it
|
31
|
+
it 'should distribute queries between slaves in a slave group in round-robin' do
|
32
32
|
OctopusHelper.using_environment :replicated_slave_grouped do
|
33
33
|
# The query goes to :master(`octopus_shard_1`)
|
34
|
-
Cat.create!(:name =>
|
34
|
+
Cat.create!(:name => 'Thiago1')
|
35
35
|
# The query goes to :master(`octopus_shard_1`)
|
36
|
-
Cat.create!(:name =>
|
36
|
+
Cat.create!(:name => 'Thiago2')
|
37
37
|
|
38
38
|
# The query goes to :slave32(`octopus_shard_2`)
|
39
|
-
Cat.using(slave_group
|
39
|
+
expect(Cat.using(:slave_group => :slaves3).count).to eq(0)
|
40
40
|
# The query goes to :slave31(`octopus_shard_1`)
|
41
|
-
Cat.using(slave_group
|
41
|
+
expect(Cat.using(:slave_group => :slaves3).count).to eq(2)
|
42
42
|
# The query goes to :slave32(`octopus_shard_2`)
|
43
|
-
Cat.using(slave_group
|
43
|
+
expect(Cat.using(:slave_group => :slaves3).count).to eq(0)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
it
|
47
|
+
it 'should make queries to master when slave groups are configured but not selected' do
|
48
48
|
OctopusHelper.using_environment :replicated_slave_grouped do
|
49
49
|
# All the queries go to :master(`octopus_shard_1`)
|
50
50
|
|
51
|
-
Cat.create!(:name =>
|
52
|
-
Cat.create!(:name =>
|
51
|
+
Cat.create!(:name => 'Thiago1')
|
52
|
+
Cat.create!(:name => 'Thiago2')
|
53
53
|
|
54
54
|
# In `database.yml` and `shards.yml`, we have configured 1 master and 4 slaves.
|
55
55
|
# So we can ensure Octopus is not distributing queries between them
|
56
56
|
# by asserting 1 + 4 = 5 queries go to :master(`octopus_shard_1`)
|
57
|
-
Cat.count.
|
58
|
-
Cat.count.
|
59
|
-
Cat.count.
|
60
|
-
Cat.count.
|
61
|
-
Cat.count.
|
57
|
+
expect(Cat.count).to eq(2)
|
58
|
+
expect(Cat.count).to eq(2)
|
59
|
+
expect(Cat.count).to eq(2)
|
60
|
+
expect(Cat.count).to eq(2)
|
61
|
+
expect(Cat.count).to eq(2)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|
@@ -1,69 +1,69 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
4
|
-
it
|
3
|
+
describe 'when the database is replicated' do
|
4
|
+
it 'should send all writes/reads queries to master when you have a non replicated model' do
|
5
5
|
OctopusHelper.using_environment :production_replicated do
|
6
|
-
u = User.create!(:name =>
|
7
|
-
User.count.
|
8
|
-
User.find(u.id).
|
6
|
+
u = User.create!(:name => 'Replicated')
|
7
|
+
expect(User.count).to eq(1)
|
8
|
+
expect(User.find(u.id)).to eq(u)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
it
|
12
|
+
it 'should send all writes queries to master' do
|
13
13
|
OctopusHelper.using_environment :production_replicated do
|
14
|
-
Cat.create!(:name =>
|
15
|
-
Cat.find_by_name(
|
16
|
-
Client.create!(:name =>
|
17
|
-
Client.find_by_name(
|
14
|
+
Cat.create!(:name => 'Slave Cat')
|
15
|
+
expect(Cat.find_by_name('Slave Cat')).to be_nil
|
16
|
+
Client.create!(:name => 'Slave Client')
|
17
|
+
expect(Client.find_by_name('Slave Client')).not_to be_nil
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it 'should allow to create multiple models on the master' do
|
22
22
|
OctopusHelper.using_environment :production_replicated do
|
23
|
-
Cat.create!([{:name =>
|
24
|
-
Cat.find_by_name(
|
25
|
-
Cat.find_by_name(
|
23
|
+
Cat.create!([{ :name => 'Slave Cat 1' }, { :name => 'Slave Cat 2' }])
|
24
|
+
expect(Cat.find_by_name('Slave Cat 1')).to be_nil
|
25
|
+
expect(Cat.find_by_name('Slave Cat 2')).to be_nil
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
describe
|
30
|
-
include_context
|
29
|
+
describe 'When enabling the query cache' do
|
30
|
+
include_context 'with query cache enabled'
|
31
31
|
|
32
|
-
it
|
32
|
+
it 'should do the queries with cache' do
|
33
33
|
OctopusHelper.using_environment :replicated_with_one_slave do
|
34
|
-
cat1 = Cat.using(:master).create!(:name =>
|
35
|
-
|
36
|
-
Cat.using(:master).find(cat1.id).
|
37
|
-
Cat.using(:master).find(cat1.id).
|
38
|
-
Cat.using(:master).find(cat1.id).
|
39
|
-
|
40
|
-
cat3 = Cat.using(:slave1).create!(:name =>
|
41
|
-
|
42
|
-
Cat.find(cat3.id).id.
|
43
|
-
Cat.find(cat3.id).id.
|
44
|
-
Cat.find(cat3.id).id.
|
45
|
-
|
46
|
-
counter.query_count.
|
34
|
+
cat1 = Cat.using(:master).create!(:name => 'Master Cat 1')
|
35
|
+
_ct2 = Cat.using(:master).create!(:name => 'Master Cat 2')
|
36
|
+
expect(Cat.using(:master).find(cat1.id)).to eq(cat1)
|
37
|
+
expect(Cat.using(:master).find(cat1.id)).to eq(cat1)
|
38
|
+
expect(Cat.using(:master).find(cat1.id)).to eq(cat1)
|
39
|
+
|
40
|
+
cat3 = Cat.using(:slave1).create!(:name => 'Slave Cat 3')
|
41
|
+
_ct4 = Cat.using(:slave1).create!(:name => 'Slave Cat 4')
|
42
|
+
expect(Cat.find(cat3.id).id).to eq(cat3.id)
|
43
|
+
expect(Cat.find(cat3.id).id).to eq(cat3.id)
|
44
|
+
expect(Cat.find(cat3.id).id).to eq(cat3.id)
|
45
|
+
|
46
|
+
expect(counter.query_count).to eq(16)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
it
|
52
|
-
Cat.create!(:name =>
|
51
|
+
it 'should allow #using syntax to send queries to master' do
|
52
|
+
Cat.create!(:name => 'Master Cat')
|
53
53
|
|
54
54
|
OctopusHelper.using_environment :production_fully_replicated do
|
55
|
-
Cat.using(:master).find_by_name(
|
55
|
+
expect(Cat.using(:master).find_by_name('Master Cat')).not_to be_nil
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
it
|
59
|
+
it 'should send the count query to a slave' do
|
60
60
|
OctopusHelper.using_environment :production_replicated do
|
61
|
-
Cat.create!(:name =>
|
62
|
-
Cat.count.
|
61
|
+
Cat.create!(:name => 'Slave Cat')
|
62
|
+
expect(Cat.count).to eq(0)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
def active_support_subscribed(callback, *args, &
|
66
|
+
def active_support_subscribed(callback, *args, &_block)
|
67
67
|
subscriber = ActiveSupport::Notifications.subscribe(*args, &callback)
|
68
68
|
yield
|
69
69
|
ensure
|
@@ -71,51 +71,46 @@ describe "when the database is replicated" do
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
|
75
|
-
describe "when the database is replicated and the entire application is replicated" do
|
74
|
+
describe 'when the database is replicated and the entire application is replicated' do
|
76
75
|
before(:each) do
|
77
|
-
Octopus.
|
78
|
-
OctopusHelper.clean_connection_proxy
|
76
|
+
allow(Octopus).to receive(:env).and_return('production_fully_replicated')
|
77
|
+
OctopusHelper.clean_connection_proxy
|
79
78
|
end
|
80
79
|
|
81
|
-
it
|
80
|
+
it 'should send all writes queries to master' do
|
82
81
|
OctopusHelper.using_environment :production_fully_replicated do
|
83
|
-
Cat.create!(:name =>
|
84
|
-
Cat.find_by_name(
|
85
|
-
Client.create!(:name =>
|
86
|
-
Client.find_by_name(
|
82
|
+
Cat.create!(:name => 'Slave Cat')
|
83
|
+
expect(Cat.find_by_name('Slave Cat')).to be_nil
|
84
|
+
Client.create!(:name => 'Slave Client')
|
85
|
+
expect(Client.find_by_name('Slave Client')).to be_nil
|
87
86
|
end
|
88
87
|
end
|
89
88
|
|
90
|
-
it
|
89
|
+
it 'should send all writes queries to master' do
|
91
90
|
OctopusHelper.using_environment :production_fully_replicated do
|
92
|
-
Cat.create!(:name =>
|
93
|
-
Cat.find_by_name(
|
94
|
-
Client.create!(:name =>
|
95
|
-
Client.find_by_name(
|
91
|
+
Cat.create!(:name => 'Slave Cat')
|
92
|
+
expect(Cat.find_by_name('Slave Cat')).to be_nil
|
93
|
+
Client.create!(:name => 'Slave Client')
|
94
|
+
expect(Client.find_by_name('Slave Client')).to be_nil
|
96
95
|
end
|
97
96
|
end
|
98
97
|
|
99
|
-
it
|
100
|
-
Keyboard.create!(:name =>
|
98
|
+
it 'should work with validate_uniquess_of' do
|
99
|
+
Keyboard.create!(:name => 'thiago')
|
101
100
|
|
102
101
|
OctopusHelper.using_environment :production_fully_replicated do
|
103
|
-
k = Keyboard.new(:name =>
|
104
|
-
k.save.
|
105
|
-
k.errors.full_messages.
|
102
|
+
k = Keyboard.new(:name => 'thiago')
|
103
|
+
expect(k.save).to be false
|
104
|
+
expect(k.errors.full_messages).to eq(['Name has already been taken'])
|
106
105
|
end
|
107
106
|
end
|
108
107
|
|
109
|
-
it
|
108
|
+
it 'should reset current shard if slave throws an exception' do
|
110
109
|
OctopusHelper.using_environment :production_fully_replicated do
|
111
|
-
Cat.create!(:name =>
|
112
|
-
Cat.connection.current_shard.
|
113
|
-
|
114
|
-
|
115
|
-
rescue
|
116
|
-
end
|
117
|
-
Cat.connection.current_shard.should eql(:master)
|
110
|
+
Cat.create!(:name => 'Slave Cat')
|
111
|
+
expect(Cat.connection.current_shard).to eql(:master)
|
112
|
+
Cat.where(:rubbish => true)
|
113
|
+
expect(Cat.connection.current_shard).to eql(:master)
|
118
114
|
end
|
119
115
|
end
|
120
116
|
end
|
121
|
-
|
@@ -1,17 +1,63 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octopus::ScopeProxy do
|
4
|
-
it
|
5
|
-
@user1 = User.using(:brazil).create!(:name =>
|
6
|
-
@user2 = User.using(:brazil).create!(:name =>
|
7
|
-
@user3 = User.using(:brazil).create!(:name =>
|
8
|
-
|
9
|
-
User.using(:brazil).where(:name =>
|
10
|
-
User.using(:brazil).where(:name =>
|
11
|
-
User.using(:brazil).where(:name =>
|
4
|
+
it 'should allow nested queries' do
|
5
|
+
@user1 = User.using(:brazil).create!(:name => 'Thiago P', :number => 3)
|
6
|
+
@user2 = User.using(:brazil).create!(:name => 'Thiago', :number => 1)
|
7
|
+
@user3 = User.using(:brazil).create!(:name => 'Thiago', :number => 2)
|
8
|
+
|
9
|
+
expect(User.using(:brazil).where(:name => 'Thiago').where(:number => 4).order(:number).all).to eq([])
|
10
|
+
expect(User.using(:brazil).where(:name => 'Thiago').using(:canada).where(:number => 2).using(:brazil).order(:number).all).to eq([@user3])
|
11
|
+
expect(User.using(:brazil).where(:name => 'Thiago').using(:canada).where(:number => 4).using(:brazil).order(:number).all).to eq([])
|
12
|
+
end
|
13
|
+
|
14
|
+
context "When array-like-selecting an item in a group" do
|
15
|
+
before(:each) do
|
16
|
+
User.using(:brazil).create!(:name => "Evan", :number => 1)
|
17
|
+
User.using(:brazil).create!(:name => "Evan", :number => 2)
|
18
|
+
User.using(:brazil).create!(:name => "Evan", :number => 3)
|
19
|
+
@evans = User.using(:brazil).where(:name => "Evan")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "allows a block to select an item" do
|
23
|
+
@evans.select{|u| u.number == 2}.first.number.should eq(2)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "When selecting a field within a scope" do
|
28
|
+
before(:each) do
|
29
|
+
User.using(:brazil).create!(:name => "Evan", :number => 4)
|
30
|
+
@evan = User.using(:brazil).where(:name => "Evan")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "allows single field selection" do
|
34
|
+
@evan.select("name").first.name.should eq("Evan")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allows selection by array" do
|
38
|
+
@evan.select(["name"]).first.name.should eq("Evan")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "allows multiple selection by string" do
|
42
|
+
@evan.select("id, name").first.id.should be_a Fixnum
|
43
|
+
end
|
44
|
+
|
45
|
+
it "allows multiple selection by array" do
|
46
|
+
@evan.select(["id", "name"]).first.id.should be_a Fixnum
|
47
|
+
end
|
48
|
+
|
49
|
+
if Octopus.rails4?
|
50
|
+
it "allows multiple selection by symbol" do
|
51
|
+
@evan.select(:id, :name).first.id.should be_a Fixnum
|
52
|
+
end
|
53
|
+
|
54
|
+
it "allows multiple selection by string and symbol" do
|
55
|
+
@evan.select(:id, "name").first.id.should be_a Fixnum
|
56
|
+
end
|
57
|
+
end
|
12
58
|
end
|
13
59
|
|
14
60
|
it "should raise a exception when trying to send a query to a shard that don't exists" do
|
15
|
-
|
61
|
+
expect { User.using(:dont_exists).all }.to raise_exception('Nonexistent Shard Name: dont_exists')
|
16
62
|
end
|
17
63
|
end
|