@@ -31,6 +31,62 @@ def resolve(adapter_name) # :nodoc:
31
31
class_name , path_to_adapter = @adapters [ adapter_name . to_s ]
32
32
33
33
unless class_name
34
+ # To provide better error messages for adapters expecting the pre-7.2 adapter registration API, we attempt
35
+ # to load the adapter file from the old location which was required by convention, and then raise an error
36
+ # describing how to upgrade the adapter to the new API.
37
+ legacy_adapter_path = "active_record/connection_adapters/#{ adapter_name } _adapter"
38
+ legacy_adapter_connection_method_name = "#{ adapter_name } _connection" . to_sym
39
+
40
+ begin
41
+ require legacy_adapter_path
42
+ # If we reach here it means we found the found a file that may be the legacy adapter and should raise.
43
+ if ActiveRecord ::ConnectionHandling . method_defined? ( legacy_adapter_connection_method_name )
44
+ # If we find the connection method then we care certain it is a legacy adapter.
45
+ deprecation_message = <<~MSG . squish
46
+ Database configuration specifies '#{ adapter_name } ' adapter but that adapter has not been registered.
47
+ Rails 7.2 has changed the way Active Record database adapters are loaded. The adapter needs to be
48
+ updated to register itself rather than being loaded by convention.
49
+ Ensure that the adapter in the Gemfile is at the latest version. If it is, then the adapter may need to
50
+ be modified.
51
+ See:
52
+ https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters.html#method-c-register
53
+ MSG
54
+
55
+ exception_message = <<~MSG . squish
56
+ Database configuration specifies '#{ adapter_name } ' adapter but that adapter has not been registered.
57
+ Ensure that the adapter in the Gemfile is at the latest version. If it is, then the adapter may need to
58
+ be modified.
59
+ MSG
60
+ else
61
+ # If we do not find the connection method we are much less certain it is a legacy adapter. Even though the
62
+ # file exists in the location defined by convenntion, it does not necessarily mean that file is supposed
63
+ # to define the adapter the legacy way. So raise an error that explains both possibilities.
64
+ deprecation_message = <<~MSG . squish
65
+ Database configuration specifies nonexistent '#{ adapter_name } ' adapter.
66
+ Available adapters are: #{ @adapters . keys . sort . join ( ", " ) } .
67
+ Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary
68
+ adapter gem to your Gemfile if it's not in the list of available adapters.
69
+ Rails 7.2 has changed the way Active Record database adapters are loaded. Ensure that the adapter in
70
+ the Gemfile is at the latest version. If it is up to date, the adapter may need to be modified.
71
+ See:
72
+ https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters.html#method-c-register
73
+ MSG
74
+
75
+ exception_message = <<~MSG . squish
76
+ Database configuration specifies nonexistent '#{ adapter_name } ' adapter.
77
+ Available adapters are: #{ @adapters . keys . sort . join ( ", " ) } .
78
+ Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary
79
+ adapter gem to your Gemfile and that it is at its latest version. If it is up to date, the adapter may
80
+ need to be modified.
81
+ MSG
82
+ end
83
+
84
+ ActiveRecord . deprecator . warn ( deprecation_message )
85
+ raise AdapterNotFound , exception_message
86
+ rescue LoadError => error
87
+ # The adapter was not found in the legacy location so fall through to the error handling for a missing adapter.
88
+ end
89
+
34
90
raise AdapterNotFound , <<~MSG . squish
35
91
Database configuration specifies nonexistent '#{ adapter_name } ' adapter.
36
92
Available adapters are: #{ @adapters . keys . sort . join ( ", " ) } .
0 commit comments