Method: ActiveRecord::ConnectionAdapters.resolve

Defined in:
activerecord/lib/active_record/connection_adapters.rb

.resolve(adapter_name) ⇒ Object

:nodoc:



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'activerecord/lib/active_record/connection_adapters.rb', line 26

def resolve(adapter_name) # :nodoc:
  # Require the adapter itself and give useful feedback about
  #   1. Missing adapter gems.
  #   2. Incorrectly registered adapters.
  #   3. Adapter gems' missing dependencies.
  class_name, path_to_adapter = @adapters[adapter_name.to_s]

  unless class_name
    # To provide better error messages for adapters expecting the pre-7.2 adapter registration API, we attempt
    # to load the adapter file from the old location which was required by convention, and then raise an error
    # describing how to upgrade the adapter to the new API.
    legacy_adapter_path = "active_record/connection_adapters/#{adapter_name}_adapter"
    legacy_adapter_connection_method_name = "#{adapter_name}_connection".to_sym

    begin
      require legacy_adapter_path
      # If we reach here it means we found the found a file that may be the legacy adapter and should raise.
      if ActiveRecord::ConnectionHandling.method_defined?(legacy_adapter_connection_method_name)
        # If we find the connection method then we care certain it is a legacy adapter.
        deprecation_message = "          Database configuration specifies '\#{adapter_name}' adapter but that adapter has not been registered.\n          Rails 7.2 has changed the way Active Record database adapters are loaded. The adapter needs to be\n          updated to register itself rather than being loaded by convention.\n          Ensure that the adapter in the Gemfile is at the latest version. If it is, then the adapter may need to\n          be modified.\n          See:\n          https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters.html#method-c-register\n        MSG\n\n        exception_message = <<~MSG.squish\n          Database configuration specifies '\#{adapter_name}' adapter but that adapter has not been registered.\n          Ensure that the adapter in the Gemfile is at the latest version. If it is, then the adapter may need to\n          be modified.\n        MSG\n      else\n        # If we do not find the connection method we are much less certain it is a legacy adapter. Even though the\n        # file exists in the location defined by convenntion, it does not necessarily mean that file is supposed\n        # to define the adapter the legacy way. So raise an error that explains both possibilities.\n        deprecation_message = <<~MSG.squish\n          Database configuration specifies nonexistent '\#{adapter_name}' adapter.\n          Available adapters are: \#{@adapters.keys.sort.join(\", \")}.\n          Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary\n          adapter gem to your Gemfile if it's not in the list of available adapters.\n          Rails 7.2 has changed the way Active Record database adapters are loaded. Ensure that the adapter in\n          the Gemfile is at the latest version. If it is up to date, the adapter may need to be modified.\n          See:\n          https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters.html#method-c-register\n        MSG\n\n        exception_message = <<~MSG.squish\n          Database configuration specifies nonexistent '\#{adapter_name}' adapter.\n          Available adapters are: \#{@adapters.keys.sort.join(\", \")}.\n          Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary\n          adapter gem to your Gemfile and that it is at its latest version. If it is up to date, the adapter may\n          need to be modified.\n        MSG\n      end\n\n      ActiveRecord.deprecator.warn(deprecation_message)\n      raise AdapterNotFound, exception_message\n    rescue LoadError => error\n      # The adapter was not found in the legacy location so fall through to the error handling for a missing adapter.\n    end\n\n    raise AdapterNotFound, <<~MSG.squish\n      Database configuration specifies nonexistent '\#{adapter_name}' adapter.\n      Available adapters are: \#{@adapters.keys.sort.join(\", \")}.\n      Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary\n      adapter gem to your Gemfile if it's not in the list of available adapters.\n    MSG\n  end\n\n  unless Object.const_defined?(class_name)\n    begin\n      require path_to_adapter\n    rescue LoadError => error\n      # We couldn't require the adapter itself.\n      if error.path == path_to_adapter\n        # We can assume here that a non-builtin adapter was specified and the path\n        # registered by the adapter gem is incorrect.\n        raise LoadError, \"Error loading the '\#{adapter_name}' Active Record adapter. Ensure that the path registered by the adapter gem is correct. \#{error.message}\", error.backtrace\n      else\n        # Bubbled up from the adapter require. Prefix the exception message\n        # with some guidance about how to address it and reraise.\n        raise LoadError, \"Error loading the '\#{adapter_name}' Active Record adapter. Missing a gem it depends on? \#{error.message}\", error.backtrace\n      end\n    end\n  end\n\n  begin\n    Object.const_get(class_name)\n  rescue NameError => error\n    raise AdapterNotFound, \"Could not load the \#{class_name} Active Record adapter (\#{error.message}).\"\n  end\nend\n".squish