switchman 1.2.10 → 1.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/models/switchman/shard.rb +2 -2
- data/lib/switchman/active_record/connection_handler.rb +18 -9
- data/lib/switchman/active_record/connection_pool.rb +3 -0
- data/lib/switchman/active_record/query_cache.rb +6 -1
- data/lib/switchman/active_record/query_methods.rb +1 -1
- data/lib/switchman/connection_pool_proxy.rb +1 -1
- data/lib/switchman/r_spec_helper.rb +73 -61
- data/lib/switchman/test_helper.rb +8 -2
- data/lib/switchman/version.rb +1 -1
- data/lib/tasks/switchman.rake +5 -0
- data/spec/dummy/config/database.yml +2 -4
- data/spec/dummy/config/environments/development.rb +0 -4
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/db/schema.rb +12 -12
- data/spec/dummy/db/shard_120.sqlite3 +0 -0
- data/spec/dummy/db/shard_156.sqlite3 +0 -0
- data/spec/dummy/db/shard_193.sqlite3 +0 -0
- data/spec/dummy/db/shard_23.sqlite3 +0 -0
- data/spec/dummy/db/shard_234.sqlite3 +0 -0
- data/spec/dummy/db/shard_257.sqlite3 +0 -0
- data/spec/dummy/db/shard_277.sqlite3 +0 -0
- data/spec/dummy/db/shard_311.sqlite3 +0 -0
- data/spec/dummy/db/shard_324.sqlite3 +0 -0
- data/spec/dummy/db/shard_342.sqlite3 +0 -0
- data/spec/dummy/db/shard_375.sqlite3 +0 -0
- data/spec/dummy/db/shard_389.sqlite3 +0 -0
- data/spec/dummy/db/shard_428.sqlite3 +0 -0
- data/spec/dummy/db/shard_452.sqlite3 +0 -0
- data/spec/dummy/db/shard_462.sqlite3 +0 -0
- data/spec/dummy/db/shard_483.sqlite3 +0 -0
- data/spec/dummy/db/shard_508.sqlite3 +0 -0
- data/spec/dummy/db/shard_531.sqlite3 +0 -0
- data/spec/dummy/db/shard_554.sqlite3 +0 -0
- data/spec/dummy/db/shard_577.sqlite3 +0 -0
- data/spec/dummy/db/shard_58.sqlite3 +0 -0
- data/spec/dummy/db/shard_600.sqlite3 +0 -0
- data/spec/dummy/db/shard_623.sqlite3 +0 -0
- data/spec/dummy/db/shard_646.sqlite3 +0 -0
- data/spec/dummy/db/shard_668.sqlite3 +0 -0
- data/spec/dummy/db/shard_680.sqlite3 +0 -0
- data/spec/dummy/db/shard_708.sqlite3 +0 -0
- data/spec/dummy/db/shard_735.sqlite3 +0 -0
- data/spec/dummy/db/shard_753.sqlite3 +0 -0
- data/spec/dummy/db/shard_76.sqlite3 +0 -0
- data/spec/dummy/db/shard_802.sqlite3 +0 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +7 -0
- data/spec/dummy/log/test.log +106293 -0
- data/spec/dummy/tmp/cache/313/970/shard%2F30 +1 -0
- data/spec/dummy/tmp/cache/316/980/shard%2F15 +1 -0
- data/spec/dummy/tmp/cache/317/9D0/shard%2F52 +1 -0
- data/spec/dummy/tmp/cache/31C/A40/shard%2F75 +1 -0
- data/spec/dummy/tmp/cache/345/E00/shard%2F500 +1 -0
- data/spec/dummy/tmp/cache/346/DF0/shard%2F321 +1 -0
- data/spec/dummy/tmp/cache/349/DE0/shard%2F126 +1 -0
- data/spec/dummy/tmp/cache/349/E10/shard%2F153 +0 -0
- data/spec/dummy/tmp/cache/349/E20/shard%2F243 +1 -0
- data/spec/dummy/tmp/cache/34A/E20/shard%2F154 +0 -0
- data/spec/dummy/tmp/cache/34A/E40/shard%2F172 +1 -0
- data/spec/dummy/tmp/cache/34A/E70/shard%2F523 +1 -0
- data/spec/dummy/tmp/cache/34A/EB0/shard%2F721 +1 -0
- data/spec/dummy/tmp/cache/34A/EC0/shard%2F730 +1 -0
- data/spec/dummy/tmp/cache/34B/E40/shard%2F245 +0 -0
- data/spec/dummy/tmp/cache/34B/E60/shard%2F425 +1 -0
- data/spec/dummy/tmp/cache/34B/EC0/shard%2F722 +1 -0
- data/spec/dummy/tmp/cache/34C/E50/shard%2F246 +0 -0
- data/spec/dummy/tmp/cache/34C/E80/shard%2F273 +1 -0
- data/spec/dummy/tmp/cache/34C/E90/shard%2F363 +0 -0
- data/spec/dummy/tmp/cache/34C/E90/shard%2F444 +1 -0
- data/spec/dummy/tmp/cache/34C/EA0/shard%2F615 +1 -0
- data/spec/dummy/tmp/cache/34C/F00/shard%2F750 +1 -0
- data/spec/dummy/tmp/cache/34D/E80/shard%2F265 +1 -0
- data/spec/dummy/tmp/cache/34D/ED0/shard%2F391 +1 -0
- data/spec/dummy/tmp/cache/34D/EE0/shard%2F724 +1 -0
- data/spec/dummy/tmp/cache/34E/ED0/shard%2F383 +1 -0
- data/spec/dummy/tmp/cache/34E/EF0/shard%2F725 +1 -0
- data/spec/dummy/tmp/cache/34E/F00/shard%2F653 +1 -0
- data/spec/dummy/tmp/cache/34F/EE0/shard%2F546 +1 -0
- data/spec/dummy/tmp/cache/34F/F00/shard%2F807 +1 -0
- data/spec/dummy/tmp/cache/34F/F30/shard%2F672 +1 -0
- data/spec/dummy/tmp/cache/350/EA0/shard%2F178 +1 -0
- data/spec/dummy/tmp/cache/350/EB0/shard%2F349 +1 -0
- data/spec/dummy/tmp/cache/350/F00/shard%2F718 +0 -0
- data/spec/dummy/tmp/cache/350/F40/shard%2F592 +1 -0
- data/spec/dummy/tmp/cache/351/F00/shard%2F386 +0 -0
- data/spec/dummy/tmp/cache/351/F10/shard%2F638 +1 -0
- data/spec/dummy/tmp/cache/351/F10/shard%2F719 +0 -0
- data/spec/dummy/tmp/cache/352/F10/shard%2F387 +0 -0
- data/spec/dummy/tmp/cache/352/F10/shard%2F468 +1 -0
- data/spec/dummy/tmp/cache/352/F30/shard%2F729 +1 -0
- data/spec/dummy/tmp/cache/353/F10/shard%2F298 +1 -0
- data/spec/dummy/tmp/cache/353/F50/shard%2F739 +1 -0
- data/spec/dummy/tmp/cache/354/F50/shard%2F569 +1 -0
- data/spec/dummy/tmp/cache/354/F80/shard%2F677 +1 -0
- data/spec/dummy/tmp/cache/354/F90/shard%2F767 +1 -0
- data/spec/dummy/tmp/cache/358/FE0/shard%2F699 +1 -0
- data/spec/dummy/tmp/cache/3A5/EA0/shard%2F10004 +1 -0
- data/spec/dummy/tmp/cache/3A6/F40/shard%2F10410 +1 -0
- data/spec/dummy/tmp/cache/3A7/F30/shard%2F10231 +1 -0
- data/spec/dummy/tmp/cache/3A8/F90/shard%2F10520 +1 -0
- data/spec/dummy/tmp/cache/3A9/F40/shard%2F10062 +1 -0
- data/spec/dummy/tmp/cache/3A9/F80/shard%2F10341 +1 -0
- data/spec/dummy/tmp/cache/3AA/F10/shard%2F10027 +1 -0
- data/spec/dummy/tmp/cache/3AA/F10/shard%2F10108 +1 -0
- data/spec/dummy/tmp/cache/3AA/FB0/shard%2F10441 +1 -0
- data/spec/dummy/tmp/cache/3AA/FC0/shard%2F10612 +1 -0
- data/spec/dummy/tmp/cache/3AB/010/shard%2F10730 +1 -0
- data/spec/dummy/tmp/cache/3AB/F70/shard%2F10154 +1 -0
- data/spec/dummy/tmp/cache/3AC/020/shard%2F10650 +1 -0
- data/spec/dummy/tmp/cache/3AC/FC0/shard%2F10272 +1 -0
- data/spec/dummy/tmp/cache/3AD/000/shard%2F10543 +1 -0
- data/spec/dummy/tmp/cache/3AD/F50/shard%2F10039 +1 -0
- data/spec/dummy/tmp/cache/3AD/F90/shard%2F10318 +1 -0
- data/spec/dummy/tmp/cache/3AD/FA0/shard%2F10246 +1 -0
- data/spec/dummy/tmp/cache/3AE/060/shard%2F10751 +1 -0
- data/spec/dummy/tmp/cache/3AE/FF0/shard%2F10364 +1 -0
- data/spec/dummy/tmp/cache/3AF/030/shard%2F10635 +1 -0
- data/spec/dummy/tmp/cache/3B0/FE0/shard%2F10177 +1 -0
- data/spec/dummy/tmp/cache/3B1/030/shard%2F10295 +1 -0
- data/spec/dummy/tmp/cache/3B1/040/shard%2F10466 +1 -0
- data/spec/dummy/tmp/cache/3B1/090/shard%2F10673 +1 -0
- data/spec/dummy/tmp/cache/3B2/070/shard%2F10566 +1 -0
- data/spec/dummy/tmp/cache/3B3/060/shard%2F10387 +1 -0
- data/spec/dummy/tmp/cache/3B5/0B0/shard%2F10497 +1 -0
- data/spec/dummy/tmp/cache/3B6/100/shard%2F10696 +1 -0
- data/spec/dummy/tmp/cache/3B7/0E0/shard%2F10589 +1 -0
- data/spec/dummy/tmp/cache/3BA/160/shard%2F10799 +1 -0
- data/spec/lib/action_controller/caching_spec.rb +4 -4
- data/spec/lib/active_record/abstract_adapter_spec.rb +1 -1
- data/spec/lib/active_record/association_spec.rb +83 -67
- data/spec/lib/active_record/attribute_methods_spec.rb +29 -29
- data/spec/lib/active_record/base_spec.rb +20 -20
- data/spec/lib/active_record/calculations_spec.rb +79 -59
- data/spec/lib/active_record/connection_handler_spec.rb +14 -14
- data/spec/lib/active_record/connection_pool_spec.rb +7 -7
- data/spec/lib/active_record/finder_methods_spec.rb +8 -8
- data/spec/lib/active_record/query_cache_spec.rb +38 -38
- data/spec/lib/active_record/query_methods_spec.rb +45 -45
- data/spec/lib/active_record/relation_spec.rb +8 -8
- data/spec/lib/active_record/spawn_methods_spec.rb +11 -11
- data/spec/lib/connection_pool_proxy_spec.rb +2 -2
- data/spec/lib/database_server_spec.rb +37 -37
- data/spec/lib/default_shard_spec.rb +3 -3
- data/spec/lib/r_spec_helper_spec.rb +33 -7
- data/spec/lib/rails_spec.rb +5 -5
- data/spec/lib/shackles_spec.rb +35 -35
- data/spec/models/shard_spec.rb +104 -104
- data/spec/spec_helper.rb +1 -1
- metadata +254 -29
|
@@ -6,39 +6,39 @@ module Switchman
|
|
|
6
6
|
include RSpecHelper
|
|
7
7
|
|
|
8
8
|
it "should use different proxies for different categories" do
|
|
9
|
-
Shard.connection_pool.
|
|
9
|
+
expect(Shard.connection_pool).not_to eq User.connection_pool
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
it "should share underlying pools for different categories on the same shard" do
|
|
13
|
-
Shard.connection_pool.current_pool.
|
|
13
|
+
expect(Shard.connection_pool.current_pool).to eq User.connection_pool.current_pool
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it "should insert sharding for connections established after initialization" do
|
|
17
|
-
User.connection_pool.
|
|
17
|
+
expect(User.connection_pool).to eq ::ActiveRecord::Base.connection_pool
|
|
18
18
|
begin
|
|
19
19
|
config = { :adapter => 'sqlite3', :database => ':memory:', :something_unique_in_the_spec => true }
|
|
20
20
|
User.establish_connection(config)
|
|
21
|
-
User.connection_pool.
|
|
22
|
-
User.connection_pool.spec.config.
|
|
23
|
-
User.connection_pool.
|
|
21
|
+
expect(User.connection_pool).not_to eq ::ActiveRecord::Base.connection_pool
|
|
22
|
+
expect(User.connection_pool.spec.config).to eq config
|
|
23
|
+
expect(User.connection_pool).to be_is_a(ConnectionPoolProxy)
|
|
24
24
|
@shard2.activate do
|
|
25
|
-
User.connection_pool.spec.config.
|
|
26
|
-
User.connection_pool.spec.config.
|
|
25
|
+
expect(User.connection_pool.spec.config).to eq ::ActiveRecord::Base.connection_pool.spec.config
|
|
26
|
+
expect(User.connection_pool.spec.config).not_to eq config
|
|
27
27
|
end
|
|
28
28
|
ensure
|
|
29
29
|
User.remove_connection
|
|
30
|
-
User.connection_pool.
|
|
31
|
-
User.connection_pool.
|
|
30
|
+
expect(User.connection_pool).to eq ::ActiveRecord::Base.connection_pool
|
|
31
|
+
expect(User.connection_pool).to be_is_a(ConnectionPoolProxy)
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should set up separate pools for different categories" do
|
|
36
|
-
User.connection_pool.
|
|
36
|
+
expect(User.connection_pool).not_to eq MirrorUser.connection_pool
|
|
37
37
|
mu = MirrorUser.create!
|
|
38
|
-
MirrorUser.find(mu.local_id).
|
|
38
|
+
expect(MirrorUser.find(mu.local_id)).to eq mu
|
|
39
39
|
# didn't activate the :mirror_universe category
|
|
40
|
-
@shard1.activate { MirrorUser.find(mu.local_id).
|
|
41
|
-
@shard1.activate(:mirror_universe) { MirrorUser.
|
|
40
|
+
@shard1.activate { expect(MirrorUser.find(mu.local_id)).to eq mu }
|
|
41
|
+
@shard1.activate(:mirror_universe) { expect(MirrorUser.where(id: mu.local_id).first).to eq nil }
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
44
|
end
|
|
@@ -4,24 +4,24 @@ module Switchman
|
|
|
4
4
|
module ActiveRecord
|
|
5
5
|
describe ConnectionPool do
|
|
6
6
|
it "should be able to access another shard on a db server after the 'primary' shard is gone" do
|
|
7
|
-
|
|
7
|
+
skip 'A "real" database"' unless Shard.default.database_server.shareable?
|
|
8
8
|
# separate connections
|
|
9
9
|
server = DatabaseServer.create(:config => Shard.default.database_server.config.dup)
|
|
10
10
|
s1 = server.shards.create!(:name => 'non_existent_shard') # don't actually create any schema
|
|
11
11
|
s2 = server.shards.create! # inherit's the default shard's config, which is functional
|
|
12
12
|
s1.activate do
|
|
13
|
-
|
|
13
|
+
expect { User.count }.to raise_exception
|
|
14
14
|
end
|
|
15
15
|
# the config for s1 should not be the permanent default for all new
|
|
16
16
|
# connections now
|
|
17
17
|
s2.activate do
|
|
18
|
-
|
|
18
|
+
expect { User.count }.not_to raise_exception
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
describe "clear_idle_connections!" do
|
|
23
23
|
before do
|
|
24
|
-
|
|
24
|
+
skip 'A "real" database"' unless Shard.default.database_server.shareable?
|
|
25
25
|
@server = DatabaseServer.create(:config => Shard.default.database_server.config.dup)
|
|
26
26
|
@shard = @server.shards.create!
|
|
27
27
|
@conn, @pool = @shard.activate{ [User.connection, User.connection_pool.current_pool] }
|
|
@@ -36,7 +36,7 @@ module Switchman
|
|
|
36
36
|
it "should remove idle connections" do
|
|
37
37
|
@pool.checkin(@conn)
|
|
38
38
|
@pool.clear_idle_connections!(@conn.last_query_at + 1)
|
|
39
|
-
@pool.connections.
|
|
39
|
+
expect(@pool.connections).to be_empty
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
it "should not affect idle but checked out connections" do
|
|
@@ -53,7 +53,7 @@ module Switchman
|
|
|
53
53
|
|
|
54
54
|
describe "release_connection" do
|
|
55
55
|
before do
|
|
56
|
-
|
|
56
|
+
skip 'A "real" database"' unless Shard.default.database_server.shareable?
|
|
57
57
|
@server = DatabaseServer.create(:config => Shard.default.database_server.config.dup)
|
|
58
58
|
@shard = @server.shards.create!
|
|
59
59
|
@pool = @shard.activate{ User.connection_pool.current_pool }
|
|
@@ -73,7 +73,7 @@ module Switchman
|
|
|
73
73
|
it "should still work if idle timeout is not configured" do
|
|
74
74
|
@pool.spec.config[:idle_timeout] = nil
|
|
75
75
|
@pool.expects(:clear_idle_connections!).never
|
|
76
|
-
|
|
76
|
+
expect { @pool.release_connection }.not_to raise_exception
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
end
|
|
@@ -11,17 +11,17 @@ module Switchman
|
|
|
11
11
|
|
|
12
12
|
describe "#find_one" do
|
|
13
13
|
it "should find with a global id" do
|
|
14
|
-
User.find(@user.global_id).
|
|
14
|
+
expect(User.find(@user.global_id)).to eq @user
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
describe "#find_by_attributes" do
|
|
19
19
|
it "should find with a global id" do
|
|
20
|
-
User.find_by_id(@user.global_id).
|
|
20
|
+
expect(User.find_by_id(@user.global_id)).to eq @user
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
it "should find with an array of global ids" do
|
|
24
|
-
User.find_by_id([@user.global_id]).
|
|
24
|
+
expect(User.find_by_id([@user.global_id])).to eq @user
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
@@ -29,21 +29,21 @@ module Switchman
|
|
|
29
29
|
it "should initialize with the shard from the scope" do
|
|
30
30
|
@user.destroy
|
|
31
31
|
u = User.shard(@shard1).where(id: @user).first_or_initialize
|
|
32
|
-
u.
|
|
33
|
-
u.shard.
|
|
32
|
+
expect(u).to be_new_record
|
|
33
|
+
expect(u.shard).to eq @shard1
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
describe "#exists?" do
|
|
38
38
|
it "should work for an out-of-shard scope" do
|
|
39
39
|
scope = @shard1.activate { User.where(id: @user) }
|
|
40
|
-
scope.shard_value.
|
|
41
|
-
scope.exists
|
|
40
|
+
expect(scope.shard_value).to eq @shard1
|
|
41
|
+
expect(scope.exists?).to eq true
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
it "should work for a multi-shard scope" do
|
|
45
45
|
user2 = @shard2.activate { User.create!(name: "multi-shard exists") }
|
|
46
|
-
User.where(name: "multi-shard exists").shard(::Rails.version < '4' ? Shard.scoped : Shard.all).exists
|
|
46
|
+
expect(User.where(name: "multi-shard exists").shard(::Rails.version < '4' ? Shard.scoped : Shard.all).exists?).to eq true
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
end
|
|
@@ -53,27 +53,27 @@ module Switchman
|
|
|
53
53
|
@shard3.activate do
|
|
54
54
|
User.create!
|
|
55
55
|
end
|
|
56
|
-
@shard1.activate { User.all.to_a }.
|
|
56
|
+
expect(@shard1.activate { User.all.to_a }).not_to eq @shard3.activate { User.all.to_a }
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
describe "query_cache_enabled" do
|
|
60
60
|
it "should be shared across shards on the same server" do
|
|
61
61
|
@shard1.activate{ User.connection.query_cache_enabled = true }
|
|
62
62
|
@shard3.activate{ User.connection.query_cache_enabled = false }
|
|
63
|
-
@shard1.activate{ User.connection.query_cache_enabled.
|
|
63
|
+
@shard1.activate{ expect(User.connection.query_cache_enabled).to eq false }
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
it "should be shared across servers" do
|
|
67
67
|
@shard1.activate{ User.connection.query_cache_enabled = true }
|
|
68
68
|
@shard2.activate{ User.connection.query_cache_enabled = false }
|
|
69
|
-
@shard1.activate{ User.connection.query_cache_enabled.
|
|
69
|
+
@shard1.activate{ expect(User.connection.query_cache_enabled).to eq false }
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
it "should be distinct across threads" do
|
|
73
73
|
User.connection.query_cache_enabled = true
|
|
74
74
|
threaded(
|
|
75
75
|
lambda{ |cc| User.connection.query_cache_enabled = false; cc.call },
|
|
76
|
-
lambda{ User.connection.query_cache_enabled.
|
|
76
|
+
lambda{ expect(User.connection.query_cache_enabled).to eq true })
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
@@ -84,10 +84,10 @@ module Switchman
|
|
|
84
84
|
lambda{ |cc|
|
|
85
85
|
User.connection.query_cache_enabled = false
|
|
86
86
|
User.connection.enable_query_cache!
|
|
87
|
-
User.connection.query_cache_enabled.
|
|
87
|
+
expect(User.connection.query_cache_enabled).to eq true
|
|
88
88
|
cc.call
|
|
89
89
|
},
|
|
90
|
-
lambda{ User.connection.query_cache_enabled.
|
|
90
|
+
lambda{ expect(User.connection.query_cache_enabled).to eq false })
|
|
91
91
|
end
|
|
92
92
|
end
|
|
93
93
|
|
|
@@ -98,10 +98,10 @@ module Switchman
|
|
|
98
98
|
lambda{ |cc|
|
|
99
99
|
User.connection.query_cache_enabled = true
|
|
100
100
|
User.connection.disable_query_cache!
|
|
101
|
-
User.connection.query_cache_enabled.
|
|
101
|
+
expect(User.connection.query_cache_enabled).to eq false
|
|
102
102
|
cc.call
|
|
103
103
|
},
|
|
104
|
-
lambda{ User.connection.query_cache_enabled.
|
|
104
|
+
lambda{ expect(User.connection.query_cache_enabled).to eq true })
|
|
105
105
|
end
|
|
106
106
|
end
|
|
107
107
|
|
|
@@ -112,15 +112,15 @@ module Switchman
|
|
|
112
112
|
User.connection.disable_query_cache!
|
|
113
113
|
threaded(
|
|
114
114
|
lambda{ |cc| User.connection.cache{ cc.call } },
|
|
115
|
-
lambda{ User.connection.query_cache_enabled.
|
|
115
|
+
lambda{ expect(User.connection.query_cache_enabled).to eq false })
|
|
116
116
|
end
|
|
117
117
|
|
|
118
118
|
it "should only enable for the duration of the block" do
|
|
119
119
|
User.connection.disable_query_cache!
|
|
120
120
|
User.connection.cache do
|
|
121
|
-
User.connection.query_cache_enabled.
|
|
121
|
+
expect(User.connection.query_cache_enabled).to eq true
|
|
122
122
|
end
|
|
123
|
-
User.connection.query_cache_enabled.
|
|
123
|
+
expect(User.connection.query_cache_enabled).to eq false
|
|
124
124
|
end
|
|
125
125
|
|
|
126
126
|
it "should clear query cache if disabling query cache after block" do
|
|
@@ -128,7 +128,7 @@ module Switchman
|
|
|
128
128
|
User.connection.cache do
|
|
129
129
|
User.connection.query_cache[:key][:binds] = :value
|
|
130
130
|
end
|
|
131
|
-
User.connection.query_cache[:key][:binds].
|
|
131
|
+
expect(User.connection.query_cache[:key][:binds]).to be_nil
|
|
132
132
|
end
|
|
133
133
|
|
|
134
134
|
it "should not clear query cache if the cache was already enabled" do
|
|
@@ -136,7 +136,7 @@ module Switchman
|
|
|
136
136
|
User.connection.cache do
|
|
137
137
|
User.connection.query_cache[:key][:binds] = :value
|
|
138
138
|
end
|
|
139
|
-
User.connection.query_cache[:key][:binds].
|
|
139
|
+
expect(User.connection.query_cache[:key][:binds]).to eq :value
|
|
140
140
|
end
|
|
141
141
|
end
|
|
142
142
|
|
|
@@ -147,21 +147,21 @@ module Switchman
|
|
|
147
147
|
User.connection.enable_query_cache!
|
|
148
148
|
threaded(
|
|
149
149
|
lambda{ |cc| User.connection.uncached{ cc.call } },
|
|
150
|
-
lambda{ User.connection.query_cache_enabled.
|
|
150
|
+
lambda{ expect(User.connection.query_cache_enabled).to eq true })
|
|
151
151
|
end
|
|
152
152
|
|
|
153
153
|
it "should only disable for the duration of the block" do
|
|
154
154
|
User.connection.enable_query_cache!
|
|
155
155
|
User.connection.uncached do
|
|
156
|
-
User.connection.query_cache_enabled.
|
|
156
|
+
expect(User.connection.query_cache_enabled).to eq false
|
|
157
157
|
end
|
|
158
|
-
User.connection.query_cache_enabled.
|
|
158
|
+
expect(User.connection.query_cache_enabled).to eq true
|
|
159
159
|
end
|
|
160
160
|
|
|
161
161
|
it "should not clear query cache" do
|
|
162
162
|
User.connection.query_cache[:key][:binds] = :value
|
|
163
163
|
User.connection.uncached{}
|
|
164
|
-
User.connection.query_cache[:key][:binds].
|
|
164
|
+
expect(User.connection.query_cache[:key][:binds]).to eq :value
|
|
165
165
|
end
|
|
166
166
|
end
|
|
167
167
|
|
|
@@ -169,26 +169,26 @@ module Switchman
|
|
|
169
169
|
it "should cache when query cache enabled" do
|
|
170
170
|
User.connection.enable_query_cache!
|
|
171
171
|
User.all.to_a
|
|
172
|
-
User.connection.query_cache.
|
|
172
|
+
expect(User.connection.query_cache).not_to be_empty
|
|
173
173
|
end
|
|
174
174
|
|
|
175
175
|
it "should not cache when query cache disabled" do
|
|
176
176
|
User.connection.disable_query_cache!
|
|
177
177
|
User.all.to_a
|
|
178
|
-
User.connection.query_cache.
|
|
178
|
+
expect(User.connection.query_cache).to be_empty
|
|
179
179
|
end
|
|
180
180
|
|
|
181
181
|
it "should not cache when query cache disabled but other thread's enabled" do
|
|
182
182
|
User.connection.disable_query_cache!
|
|
183
183
|
threaded(
|
|
184
184
|
lambda{ |cc| User.connection.cache{ cc.call } },
|
|
185
|
-
lambda{ User.all.to_a; User.connection.query_cache.
|
|
185
|
+
lambda{ User.all.to_a; expect(User.connection.query_cache).to be_empty })
|
|
186
186
|
end
|
|
187
187
|
|
|
188
188
|
it "should not cache when query is locked" do
|
|
189
189
|
User.connection.enable_query_cache!
|
|
190
190
|
User.lock.to_a
|
|
191
|
-
User.connection.query_cache.
|
|
191
|
+
expect(User.connection.query_cache).to be_empty
|
|
192
192
|
end
|
|
193
193
|
end
|
|
194
194
|
|
|
@@ -197,7 +197,7 @@ module Switchman
|
|
|
197
197
|
User.connection.enable_query_cache!
|
|
198
198
|
User.all.to_a
|
|
199
199
|
User.create!
|
|
200
|
-
User.connection.query_cache.
|
|
200
|
+
expect(User.connection.query_cache).to be_empty
|
|
201
201
|
end
|
|
202
202
|
|
|
203
203
|
it "should not clear thread's query cache if disabled" do
|
|
@@ -205,7 +205,7 @@ module Switchman
|
|
|
205
205
|
User.all.to_a
|
|
206
206
|
User.connection.disable_query_cache!
|
|
207
207
|
User.create!
|
|
208
|
-
User.connection.query_cache.
|
|
208
|
+
expect(User.connection.query_cache).not_to be_empty
|
|
209
209
|
end
|
|
210
210
|
|
|
211
211
|
it "should not clear thread's query cache if disabled but other thread's enabled" do
|
|
@@ -214,7 +214,7 @@ module Switchman
|
|
|
214
214
|
User.connection.disable_query_cache!
|
|
215
215
|
threaded(
|
|
216
216
|
lambda{ |cc| User.create!; cc.call },
|
|
217
|
-
lambda{ User.connection.query_cache.
|
|
217
|
+
lambda{ expect(User.connection.query_cache).not_to be_empty })
|
|
218
218
|
end
|
|
219
219
|
|
|
220
220
|
it "should not clear other thread's query cache" do
|
|
@@ -223,7 +223,7 @@ module Switchman
|
|
|
223
223
|
User.connection.disable_query_cache!
|
|
224
224
|
threaded(
|
|
225
225
|
lambda{ |cc| User.create!; cc.call },
|
|
226
|
-
lambda{ User.connection.query_cache.
|
|
226
|
+
lambda{ expect(User.connection.query_cache).not_to be_empty })
|
|
227
227
|
end
|
|
228
228
|
end
|
|
229
229
|
|
|
@@ -236,7 +236,7 @@ module Switchman
|
|
|
236
236
|
User.connection.enable_query_cache!
|
|
237
237
|
User.all.to_a
|
|
238
238
|
User.update_all(updated_at: Time.now)
|
|
239
|
-
User.connection.query_cache.
|
|
239
|
+
expect(User.connection.query_cache).to be_empty
|
|
240
240
|
end
|
|
241
241
|
|
|
242
242
|
it "should not clear thread's query cache if disabled" do
|
|
@@ -244,7 +244,7 @@ module Switchman
|
|
|
244
244
|
User.all.to_a
|
|
245
245
|
User.connection.disable_query_cache!
|
|
246
246
|
User.update_all(updated_at: Time.now)
|
|
247
|
-
User.connection.query_cache.
|
|
247
|
+
expect(User.connection.query_cache).not_to be_empty
|
|
248
248
|
end
|
|
249
249
|
|
|
250
250
|
context "non-transactional" do
|
|
@@ -262,7 +262,7 @@ module Switchman
|
|
|
262
262
|
User.connection.disable_query_cache!
|
|
263
263
|
threaded(
|
|
264
264
|
lambda{ |cc| User.update_all(updated_at: Time.now); cc.call },
|
|
265
|
-
lambda{ User.connection.query_cache.
|
|
265
|
+
lambda{ expect(User.connection.query_cache).not_to be_empty })
|
|
266
266
|
end
|
|
267
267
|
|
|
268
268
|
it "should not clear other thread's query cache" do
|
|
@@ -271,7 +271,7 @@ module Switchman
|
|
|
271
271
|
User.connection.disable_query_cache!
|
|
272
272
|
threaded(
|
|
273
273
|
lambda{ |cc| User.update_all(updated_at: Time.now); cc.call },
|
|
274
|
-
lambda{ User.connection.query_cache.
|
|
274
|
+
lambda{ expect(User.connection.query_cache).not_to be_empty })
|
|
275
275
|
end
|
|
276
276
|
end
|
|
277
277
|
end
|
|
@@ -285,7 +285,7 @@ module Switchman
|
|
|
285
285
|
User.connection.enable_query_cache!
|
|
286
286
|
User.all.to_a
|
|
287
287
|
User.delete_all
|
|
288
|
-
User.connection.query_cache.
|
|
288
|
+
expect(User.connection.query_cache).to be_empty
|
|
289
289
|
end
|
|
290
290
|
|
|
291
291
|
it "should not clear thread's query cache if disabled" do
|
|
@@ -293,7 +293,7 @@ module Switchman
|
|
|
293
293
|
User.all.to_a
|
|
294
294
|
User.connection.disable_query_cache!
|
|
295
295
|
User.delete_all
|
|
296
|
-
User.connection.query_cache.
|
|
296
|
+
expect(User.connection.query_cache).not_to be_empty
|
|
297
297
|
end
|
|
298
298
|
|
|
299
299
|
context "non-transactional" do
|
|
@@ -311,7 +311,7 @@ module Switchman
|
|
|
311
311
|
User.connection.disable_query_cache!
|
|
312
312
|
threaded(
|
|
313
313
|
lambda{ |cc| User.delete_all; cc.call },
|
|
314
|
-
lambda{ User.connection.query_cache.
|
|
314
|
+
lambda{ expect(User.connection.query_cache).not_to be_empty })
|
|
315
315
|
end
|
|
316
316
|
|
|
317
317
|
it "should not clear other thread's query cache" do
|
|
@@ -320,22 +320,22 @@ module Switchman
|
|
|
320
320
|
User.connection.disable_query_cache!
|
|
321
321
|
threaded(
|
|
322
322
|
lambda{ |cc| User.delete_all; cc.call },
|
|
323
|
-
lambda{ User.connection.query_cache.
|
|
323
|
+
lambda{ expect(User.connection.query_cache).not_to be_empty })
|
|
324
324
|
end
|
|
325
325
|
|
|
326
326
|
it "should clear cache for all connections" do
|
|
327
327
|
u = User.create!(name: 'a')
|
|
328
328
|
User.connection.cache do
|
|
329
|
-
u.reload.name.
|
|
329
|
+
expect(u.reload.name).to eq 'a'
|
|
330
330
|
::Shackles.activate(:slave) do
|
|
331
|
-
u.reload.name.
|
|
331
|
+
expect(u.reload.name).to eq 'a'
|
|
332
332
|
end
|
|
333
|
-
u.reload.name.
|
|
333
|
+
expect(u.reload.name).to eq 'a'
|
|
334
334
|
u.name = 'b'
|
|
335
335
|
u.save!
|
|
336
|
-
u.reload.name.
|
|
336
|
+
expect(u.reload.name).to eq 'b'
|
|
337
337
|
::Shackles.activate(:slave) do
|
|
338
|
-
u.reload.name.
|
|
338
|
+
expect(u.reload.name).to eq 'b'
|
|
339
339
|
end
|
|
340
340
|
end
|
|
341
341
|
end
|
|
@@ -16,88 +16,88 @@ module Switchman
|
|
|
16
16
|
|
|
17
17
|
describe "#primary_shard" do
|
|
18
18
|
it "should be the shard if it's a shard" do
|
|
19
|
-
User.shard(Shard.default).primary_shard.
|
|
20
|
-
User.shard(@shard1).primary_shard.
|
|
19
|
+
expect(User.shard(Shard.default).primary_shard).to eq Shard.default
|
|
20
|
+
expect(User.shard(@shard1).primary_shard).to eq @shard1
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
it "should be the first shard of an array of shards" do
|
|
24
|
-
User.shard([Shard.default, @shard1]).primary_shard.
|
|
25
|
-
User.shard([@shard1, Shard.default]).primary_shard.
|
|
24
|
+
expect(User.shard([Shard.default, @shard1]).primary_shard).to eq Shard.default
|
|
25
|
+
expect(User.shard([@shard1, Shard.default]).primary_shard).to eq @shard1
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it "should be the object's shard if it's a model" do
|
|
29
|
-
User.shard(@user1).primary_shard.
|
|
30
|
-
User.shard(@user2).primary_shard.
|
|
29
|
+
expect(User.shard(@user1).primary_shard).to eq Shard.default
|
|
30
|
+
expect(User.shard(@user2).primary_shard).to eq @shard1
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
it "should be the default shard if it's a scope of Shard" do
|
|
34
|
-
User.shard(::Rails.version < '4' ? Shard.scoped : Shard.all).primary_shard.
|
|
34
|
+
expect(User.shard(::Rails.version < '4' ? Shard.scoped : Shard.all).primary_shard).to eq Shard.default
|
|
35
35
|
@shard1.activate do
|
|
36
|
-
User.shard(::Rails.version < '4' ? Shard.scoped : Shard.all).primary_shard.
|
|
36
|
+
expect(User.shard(::Rails.version < '4' ? Shard.scoped : Shard.all).primary_shard).to eq Shard.default
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "should default to the current shard" do
|
|
42
42
|
relation = ::Rails.version < '4' ? User.scoped : User.all
|
|
43
|
-
relation.shard_value.
|
|
44
|
-
relation.shard_source_value.
|
|
43
|
+
expect(relation.shard_value).to eq Shard.default
|
|
44
|
+
expect(relation.shard_source_value).to eq :implicit
|
|
45
45
|
|
|
46
46
|
@shard1.activate do
|
|
47
|
-
relation.shard_value.
|
|
47
|
+
expect(relation.shard_value).to eq Shard.default
|
|
48
48
|
|
|
49
49
|
relation = ::Rails.version < '4' ? User.scoped : User.all
|
|
50
|
-
relation.shard_value.
|
|
51
|
-
relation.shard_source_value.
|
|
50
|
+
expect(relation.shard_value).to eq @shard1
|
|
51
|
+
expect(relation.shard_source_value).to eq :implicit
|
|
52
52
|
end
|
|
53
|
-
relation.shard_value.
|
|
53
|
+
expect(relation.shard_value).to eq @shard1
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
describe "with primary key conditions" do
|
|
57
57
|
it "should be changeable, and change conditions when it is changed" do
|
|
58
58
|
relation = User.where(:id => @user1).shard(@shard1)
|
|
59
|
-
relation.shard_value.
|
|
60
|
-
relation.shard_source_value.
|
|
61
|
-
relation.where_values.first.right.
|
|
59
|
+
expect(relation.shard_value).to eq @shard1
|
|
60
|
+
expect(relation.shard_source_value).to eq :explicit
|
|
61
|
+
expect(relation.where_values.first.right).to eq @user1.global_id
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
it "should infer the shard from a single argument" do
|
|
65
65
|
relation = User.where(:id => @user2)
|
|
66
66
|
# execute on @shard1, with id local to that shard
|
|
67
|
-
relation.shard_value.
|
|
68
|
-
relation.where_values.first.right.
|
|
67
|
+
expect(relation.shard_value).to eq @shard1
|
|
68
|
+
expect(relation.where_values.first.right).to eq @user2.local_id
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
it "should infer the shard from multiple arguments" do
|
|
72
72
|
relation = User.where(:id => [@user2, @user2])
|
|
73
73
|
# execute on @shard1, with id local to that shard
|
|
74
|
-
relation.shard_value.
|
|
75
|
-
relation.where_values.first.right.
|
|
74
|
+
expect(relation.shard_value).to eq @shard1
|
|
75
|
+
expect(relation.where_values.first.right).to eq [@user2.local_id, @user2.local_id]
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
it "should infer the correct shard from an array of 1" do
|
|
79
79
|
relation = User.where(:id => [@user2])
|
|
80
80
|
# execute on @shard1, with id local to that shard
|
|
81
|
-
relation.shard_value.
|
|
82
|
-
relation.where_values.first.right.
|
|
81
|
+
expect(relation.shard_value).to eq @shard1
|
|
82
|
+
expect(relation.where_values.first.right).to eq [@user2.local_id]
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
it "should do nothing when it's an array of 0" do
|
|
86
86
|
relation = User.where(:id => [])
|
|
87
87
|
# execute on @shard1, with id local to that shard
|
|
88
|
-
relation.shard_value.
|
|
89
|
-
relation.where_values.first.right.
|
|
88
|
+
expect(relation.shard_value).to eq Shard.default
|
|
89
|
+
expect(relation.where_values.first.right).to eq []
|
|
90
90
|
end
|
|
91
91
|
|
|
92
92
|
it "should order the shards preferring the shard it already had as primary" do
|
|
93
93
|
relation = User.where(:id => [@user1, @user2])
|
|
94
|
-
relation.shard_value.
|
|
95
|
-
relation.where_values.first.right.
|
|
94
|
+
expect(relation.shard_value).to eq [Shard.default, @shard1]
|
|
95
|
+
expect(relation.where_values.first.right).to eq [@user1.local_id, @user2.global_id]
|
|
96
96
|
|
|
97
97
|
@shard1.activate do
|
|
98
98
|
relation = User.where(:id => [@user1, @user2])
|
|
99
|
-
relation.shard_value.
|
|
100
|
-
relation.where_values.first.right.
|
|
99
|
+
expect(relation.shard_value).to eq [@shard1, Shard.default]
|
|
100
|
+
expect(relation.where_values.first.right).to eq [@user1.global_id, @user2.local_id]
|
|
101
101
|
end
|
|
102
102
|
end
|
|
103
103
|
end
|
|
@@ -105,36 +105,36 @@ module Switchman
|
|
|
105
105
|
describe "with foreign key conditions" do
|
|
106
106
|
it "should be changeable, and change conditions when it is changed" do
|
|
107
107
|
relation = Appendage.where(:user_id => @user1)
|
|
108
|
-
relation.shard_value.
|
|
109
|
-
relation.shard_source_value.
|
|
110
|
-
relation.where_values.first.right.
|
|
108
|
+
expect(relation.shard_value).to eq Shard.default
|
|
109
|
+
expect(relation.shard_source_value).to eq :implicit
|
|
110
|
+
expect(relation.where_values.first.right).to eq @user1.local_id
|
|
111
111
|
|
|
112
112
|
relation = relation.shard(@shard1)
|
|
113
|
-
relation.shard_value.
|
|
114
|
-
relation.shard_source_value.
|
|
115
|
-
relation.where_values.first.right.
|
|
113
|
+
expect(relation.shard_value).to eq @shard1
|
|
114
|
+
expect(relation.shard_source_value).to eq :explicit
|
|
115
|
+
expect(relation.where_values.first.right).to eq @user1.global_id
|
|
116
116
|
end
|
|
117
117
|
|
|
118
118
|
it "should translate ids based on current shard" do
|
|
119
119
|
relation = Appendage.where(:user_id => [@user1, @user2])
|
|
120
|
-
relation.where_values.first.right.
|
|
120
|
+
expect(relation.where_values.first.right).to eq [@user1.local_id, @user2.global_id]
|
|
121
121
|
|
|
122
122
|
@shard1.activate do
|
|
123
123
|
relation = Appendage.where(:user_id => [@user1, @user2])
|
|
124
|
-
relation.where_values.first.right.
|
|
124
|
+
expect(relation.where_values.first.right).to eq [@user1.global_id, @user2.local_id]
|
|
125
125
|
end
|
|
126
126
|
end
|
|
127
127
|
|
|
128
128
|
it "should translate ids in joins" do
|
|
129
129
|
relation = User.joins(:appendage).where(appendages: { user_id: [@user1, @user2]})
|
|
130
|
-
relation.where_values.first.right.
|
|
130
|
+
expect(relation.where_values.first.right).to eq [@user1.local_id, @user2.global_id]
|
|
131
131
|
end
|
|
132
132
|
|
|
133
133
|
it "should translate ids according to the current shard of the foreign type" do
|
|
134
134
|
@shard1.activate(:mirror_universe) do
|
|
135
135
|
mirror_user = MirrorUser.create!
|
|
136
136
|
relation = User.where(mirror_user_id: mirror_user)
|
|
137
|
-
relation.where_values.first.right.
|
|
137
|
+
expect(relation.where_values.first.right).to eq mirror_user.global_id
|
|
138
138
|
end
|
|
139
139
|
end
|
|
140
140
|
end
|
|
@@ -143,20 +143,20 @@ module Switchman
|
|
|
143
143
|
it "should properly construct the query" do
|
|
144
144
|
child = @user1.children.create!
|
|
145
145
|
grandchild = child.children.create!
|
|
146
|
-
child.reload.parent.
|
|
146
|
+
expect(child.reload.parent).to eq @user1
|
|
147
147
|
|
|
148
148
|
relation = @user1.association(:grandchildren)
|
|
149
149
|
relation = ::Rails.version < '4' ? relation.scoped : relation.scope
|
|
150
150
|
|
|
151
151
|
attribute = relation.where_values.first.left
|
|
152
|
-
attribute.name.to_s.
|
|
153
|
-
attribute.relation.class.
|
|
152
|
+
expect(attribute.name.to_s).to eq 'parent_id'
|
|
153
|
+
expect(attribute.relation.class).to eq Arel::Nodes::TableAlias
|
|
154
154
|
|
|
155
155
|
rel, column = relation.send(:relation_and_column, attribute)
|
|
156
|
-
relation.send(:sharded_primary_key?, rel, column).
|
|
157
|
-
relation.send(:sharded_foreign_key?, rel, column).
|
|
156
|
+
expect(relation.send(:sharded_primary_key?, rel, column)).to eq false
|
|
157
|
+
expect(relation.send(:sharded_foreign_key?, rel, column)).to eq true
|
|
158
158
|
|
|
159
|
-
@user1.grandchildren.
|
|
159
|
+
expect(@user1.grandchildren).to eq [grandchild]
|
|
160
160
|
end
|
|
161
161
|
end
|
|
162
162
|
end
|