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.
- #shard_value=(value) ⇒ Object
-
#where!(opts, *rest) ⇒ Object
use a temp variable so that the new where clause is built before self.where_clause is read, since build_where_clause might mutate self.where_clause.
Instance Method Details
#all_shards ⇒ Object
the shard value as an array or a relation
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/switchman/active_record/query_methods.rb', line 70 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
83 84 85 |
# File 'lib/switchman/active_record/query_methods.rb', line 83 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/switchman/active_record/query_methods.rb', line 51 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
35 36 37 |
# File 'lib/switchman/active_record/query_methods.rb', line 35 def shard(value, source = :explicit) spawn.shard!(value, source) end |
#shard!(value, source = :explicit) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/switchman/active_record/query_methods.rb', line 39 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 transpose_predicates(nil, old_primary_shard, primary_shard) if old_primary_shard != primary_shard self end |
#shard_source_value ⇒ Object
19 20 21 |
# File 'lib/switchman/active_record/query_methods.rb', line 19 def shard_source_value @values[:shard_source] end |
#shard_source_value=(value) ⇒ Object
29 30 31 32 33 |
# File 'lib/switchman/active_record/query_methods.rb', line 29 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
15 16 17 |
# File 'lib/switchman/active_record/query_methods.rb', line 15 def shard_value @values[:shard] end |
#shard_value=(value) ⇒ Object
23 24 25 26 27 |
# File 'lib/switchman/active_record/query_methods.rb', line 23 def shard_value=(value) raise ::ActiveRecord::ImmutableRelation if @loaded @values[:shard] = value end |
#where!(opts, *rest) ⇒ Object
use a temp variable so that the new where clause is built before self.where_clause is read, since build_where_clause might mutate self.where_clause
89 90 91 92 93 |
# File 'lib/switchman/active_record/query_methods.rb', line 89 def where!(opts, *rest) new_clause = build_where_clause(opts, rest) self.where_clause += new_clause self end |