activerecord-turntable 2.5.0 → 3.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -0
- data/.travis.yml +3 -7
- data/CHANGELOG.md +4 -14
- data/Gemfile +3 -0
- data/Guardfile +12 -7
- data/README.md +11 -19
- data/Rakefile +14 -15
- data/activerecord-turntable.gemspec +24 -27
- data/gemfiles/rails5_0.gemfile +6 -0
- data/lib/active_record/turntable/active_record_ext/abstract_adapter.rb +24 -20
- data/lib/active_record/turntable/active_record_ext/activerecord_import_ext.rb +6 -16
- data/lib/active_record/turntable/active_record_ext/acts_as_archive_extension.rb +25 -16
- data/lib/active_record/turntable/active_record_ext/association.rb +33 -14
- data/lib/active_record/turntable/active_record_ext/association_preloader.rb +4 -24
- data/lib/active_record/turntable/active_record_ext/clever_load.rb +2 -2
- data/lib/active_record/turntable/active_record_ext/connection_handler_extension.rb +11 -15
- data/lib/active_record/turntable/active_record_ext/database_tasks.rb +9 -9
- data/lib/active_record/turntable/active_record_ext/fixtures.rb +11 -41
- data/lib/active_record/turntable/active_record_ext/locking_optimistic.rb +40 -147
- data/lib/active_record/turntable/active_record_ext/log_subscriber.rb +6 -37
- data/lib/active_record/turntable/active_record_ext/migration_proxy.rb +1 -1
- data/lib/active_record/turntable/active_record_ext/persistence.rb +54 -148
- data/lib/active_record/turntable/active_record_ext/relation.rb +17 -45
- data/lib/active_record/turntable/active_record_ext/schema_dumper.rb +80 -78
- data/lib/active_record/turntable/active_record_ext/sequencer.rb +6 -15
- data/lib/active_record/turntable/active_record_ext/transactions.rb +14 -9
- data/lib/active_record/turntable/active_record_ext.rb +15 -16
- data/lib/active_record/turntable/algorithm/modulo_algorithm.rb +1 -1
- data/lib/active_record/turntable/algorithm/range_algorithm.rb +9 -9
- data/lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb +12 -12
- data/lib/active_record/turntable/base.rb +27 -14
- data/lib/active_record/turntable/cluster.rb +10 -13
- data/lib/active_record/turntable/cluster_helper_methods.rb +6 -6
- data/lib/active_record/turntable/config.rb +3 -3
- data/lib/active_record/turntable/connection_proxy/mixable.rb +1 -1
- data/lib/active_record/turntable/connection_proxy.rb +23 -22
- data/lib/active_record/turntable/helpers/test_helper.rb +4 -4
- data/lib/active_record/turntable/master_shard.rb +12 -7
- data/lib/active_record/turntable/migration.rb +41 -47
- data/lib/active_record/turntable/mixer/fader/calculate_shards_sum_result.rb +7 -7
- data/lib/active_record/turntable/mixer/fader/select_shards_merge_result.rb +12 -12
- data/lib/active_record/turntable/mixer.rb +121 -121
- data/lib/active_record/turntable/plugin.rb +1 -1
- data/lib/active_record/turntable/pool_proxy.rb +7 -19
- data/lib/active_record/turntable/query_cache.rb +41 -0
- data/lib/active_record/turntable/railtie.rb +7 -5
- data/lib/active_record/turntable/railties/databases.rake +19 -20
- data/lib/active_record/turntable/seq_shard.rb +15 -15
- data/lib/active_record/turntable/sequencer/api.rb +7 -7
- data/lib/active_record/turntable/sequencer/barrage.rb +6 -7
- data/lib/active_record/turntable/sequencer/mysql.rb +2 -2
- data/lib/active_record/turntable/sequencer.rb +15 -15
- data/lib/active_record/turntable/shard.rb +23 -20
- data/lib/active_record/turntable/sql_tree_patch.rb +59 -57
- data/lib/active_record/turntable/util.rb +1 -21
- data/lib/active_record/turntable/version.rb +1 -1
- data/lib/active_record/turntable.rb +14 -19
- data/lib/activerecord-turntable.rb +2 -2
- data/script/performance/algorithm +8 -9
- data/spec/active_record/turntable/active_record_ext/association_preloader_spec.rb +4 -4
- data/spec/active_record/turntable/active_record_ext/association_spec.rb +5 -14
- data/spec/active_record/turntable/active_record_ext/clever_load_spec.rb +6 -6
- data/spec/active_record/turntable/active_record_ext/fixture_set_spec.rb +4 -4
- data/spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb +2 -15
- data/spec/active_record/turntable/active_record_ext/migration_spec.rb +4 -4
- data/spec/active_record/turntable/active_record_ext/persistence_spec.rb +17 -15
- data/spec/active_record/turntable/active_record_ext/sequencer_spec.rb +1 -1
- data/spec/active_record/turntable/active_record_ext/test_fixtures_spec.rb +3 -3
- data/spec/active_record/turntable/algorithm/modulo_algorithm_spec.rb +2 -3
- data/spec/active_record/turntable/algorithm/range_algorithm_spec.rb +2 -3
- data/spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb +2 -3
- data/spec/active_record/turntable/algorithm_spec.rb +5 -5
- data/spec/active_record/turntable/base_spec.rb +1 -1
- data/spec/active_record/turntable/cluster_spec.rb +1 -1
- data/spec/active_record/turntable/config_spec.rb +1 -1
- data/spec/active_record/turntable/connection_proxy_spec.rb +16 -17
- data/spec/active_record/turntable/finder_spec.rb +1 -1
- data/spec/active_record/turntable/mixer/fader_spec.rb +1 -1
- data/spec/active_record/turntable/mixer_spec.rb +2 -4
- data/spec/active_record/turntable/query_cache_spec.rb +28 -0
- data/spec/active_record/turntable/sequencer/api_spec.rb +4 -4
- data/spec/active_record/turntable/sequencer/barrage_spec.rb +2 -2
- data/spec/active_record/turntable/sequencer/mysql_spec.rb +1 -1
- data/spec/active_record/turntable/shard_spec.rb +1 -1
- data/spec/active_record/turntable/sql_tree_patch_spec.rb +2 -2
- data/spec/active_record/turntable/transaction_spec.rb +2 -2
- data/spec/active_record/turntable_spec.rb +3 -3
- data/spec/migrations/002_create_user_statuses.rb +4 -4
- data/spec/migrations/003_create_cards.rb +3 -3
- data/spec/migrations/004_create_cards_users.rb +2 -2
- data/spec/models/user_status.rb +0 -1
- data/spec/spec_helper.rb +15 -14
- data/spec/support/turntable_helper.rb +4 -4
- metadata +98 -59
- data/gemfiles/rails4_0.gemfile +0 -7
- data/gemfiles/rails4_1.gemfile +0 -7
- data/gemfiles/rails4_2.gemfile +0 -7
- data/lib/active_record/turntable/rack/connection_management.rb +0 -18
- data/lib/active_record/turntable/rack/query_cache.rb +0 -40
- data/lib/active_record/turntable/rack.rb +0 -8
- data/spec/active_record/turntable/rack/query_cache_spec.rb +0 -19
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::ActiveRecordExt::Association do
|
4
4
|
before(:all) do
|
@@ -11,7 +11,7 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Association do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
let!(:user) do
|
14
|
-
user = User.new({:
|
14
|
+
user = User.new({ nickname: "user1" })
|
15
15
|
user.id = 1
|
16
16
|
user.save
|
17
17
|
user
|
@@ -47,20 +47,11 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Association do
|
|
47
47
|
ActiveRecord::Base.turntable_config.instance_variable_get(:@config)[:raise_on_not_specified_shard_query] = true
|
48
48
|
end
|
49
49
|
let(:cards_user) { CardsUser.where(user: user).first }
|
50
|
-
let(:cards_users_history) { cards_users_histories.find { |history| history.user_id == user.id } }
|
51
50
|
|
52
51
|
context "associated objects has same turntable_key" do
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
it { is_expected.to include(*cards_users_histories.select { |history| history.cards_user_id == cards_user.id }) }
|
57
|
-
end
|
58
|
-
|
59
|
-
context "AssociationRelation#where" do
|
60
|
-
subject { cards_user.cards_users_histories.where(id: cards_users_history.id).to_a }
|
61
|
-
it { expect { subject }.to_not raise_error }
|
62
|
-
it { is_expected.to include(cards_users_history) }
|
63
|
-
end
|
52
|
+
subject { cards_user.cards_users_histories.to_a }
|
53
|
+
it { expect { subject }.to_not raise_error }
|
54
|
+
it { is_expected.to include(*cards_users_histories.select { |history| history.cards_user_id == cards_user.id }) }
|
64
55
|
end
|
65
56
|
|
66
57
|
context "associated objects has different turntable_key" do
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "logger"
|
3
3
|
|
4
4
|
describe ActiveRecord::Turntable::ActiveRecordExt::CleverLoad do
|
5
5
|
before(:all) do
|
@@ -10,14 +10,14 @@ describe ActiveRecord::Turntable::ActiveRecordExt::CleverLoad do
|
|
10
10
|
establish_connection_to(:test)
|
11
11
|
truncate_shard
|
12
12
|
|
13
|
-
@user1 = User.new({:
|
13
|
+
@user1 = User.new({ nickname: "user1" })
|
14
14
|
@user1.id = 1
|
15
15
|
@user1.save
|
16
|
-
@user1_status = @user1.create_user_status(:
|
17
|
-
@user2 = User.new({:
|
16
|
+
@user1_status = @user1.create_user_status(hp: 10, mp: 10)
|
17
|
+
@user2 = User.new({ nickname: "user2" })
|
18
18
|
@user2.id = 2
|
19
19
|
@user2.save
|
20
|
-
@user2_status = @user2.create_user_status(:
|
20
|
+
@user2_status = @user2.create_user_status(hp: 20, mp: 10)
|
21
21
|
end
|
22
22
|
|
23
23
|
context "When a model has has_one relation" do
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "active_record"
|
4
|
+
require "active_record/turntable/active_record_ext/fixtures"
|
5
5
|
|
6
6
|
describe ActiveRecord::FixtureSet do
|
7
7
|
before(:all) do
|
@@ -21,7 +21,7 @@ describe ActiveRecord::FixtureSet do
|
|
21
21
|
subject { ActiveRecord::FixtureSet.create_fixtures(fixtures_root, "cards") }
|
22
22
|
it { is_expected.to be_instance_of(Array) }
|
23
23
|
it "creates card records" do
|
24
|
-
expect {subject}.to change {Card.count}.from(0).to(cards.size)
|
24
|
+
expect { subject }.to change { Card.count }.from(0).to(cards.size)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::ActiveRecordExt::LockingOptimistic do
|
4
4
|
before(:all) do
|
@@ -10,14 +10,10 @@ describe ActiveRecord::Turntable::ActiveRecordExt::LockingOptimistic do
|
|
10
10
|
truncate_shard
|
11
11
|
end
|
12
12
|
|
13
|
-
before
|
13
|
+
before do
|
14
14
|
ActiveRecord::Base.turntable_config.instance_variable_get(:@config)[:raise_on_not_specified_shard_update] = true
|
15
15
|
end
|
16
16
|
|
17
|
-
after(:each) do
|
18
|
-
ActiveRecord::Base.turntable_config.instance_variable_get(:@config)[:raise_on_not_specified_shard_update] = false
|
19
|
-
end
|
20
|
-
|
21
17
|
let!(:user_status) do
|
22
18
|
user_status = UserStatus.new(user_id: 1)
|
23
19
|
user_status.id = 10
|
@@ -29,13 +25,4 @@ describe ActiveRecord::Turntable::ActiveRecordExt::LockingOptimistic do
|
|
29
25
|
subject { user_status.update_attributes(hp: 20) }
|
30
26
|
it { expect { subject }.to change(user_status, :lock_version).by(1) }
|
31
27
|
end
|
32
|
-
|
33
|
-
describe "Json serialized column is saved" do
|
34
|
-
before do
|
35
|
-
user_status.update_attributes(data: {foo: 'bar'})
|
36
|
-
user_status.reload
|
37
|
-
end
|
38
|
-
subject { user_status.data }
|
39
|
-
it { expect { subject }.to_not raise_error }
|
40
|
-
end
|
41
28
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Migration do
|
4
4
|
before(:all) do
|
@@ -16,11 +16,11 @@ describe ActiveRecord::Turntable::Migration do
|
|
16
16
|
context "With clusters definitions" do
|
17
17
|
let(:migration_class) {
|
18
18
|
klass = Class.new(ActiveRecord::Migration) {
|
19
|
-
|
19
|
+
clusters :user_cluster
|
20
20
|
}
|
21
21
|
}
|
22
|
-
let(:cluster_config) { ActiveRecord::Base.turntable_config[
|
23
|
-
let(:user_cluster_shards) { cluster_config[
|
22
|
+
let(:cluster_config) { ActiveRecord::Base.turntable_config[:clusters][:user_cluster] }
|
23
|
+
let(:user_cluster_shards) { cluster_config[:shards].map { |s| s[:connection] } }
|
24
24
|
|
25
25
|
it { is_expected.to eq(user_cluster_shards) }
|
26
26
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "logger"
|
3
3
|
|
4
4
|
describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
5
5
|
before(:all) do
|
@@ -19,20 +19,20 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
let(:user) {
|
22
|
-
u = User.new({:
|
22
|
+
u = User.new({ nickname: "foobar" })
|
23
23
|
u.id = 1
|
24
24
|
u.updated_at = Time.current - 1.day
|
25
25
|
u.save
|
26
26
|
u
|
27
27
|
}
|
28
28
|
let(:user_status){
|
29
|
-
stat = user.create_user_status(:
|
29
|
+
stat = user.create_user_status(hp: 10, mp: 10)
|
30
30
|
stat.updated_at = Time.current - 1.day
|
31
31
|
stat.save
|
32
32
|
stat
|
33
33
|
}
|
34
34
|
let(:card){
|
35
|
-
Card.create!(:
|
35
|
+
Card.create!(name: "foobar")
|
36
36
|
}
|
37
37
|
let(:cards_user){
|
38
38
|
user.cards_users.create(card: card)
|
@@ -41,7 +41,7 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
41
41
|
context "with blob column" do
|
42
42
|
let(:blob_value) { "\123\123\123" }
|
43
43
|
let(:user) {
|
44
|
-
u = User.new(:
|
44
|
+
u = User.new(nickname: "x", blob: blob_value)
|
45
45
|
u.id = 1
|
46
46
|
u.save
|
47
47
|
u
|
@@ -121,11 +121,14 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
121
121
|
end
|
122
122
|
|
123
123
|
it "should change updated_at when updating" do
|
124
|
-
cards_user
|
124
|
+
cards_user
|
125
125
|
|
126
|
-
|
127
|
-
|
128
|
-
|
126
|
+
Timecop.travel(1.day.from_now) do
|
127
|
+
expect {
|
128
|
+
cards_user.num = 2
|
129
|
+
cards_user.save!
|
130
|
+
}.to change(cards_user, :updated_at)
|
131
|
+
end
|
129
132
|
end
|
130
133
|
|
131
134
|
it "should send shard_key condition when destroying" do
|
@@ -148,7 +151,7 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
148
151
|
|
149
152
|
expect { cards_user.reload }.to_not raise_error
|
150
153
|
|
151
|
-
expect(strio.string.split("\n").select {|stmt| stmt =~ /SELECT/ and stmt !~ /Turntable/ }).to have(1).items
|
154
|
+
expect(strio.string.split("\n").select { |stmt| stmt =~ /SELECT/ and stmt !~ /Turntable/ }).to have(1).items
|
152
155
|
end
|
153
156
|
|
154
157
|
it "should execute one query when touching" do
|
@@ -157,7 +160,7 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
157
160
|
ActiveRecord::Base.logger = Logger.new(strio)
|
158
161
|
|
159
162
|
expect { cards_user.touch }.to_not raise_error
|
160
|
-
expect(strio.string.split("\n").select {|stmt| stmt =~ /UPDATE/ and stmt !~ /Turntable/ }).to have(1).items
|
163
|
+
expect(strio.string.split("\n").select { |stmt| stmt =~ /UPDATE/ and stmt !~ /Turntable/ }).to have(1).items
|
161
164
|
end
|
162
165
|
|
163
166
|
it "should execute one query when locking" do
|
@@ -166,7 +169,7 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
166
169
|
ActiveRecord::Base.logger = Logger.new(strio)
|
167
170
|
|
168
171
|
expect { cards_user.lock! }.to_not raise_error
|
169
|
-
expect(strio.string.split("\n").select {|stmt| stmt =~ /SELECT/ and stmt !~ /Turntable/ }).to have(1).items
|
172
|
+
expect(strio.string.split("\n").select { |stmt| stmt =~ /SELECT/ and stmt !~ /Turntable/ }).to have(1).items
|
170
173
|
end
|
171
174
|
|
172
175
|
it "should execute one query when update_columns" do
|
@@ -175,7 +178,7 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
175
178
|
ActiveRecord::Base.logger = Logger.new(strio)
|
176
179
|
|
177
180
|
expect { cards_user.update_columns(num: 10) }.to_not raise_error
|
178
|
-
expect(strio.string.split("\n").select {|stmt| stmt =~ /UPDATE/ and stmt !~ /Turntable/ }).to have(1).items
|
181
|
+
expect(strio.string.split("\n").select { |stmt| stmt =~ /UPDATE/ and stmt !~ /Turntable/ }).to have(1).items
|
179
182
|
end
|
180
183
|
end
|
181
184
|
|
@@ -205,5 +208,4 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Persistence do
|
|
205
208
|
it { is_expected.to be_instance_of(CardsUser) }
|
206
209
|
it { is_expected.to eq(cards_user) }
|
207
210
|
end
|
208
|
-
|
209
211
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "active_record"
|
4
|
+
require "active_record/turntable/active_record_ext/fixtures"
|
5
5
|
|
6
6
|
describe ActiveRecord::TestFixtures do
|
7
7
|
before(:all) do
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Algorithm::ModuloAlgorithm do
|
4
4
|
before(:all) do
|
@@ -27,9 +27,8 @@ describe ActiveRecord::Turntable::Algorithm::ModuloAlgorithm do
|
|
27
27
|
|
28
28
|
context "#calculate with a value that is not a number" do
|
29
29
|
it "raises ActiveRecord::Turntable::CannotSpecifyShardError" do
|
30
|
-
expect { @alg.calculate(
|
30
|
+
expect { @alg.calculate("a") }.to raise_error(ActiveRecord::Turntable::CannotSpecifyShardError)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
34
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Algorithm::RangeAlgorithm do
|
4
4
|
before(:all) do
|
@@ -27,9 +27,8 @@ describe ActiveRecord::Turntable::Algorithm::RangeAlgorithm do
|
|
27
27
|
|
28
28
|
context "#calculate with 10000000" do
|
29
29
|
it "raises ActiveRecord::Turntable::CannotSpecifyShardError" do
|
30
|
-
expect { @alg.calculate(
|
30
|
+
expect { @alg.calculate(10_000_000) }.to raise_error(ActiveRecord::Turntable::CannotSpecifyShardError)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
34
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm do
|
4
4
|
before(:all) do
|
@@ -27,9 +27,8 @@ describe ActiveRecord::Turntable::Algorithm::RangeBsearchAlgorithm do
|
|
27
27
|
|
28
28
|
context "#calculate with 10000000" do
|
29
29
|
it "raises ActiveRecord::Turntable::CannotSpecifyShardError" do
|
30
|
-
expect { @alg.calculate(
|
30
|
+
expect { @alg.calculate(10_000_000) }.to raise_error(ActiveRecord::Turntable::CannotSpecifyShardError)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
34
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Algorithm do
|
4
4
|
before(:all) do
|
@@ -31,7 +31,7 @@ describe ActiveRecord::Turntable::Algorithm do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "called with 10 returns {\"user_shard_1\" => 10}" do
|
34
|
-
expect(algorithm.calculate_used_shards_with_weight(10)).to eq({"user_shard_1" => 10})
|
34
|
+
expect(algorithm.calculate_used_shards_with_weight(10)).to eq({ "user_shard_1" => 10 })
|
35
35
|
end
|
36
36
|
|
37
37
|
it "called with 65000 returns 2 items" do
|
@@ -39,7 +39,7 @@ describe ActiveRecord::Turntable::Algorithm do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
it "called with 65000 returns {\"user_shard_1\" => 39999, \"user_shard_2\" => 25001}" do
|
42
|
-
expect(algorithm.calculate_used_shards_with_weight(65000)).to eq({"user_shard_1" => 39999, "user_shard_2" => 25001})
|
42
|
+
expect(algorithm.calculate_used_shards_with_weight(65000)).to eq({ "user_shard_1" => 39999, "user_shard_2" => 25001 })
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -70,7 +70,7 @@ describe ActiveRecord::Turntable::Algorithm do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
it "called with 10 returns {\"user_shard_1\" => 10}" do
|
73
|
-
expect(algorithm.calculate_used_shards_with_weight(10)).to eq({"user_shard_1" => 10})
|
73
|
+
expect(algorithm.calculate_used_shards_with_weight(10)).to eq({ "user_shard_1" => 10 })
|
74
74
|
end
|
75
75
|
|
76
76
|
it "called with 65000 returns 2 items" do
|
@@ -78,7 +78,7 @@ describe ActiveRecord::Turntable::Algorithm do
|
|
78
78
|
end
|
79
79
|
|
80
80
|
it "called with 65000 returns {\"user_shard_1\" => 39999, \"user_shard_2\" => 25001}" do
|
81
|
-
expect(algorithm.calculate_used_shards_with_weight(65000)).to eq({"user_shard_1" => 39999, "user_shard_2" => 25001})
|
81
|
+
expect(algorithm.calculate_used_shards_with_weight(65000)).to eq({ "user_shard_1" => 39999, "user_shard_2" => 25001 })
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::ConnectionProxy do
|
4
4
|
before(:all) do
|
@@ -91,7 +91,6 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
91
91
|
expect {
|
92
92
|
@user1.save!
|
93
93
|
}.not_to raise_error
|
94
|
-
|
95
94
|
end
|
96
95
|
|
97
96
|
it "should be saved to user_shard_2 with id = 30000" do
|
@@ -102,7 +101,7 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
102
101
|
end
|
103
102
|
|
104
103
|
it "User.where('id IN (1, 30000)') returns 2 record" do
|
105
|
-
expect(User.where(:
|
104
|
+
expect(User.where(id: [1, 30000]).all.size).to eq(2)
|
106
105
|
end
|
107
106
|
|
108
107
|
it "count should be 2" do
|
@@ -120,11 +119,11 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
120
119
|
truncate_shard
|
121
120
|
@user1 = User.new
|
122
121
|
@user1.id = 1
|
123
|
-
@user1.nickname =
|
122
|
+
@user1.nickname = "user1"
|
124
123
|
@user1.save!
|
125
124
|
@user2 = User.new
|
126
125
|
@user2.id = 30000
|
127
|
-
@user2.nickname =
|
126
|
+
@user2.nickname = "user2"
|
128
127
|
@user2.save!
|
129
128
|
end
|
130
129
|
|
@@ -167,11 +166,11 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
167
166
|
truncate_shard
|
168
167
|
@user1 = User.new
|
169
168
|
@user1.id = 1
|
170
|
-
@user1.nickname =
|
169
|
+
@user1.nickname = "user1"
|
171
170
|
@user1.save!
|
172
171
|
@user2 = User.new
|
173
172
|
@user2.id = 30000
|
174
|
-
@user2.nickname =
|
173
|
+
@user2.nickname = "user2"
|
175
174
|
@user2.save!
|
176
175
|
end
|
177
176
|
|
@@ -185,11 +184,11 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
185
184
|
truncate_shard
|
186
185
|
@user1 = User.new
|
187
186
|
@user1.id = 1
|
188
|
-
@user1.nickname =
|
187
|
+
@user1.nickname = "user1"
|
189
188
|
@user1.save!
|
190
189
|
@user2 = User.new
|
191
190
|
@user2.id = 30000
|
192
|
-
@user2.nickname =
|
191
|
+
@user2.nickname = "user2"
|
193
192
|
@user2.save!
|
194
193
|
end
|
195
194
|
|
@@ -203,15 +202,15 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
203
202
|
truncate_shard
|
204
203
|
@user1 = User.new
|
205
204
|
@user1.id = 1
|
206
|
-
@user1.nickname =
|
205
|
+
@user1.nickname = "user1"
|
207
206
|
@user1.save!
|
208
207
|
@user2 = User.new
|
209
208
|
@user2.id = 30000
|
210
|
-
@user2.nickname =
|
209
|
+
@user2.nickname = "user2"
|
211
210
|
@user2.save!
|
212
211
|
end
|
213
212
|
|
214
|
-
subject { User.exists?(nickname:
|
213
|
+
subject { User.exists?(nickname: "user2") }
|
215
214
|
it { is_expected.to be_truthy }
|
216
215
|
end
|
217
216
|
|
@@ -221,15 +220,15 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
221
220
|
truncate_shard
|
222
221
|
@user1 = User.new
|
223
222
|
@user1.id = 1
|
224
|
-
@user1.nickname =
|
223
|
+
@user1.nickname = "user1"
|
225
224
|
@user1.save!
|
226
225
|
@user2 = User.new
|
227
226
|
@user2.id = 30000
|
228
|
-
@user2.nickname =
|
227
|
+
@user2.nickname = "user2"
|
229
228
|
@user2.save!
|
230
229
|
end
|
231
230
|
|
232
|
-
subject { User.exists?(nickname:
|
231
|
+
subject { User.exists?(nickname: "user999") }
|
233
232
|
it { is_expected.to be_falsey }
|
234
233
|
end
|
235
234
|
|
@@ -239,11 +238,11 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
239
238
|
truncate_shard
|
240
239
|
@user1 = User.new
|
241
240
|
@user1.id = 1
|
242
|
-
@user1.nickname =
|
241
|
+
@user1.nickname = "user1"
|
243
242
|
@user1.save!
|
244
243
|
@user2 = User.new
|
245
244
|
@user2.id = 30000
|
246
|
-
@user2.nickname =
|
245
|
+
@user2.nickname = "user2"
|
247
246
|
@user2.save!
|
248
247
|
end
|
249
248
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Mixer do
|
4
4
|
before(:all) do
|
@@ -85,7 +85,7 @@ describe ActiveRecord::Turntable::Mixer do
|
|
85
85
|
}
|
86
86
|
|
87
87
|
it { is_expected.to be_instance_of Array }
|
88
|
-
it { is_expected.to eq([1,2,3,4,5]) }
|
88
|
+
it { is_expected.to eq([1, 2, 3, 4, 5]) }
|
89
89
|
end
|
90
90
|
|
91
91
|
context "When call find_shard_keys with not determine shardkey condition" do
|
@@ -108,7 +108,5 @@ describe ActiveRecord::Turntable::Mixer do
|
|
108
108
|
it { is_expected.to eq([]) }
|
109
109
|
end
|
110
110
|
end
|
111
|
-
|
112
111
|
end
|
113
|
-
|
114
112
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_support/executor"
|
3
|
+
|
4
|
+
describe ActiveRecord::Turntable::QueryCache do
|
5
|
+
before(:all) do
|
6
|
+
reload_turntable!(File.join(File.dirname(__FILE__), "../../config/turntable.yml"))
|
7
|
+
end
|
8
|
+
|
9
|
+
before do
|
10
|
+
establish_connection_to(:test)
|
11
|
+
truncate_shard
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:mw) {
|
15
|
+
executor = Class.new(ActiveSupport::Executor)
|
16
|
+
ActiveRecord::Turntable::QueryCache.install_executor_hooks executor
|
17
|
+
lambda { |env|
|
18
|
+
executor.wrap {
|
19
|
+
[200, {}, nil]
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
subject { mw.call({}) }
|
24
|
+
|
25
|
+
it "should returns 200 response" do
|
26
|
+
expect(subject.first).to eq(200)
|
27
|
+
end
|
28
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Sequencer::Api do
|
4
4
|
before(:all) do
|
@@ -10,7 +10,7 @@ describe ActiveRecord::Turntable::Sequencer::Api do
|
|
10
10
|
let(:klass) { Class.new }
|
11
11
|
let(:api_host) { "example.example" }
|
12
12
|
let(:api_port) { 80 }
|
13
|
-
let(:options) { {
|
13
|
+
let(:options) { { api_host: api_host, api_port: api_port }.with_indifferent_access }
|
14
14
|
let(:api_response) { 1024 }
|
15
15
|
|
16
16
|
let(:next_sequence_uri) { "http://#{api_host}/sequences/#{sequence_name}/new" }
|
@@ -18,7 +18,7 @@ describe ActiveRecord::Turntable::Sequencer::Api do
|
|
18
18
|
|
19
19
|
describe "#next_sequence_value" do
|
20
20
|
before do
|
21
|
-
stub_request(:get, next_sequence_uri).to_return(:
|
21
|
+
stub_request(:get, next_sequence_uri).to_return(body: api_response.to_s)
|
22
22
|
end
|
23
23
|
|
24
24
|
subject { sequencer.next_sequence_value(sequence_name) }
|
@@ -28,7 +28,7 @@ describe ActiveRecord::Turntable::Sequencer::Api do
|
|
28
28
|
|
29
29
|
describe "#current_sequence_value" do
|
30
30
|
before do
|
31
|
-
stub_request(:get, current_sequence_uri).to_return(:
|
31
|
+
stub_request(:get, current_sequence_uri).to_return(body: api_response.to_s)
|
32
32
|
end
|
33
33
|
|
34
34
|
subject { sequencer.current_sequence_value(sequence_name) }
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActiveRecord::Turntable::Sequencer::Barrage do
|
4
4
|
before(:all) do
|
@@ -7,7 +7,7 @@ describe ActiveRecord::Turntable::Sequencer::Barrage do
|
|
7
7
|
|
8
8
|
let(:sequencer) { ActiveRecord::Turntable::Sequencer::Barrage.new(klass, options) }
|
9
9
|
let(:sequence_name) { "hogefuga" }
|
10
|
-
let(:options) { {
|
10
|
+
let(:options) { { options: { generators: [{ name: "sequence", length: 16 }] } }.with_indifferent_access }
|
11
11
|
let(:klass) { Class.new }
|
12
12
|
|
13
13
|
describe "#next_sequence_value" do
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe "transaction" do
|
4
4
|
before(:all) do
|
@@ -13,7 +13,7 @@ describe "transaction" do
|
|
13
13
|
|
14
14
|
describe "all_cluster_transaction" do
|
15
15
|
let(:all_clusters) { clusters.values }
|
16
|
-
let(:shards) { all_clusters.
|
16
|
+
let(:shards) { all_clusters.flat_map { |c| c.shards.values } }
|
17
17
|
|
18
18
|
it "all shards should begin transaction" do
|
19
19
|
User.all_cluster_transaction {
|