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



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

def clear_query_caches_for_current_thread
  ::ActiveRecord::Base.connection_handlers.each_value do |handler|
    handler.connection_pool_list.each do |pool|
      pool.connection(switch_shard: false).clear_query_cache if pool.active_connection?
    end
  end
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)


29
30
31
32
33
34
# File 'lib/switchman/active_record/base.rb', line 29

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

#reset_column_informationObject



56
57
58
59
# File 'lib/switchman/active_record/base.rb', line 56

def reset_column_information
  @sharded_column_values = {}
  super
end

#shard_categoryObject



14
15
16
# File 'lib/switchman/active_record/base.rb', line 14

def shard_category
  connection_specification_name.to_sym
end

#shard_category=(category) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/switchman/active_record/base.rb', line 18

def shard_category=(category)
  categories = Shard.const_get(:CATEGORIES)
  if categories[shard_category]
    categories[shard_category].delete(self)
    categories.delete(shard_category) if categories[shard_category].empty?
  end
  categories[category] ||= []
  categories[category] << self
  self.connection_specification_name = category.to_s
end

#transactionObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/switchman/active_record/base.rb', line 36

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

#unscopedObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/switchman/active_record/base.rb', line 61

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