Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reset_on_column_errors.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ace52ebff50acb99a2471d4a47a85a8f1e21cba (plain)
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
# frozen_string_literal: true

module ResetOnColumnErrors
  extend ActiveSupport::Concern

  MAX_RESET_PERIOD = 10.minutes

  included do |base|
    base.rescue_from ActiveRecord::StatementInvalid, with: :reset_on_union_error
    base.rescue_from ActiveModel::UnknownAttributeError, with: :reset_on_unknown_attribute_error

    base.class_attribute :previous_reset_columns_from_error
  end

  class_methods do
    def do_reset(exception)
      class_to_be_reset = base_class

      class_to_be_reset.reset_column_information
      Gitlab::ErrorTracking.log_exception(exception, { reset_model_name: class_to_be_reset.name })

      class_to_be_reset.previous_reset_columns_from_error = Time.current
    end

    def reset_on_union_error(exception)
      if exception.message.include?("each UNION query must have the same number of columns") && should_reset?
        do_reset(exception)
      end

      raise
    end

    def should_reset?
      return false if base_class.previous_reset_columns_from_error? &&
        base_class.previous_reset_columns_from_error > MAX_RESET_PERIOD.ago

      Feature.enabled?(:reset_column_information_on_statement_invalid, type: :ops)
    end
  end

  def reset_on_union_error(exception)
    self.class.reset_on_union_error(exception)
  end

  def reset_on_unknown_attribute_error(exception)
    self.class.do_reset(exception) if self.class.should_reset?

    raise
  end
end