activerecord-turntable 1.1.2 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.travis.yml +3 -5
- data/CHANGELOG.md +70 -0
- data/Guardfile +7 -5
- data/README.md +490 -0
- data/Rakefile +37 -22
- data/activerecord-turntable.gemspec +37 -34
- data/gemfiles/rails4_0.gemfile +6 -0
- data/gemfiles/rails4_1.gemfile +6 -0
- data/lib/active_record/turntable/active_record_ext/abstract_adapter.rb +14 -29
- data/lib/active_record/turntable/active_record_ext/activerecord_import_ext.rb +45 -0
- data/lib/active_record/turntable/active_record_ext/acts_as_archive_extension.rb +21 -0
- data/lib/active_record/turntable/active_record_ext/association.rb +85 -0
- data/lib/active_record/turntable/active_record_ext/association_preloader.rb +37 -0
- data/lib/active_record/turntable/active_record_ext/clever_load.rb +33 -76
- data/lib/active_record/turntable/active_record_ext/connection_handler_extension.rb +31 -0
- data/lib/active_record/turntable/active_record_ext/database_tasks.rb +81 -0
- data/lib/active_record/turntable/active_record_ext/fixtures.rb +54 -42
- data/lib/active_record/turntable/active_record_ext/locking_optimistic.rb +101 -0
- data/lib/active_record/turntable/active_record_ext/log_subscriber.rb +28 -46
- data/lib/active_record/turntable/active_record_ext/migration_proxy.rb +7 -0
- data/lib/active_record/turntable/active_record_ext/persistence.rb +96 -94
- data/lib/active_record/turntable/active_record_ext/relation.rb +31 -0
- data/lib/active_record/turntable/active_record_ext/schema_dumper.rb +18 -28
- data/lib/active_record/turntable/active_record_ext/transactions.rb +9 -3
- data/lib/active_record/turntable/active_record_ext.rb +26 -11
- data/lib/active_record/turntable/algorithm/base.rb +1 -1
- data/lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb +1 -1
- data/lib/active_record/turntable/algorithm.rb +7 -3
- data/lib/active_record/turntable/base.rb +67 -14
- data/lib/active_record/turntable/cluster.rb +46 -2
- data/lib/active_record/turntable/config.rb +1 -1
- data/lib/active_record/turntable/connection_proxy/mixable.rb +7 -29
- data/lib/active_record/turntable/connection_proxy.rb +61 -72
- data/lib/active_record/turntable/error.rb +5 -6
- data/lib/active_record/turntable/helpers.rb +5 -1
- data/lib/active_record/turntable/migration.rb +9 -49
- data/lib/active_record/turntable/mixer/fader/calculate_shards_sum_result.rb +13 -2
- data/lib/active_record/turntable/mixer/fader/select_shards_merge_result.rb +17 -6
- data/lib/active_record/turntable/mixer/fader/specified_shard.rb +3 -1
- data/lib/active_record/turntable/mixer/fader.rb +12 -10
- data/lib/active_record/turntable/mixer.rb +59 -29
- data/lib/active_record/turntable/plugin.rb +6 -0
- data/lib/active_record/turntable/pool_proxy.rb +12 -19
- data/lib/active_record/turntable/rack/query_cache.rb +20 -23
- data/lib/active_record/turntable/rack.rb +4 -2
- data/lib/active_record/turntable/railtie.rb +4 -3
- data/lib/active_record/turntable/railties/databases.rake +81 -122
- data/lib/active_record/turntable/seq_shard.rb +1 -1
- data/lib/active_record/turntable/sequencer/api.rb +1 -1
- data/lib/active_record/turntable/sequencer/barrage.rb +28 -0
- data/lib/active_record/turntable/sequencer.rb +27 -9
- data/lib/active_record/turntable/shard.rb +2 -2
- data/lib/active_record/turntable/sql_tree_patch.rb +1 -1
- data/lib/active_record/turntable/version.rb +1 -1
- data/lib/active_record/turntable.rb +26 -16
- data/lib/generators/templates/turntable.yml +9 -7
- data/spec/active_record/turntable/active_record_ext/association_preloader_spec.rb +78 -0
- data/spec/active_record/turntable/active_record_ext/association_spec.rb +72 -0
- data/spec/active_record/turntable/active_record_ext/clever_load_spec.rb +25 -46
- data/spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb +28 -0
- data/spec/active_record/turntable/active_record_ext/persistence_spec.rb +46 -25
- data/spec/active_record/turntable/algorithm/range_algorithm_spec.rb +4 -4
- data/spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb +35 -0
- data/spec/active_record/turntable/algorithm_spec.rb +28 -12
- data/spec/active_record/turntable/base_spec.rb +1 -1
- data/spec/active_record/turntable/cluster_spec.rb +27 -5
- data/spec/active_record/turntable/config_spec.rb +2 -2
- data/spec/active_record/turntable/connection_proxy_spec.rb +112 -45
- data/spec/active_record/turntable/finder_spec.rb +24 -11
- data/spec/active_record/turntable/mixer_spec.rb +21 -21
- data/spec/active_record/turntable/rack/query_cache_spec.rb +19 -0
- data/spec/active_record/turntable/sequencer/api_spec.rb +38 -0
- data/spec/active_record/turntable/sequencer/barrage_spec.rb +22 -0
- data/spec/active_record/turntable/sequencer/mysql_spec.rb +22 -0
- data/spec/active_record/turntable/shard_spec.rb +1 -1
- data/spec/active_record/turntable/transaction_spec.rb +35 -0
- data/spec/active_record/turntable_spec.rb +4 -4
- data/spec/config/database.yml +24 -34
- data/spec/config/turntable.yml +18 -1
- data/spec/fabricators/turntable_fabricator.rb +0 -2
- data/spec/models/card.rb +3 -0
- data/spec/models/cards_user.rb +10 -0
- data/spec/models/cards_users_histories.rb +7 -0
- data/spec/models/events_users_history.rb +7 -0
- data/spec/models/user.rb +7 -0
- data/spec/models/user_status.rb +6 -0
- data/spec/spec_helper.rb +10 -4
- data/spec/support/matchers/be_saved_to.rb +6 -0
- data/spec/support/turntable_helper.rb +29 -0
- metadata +124 -74
- data/README.rdoc +0 -294
- data/gemfiles/rails3_0.gemfile +0 -7
- data/gemfiles/rails3_1.gemfile +0 -6
- data/gemfiles/rails3_2.gemfile +0 -6
- data/lib/active_record/turntable/compatible.rb +0 -19
- data/sample_app/.gitignore +0 -16
- data/sample_app/Gemfile +0 -41
- data/sample_app/README.rdoc +0 -261
- data/sample_app/Rakefile +0 -7
- data/sample_app/app/assets/images/rails.png +0 -0
- data/sample_app/app/assets/javascripts/application.js +0 -15
- data/sample_app/app/assets/stylesheets/application.css +0 -13
- data/sample_app/app/controllers/application_controller.rb +0 -3
- data/sample_app/app/helpers/application_helper.rb +0 -2
- data/sample_app/app/mailers/.gitkeep +0 -0
- data/sample_app/app/models/.gitkeep +0 -0
- data/sample_app/app/models/user.rb +0 -4
- data/sample_app/app/views/layouts/application.html.erb +0 -14
- data/sample_app/config/application.rb +0 -65
- data/sample_app/config/boot.rb +0 -6
- data/sample_app/config/database.yml +0 -70
- data/sample_app/config/environment.rb +0 -5
- data/sample_app/config/environments/development.rb +0 -37
- data/sample_app/config/environments/production.rb +0 -67
- data/sample_app/config/environments/test.rb +0 -37
- data/sample_app/config/initializers/backtrace_silencers.rb +0 -7
- data/sample_app/config/initializers/inflections.rb +0 -15
- data/sample_app/config/initializers/mime_types.rb +0 -5
- data/sample_app/config/initializers/secret_token.rb +0 -7
- data/sample_app/config/initializers/session_store.rb +0 -8
- data/sample_app/config/initializers/wrap_parameters.rb +0 -14
- data/sample_app/config/locales/en.yml +0 -5
- data/sample_app/config/routes.rb +0 -58
- data/sample_app/config/turntable.yml +0 -64
- data/sample_app/config.ru +0 -4
- data/sample_app/db/migrate/20120316073058_create_users.rb +0 -11
- data/sample_app/db/seeds.rb +0 -7
- data/sample_app/lib/assets/.gitkeep +0 -0
- data/sample_app/lib/tasks/.gitkeep +0 -0
- data/sample_app/log/.gitkeep +0 -0
- data/sample_app/public/404.html +0 -26
- data/sample_app/public/422.html +0 -26
- data/sample_app/public/500.html +0 -25
- data/sample_app/public/favicon.ico +0 -0
- data/sample_app/public/index.html +0 -241
- data/sample_app/public/robots.txt +0 -5
- data/sample_app/script/rails +0 -6
- 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/test_models.rb +0 -27
- data/spec/turntable_helper.rb +0 -29
@@ -7,59 +7,46 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
7
7
|
|
8
8
|
context "When initialized" do
|
9
9
|
before do
|
10
|
-
establish_connection_to(
|
10
|
+
establish_connection_to(:test)
|
11
11
|
truncate_shard
|
12
12
|
end
|
13
|
+
|
13
14
|
let(:cluster) { ActiveRecord::Turntable::Cluster.new(User, ActiveRecord::Base.turntable_config[:clusters][:user_cluster]) }
|
14
15
|
subject { ActiveRecord::Turntable::ConnectionProxy.new(cluster) }
|
15
|
-
its(:master_connection) { should == ActiveRecord::Base.connection }
|
16
|
-
end
|
17
16
|
|
18
|
-
|
19
|
-
it "should proxies columns" do
|
20
|
-
pending "spec not implemented yet"
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should proxies columns_hash" do
|
24
|
-
pending "spec not implemented yet"
|
25
|
-
end
|
17
|
+
its(:master_connection) { is_expected.to eql(ActiveRecord::Base.connection) }
|
26
18
|
end
|
27
19
|
|
28
20
|
context "User insert with id" do
|
29
21
|
before do
|
30
|
-
establish_connection_to(
|
22
|
+
establish_connection_to(:test)
|
31
23
|
truncate_shard
|
32
|
-
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
33
24
|
end
|
34
25
|
|
35
26
|
it "should be saved to user_shard_1 with id = 1" do
|
36
27
|
user = User.new
|
37
28
|
user.id = 1
|
38
|
-
|
39
|
-
lambda {
|
29
|
+
expect {
|
40
30
|
user.save!
|
41
|
-
}.
|
31
|
+
}.not_to raise_error
|
42
32
|
end
|
43
33
|
|
44
34
|
it "should be saved to user_shard_2 with id = 30000" do
|
45
35
|
user = User.new
|
46
36
|
user.id = 30000
|
47
|
-
|
48
|
-
lambda {
|
37
|
+
expect {
|
49
38
|
user.save!
|
50
|
-
}.
|
39
|
+
}.not_to raise_error
|
51
40
|
end
|
52
41
|
|
53
42
|
it "should be saved to user_shard_2 with id = 30000 with SQL injection attack" do
|
54
43
|
user = User.new
|
55
44
|
user.id = 30000
|
56
45
|
user.nickname = "hogehgoge'00"
|
57
|
-
|
58
|
-
lambda {
|
46
|
+
expect {
|
59
47
|
user.save!
|
60
|
-
}.
|
48
|
+
}.not_to raise_error
|
61
49
|
user.reload
|
62
|
-
|
63
50
|
end
|
64
51
|
|
65
52
|
it "should should be saved the same string when includes escaped string" do
|
@@ -68,13 +55,28 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
68
55
|
user.nickname = "hoge@\n@\\@@\\nhoge\\\nhoge\\n"
|
69
56
|
user.save!
|
70
57
|
user.reload
|
71
|
-
user.nickname.
|
58
|
+
expect(user.nickname).to eq("hoge@\n@\\@@\\nhoge\\\nhoge\\n")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "When have no users" do
|
63
|
+
before do
|
64
|
+
establish_connection_to(:test)
|
65
|
+
truncate_shard
|
66
|
+
end
|
67
|
+
|
68
|
+
it "User.#count should be zero" do
|
69
|
+
expect(User.count).to be_zero
|
70
|
+
end
|
71
|
+
|
72
|
+
it "User.all should have no item" do
|
73
|
+
expect(User.all.to_a).to have(0).items
|
72
74
|
end
|
73
75
|
end
|
74
76
|
|
75
77
|
context "When have 2 Users in different shards" do
|
76
78
|
before do
|
77
|
-
establish_connection_to(
|
79
|
+
establish_connection_to(:test)
|
78
80
|
truncate_shard
|
79
81
|
@user1 = User.new
|
80
82
|
@user1.id = 1
|
@@ -86,35 +88,35 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
86
88
|
|
87
89
|
it "should be saved to user_shard_1 with id = 1" do
|
88
90
|
@user1.nickname = "foobar"
|
89
|
-
|
91
|
+
expect {
|
90
92
|
@user1.save!
|
91
|
-
}.
|
93
|
+
}.not_to raise_error
|
92
94
|
|
93
95
|
end
|
94
96
|
|
95
97
|
it "should be saved to user_shard_2 with id = 30000" do
|
96
98
|
@user2.nickname = "hogehoge"
|
97
|
-
|
99
|
+
expect {
|
98
100
|
@user2.save!
|
99
|
-
}.
|
101
|
+
}.not_to raise_error
|
100
102
|
end
|
101
103
|
|
102
104
|
it "User.where('id IN (1, 30000)') returns 2 record" do
|
103
|
-
User.where(:id => [1, 30000]).all.size.
|
105
|
+
expect(User.where(:id => [1, 30000]).all.size).to eq(2)
|
104
106
|
end
|
105
107
|
|
106
108
|
it "count should be 2" do
|
107
|
-
User.count.
|
109
|
+
expect(User.count).to eq(2)
|
108
110
|
end
|
109
111
|
|
110
112
|
it "User.all returns 2 User object" do
|
111
|
-
User.all.size.
|
113
|
+
expect(User.all.size).to eq(2)
|
112
114
|
end
|
113
115
|
end
|
114
116
|
|
115
117
|
context "When calling with_all" do
|
116
118
|
before do
|
117
|
-
establish_connection_to(
|
119
|
+
establish_connection_to(:test)
|
118
120
|
truncate_shard
|
119
121
|
@user1 = User.new
|
120
122
|
@user1.id = 1
|
@@ -132,12 +134,12 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
132
134
|
User.count
|
133
135
|
end
|
134
136
|
}
|
135
|
-
it {
|
137
|
+
it { is_expected.to have(3).items }
|
136
138
|
|
137
139
|
it "returns User.count of each shards" do
|
138
|
-
subject[0].
|
139
|
-
subject[1].
|
140
|
-
subject[2].
|
140
|
+
expect(subject[0]).to eq(1)
|
141
|
+
expect(subject[1]).to eq(1)
|
142
|
+
expect(subject[2]).to eq(0)
|
141
143
|
end
|
142
144
|
end
|
143
145
|
|
@@ -148,11 +150,11 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
148
150
|
raise "Unko Error"
|
149
151
|
end
|
150
152
|
}
|
151
|
-
it {
|
152
|
-
it {
|
153
|
+
it { expect { subject }.not_to raise_error }
|
154
|
+
it { is_expected.to have(3).items }
|
153
155
|
it "collection " do
|
154
156
|
subject.each do |s|
|
155
|
-
s.
|
157
|
+
expect(s).to be_instance_of(RuntimeError)
|
156
158
|
end
|
157
159
|
end
|
158
160
|
end
|
@@ -160,27 +162,92 @@ describe ActiveRecord::Turntable::ConnectionProxy do
|
|
160
162
|
end
|
161
163
|
|
162
164
|
context "When calling exists? with shard_key" do
|
165
|
+
before do
|
166
|
+
establish_connection_to(:test)
|
167
|
+
truncate_shard
|
168
|
+
@user1 = User.new
|
169
|
+
@user1.id = 1
|
170
|
+
@user1.nickname = 'user1'
|
171
|
+
@user1.save!
|
172
|
+
@user2 = User.new
|
173
|
+
@user2.id = 30000
|
174
|
+
@user2.nickname = 'user2'
|
175
|
+
@user2.save!
|
176
|
+
end
|
177
|
+
|
163
178
|
subject { User.exists?(id: 1) }
|
164
|
-
it {
|
179
|
+
it { is_expected.to be_truthy }
|
165
180
|
end
|
166
181
|
|
167
182
|
context "When calling exists? with non-existed shard_key" do
|
183
|
+
before do
|
184
|
+
establish_connection_to(:test)
|
185
|
+
truncate_shard
|
186
|
+
@user1 = User.new
|
187
|
+
@user1.id = 1
|
188
|
+
@user1.nickname = 'user1'
|
189
|
+
@user1.save!
|
190
|
+
@user2 = User.new
|
191
|
+
@user2.id = 30000
|
192
|
+
@user2.nickname = 'user2'
|
193
|
+
@user2.save!
|
194
|
+
end
|
195
|
+
|
168
196
|
subject { User.exists?(id: 3) }
|
169
|
-
it {
|
197
|
+
it { is_expected.to be_falsey }
|
170
198
|
end
|
171
199
|
|
172
200
|
context "When calling exists? with non shard_key" do
|
201
|
+
before do
|
202
|
+
establish_connection_to(:test)
|
203
|
+
truncate_shard
|
204
|
+
@user1 = User.new
|
205
|
+
@user1.id = 1
|
206
|
+
@user1.nickname = 'user1'
|
207
|
+
@user1.save!
|
208
|
+
@user2 = User.new
|
209
|
+
@user2.id = 30000
|
210
|
+
@user2.nickname = 'user2'
|
211
|
+
@user2.save!
|
212
|
+
end
|
213
|
+
|
173
214
|
subject { User.exists?(nickname: 'user2') }
|
174
|
-
it {
|
215
|
+
it { is_expected.to be_truthy }
|
175
216
|
end
|
176
217
|
|
177
218
|
context "When calling exists? with non-existed non shard_key" do
|
219
|
+
before do
|
220
|
+
establish_connection_to(:test)
|
221
|
+
truncate_shard
|
222
|
+
@user1 = User.new
|
223
|
+
@user1.id = 1
|
224
|
+
@user1.nickname = 'user1'
|
225
|
+
@user1.save!
|
226
|
+
@user2 = User.new
|
227
|
+
@user2.id = 30000
|
228
|
+
@user2.nickname = 'user2'
|
229
|
+
@user2.save!
|
230
|
+
end
|
231
|
+
|
178
232
|
subject { User.exists?(nickname: 'user999') }
|
179
|
-
it {
|
233
|
+
it { is_expected.to be_falsey }
|
180
234
|
end
|
181
235
|
|
182
236
|
context "#table_exists?" do
|
237
|
+
before do
|
238
|
+
establish_connection_to(:test)
|
239
|
+
truncate_shard
|
240
|
+
@user1 = User.new
|
241
|
+
@user1.id = 1
|
242
|
+
@user1.nickname = 'user1'
|
243
|
+
@user1.save!
|
244
|
+
@user2 = User.new
|
245
|
+
@user2.id = 30000
|
246
|
+
@user2.nickname = 'user2'
|
247
|
+
@user2.save!
|
248
|
+
end
|
249
|
+
|
183
250
|
subject { User.connection.table_exists?(:users) }
|
184
|
-
it {
|
251
|
+
it { is_expected.to be_truthy }
|
185
252
|
end
|
186
253
|
end
|
@@ -7,21 +7,34 @@ describe "ActiveRecord::FinderMethods" do
|
|
7
7
|
|
8
8
|
context "User insert with id" do
|
9
9
|
before do
|
10
|
-
establish_connection_to(
|
10
|
+
establish_connection_to(:test)
|
11
11
|
truncate_shard
|
12
|
-
|
13
|
-
@user = User.new
|
14
|
-
@user.id = 1
|
15
|
-
@user.save
|
12
|
+
user
|
16
13
|
end
|
17
14
|
|
18
|
-
|
19
|
-
User.
|
20
|
-
|
15
|
+
let(:user) {
|
16
|
+
u = User.new
|
17
|
+
u.id = 1
|
18
|
+
u.save
|
19
|
+
u
|
20
|
+
}
|
21
|
+
|
22
|
+
describe "User#find" do
|
23
|
+
context "With existing users.id" do
|
24
|
+
subject { User.find(1) }
|
25
|
+
|
26
|
+
it "#find should be returns user" do
|
27
|
+
is_expected.to eq(user)
|
28
|
+
end
|
29
|
+
end
|
21
30
|
|
22
|
-
|
23
|
-
|
31
|
+
context "With users.id not existing" do
|
32
|
+
subject { User.find(2) }
|
33
|
+
|
34
|
+
it "#find should raise error" do
|
35
|
+
expect { subject }.to raise_error
|
36
|
+
end
|
37
|
+
end
|
24
38
|
end
|
25
39
|
end
|
26
40
|
end
|
27
|
-
|
@@ -6,7 +6,7 @@ describe ActiveRecord::Turntable::Mixer do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
before do
|
9
|
-
establish_connection_to(
|
9
|
+
establish_connection_to(:test)
|
10
10
|
truncate_shard
|
11
11
|
@cluster = ActiveRecord::Turntable::Cluster.new(User, ActiveRecord::Base.turntable_config[:clusters][:user_cluster])
|
12
12
|
@connection_proxy = ActiveRecord::Turntable::ConnectionProxy.new(@cluster)
|
@@ -24,9 +24,9 @@ describe ActiveRecord::Turntable::Mixer do
|
|
24
24
|
@mixer.send(:divide_insert_values, tree, "id")
|
25
25
|
}
|
26
26
|
|
27
|
-
it {
|
28
|
-
it {
|
29
|
-
it { [1].
|
27
|
+
it { is_expected.to be_instance_of(Hash) }
|
28
|
+
it { is_expected.to have_key(1) }
|
29
|
+
it { expect([1]).to have(1).item }
|
30
30
|
end
|
31
31
|
|
32
32
|
context "When call divide_insert_values with Bulk INSERT and shard_key 'id'" do
|
@@ -35,11 +35,11 @@ describe ActiveRecord::Turntable::Mixer do
|
|
35
35
|
@mixer.send(:divide_insert_values, tree, "id")
|
36
36
|
}
|
37
37
|
|
38
|
-
it {
|
39
|
-
it {
|
40
|
-
it { [1].
|
41
|
-
it { [2].
|
42
|
-
it { [3].
|
38
|
+
it { is_expected.to be_instance_of(Hash) }
|
39
|
+
it { is_expected.to have_key(3) }
|
40
|
+
it { expect([1]).to have(1).item }
|
41
|
+
it { expect([2]).to have(1).item }
|
42
|
+
it { expect([3]).to have(1).item }
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -50,8 +50,8 @@ describe ActiveRecord::Turntable::Mixer do
|
|
50
50
|
@mixer.find_shard_keys(tree.where, "users", "id")
|
51
51
|
}
|
52
52
|
|
53
|
-
it {
|
54
|
-
it {
|
53
|
+
it { is_expected.to be_instance_of Array }
|
54
|
+
it { is_expected.to eq([1]) }
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -62,8 +62,8 @@ describe ActiveRecord::Turntable::Mixer do
|
|
62
62
|
@mixer.find_shard_keys(tree.where, "users", "id")
|
63
63
|
}
|
64
64
|
|
65
|
-
it {
|
66
|
-
it {
|
65
|
+
it { is_expected.to be_instance_of Array }
|
66
|
+
it { is_expected.to eq([1]) }
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -74,8 +74,8 @@ describe ActiveRecord::Turntable::Mixer do
|
|
74
74
|
@mixer.find_shard_keys(tree.where, "users", "id")
|
75
75
|
}
|
76
76
|
|
77
|
-
it {
|
78
|
-
it {
|
77
|
+
it { is_expected.to be_instance_of Array }
|
78
|
+
it { is_expected.to eq([1]) }
|
79
79
|
end
|
80
80
|
|
81
81
|
context "When call find_shard_keys with shardkey collection condition" do
|
@@ -84,8 +84,8 @@ describe ActiveRecord::Turntable::Mixer do
|
|
84
84
|
@mixer.find_shard_keys(tree.where, "users", "id")
|
85
85
|
}
|
86
86
|
|
87
|
-
it {
|
88
|
-
it {
|
87
|
+
it { is_expected.to be_instance_of Array }
|
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
|
@@ -94,8 +94,8 @@ describe ActiveRecord::Turntable::Mixer do
|
|
94
94
|
@mixer.find_shard_keys(tree.where, "users", "id")
|
95
95
|
}
|
96
96
|
|
97
|
-
it {
|
98
|
-
it {
|
97
|
+
it { is_expected.to be_instance_of Array }
|
98
|
+
it { is_expected.to eq([]) }
|
99
99
|
end
|
100
100
|
|
101
101
|
context "When call find_shard_keys with except table definition SQL" do
|
@@ -104,8 +104,8 @@ describe ActiveRecord::Turntable::Mixer do
|
|
104
104
|
@mixer.find_shard_keys(tree.where, "users", "id")
|
105
105
|
}
|
106
106
|
|
107
|
-
it {
|
108
|
-
it {
|
107
|
+
it { is_expected.to be_instance_of Array }
|
108
|
+
it { is_expected.to eq([]) }
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Turntable::Rack::QueryCache do
|
4
|
+
before(:all) do
|
5
|
+
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
6
|
+
end
|
7
|
+
|
8
|
+
before do
|
9
|
+
establish_connection_to(:test)
|
10
|
+
truncate_shard
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:mw) { ActiveRecord::Turntable::Rack::QueryCache.new lambda {|env| [200, {}]} }
|
14
|
+
subject { mw.call({}) }
|
15
|
+
|
16
|
+
it "should returns 200 response" do
|
17
|
+
expect(subject.first).to eq(200)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Turntable::Sequencer::Api do
|
4
|
+
before(:all) do
|
5
|
+
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:sequencer) { ActiveRecord::Turntable::Sequencer::Api.new(klass, options) }
|
9
|
+
let(:sequence_name) { "hogefuga" }
|
10
|
+
let(:klass) { Class.new }
|
11
|
+
let(:api_host) { "example.example" }
|
12
|
+
let(:api_port) { 80 }
|
13
|
+
let(:options) { { "api_host" => api_host, "api_port" => api_port } }
|
14
|
+
let(:api_response) { 1024 }
|
15
|
+
|
16
|
+
let(:next_sequence_uri) { "http://#{api_host}/sequences/#{sequence_name}/new" }
|
17
|
+
let(:current_sequence_uri) { "http://#{api_host}/sequences/#{sequence_name}" }
|
18
|
+
|
19
|
+
describe "#next_sequence_value" do
|
20
|
+
before do
|
21
|
+
stub_request(:get, next_sequence_uri).to_return(:body => api_response.to_s)
|
22
|
+
end
|
23
|
+
|
24
|
+
subject { sequencer.next_sequence_value(sequence_name) }
|
25
|
+
it { is_expected.to be_kind_of(Integer) }
|
26
|
+
it { is_expected.to eq api_response }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#current_sequence_value" do
|
30
|
+
before do
|
31
|
+
stub_request(:get, current_sequence_uri).to_return(:body => api_response.to_s)
|
32
|
+
end
|
33
|
+
|
34
|
+
subject { sequencer.current_sequence_value(sequence_name) }
|
35
|
+
it { is_expected.to be_kind_of(Integer) }
|
36
|
+
it { is_expected.to eq api_response }
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Turntable::Sequencer::Barrage do
|
4
|
+
before(:all) do
|
5
|
+
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:sequencer) { ActiveRecord::Turntable::Sequencer::Barrage.new(klass, options) }
|
9
|
+
let(:sequence_name) { "hogefuga" }
|
10
|
+
let(:options) { { "options" => { "generators" => [ {"name" => "sequence", "length" => 16} ] } } }
|
11
|
+
let(:klass) { Class.new }
|
12
|
+
|
13
|
+
describe "#next_sequence_value" do
|
14
|
+
subject { sequencer.next_sequence_value("hogefuga") }
|
15
|
+
it { is_expected.to be_kind_of(Integer) }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#current_sequence_value" do
|
19
|
+
subject { sequencer.current_sequence_value("hogefuga") }
|
20
|
+
it { is_expected.to be_kind_of(Integer) }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Turntable::Sequencer::Mysql do
|
4
|
+
before(:all) do
|
5
|
+
reload_turntable!(File.join(File.dirname(__FILE__), "../../../config/turntable.yml"))
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:sequencer) { ActiveRecord::Turntable::Sequencer::Mysql.new(klass, options) }
|
9
|
+
let(:sequence_name) { "users_id_seq" }
|
10
|
+
let(:options) { {} }
|
11
|
+
let(:klass) { User }
|
12
|
+
|
13
|
+
describe "#next_sequence_value" do
|
14
|
+
subject { sequencer.next_sequence_value(sequence_name) }
|
15
|
+
it { is_expected.to be_kind_of(Integer) }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#current_sequence_value" do
|
19
|
+
subject { sequencer.current_sequence_value(sequence_name) }
|
20
|
+
it { is_expected.to be_kind_of(Integer) }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "transaction" do
|
4
|
+
before(:all) do
|
5
|
+
reload_turntable!(File.join(File.dirname(__FILE__), "../../config/turntable.yml"))
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
establish_connection_to(:test)
|
10
|
+
truncate_shard
|
11
|
+
end
|
12
|
+
let(:clusters) { ActiveRecord::Base.turntable_clusters }
|
13
|
+
|
14
|
+
describe "all_cluster_transaction" do
|
15
|
+
let(:all_clusters) { clusters.values.map { |v| v.values.first } }
|
16
|
+
let(:shards) { all_clusters.map { |c| c.shards.values }.flatten(1) }
|
17
|
+
|
18
|
+
it "all shards should begin transaction" do
|
19
|
+
User.all_cluster_transaction {
|
20
|
+
expect(shards.map(&:connection).map(&:open_transactions)).to all(be == 1)
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "cluster_transaction" do
|
26
|
+
let(:cluster) { clusters[:user_cluster].values.first }
|
27
|
+
let(:shards) { cluster.shards.values }
|
28
|
+
|
29
|
+
it "all shards in the cluster should begin transaction" do
|
30
|
+
User.user_cluster_transaction {
|
31
|
+
expect(shards.map(&:connection).map(&:open_transactions)).to all(be == 1)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -8,9 +8,9 @@ describe ActiveRecord::Turntable do
|
|
8
8
|
context "#config_file" do
|
9
9
|
it "should return Rails.root/config/turntable.yml default" do
|
10
10
|
unless defined?(::Rails); class ::Rails; end; end
|
11
|
-
|
11
|
+
allow(Rails).to receive(:root) { "/path/to/rails_root" }
|
12
12
|
ActiveRecord::Base.turntable_config_file = nil
|
13
|
-
ActiveRecord::Base.turntable_config_file.
|
13
|
+
expect(ActiveRecord::Base.turntable_config_file).to eq("/path/to/rails_root/config/turntable.yml")
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -19,12 +19,12 @@ describe ActiveRecord::Turntable do
|
|
19
19
|
ActiveRecord::Base.send(:include, ActiveRecord::Turntable)
|
20
20
|
filename = "hogefuga"
|
21
21
|
ActiveRecord::Base.turntable_config_file = filename
|
22
|
-
ActiveRecord::Base.turntable_config_file.
|
22
|
+
expect(ActiveRecord::Base.turntable_config_file).to eq(filename)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
context "#config" do
|
27
27
|
subject { ActiveRecord::Base.turntable_config }
|
28
|
-
it {
|
28
|
+
it { is_expected.to be_instance_of(ActiveRecord::Turntable::Config) }
|
29
29
|
end
|
30
30
|
end
|
data/spec/config/database.yml
CHANGED
@@ -1,45 +1,35 @@
|
|
1
|
-
|
1
|
+
default: &default
|
2
2
|
adapter: mysql2
|
3
3
|
username: root
|
4
4
|
password:
|
5
5
|
host: localhost
|
6
6
|
port: 3306
|
7
7
|
encoding: utf8
|
8
|
-
database:
|
8
|
+
database: turntable_test
|
9
|
+
|
10
|
+
test:
|
11
|
+
<<: *default
|
12
|
+
database: turntable_test
|
9
13
|
seq:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
password:
|
14
|
-
host: localhost
|
15
|
-
port: 3306
|
16
|
-
encoding: utf8
|
17
|
-
database: turntable_seq_test
|
18
|
-
# seq_type: api
|
19
|
-
api_host: localhost
|
20
|
-
api_port: 9292
|
14
|
+
user_seq:
|
15
|
+
<<: *default
|
16
|
+
database: turntable_user_seq_test
|
21
17
|
shards:
|
22
18
|
user_shard_1:
|
23
|
-
|
24
|
-
|
25
|
-
password:
|
26
|
-
host: localhost
|
27
|
-
port: 3306
|
28
|
-
encoding: utf8
|
29
|
-
database: turntable1_test
|
19
|
+
<<: *default
|
20
|
+
database: turntable_user_shard_1_test
|
30
21
|
user_shard_2:
|
31
|
-
|
32
|
-
|
33
|
-
password:
|
34
|
-
host: localhost
|
35
|
-
port: 3306
|
36
|
-
encoding: utf8
|
37
|
-
database: turntable2_test
|
22
|
+
<<: *default
|
23
|
+
database: turntable_user_shard_2_test
|
38
24
|
user_shard_3:
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
25
|
+
<<: *default
|
26
|
+
database: turntable_user_shard_3_test
|
27
|
+
user_shard_4:
|
28
|
+
<<: *default
|
29
|
+
database: turntable_user_shard_4_test
|
30
|
+
user_shard_5:
|
31
|
+
<<: *default
|
32
|
+
database: turntable_user_shard_5_test
|
33
|
+
user_shard_6:
|
34
|
+
<<: *default
|
35
|
+
database: turntable_user_shard_6_test
|