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

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

module UseSqlFunctionForPrimaryKeyLookups
  extend ActiveSupport::Concern

  class_methods do
    def find(*args)
      return super unless Feature.enabled?(:use_sql_functions_for_primary_key_lookups, Feature.current_request)
      return super unless args.one?
      return super if block_given? || primary_key.nil? || scope_attributes?

      return_array = false
      id = args.first

      if id.is_a?(Array)
        return super if id.many?

        return_array = true

        id = id.first
      end

      return super if id.nil? || (id.is_a?(String) && !id.number?)

      from_clause = "find_#{table_name}_by_id(?) #{quoted_table_name}"
      filter_empty_row = "#{quoted_table_name}.#{connection.quote_column_name(primary_key)} IS NOT NULL"
      query = from(from_clause).where(filter_empty_row).limit(1).to_sql
      # Using find_by_sql so we get query cache working
      record = find_by_sql([query, id]).first

      unless record
        message = "Couldn't find #{name} with '#{primary_key}'=#{id}"
        raise(ActiveRecord::RecordNotFound.new(message, name, primary_key, id))
      end

      return_array ? [record] : record
    end
  end
end