File: switch.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (57 lines) | stat: -rw-r--r-- 1,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

module Ci
  module Partitionable
    module Switch
      extend ActiveSupport::Concern

      # These methods are cached at the class level and depend on the value
      # of `table_name`, changing that value resets them.
      # `cached_find_by_statement` is used to cache SQL statements which can
      # include the table name.
      #
      SWAPABLE_METHODS = %i[table_name quoted_table_name arel_table
        predicate_builder cached_find_by_statement].freeze

      included do |base|
        partitioned = Class.new(base) do
          self.table_name = base.routing_table_name

          def self.routing_class?
            true
          end
        end

        base.const_set(:Partitioned, partitioned)
      end

      class_methods do
        def routing_class?
          false
        end

        def routing_table_enabled?
          return false if routing_class?

          Gitlab::SafeRequestStore.fetch(routing_table_name_flag) do
            ::Feature.enabled?(routing_table_name_flag, :request, type: :gitlab_com_derisk)
          end
        end

        # We're delegating them to the `Partitioned` model.
        # They do not require any check override since they come from AR core
        # (are always defined) and we're using `super` to get the value.
        #
        SWAPABLE_METHODS.each do |name|
          define_method(name) do |*args, &block|
            if routing_table_enabled?
              self::Partitioned.public_send(name, *args, &block) # rubocop: disable GitlabSecurity/PublicSend
            else
              super(*args, &block)
            end
          end
        end
      end
    end
  end
end