misha-ar-octopus 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.rubocop.yml +46 -0
- data/.rubocop_todo.yml +56 -0
- data/.ruby-version +1 -0
- data/.travis.yml +18 -0
- data/Appraisals +16 -0
- data/Gemfile +4 -0
- data/README.mkdn +242 -0
- data/Rakefile +172 -0
- data/TODO.txt +7 -0
- data/ar-octopus.gemspec +42 -0
- data/gemfiles/rails32.gemfile +7 -0
- data/gemfiles/rails4.gemfile +7 -0
- data/gemfiles/rails41.gemfile +7 -0
- data/gemfiles/rails42.gemfile +7 -0
- data/init.rb +1 -0
- data/lib/ar-octopus.rb +1 -0
- data/lib/octopus.rb +181 -0
- data/lib/octopus/abstract_adapter.rb +35 -0
- data/lib/octopus/association.rb +13 -0
- data/lib/octopus/association_shard_tracking.rb +114 -0
- data/lib/octopus/collection_association.rb +11 -0
- data/lib/octopus/collection_proxy.rb +16 -0
- data/lib/octopus/exception.rb +4 -0
- data/lib/octopus/has_and_belongs_to_many_association.rb +9 -0
- data/lib/octopus/load_balancing.rb +4 -0
- data/lib/octopus/load_balancing/round_robin.rb +20 -0
- data/lib/octopus/log_subscriber.rb +22 -0
- data/lib/octopus/migration.rb +168 -0
- data/lib/octopus/model.rb +210 -0
- data/lib/octopus/persistence.rb +39 -0
- data/lib/octopus/proxy.rb +534 -0
- data/lib/octopus/railtie.rb +11 -0
- data/lib/octopus/relation_proxy.rb +35 -0
- data/lib/octopus/scope_proxy.rb +61 -0
- data/lib/octopus/shard_tracking.rb +41 -0
- data/lib/octopus/shard_tracking/attribute.rb +22 -0
- data/lib/octopus/shard_tracking/dynamic.rb +11 -0
- data/lib/octopus/singular_association.rb +9 -0
- data/lib/octopus/slave_group.rb +13 -0
- data/lib/octopus/version.rb +3 -0
- data/lib/tasks/octopus.rake +16 -0
- data/rails/init.rb +1 -0
- data/sample_app/.gitignore +4 -0
- data/sample_app/.rspec +1 -0
- data/sample_app/Gemfile +20 -0
- data/sample_app/README +3 -0
- data/sample_app/README.rdoc +261 -0
- data/sample_app/Rakefile +7 -0
- data/sample_app/app/assets/images/rails.png +0 -0
- data/sample_app/app/assets/javascripts/application.js +15 -0
- data/sample_app/app/assets/stylesheets/application.css +13 -0
- data/sample_app/app/controllers/application_controller.rb +4 -0
- data/sample_app/app/helpers/application_helper.rb +2 -0
- data/sample_app/app/mailers/.gitkeep +0 -0
- data/sample_app/app/models/.gitkeep +0 -0
- data/sample_app/app/models/item.rb +3 -0
- data/sample_app/app/models/user.rb +3 -0
- data/sample_app/app/views/layouts/application.html.erb +14 -0
- data/sample_app/autotest/discover.rb +2 -0
- data/sample_app/config.ru +4 -0
- data/sample_app/config/application.rb +62 -0
- data/sample_app/config/boot.rb +6 -0
- data/sample_app/config/cucumber.yml +8 -0
- data/sample_app/config/database.yml +28 -0
- data/sample_app/config/environment.rb +5 -0
- data/sample_app/config/environments/development.rb +37 -0
- data/sample_app/config/environments/production.rb +67 -0
- data/sample_app/config/environments/test.rb +37 -0
- data/sample_app/config/initializers/backtrace_silencers.rb +7 -0
- data/sample_app/config/initializers/inflections.rb +15 -0
- data/sample_app/config/initializers/mime_types.rb +5 -0
- data/sample_app/config/initializers/secret_token.rb +7 -0
- data/sample_app/config/initializers/session_store.rb +8 -0
- data/sample_app/config/initializers/wrap_parameters.rb +14 -0
- data/sample_app/config/locales/en.yml +5 -0
- data/sample_app/config/routes.rb +58 -0
- data/sample_app/config/shards.yml +28 -0
- data/sample_app/db/migrate/20100720172715_create_users.rb +15 -0
- data/sample_app/db/migrate/20100720172730_create_items.rb +16 -0
- data/sample_app/db/migrate/20100720210335_create_sample_users.rb +11 -0
- data/sample_app/db/schema.rb +29 -0
- data/sample_app/db/seeds.rb +16 -0
- data/sample_app/doc/README_FOR_APP +2 -0
- data/sample_app/features/migrate.feature +45 -0
- data/sample_app/features/seed.feature +15 -0
- data/sample_app/features/step_definitions/seeds_steps.rb +13 -0
- data/sample_app/features/step_definitions/web_steps.rb +218 -0
- data/sample_app/features/support/database.rb +13 -0
- data/sample_app/features/support/env.rb +57 -0
- data/sample_app/features/support/paths.rb +33 -0
- data/sample_app/lib/assets/.gitkeep +0 -0
- data/sample_app/lib/tasks/.gitkeep +0 -0
- data/sample_app/lib/tasks/cucumber.rake +64 -0
- data/sample_app/log/.gitkeep +0 -0
- data/sample_app/public/404.html +26 -0
- data/sample_app/public/422.html +26 -0
- data/sample_app/public/500.html +26 -0
- data/sample_app/public/favicon.ico +0 -0
- data/sample_app/public/images/rails.png +0 -0
- data/sample_app/public/index.html +279 -0
- data/sample_app/public/javascripts/application.js +2 -0
- data/sample_app/public/javascripts/controls.js +965 -0
- data/sample_app/public/javascripts/dragdrop.js +974 -0
- data/sample_app/public/javascripts/effects.js +1123 -0
- data/sample_app/public/javascripts/prototype.js +4874 -0
- data/sample_app/public/javascripts/rails.js +118 -0
- data/sample_app/public/robots.txt +5 -0
- data/sample_app/public/stylesheets/.gitkeep +0 -0
- data/sample_app/script/cucumber +10 -0
- data/sample_app/script/rails +6 -0
- data/sample_app/spec/models/item_spec.rb +5 -0
- data/sample_app/spec/models/user_spec.rb +5 -0
- data/sample_app/spec/spec_helper.rb +27 -0
- data/sample_app/vendor/assets/javascripts/.gitkeep +0 -0
- data/sample_app/vendor/assets/stylesheets/.gitkeep +0 -0
- data/sample_app/vendor/plugins/.gitkeep +0 -0
- data/spec/config/shards.yml +217 -0
- data/spec/migrations/10_create_users_using_replication.rb +9 -0
- data/spec/migrations/11_add_field_in_all_slaves.rb +11 -0
- data/spec/migrations/12_create_users_using_block.rb +23 -0
- data/spec/migrations/13_create_users_using_block_and_using.rb +15 -0
- data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +11 -0
- data/spec/migrations/15_create_user_on_shards_of_default_group_with_versions.rb +9 -0
- data/spec/migrations/1_create_users_on_master.rb +9 -0
- data/spec/migrations/2_create_users_on_canada.rb +11 -0
- data/spec/migrations/3_create_users_on_both_shards.rb +11 -0
- data/spec/migrations/4_create_users_on_shards_of_a_group.rb +11 -0
- data/spec/migrations/5_create_users_on_multiples_groups.rb +11 -0
- data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +11 -0
- data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +11 -0
- data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +11 -0
- data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +11 -0
- data/spec/octopus/association_shard_tracking_spec.rb +714 -0
- data/spec/octopus/collection_proxy_spec.rb +16 -0
- data/spec/octopus/log_subscriber_spec.rb +19 -0
- data/spec/octopus/migration_spec.rb +115 -0
- data/spec/octopus/model_spec.rb +693 -0
- data/spec/octopus/octopus_spec.rb +123 -0
- data/spec/octopus/proxy_spec.rb +307 -0
- data/spec/octopus/relation_proxy_spec.rb +93 -0
- data/spec/octopus/replicated_slave_grouped_spec.rb +91 -0
- data/spec/octopus/replication_spec.rb +137 -0
- data/spec/octopus/scope_proxy_spec.rb +63 -0
- data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +55 -0
- data/spec/octopus/sharded_spec.rb +33 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +15 -0
- data/spec/support/database_connection.rb +4 -0
- data/spec/support/database_models.rb +118 -0
- data/spec/support/octopus_helper.rb +55 -0
- data/spec/support/query_count.rb +17 -0
- data/spec/support/shared_contexts.rb +18 -0
- data/spec/tasks/octopus.rake_spec.rb +32 -0
- metadata +388 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Octopus, :shards => [] do
|
4
|
+
describe '#config' do
|
5
|
+
it 'should load shards.yml file to start working' do
|
6
|
+
expect(Octopus.config).to be_kind_of(HashWithIndifferentAccess)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "when config file doesn't exist" do
|
10
|
+
before(:each) do
|
11
|
+
allow(Octopus).to receive(:directory).and_return('/tmp')
|
12
|
+
Octopus.instance_variable_set(:@config, nil)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return an empty HashWithIndifferentAccess' do
|
16
|
+
expect(Octopus.config).to eq(HashWithIndifferentAccess.new)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#directory' do
|
22
|
+
it 'should return the directory that contains the shards.yml file' do
|
23
|
+
expect(Octopus.directory).to eq(File.expand_path(File.dirname(__FILE__) + '/../'))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#env' do
|
28
|
+
it "should return 'production' when is outside of a rails application" do
|
29
|
+
expect(Octopus.env).to eq('octopus')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#shards=' do
|
34
|
+
after(:each) do
|
35
|
+
Octopus.instance_variable_set(:@config, nil)
|
36
|
+
Octopus::Model.send(:class_variable_set, :@@connection_proxy, nil)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should permit users to configure shards on initializer files, instead of on a yml file.' do
|
40
|
+
expect { User.using(:crazy_shard).create!(:name => 'Joaquim') }.to raise_error
|
41
|
+
|
42
|
+
Octopus.setup do |config|
|
43
|
+
config.shards = { :crazy_shard => { :adapter => 'mysql2', :database => 'octopus_shard_5', :username => 'root', :password => '' } }
|
44
|
+
end
|
45
|
+
|
46
|
+
expect { User.using(:crazy_shard).create!(:name => 'Joaquim') }.not_to raise_error
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#setup' do
|
51
|
+
it 'should have the default octopus environment as production' do
|
52
|
+
expect(Octopus.environments).to eq(['production'])
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should allow the user to configure the octopus environments' do
|
56
|
+
Octopus.setup do |config|
|
57
|
+
config.environments = [:production, :staging]
|
58
|
+
end
|
59
|
+
|
60
|
+
expect(Octopus.environments).to eq(%w(production staging))
|
61
|
+
|
62
|
+
Octopus.setup do |config|
|
63
|
+
config.environments = [:production]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#enabled?' do
|
69
|
+
before do
|
70
|
+
Rails = double
|
71
|
+
end
|
72
|
+
|
73
|
+
after do
|
74
|
+
Object.send(:remove_const, :Rails)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should be if octopus is configured and should hook into current environment' do
|
78
|
+
allow(Rails).to receive(:env).and_return('production')
|
79
|
+
|
80
|
+
expect(Octopus).to be_enabled
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should not be if octopus should not hook into current environment' do
|
84
|
+
allow(Rails).to receive(:env).and_return('staging')
|
85
|
+
|
86
|
+
expect(Octopus).not_to be_enabled
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#fully_replicated' do
|
91
|
+
before do
|
92
|
+
OctopusHelper.using_environment :production_replicated do
|
93
|
+
OctopusHelper.clean_all_shards([:slave1, :slave2, :slave3, :slave4])
|
94
|
+
4.times { |i| User.using(:"slave#{i + 1}").create!(:name => 'Slave User') }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'sends queries to slaves' do
|
99
|
+
OctopusHelper.using_environment :production_replicated do
|
100
|
+
expect(User.count).to eq(0)
|
101
|
+
4.times do |_i|
|
102
|
+
Octopus.fully_replicated do
|
103
|
+
expect(User.count).to eq(1)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'allows nesting' do
|
110
|
+
OctopusHelper.using_environment :production_replicated do
|
111
|
+
Octopus.fully_replicated do
|
112
|
+
expect(User.count).to eq(1)
|
113
|
+
|
114
|
+
Octopus.fully_replicated do
|
115
|
+
expect(User.count).to eq(1)
|
116
|
+
end
|
117
|
+
|
118
|
+
expect(User.count).to eq(1)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,307 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Octopus::Proxy do
|
4
|
+
let(:proxy) { subject }
|
5
|
+
|
6
|
+
describe 'creating a new instance', :shards => [] do
|
7
|
+
it 'should initialize all shards and groups' do
|
8
|
+
# FIXME: Don't test implementation details
|
9
|
+
expect(proxy.instance_variable_get(:@shards)).to include('canada', 'brazil', 'master', 'sqlite_shard', 'russia', 'alone_shard',
|
10
|
+
'aug2009', 'postgresql_shard', 'aug2010', 'aug2011')
|
11
|
+
|
12
|
+
expect(proxy.instance_variable_get(:@shards)).to include('protocol_shard')
|
13
|
+
|
14
|
+
expect(proxy.has_group?('country_shards')).to be true
|
15
|
+
expect(proxy.shards_for_group('country_shards')).to include(:canada, :brazil, :russia)
|
16
|
+
|
17
|
+
expect(proxy.has_group?('history_shards')).to be true
|
18
|
+
expect(proxy.shards_for_group('history_shards')).to include(:aug2009, :aug2010, :aug2011)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should initialize the block attribute as false' do
|
22
|
+
expect(proxy.block).to be_falsey
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should initialize replicated attribute as false' do
|
26
|
+
expect(proxy.instance_variable_get(:@replicated)).to be_falsey
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should work with thiking sphinx' do
|
30
|
+
config = proxy.instance_variable_get(:@config)
|
31
|
+
expect(config[:adapter]).to eq('mysql2')
|
32
|
+
expect(config[:database]).to eq('octopus_shard_1')
|
33
|
+
expect(config[:username]).to eq('root')
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should create a set with all adapters, to ensure that is needed to clean the table name.' do
|
37
|
+
adapters = proxy.instance_variable_get(:@adapters)
|
38
|
+
expect(adapters).to be_kind_of(Set)
|
39
|
+
expect(adapters.to_a).to match_array(%w(sqlite3 mysql2 postgresql))
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should respond correctly to respond_to?(:pk_and_sequence_for)' do
|
43
|
+
expect(proxy.respond_to?(:pk_and_sequence_for)).to be true
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should respond correctly to respond_to?(:primary_key)' do
|
47
|
+
expect(proxy.respond_to?(:primary_key)).to be true
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when an adapter that modifies the config' do
|
51
|
+
before { OctopusHelper.octopus_env = 'modify_config' }
|
52
|
+
after { OctopusHelper.octopus_env = 'octopus' }
|
53
|
+
|
54
|
+
it 'should not fail with missing adapter second time round' do
|
55
|
+
skip 'This test was actually failing because of a typo in the error message.'
|
56
|
+
Thread.current['octopus.current_shard'] = :modify_config_read
|
57
|
+
|
58
|
+
expect { Octopus::Proxy.new(Octopus.config) }.not_to raise_error
|
59
|
+
|
60
|
+
Thread.current['octopus.current_shard'] = nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#should_clean_table_name?' do
|
65
|
+
it 'should return true when you have a environment with multiple database types' do
|
66
|
+
expect(proxy.should_clean_table_name?).to be true
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'when using a environment with a single table name' do
|
70
|
+
before(:each) do
|
71
|
+
OctopusHelper.octopus_env = 'production_replicated'
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should return false' do
|
75
|
+
expect(proxy.should_clean_table_name?).to be false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'should raise error if you have duplicated shard names' do
|
81
|
+
before(:each) do
|
82
|
+
OctopusHelper.octopus_env = 'production_raise_error'
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should raise the error' do
|
86
|
+
expect { proxy }.to raise_error('You have duplicated shard names!')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "should initialize just the master when you don't have a shards.yml file" do
|
91
|
+
before(:each) do
|
92
|
+
OctopusHelper.octopus_env = 'crazy_environment'
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should initialize just the master shard' do
|
96
|
+
expect(proxy.instance_variable_get(:@shards).keys).to eq(['master'])
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should not initialize replication' do
|
100
|
+
expect(proxy.instance_variable_get(:@replicated)).to be_nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'when you have a replicated environment' do
|
106
|
+
before(:each) do
|
107
|
+
OctopusHelper.octopus_env = 'production_replicated'
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'should have the replicated attribute as true' do
|
111
|
+
expect(proxy.instance_variable_get(:@replicated)).to be true
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should initialize the list of shards' do
|
115
|
+
expect(proxy.instance_variable_get(:@slaves_list)).to eq(%w(slave1 slave2 slave3 slave4))
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe 'when you have a rails application' do
|
120
|
+
before(:each) do
|
121
|
+
Rails = double
|
122
|
+
OctopusHelper.octopus_env = 'octopus_rails'
|
123
|
+
end
|
124
|
+
|
125
|
+
after(:each) do
|
126
|
+
Object.send(:remove_const, :Rails)
|
127
|
+
Octopus.instance_variable_set(:@config, nil)
|
128
|
+
Octopus.instance_variable_set(:@rails_env, nil)
|
129
|
+
OctopusHelper.clean_connection_proxy
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should initialize correctly octopus common variables for the environments' do
|
133
|
+
allow(Rails).to receive(:env).and_return('staging')
|
134
|
+
Octopus.instance_variable_set(:@rails_env, nil)
|
135
|
+
Octopus.instance_variable_set(:@environments, nil)
|
136
|
+
Octopus.config
|
137
|
+
|
138
|
+
expect(proxy.instance_variable_get(:@replicated)).to be true
|
139
|
+
expect(Octopus.environments).to eq(%w(staging production))
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should initialize correctly the shards for the staging environment' do
|
143
|
+
allow(Rails).to receive(:env).and_return('staging')
|
144
|
+
Octopus.instance_variable_set(:@rails_env, nil)
|
145
|
+
Octopus.instance_variable_set(:@environments, nil)
|
146
|
+
Octopus.config
|
147
|
+
|
148
|
+
expect(proxy.instance_variable_get(:@shards).keys.to_set).to eq(Set.new(%w(slave1 slave2 master)))
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'should initialize correctly the shard octopus_shard value for logging' do
|
152
|
+
allow(Rails).to receive(:env).and_return('staging')
|
153
|
+
Octopus.instance_variable_set(:@rails_env, nil)
|
154
|
+
Octopus.instance_variable_set(:@environments, nil)
|
155
|
+
Octopus.config
|
156
|
+
|
157
|
+
expect(proxy.instance_variable_get(:@shards)['slave1'].spec.config).to have_key :octopus_shard
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'should initialize correctly the shards for the production environment' do
|
161
|
+
allow(Rails).to receive(:env).and_return('production')
|
162
|
+
Octopus.instance_variable_set(:@rails_env, nil)
|
163
|
+
Octopus.instance_variable_set(:@environments, nil)
|
164
|
+
Octopus.config
|
165
|
+
|
166
|
+
expect(proxy.instance_variable_get(:@shards).keys.to_set).to eq(Set.new(%w(slave3 slave4 master)))
|
167
|
+
end
|
168
|
+
|
169
|
+
describe 'using the master connection', :shards => [:russia, :master] do
|
170
|
+
before(:each) do
|
171
|
+
allow(Rails).to receive(:env).and_return('development')
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'should use the master connection' do
|
175
|
+
user = User.create!(:name => 'Thiago')
|
176
|
+
user.name = 'New Thiago'
|
177
|
+
user.save
|
178
|
+
expect(User.find_by_name('New Thiago')).not_to be_nil
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'should work when using using syntax' do
|
182
|
+
user = User.using(:russia).create!(:name => 'Thiago')
|
183
|
+
|
184
|
+
user.name = 'New Thiago'
|
185
|
+
user.save
|
186
|
+
|
187
|
+
expect(User.using(:russia).find_by_name('New Thiago')).to eq(user)
|
188
|
+
expect(User.find_by_name('New Thiago')).to eq(user)
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'should work when using blocks' do
|
192
|
+
Octopus.using(:russia) do
|
193
|
+
@user = User.create!(:name => 'Thiago')
|
194
|
+
end
|
195
|
+
|
196
|
+
expect(User.find_by_name('Thiago')).to eq(@user)
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'should work with associations' do
|
200
|
+
u = Client.create!(:name => 'Thiago')
|
201
|
+
i = Item.create(:name => 'Item')
|
202
|
+
u.items << i
|
203
|
+
u.save
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
describe 'returning the correct connection' do
|
209
|
+
describe 'should return the shard name' do
|
210
|
+
it 'when current_shard is empty' do
|
211
|
+
expect(proxy.shard_name).to eq(:master)
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'when current_shard is empty with custom master' do
|
215
|
+
OctopusHelper.using_environment :octopus do
|
216
|
+
Octopus.config[:master_shard] = :brazil
|
217
|
+
expect(proxy.shard_name).to eq(:brazil)
|
218
|
+
Octopus.config[:master_shard] = nil
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'when current_shard is a single shard' do
|
223
|
+
proxy.current_shard = :canada
|
224
|
+
expect(proxy.shard_name).to eq(:canada)
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'when current_shard is more than one shard' do
|
228
|
+
proxy.current_shard = [:russia, :brazil]
|
229
|
+
expect(proxy.shard_name).to eq(:russia)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
describe 'should return the connection based on shard_name' do
|
234
|
+
it 'when current_shard is empty' do
|
235
|
+
expect(proxy.select_connection).to eq(proxy.instance_variable_get(:@shards)[:master].connection)
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'when current_shard is a single shard' do
|
239
|
+
proxy.current_shard = :canada
|
240
|
+
expect(proxy.select_connection).to eq(proxy.instance_variable_get(:@shards)[:canada].connection)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe 'saving multiple sharded objects at once' do
|
246
|
+
before :each do
|
247
|
+
@p = MmorpgPlayer.using(:alone_shard).create!(:player_name => 'Thiago')
|
248
|
+
end
|
249
|
+
|
250
|
+
subject { @p.save! }
|
251
|
+
|
252
|
+
context 'when the objects are created with #new and saved one at a time' do
|
253
|
+
before :each do
|
254
|
+
@p.weapons.create!(:name => 'battleaxe', :hand => 'right')
|
255
|
+
@p.skills.create!(:name => 'smiting', :weapon => @p.weapons[0])
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'should save all associated objects on the correct shard' do
|
259
|
+
expect { subject }.to_not raise_error
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
context 'when the objects are created with #new and saved at the same time' do
|
264
|
+
before :each do
|
265
|
+
@p.weapons.new(:name => 'battleaxe', :hand => 'right')
|
266
|
+
@p.skills.new(:name => 'smiting', :weapon => @p.weapons[0])
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'should save all associated objects on the correct shard' do
|
270
|
+
expect { subject }.to_not raise_error
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
describe 'connection reuse' do
|
276
|
+
before :each do
|
277
|
+
@item_brazil_conn = Item.using(:brazil).new(:name => 'Brazil Item').class.connection.select_connection
|
278
|
+
@item_canada_conn = Item.using(:canada).new(:name => 'Canada Item').class.connection.select_connection
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'reuses connections' do
|
282
|
+
expect(Item.using(:brazil).new(:name => 'Another Brazil Item').class.connection.select_connection).to eq(@item_brazil_conn)
|
283
|
+
expect(Item.using(:canada).new(:name => 'Another Canada Item').class.connection.select_connection).to eq(@item_canada_conn)
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'reuses connections after clear_active_connections! is called' do
|
287
|
+
expect(Item.using(:brazil).new(:name => 'Another Brazil Item').class.connection.select_connection).to eq(@item_brazil_conn)
|
288
|
+
expect(Item.using(:canada).new(:name => 'Another Canada Item').class.connection.select_connection).to eq(@item_canada_conn)
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'creates new connections after clear_all_connections! is called' do
|
292
|
+
Item.clear_all_connections!
|
293
|
+
expect(Item.using(:brazil).new(:name => 'Another Brazil Item').class.connection.select_connection).not_to eq(@item_brazil_conn)
|
294
|
+
expect(Item.using(:canada).new(:name => 'Another Canada Item').class.connection.select_connection).not_to eq(@item_canada_conn)
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'is consistent with connected?' do
|
298
|
+
expect(Item.connected?).to be true
|
299
|
+
expect(ActiveRecord::Base.connected?).to be true
|
300
|
+
|
301
|
+
Item.clear_all_connections!
|
302
|
+
|
303
|
+
expect(Item.connected?).to be false
|
304
|
+
expect(ActiveRecord::Base.connected?).to be false
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Octopus::RelationProxy do
|
4
|
+
describe 'shard tracking' do
|
5
|
+
before :each do
|
6
|
+
@client = Client.using(:canada).create!
|
7
|
+
@client.items << Item.using(:canada).create!
|
8
|
+
@relation = @client.items
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'remembers the shard on which a relation was created' do
|
12
|
+
expect(@relation.current_shard).to eq(:canada)
|
13
|
+
end
|
14
|
+
|
15
|
+
unless Octopus.rails3?
|
16
|
+
it 'can define collection association with the same name as ancestor private method' do
|
17
|
+
@client.comments << Comment.using(:canada).create!(open: true)
|
18
|
+
expect(@client.comments.open).to be_a_kind_of(ActiveRecord::Relation)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when comparing to other Relation objects' do
|
23
|
+
before :each do
|
24
|
+
@relation.reset
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'is equal to its clone' do
|
28
|
+
expect(@relation).to eq(@relation.clone)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if Octopus.rails4?
|
33
|
+
context 'under Rails 4' do
|
34
|
+
it 'is an Octopus::RelationProxy' do
|
35
|
+
expect{@relation.ar_relation}.not_to raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should be able to return its ActiveRecord::Relation' do
|
39
|
+
expect(@relation.ar_relation.is_a?(ActiveRecord::Relation)).to be true
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'is equal to an identically-defined, but different, RelationProxy' do
|
43
|
+
i = @client.items
|
44
|
+
expect(@relation).to eq(i)
|
45
|
+
expect(@relation.__id__).not_to eq(i.__id__)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'is equal to its own underlying ActiveRecord::Relation' do
|
49
|
+
expect(@relation).to eq(@relation.ar_relation)
|
50
|
+
expect(@relation.ar_relation).to eq(@relation)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when no explicit shard context is provided' do
|
56
|
+
it 'uses the correct shard' do
|
57
|
+
expect(@relation.count).to eq(1)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'lazily evaluates on the correct shard' do
|
61
|
+
# Do something to force Client.connection_proxy.current_shard to change
|
62
|
+
_some_count = Client.using(:brazil).count
|
63
|
+
expect(@relation.select(:client_id).count).to eq(1)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when an explicit, but different, shard context is provided' do
|
68
|
+
it 'uses the correct shard' do
|
69
|
+
expect(Item.using(:brazil).count).to eq(0)
|
70
|
+
_clients_on_brazil = Client.using(:brazil).all
|
71
|
+
Octopus.using(:brazil) do
|
72
|
+
expect(@relation.count).to eq(1)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'uses the correct shard in block when method_missing is triggered on CollectionProxy objects' do
|
77
|
+
Octopus.using(:brazil) do
|
78
|
+
@client.items.each do |item|
|
79
|
+
expect(item.current_shard).to eq(:canada)
|
80
|
+
expect(ActiveRecord::Base.connection.current_shard).to eq(:brazil)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'lazily evaluates on the correct shard' do
|
86
|
+
expect(Item.using(:brazil).count).to eq(0)
|
87
|
+
Octopus.using(:brazil) do
|
88
|
+
expect(@relation.select(:client_id).count).to eq(1)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|