activerecord-turntable 2.4.0 → 2.5.0
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/CHANGELOG.md +6 -0
- data/lib/active_record/turntable.rb +1 -0
- data/lib/active_record/turntable/active_record_ext.rb +1 -1
- data/lib/active_record/turntable/active_record_ext/association.rb +16 -123
- data/lib/active_record/turntable/sharding_condition.rb +23 -0
- data/lib/active_record/turntable/version.rb +1 -1
- data/spec/active_record/turntable/active_record_ext/association_spec.rb +12 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bae4fd19e4b7b55c12f1793b954e711e0c9693b
|
4
|
+
data.tar.gz: b5da25ad60b9b02b3d6483f59d940a87a832d65f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bef5cc2a9020bfb5f7158cef8c6935359c3bc77d8e9f7b28dfc88375eccdb9dfde5f7ae2becbf48fc07af6fd793b492f3c13735c982cfab9f1e501939c6550a
|
7
|
+
data.tar.gz: 3fda72ec78a460652cd460426f04a35bdb473f96f3bcd1241e594fc5ba401e5c619789dbe28d16652dd649eb78b98bb60ebd98919a691bee43eab4c25c8814c8
|
data/CHANGELOG.md
CHANGED
@@ -31,7 +31,7 @@ module ActiveRecord::Turntable
|
|
31
31
|
include ConnectionHandlerExtension
|
32
32
|
end
|
33
33
|
ActiveRecord::Associations::Preloader::Association.send(:include, AssociationPreloader)
|
34
|
-
ActiveRecord::Associations::Association.send(:
|
34
|
+
ActiveRecord::Associations::Association.send(:prepend, Association)
|
35
35
|
require 'active_record/turntable/active_record_ext/fixtures'
|
36
36
|
require 'active_record/turntable/active_record_ext/migration_proxy'
|
37
37
|
require 'active_record/turntable/active_record_ext/activerecord_import_ext'
|
@@ -1,139 +1,32 @@
|
|
1
|
-
require
|
1
|
+
require "active_record/associations"
|
2
2
|
|
3
3
|
module ActiveRecord::Turntable
|
4
4
|
module ActiveRecordExt
|
5
5
|
module Association
|
6
|
-
|
6
|
+
include ShardingCondition
|
7
7
|
|
8
|
-
|
9
|
-
ActiveRecord::Associations::
|
10
|
-
ActiveRecord::Associations::CollectionAssociation.send(:include, CollectionAssociationExt)
|
11
|
-
ActiveRecord::Associations::Builder::Association.valid_options += [:foreign_shard_key]
|
8
|
+
def self.prepended(mod)
|
9
|
+
ActiveRecord::Associations::Builder::Association.valid_options << :foreign_shard_key
|
12
10
|
end
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
def turntable_scope(scope, bind = nil)
|
17
|
-
if should_use_shard_key?
|
18
|
-
scope = scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
|
19
|
-
end
|
20
|
-
scope
|
21
|
-
end
|
22
|
-
|
23
|
-
module SingularAssociationExt
|
24
|
-
extend ActiveSupport::Concern
|
25
|
-
|
26
|
-
included do
|
27
|
-
if Util.ar42_or_later?
|
28
|
-
alias_method_chain :get_records, :turntable
|
29
|
-
else
|
30
|
-
alias_method_chain :find_target, :turntable
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# @note Override to add sharding condition for singular association
|
35
|
-
if Util.ar42_or_later?
|
36
|
-
def get_records_with_turntable
|
37
|
-
if reflection.scope_chain.any?(&:any?) ||
|
38
|
-
scope.eager_loading? ||
|
39
|
-
klass.current_scope ||
|
40
|
-
klass.default_scopes.any? ||
|
41
|
-
should_use_shard_key? # OPTIMIZE: Use bind values if cachable scope
|
42
|
-
|
43
|
-
return turntable_scope(scope).limit(1).to_a
|
44
|
-
end
|
45
|
-
|
46
|
-
conn = klass.connection
|
47
|
-
sc = reflection.association_scope_cache(conn, owner) do
|
48
|
-
ActiveRecord::StatementCache.create(conn) { |params|
|
49
|
-
as = ActiveRecord::Associations::AssociationScope.create { params.bind }
|
50
|
-
target_scope.merge(as.scope(self, conn)).limit(1)
|
51
|
-
}
|
52
|
-
end
|
53
|
-
|
54
|
-
binds = ActiveRecord::Associations::AssociationScope.get_bind_values(owner, reflection.chain)
|
55
|
-
sc.execute binds, klass, klass.connection
|
56
|
-
end
|
57
|
-
elsif Util.ar41_or_later?
|
58
|
-
def find_target_with_turntable
|
59
|
-
if record = turntable_scope(scope).take
|
60
|
-
set_inverse_instance record
|
61
|
-
end
|
62
|
-
end
|
63
|
-
else
|
64
|
-
def find_target_with_turntable
|
65
|
-
turntable_scope(scope).take.tap { |record| set_inverse_instance(record) }
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
12
|
+
protected
|
69
13
|
|
70
|
-
|
71
|
-
|
14
|
+
# @note Override to pass shard key conditions
|
15
|
+
def target_scope
|
16
|
+
return super unless should_use_shard_key?
|
72
17
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
end
|
18
|
+
scope = klass.where(
|
19
|
+
klass.turntable_shard_key =>
|
20
|
+
owner.send(foreign_shard_key)
|
21
|
+
)
|
22
|
+
super.merge!(scope)
|
79
23
|
end
|
80
24
|
|
81
|
-
private
|
82
|
-
|
83
|
-
if Util.ar42_or_later?
|
84
|
-
def get_records_with_turntable
|
85
|
-
if reflection.scope_chain.any?(&:any?) ||
|
86
|
-
scope.eager_loading? ||
|
87
|
-
klass.current_scope ||
|
88
|
-
klass.default_scopes.any? ||
|
89
|
-
should_use_shard_key? # OPTIMIZE: Use bind values if cachable scope
|
90
|
-
|
91
|
-
return turntable_scope(scope).to_a
|
92
|
-
end
|
93
|
-
|
94
|
-
conn = klass.connection
|
95
|
-
sc = reflection.association_scope_cache(conn, owner) do
|
96
|
-
ActiveRecord::StatementCache.create(conn) { |params|
|
97
|
-
as = ActiveRecord::Associations::AssociationScope.create { params.bind }
|
98
|
-
target_scope.merge as.scope(self, conn)
|
99
|
-
}
|
100
|
-
end
|
101
|
-
|
102
|
-
binds = ActiveRecord::Associations::AssociationScope.get_bind_values(owner, reflection.chain)
|
103
|
-
sc.execute binds, klass, klass.connection
|
104
|
-
end
|
105
|
-
else
|
106
|
-
# @note Override to add sharding condition for collection association
|
107
|
-
def find_target_with_turntable
|
108
|
-
records =
|
109
|
-
if options[:finder_sql]
|
110
|
-
reflection.klass.find_by_sql(custom_finder_sql)
|
111
|
-
else
|
112
|
-
current_scope = scope
|
113
|
-
if should_use_shard_key?
|
114
|
-
current_scope = current_scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
|
115
|
-
end
|
116
|
-
current_scope.to_a
|
117
|
-
end
|
118
|
-
records.each { |record| set_inverse_instance(record) }
|
119
|
-
records
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
25
|
private
|
125
26
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
def should_use_shard_key?
|
131
|
-
same_association_shard_key? || !!options[:foreign_shard_key]
|
132
|
-
end
|
133
|
-
|
134
|
-
def same_association_shard_key?
|
135
|
-
owner.class.turntable_enabled? && klass.turntable_enabled? && foreign_shard_key == klass.turntable_shard_key
|
136
|
-
end
|
27
|
+
def skip_statement_cache?
|
28
|
+
super || should_use_shard_key?
|
29
|
+
end
|
137
30
|
end
|
138
31
|
end
|
139
32
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActiveRecord::Turntable
|
2
|
+
module ShardingCondition
|
3
|
+
private
|
4
|
+
|
5
|
+
def foreign_shard_key
|
6
|
+
options[:foreign_shard_key] || foreign_target_model.turntable_shard_key
|
7
|
+
end
|
8
|
+
|
9
|
+
def foreign_target_model
|
10
|
+
respond_to?(:model) ? model : owner
|
11
|
+
end
|
12
|
+
|
13
|
+
def should_use_shard_key?
|
14
|
+
sharded_by_same_key? || !!options[:foreign_shard_key]
|
15
|
+
end
|
16
|
+
|
17
|
+
def sharded_by_same_key?
|
18
|
+
foreign_target_model.turntable_enabled? &&
|
19
|
+
klass.turntable_enabled? &&
|
20
|
+
foreign_shard_key == klass.turntable_shard_key
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -47,11 +47,20 @@ describe ActiveRecord::Turntable::ActiveRecordExt::Association do
|
|
47
47
|
ActiveRecord::Base.turntable_config.instance_variable_get(:@config)[:raise_on_not_specified_shard_query] = true
|
48
48
|
end
|
49
49
|
let(:cards_user) { CardsUser.where(user: user).first }
|
50
|
+
let(:cards_users_history) { cards_users_histories.find { |history| history.user_id == user.id } }
|
50
51
|
|
51
52
|
context "associated objects has same turntable_key" do
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
context "AssociationRelation#to_a" do
|
54
|
+
subject { cards_user.cards_users_histories.to_a }
|
55
|
+
it { expect { subject }.to_not raise_error }
|
56
|
+
it { is_expected.to include(*cards_users_histories.select { |history| history.cards_user_id == cards_user.id }) }
|
57
|
+
end
|
58
|
+
|
59
|
+
context "AssociationRelation#where" do
|
60
|
+
subject { cards_user.cards_users_histories.where(id: cards_users_history.id).to_a }
|
61
|
+
it { expect { subject }.to_not raise_error }
|
62
|
+
it { is_expected.to include(cards_users_history) }
|
63
|
+
end
|
55
64
|
end
|
56
65
|
|
57
66
|
context "associated objects has different turntable_key" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-turntable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gussan
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-01-
|
12
|
+
date: 2017-01-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -412,6 +412,7 @@ files:
|
|
412
412
|
- lib/active_record/turntable/sequencer/barrage.rb
|
413
413
|
- lib/active_record/turntable/sequencer/mysql.rb
|
414
414
|
- lib/active_record/turntable/shard.rb
|
415
|
+
- lib/active_record/turntable/sharding_condition.rb
|
415
416
|
- lib/active_record/turntable/sql_tree_patch.rb
|
416
417
|
- lib/active_record/turntable/util.rb
|
417
418
|
- lib/active_record/turntable/version.rb
|