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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/switchman/active_record/query_methods.rb', line 97

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



110
111
112
# File 'lib/switchman/active_record/query_methods.rb', line 110

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



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

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



62
63
64
# File 'lib/switchman/active_record/query_methods.rb', line 62

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

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

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
# File 'lib/switchman/active_record/query_methods.rb', line 66

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



30
31
32
# File 'lib/switchman/active_record/query_methods.rb', line 30

def shard_source_value
  @values[:shard_source]
end

#shard_source_value=(value) ⇒ Object



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

def shard_source_value=(value)
  if @loaded
    error_class = if ::Rails.version < "7.2"
                    ::ActiveRecord::ImmutableRelation
                  else
                    ::ActiveRecord::UnmodifiableRelation
                  end

    raise error_class
  end

  @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


26
27
28
# File 'lib/switchman/active_record/query_methods.rb', line 26

def shard_value
  @values[:shard]
end

#shard_value=(value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/switchman/active_record/query_methods.rb', line 34

def shard_value=(value)
  if @loaded
    error_class = if ::Rails.version < "7.2"
                    ::ActiveRecord::ImmutableRelation
                  else
                    ::ActiveRecord::UnmodifiableRelation
                  end

    raise error_class
  end

  @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



116
117
118
119
120
# File 'lib/switchman/active_record/query_methods.rb', line 116

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