Module: Switchman::ActiveRecord::QueryMethods
- Defined in:
- lib/switchman/active_record/query_methods.rb
Instance Method Summary collapse
-
#all_shards ⇒ Object
the shard value as an array or a relation.
- #or(other) ⇒ Object
-
#primary_shard ⇒ Object
the shard that where_values are relative to.
- #shard(value, source = :explicit) ⇒ Object
- #shard!(value, source = :explicit) ⇒ Object
- #shard_source_value ⇒ Object
- #shard_source_value=(value) ⇒ Object
-
#shard_value ⇒ Object
shard_value is one of: A shard An array or relation of shards An AR object (query runs against that object’s associated_shards) shard_source_value is one of: :implicit - inferred from current shard when relation was created, or primary key where clause :explicit - explicit set on the relation :association - a special value that scopes from associations use to use slightly different logic for foreign key transposition :to_a - a special value that Relation#to_a uses when querying multiple shards to remove primary keys from conditions that aren’t applicable to the current shard.
- #shard_value=(value) ⇒ Object
Instance Method Details
#all_shards ⇒ Object
the shard value as an array or a relation
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/switchman/active_record/query_methods.rb', line 75 def all_shards case shard_value when Shard, DefaultShard [shard_value] when ::ActiveRecord::Base shard_value.respond_to?(:associated_shards) ? shard_value.associated_shards : [shard_value.shard] when nil [Shard.current(klass.connection_class_for_self)] else shard_value end end |
#or(other) ⇒ Object
88 89 90 |
# File 'lib/switchman/active_record/query_methods.rb', line 88 def or(other) super(other.shard(primary_shard)) end |
#primary_shard ⇒ Object
the shard that where_values are relative to. if it’s multiple shards, they’re stored relative to the first shard
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/switchman/active_record/query_methods.rb', line 56 def primary_shard case shard_value when Shard, DefaultShard shard_value # associated_shards when ::ActiveRecord::Base shard_value.shard when Array shard_value.first when ::ActiveRecord::Relation Shard.default when nil Shard.current(klass.connection_class_for_self) else raise ArgumentError, "invalid shard value #{shard_value}" end end |
#shard(value, source = :explicit) ⇒ Object
37 38 39 |
# File 'lib/switchman/active_record/query_methods.rb', line 37 def shard(value, source = :explicit) spawn.shard!(value, source) end |
#shard!(value, source = :explicit) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/switchman/active_record/query_methods.rb', line 41 def shard!(value, source = :explicit) raise ArgumentError, "shard can't be nil" unless value old_primary_shard = primary_shard self.shard_value = value self.shard_source_value = source if old_primary_shard != primary_shard || source == :to_a transpose_clauses(old_primary_shard, primary_shard, remove_nonlocal_primary_keys: source == :to_a) end self end |
#shard_source_value ⇒ Object
21 22 23 |
# File 'lib/switchman/active_record/query_methods.rb', line 21 def shard_source_value @values[:shard_source] end |
#shard_source_value=(value) ⇒ Object
31 32 33 34 35 |
# File 'lib/switchman/active_record/query_methods.rb', line 31 def shard_source_value=(value) raise ::ActiveRecord::ImmutableRelation if @loaded @values[:shard_source] = value end |
#shard_value ⇒ Object
shard_value is one of:
A shard
An array or relation of shards
An AR object (query runs against that object's associated_shards)
shard_source_value is one of:
:implicit - inferred from current shard when relation was created, or primary key where clause
:explicit - explicit set on the relation
:association - a special value that scopes from associations use to use slightly different logic
for foreign key transposition
:to_a - a special value that Relation#to_a uses when querying multiple shards to
remove primary keys from conditions that aren't applicable to the current shard
17 18 19 |
# File 'lib/switchman/active_record/query_methods.rb', line 17 def shard_value @values[:shard] end |
#shard_value=(value) ⇒ Object
25 26 27 28 29 |
# File 'lib/switchman/active_record/query_methods.rb', line 25 def shard_value=(value) raise ::ActiveRecord::ImmutableRelation if @loaded @values[:shard] = value end |