Module: Switchman::ActiveRecord::QueryMethods

Defined in:
lib/switchman/active_record/query_methods.rb

Defined Under Namespace

Classes: NonTransposingValue

Instance Method Summary collapse

Instance Method Details

#all_shardsObject

the shard value as an array or a relation



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/switchman/active_record/query_methods.rb', line 83

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



96
97
98
# File 'lib/switchman/active_record/query_methods.rb', line 96

def or(other)
  super(other.shard(primary_shard))
end

#primary_shardObject

the shard that where_values are relative to. if it’s multiple shards, they’re stored relative to the first shard



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/switchman/active_record/query_methods.rb', line 64

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



48
49
50
# File 'lib/switchman/active_record/query_methods.rb', line 48

def shard(value, source = :explicit)
  spawn.shard!(value, source)
end

#shard!(value, source = :explicit) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
# File 'lib/switchman/active_record/query_methods.rb', line 52

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_valueObject



32
33
34
# File 'lib/switchman/active_record/query_methods.rb', line 32

def shard_source_value
  @values[:shard_source]
end

#shard_source_value=(value) ⇒ Object

Raises:

  • (::ActiveRecord::ImmutableRelation)


42
43
44
45
46
# File 'lib/switchman/active_record/query_methods.rb', line 42

def shard_source_value=(value)
  raise ::ActiveRecord::ImmutableRelation if @loaded

  @values[:shard_source] = value
end

#shard_valueObject

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


28
29
30
# File 'lib/switchman/active_record/query_methods.rb', line 28

def shard_value
  @values[:shard]
end

#shard_value=(value) ⇒ Object

Raises:

  • (::ActiveRecord::ImmutableRelation)


36
37
38
39
40
# File 'lib/switchman/active_record/query_methods.rb', line 36

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



102
103
104
105
106
# File 'lib/switchman/active_record/query_methods.rb', line 102

def where!(opts, *rest)
  new_clause = build_where_clause(opts, rest)
  self.where_clause += new_clause
  self
end