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



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

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



112
113
114
# File 'lib/switchman/active_record/query_methods.rb', line 112

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



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

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



64
65
66
# File 'lib/switchman/active_record/query_methods.rb', line 64

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

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

Raises:

  • (ArgumentError)


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

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



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

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


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

def shard_value
  @values[:shard]
end

#shard_value=(value) ⇒ Object



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

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



118
119
120
121
122
# File 'lib/switchman/active_record/query_methods.rb', line 118

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