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.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.travis.yml +3 -5
  4. data/CHANGELOG.md +70 -0
  5. data/Guardfile +7 -5
  6. data/README.md +490 -0
  7. data/Rakefile +37 -22
  8. data/activerecord-turntable.gemspec +37 -34
  9. data/gemfiles/rails4_0.gemfile +6 -0
  10. data/gemfiles/rails4_1.gemfile +6 -0
  11. data/lib/active_record/turntable/active_record_ext/abstract_adapter.rb +14 -29
  12. data/lib/active_record/turntable/active_record_ext/activerecord_import_ext.rb +45 -0
  13. data/lib/active_record/turntable/active_record_ext/acts_as_archive_extension.rb +21 -0
  14. data/lib/active_record/turntable/active_record_ext/association.rb +85 -0
  15. data/lib/active_record/turntable/active_record_ext/association_preloader.rb +37 -0
  16. data/lib/active_record/turntable/active_record_ext/clever_load.rb +33 -76
  17. data/lib/active_record/turntable/active_record_ext/connection_handler_extension.rb +31 -0
  18. data/lib/active_record/turntable/active_record_ext/database_tasks.rb +81 -0
  19. data/lib/active_record/turntable/active_record_ext/fixtures.rb +54 -42
  20. data/lib/active_record/turntable/active_record_ext/locking_optimistic.rb +101 -0
  21. data/lib/active_record/turntable/active_record_ext/log_subscriber.rb +28 -46
  22. data/lib/active_record/turntable/active_record_ext/migration_proxy.rb +7 -0
  23. data/lib/active_record/turntable/active_record_ext/persistence.rb +96 -94
  24. data/lib/active_record/turntable/active_record_ext/relation.rb +31 -0
  25. data/lib/active_record/turntable/active_record_ext/schema_dumper.rb +18 -28
  26. data/lib/active_record/turntable/active_record_ext/transactions.rb +9 -3
  27. data/lib/active_record/turntable/active_record_ext.rb +26 -11
  28. data/lib/active_record/turntable/algorithm/base.rb +1 -1
  29. data/lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb +1 -1
  30. data/lib/active_record/turntable/algorithm.rb +7 -3
  31. data/lib/active_record/turntable/base.rb +67 -14
  32. data/lib/active_record/turntable/cluster.rb +46 -2
  33. data/lib/active_record/turntable/config.rb +1 -1
  34. data/lib/active_record/turntable/connection_proxy/mixable.rb +7 -29
  35. data/lib/active_record/turntable/connection_proxy.rb +61 -72
  36. data/lib/active_record/turntable/error.rb +5 -6
  37. data/lib/active_record/turntable/helpers.rb +5 -1
  38. data/lib/active_record/turntable/migration.rb +9 -49
  39. data/lib/active_record/turntable/mixer/fader/calculate_shards_sum_result.rb +13 -2
  40. data/lib/active_record/turntable/mixer/fader/select_shards_merge_result.rb +17 -6
  41. data/lib/active_record/turntable/mixer/fader/specified_shard.rb +3 -1
  42. data/lib/active_record/turntable/mixer/fader.rb +12 -10
  43. data/lib/active_record/turntable/mixer.rb +59 -29
  44. data/lib/active_record/turntable/plugin.rb +6 -0
  45. data/lib/active_record/turntable/pool_proxy.rb +12 -19
  46. data/lib/active_record/turntable/rack/query_cache.rb +20 -23
  47. data/lib/active_record/turntable/rack.rb +4 -2
  48. data/lib/active_record/turntable/railtie.rb +4 -3
  49. data/lib/active_record/turntable/railties/databases.rake +81 -122
  50. data/lib/active_record/turntable/seq_shard.rb +1 -1
  51. data/lib/active_record/turntable/sequencer/api.rb +1 -1
  52. data/lib/active_record/turntable/sequencer/barrage.rb +28 -0
  53. data/lib/active_record/turntable/sequencer.rb +27 -9
  54. data/lib/active_record/turntable/shard.rb +2 -2
  55. data/lib/active_record/turntable/sql_tree_patch.rb +1 -1
  56. data/lib/active_record/turntable/version.rb +1 -1
  57. data/lib/active_record/turntable.rb +26 -16
  58. data/lib/generators/templates/turntable.yml +9 -7
  59. data/spec/active_record/turntable/active_record_ext/association_preloader_spec.rb +78 -0
  60. data/spec/active_record/turntable/active_record_ext/association_spec.rb +72 -0
  61. data/spec/active_record/turntable/active_record_ext/clever_load_spec.rb +25 -46
  62. data/spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb +28 -0
  63. data/spec/active_record/turntable/active_record_ext/persistence_spec.rb +46 -25
  64. data/spec/active_record/turntable/algorithm/range_algorithm_spec.rb +4 -4
  65. data/spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb +35 -0
  66. data/spec/active_record/turntable/algorithm_spec.rb +28 -12
  67. data/spec/active_record/turntable/base_spec.rb +1 -1
  68. data/spec/active_record/turntable/cluster_spec.rb +27 -5
  69. data/spec/active_record/turntable/config_spec.rb +2 -2
  70. data/spec/active_record/turntable/connection_proxy_spec.rb +112 -45
  71. data/spec/active_record/turntable/finder_spec.rb +24 -11
  72. data/spec/active_record/turntable/mixer_spec.rb +21 -21
  73. data/spec/active_record/turntable/rack/query_cache_spec.rb +19 -0
  74. data/spec/active_record/turntable/sequencer/api_spec.rb +38 -0
  75. data/spec/active_record/turntable/sequencer/barrage_spec.rb +22 -0
  76. data/spec/active_record/turntable/sequencer/mysql_spec.rb +22 -0
  77. data/spec/active_record/turntable/shard_spec.rb +1 -1
  78. data/spec/active_record/turntable/transaction_spec.rb +35 -0
  79. data/spec/active_record/turntable_spec.rb +4 -4
  80. data/spec/config/database.yml +24 -34
  81. data/spec/config/turntable.yml +18 -1
  82. data/spec/fabricators/turntable_fabricator.rb +0 -2
  83. data/spec/models/card.rb +3 -0
  84. data/spec/models/cards_user.rb +10 -0
  85. data/spec/models/cards_users_histories.rb +7 -0
  86. data/spec/models/events_users_history.rb +7 -0
  87. data/spec/models/user.rb +7 -0
  88. data/spec/models/user_status.rb +6 -0
  89. data/spec/spec_helper.rb +10 -4
  90. data/spec/support/matchers/be_saved_to.rb +6 -0
  91. data/spec/support/turntable_helper.rb +29 -0
  92. metadata +124 -74
  93. data/README.rdoc +0 -294
  94. data/gemfiles/rails3_0.gemfile +0 -7
  95. data/gemfiles/rails3_1.gemfile +0 -6
  96. data/gemfiles/rails3_2.gemfile +0 -6
  97. data/lib/active_record/turntable/compatible.rb +0 -19
  98. data/sample_app/.gitignore +0 -16
  99. data/sample_app/Gemfile +0 -41
  100. data/sample_app/README.rdoc +0 -261
  101. data/sample_app/Rakefile +0 -7
  102. data/sample_app/app/assets/images/rails.png +0 -0
  103. data/sample_app/app/assets/javascripts/application.js +0 -15
  104. data/sample_app/app/assets/stylesheets/application.css +0 -13
  105. data/sample_app/app/controllers/application_controller.rb +0 -3
  106. data/sample_app/app/helpers/application_helper.rb +0 -2
  107. data/sample_app/app/mailers/.gitkeep +0 -0
  108. data/sample_app/app/models/.gitkeep +0 -0
  109. data/sample_app/app/models/user.rb +0 -4
  110. data/sample_app/app/views/layouts/application.html.erb +0 -14
  111. data/sample_app/config/application.rb +0 -65
  112. data/sample_app/config/boot.rb +0 -6
  113. data/sample_app/config/database.yml +0 -70
  114. data/sample_app/config/environment.rb +0 -5
  115. data/sample_app/config/environments/development.rb +0 -37
  116. data/sample_app/config/environments/production.rb +0 -67
  117. data/sample_app/config/environments/test.rb +0 -37
  118. data/sample_app/config/initializers/backtrace_silencers.rb +0 -7
  119. data/sample_app/config/initializers/inflections.rb +0 -15
  120. data/sample_app/config/initializers/mime_types.rb +0 -5
  121. data/sample_app/config/initializers/secret_token.rb +0 -7
  122. data/sample_app/config/initializers/session_store.rb +0 -8
  123. data/sample_app/config/initializers/wrap_parameters.rb +0 -14
  124. data/sample_app/config/locales/en.yml +0 -5
  125. data/sample_app/config/routes.rb +0 -58
  126. data/sample_app/config/turntable.yml +0 -64
  127. data/sample_app/config.ru +0 -4
  128. data/sample_app/db/migrate/20120316073058_create_users.rb +0 -11
  129. data/sample_app/db/seeds.rb +0 -7
  130. data/sample_app/lib/assets/.gitkeep +0 -0
  131. data/sample_app/lib/tasks/.gitkeep +0 -0
  132. data/sample_app/log/.gitkeep +0 -0
  133. data/sample_app/public/404.html +0 -26
  134. data/sample_app/public/422.html +0 -26
  135. data/sample_app/public/500.html +0 -25
  136. data/sample_app/public/favicon.ico +0 -0
  137. data/sample_app/public/index.html +0 -241
  138. data/sample_app/public/robots.txt +0 -5
  139. data/sample_app/script/rails +0 -6
  140. data/sample_app/vendor/assets/javascripts/.gitkeep +0 -0
  141. data/sample_app/vendor/assets/stylesheets/.gitkeep +0 -0
  142. data/sample_app/vendor/plugins/.gitkeep +0 -0
  143. data/spec/test_models.rb +0 -27
  144. 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("test")
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
- context "AR3.1" do
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("test")
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
- # mock(User.turntable_cluster).select_shard(1) { User.turntable_cluster.shards[:user_shard_1] }
39
- lambda {
29
+ expect {
40
30
  user.save!
41
- }.should_not raise_error
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
- # mock(User.turntable_cluster).select_shard(30000) { User.turntable_cluster.shards[:user_shard_2] }
48
- lambda {
37
+ expect {
49
38
  user.save!
50
- }.should_not raise_error
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
- # mock(User.turntable_cluster).select_shard(30000) { User.turntable_cluster.shards[:user_shard_2] }
58
- lambda {
46
+ expect {
59
47
  user.save!
60
- }.should_not raise_error
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.should == "hoge@\n@\\@@\\nhoge\\\nhoge\\n"
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("test")
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
- lambda {
91
+ expect {
90
92
  @user1.save!
91
- }.should_not raise_error
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
- lambda {
99
+ expect {
98
100
  @user2.save!
99
- }.should_not raise_error
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.should == 2
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.should == 2
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.should == 2
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("test")
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 { should have(3).items }
137
+ it { is_expected.to have(3).items }
136
138
 
137
139
  it "returns User.count of each shards" do
138
- subject[0].should == 1
139
- subject[1].should == 1
140
- subject[2].should == 0
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 { lambda { subject }.should_not raise_error }
152
- it { should have(3).items }
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.should be_instance_of(RuntimeError)
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 { should be_true }
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 { should be_false }
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 { should be_true }
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 { should be_false }
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 { should be_true }
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("test")
10
+ establish_connection_to(:test)
11
11
  truncate_shard
12
- ActiveRecord::Base.logger = Logger.new(STDOUT)
13
- @user = User.new
14
- @user.id = 1
15
- @user.save
12
+ user
16
13
  end
17
14
 
18
- it "#find(1) should be == user" do
19
- User.find(1).should == @user
20
- end
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
- it "#find(2) should raise error" do
23
- lambda { User.find(2) }.should raise_error
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("test")
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 { should be_instance_of(Hash) }
28
- it { should have_key(1) }
29
- it { [1].should have(1).item }
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 { should be_instance_of(Hash) }
39
- it { should have_key(3) }
40
- it { [1].should have(1).item }
41
- it { [2].should have(1).item }
42
- it { [3].should have(1).item }
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 { should be_instance_of Array }
54
- it { should == [1] }
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 { should be_instance_of Array }
66
- it { should == [1] }
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 { should be_instance_of Array }
78
- it { should == [1] }
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 { should be_instance_of Array }
88
- it { should == [1,2,3,4,5] }
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 { should be_instance_of Array }
98
- it { should == [] }
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 { should be_instance_of Array }
108
- it { should == [] }
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
@@ -7,7 +7,7 @@ describe ActiveRecord::Turntable::Shard do
7
7
 
8
8
  context "When initialized" do
9
9
  before do
10
- establish_connection_to("test")
10
+ establish_connection_to(:test)
11
11
  truncate_shard
12
12
  end
13
13
 
@@ -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
- stub(Rails).root { "/path/to/rails_root" }
11
+ allow(Rails).to receive(:root) { "/path/to/rails_root" }
12
12
  ActiveRecord::Base.turntable_config_file = nil
13
- ActiveRecord::Base.turntable_config_file.should == "/path/to/rails_root/config/turntable.yml"
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.should == filename
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 { should be_instance_of(ActiveRecord::Turntable::Config) }
28
+ it { is_expected.to be_instance_of(ActiveRecord::Turntable::Config) }
29
29
  end
30
30
  end
@@ -1,45 +1,35 @@
1
- test:
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: turntable_master_test
8
+ database: turntable_test
9
+
10
+ test:
11
+ <<: *default
12
+ database: turntable_test
9
13
  seq:
10
- user_seq_1:
11
- adapter: mysql2
12
- username: root
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
- adapter: mysql2
24
- username: root
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
- adapter: mysql2
32
- username: root
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
- adapter: mysql2
40
- username: root
41
- password:
42
- host: localhost
43
- port: 3306
44
- encoding: utf8
45
- database: turntable3_test
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