Module: Switchman::ActiveRecord::Base::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#clear_query_caches_for_current_threadObject



65
66
67
68
69
# File 'lib/switchman/active_record/base.rb', line 65

def clear_query_caches_for_current_thread
  ::ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
    pool.connection(switch_shard: false).clear_query_cache if pool.active_connection?
  end
end

#current_shardObject

significant change: _don’t_ check if klasses.include?(Base) i.e. other sharded models don’t inherit the current shard of Base



73
74
75
76
77
78
79
# File 'lib/switchman/active_record/base.rb', line 73

def current_shard
  connected_to_stack.reverse_each do |hash|
    return hash[:shard] if hash[:shard] && hash[:klasses].include?(connection_classes)
  end

  default_shard
end

#find_ids_in_ranges(opts = {}, &block) ⇒ Object



9
10
11
12
# File 'lib/switchman/active_record/base.rb', line 9

def find_ids_in_ranges(opts = {}, &block)
  opts.reverse_merge!(loose: true)
  all.find_ids_in_ranges(opts, &block)
end

#integral_id?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/switchman/active_record/base.rb', line 22

def integral_id?
  @integral_id = columns_hash[primary_key]&.type == :integer if @integral_id.nil?
  @integral_id
end

#reset_column_informationObject



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

def reset_column_information
  @sharded_column_values = {}
  super
end

#sharded_modelObject



14
15
16
17
18
19
20
# File 'lib/switchman/active_record/base.rb', line 14

def sharded_model
  self.abstract_class = true

  return if self == UnshardedRecord

  Shard.send(:add_sharded_model, self)
end

#transactionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/switchman/active_record/base.rb', line 27

def transaction(**)
  if self != ::ActiveRecord::Base && current_scope
    current_scope.activate do
      db = Shard.current(connection_classes).database_server
      if ::GuardRail.environment == db.guard_rail_environment
        super
      else
        db.unguard { super }
      end
    end
  else
    db = Shard.current(connection_classes).database_server
    if ::GuardRail.environment == db.guard_rail_environment
      super
    else
      db.unguard { super }
    end
  end
end

#unscopedObject



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

def unscoped
  if block_given?
    super do
      current_scope.shard_value = nil
      yield
    end
  else
    result = super
    result.shard_value = nil
    result
  end
end