Skip to content

Commit

Permalink
Create an :active_record_connection_adapters load hook which is run a…
Browse files Browse the repository at this point in the history
…fter ActiveRecord::ConnectionAdapters is defined.

Since callbacks defined with :active_record my establish a connection (such as "active_record.initialize_database" in the active record railtie) it can create order dependence since the adapters need to be registered first or else establishing a connection will raise an exception. So, let's define a new load hook that can be used for registering adapters, and run it as soon as ActiveRecord::ConnectionAdapters is defined, before the :active_record hooks are run.
  • Loading branch information
andrewn617 committed Nov 20, 2023
1 parent 0ad26f7 commit b194c77
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Create an `active_record_connection_adapters` load hook which is run after
`ActiveRecord::ConnectionAdapters` is defined. It can be used to register custom
connection adapters.

*Andrew Novoselac*

* `DatabaseConfigurations#configs_for` can accept a symbol in the `name` parameter.

*Andrew Novoselac*
Expand Down
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/connection_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def resolve(adapter_name) # :nodoc:
register "trilogy", "ActiveRecord::ConnectionAdapters::TrilogyAdapter", "active_record/connection_adapters/trilogy_adapter"
register "postgresql", "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter", "active_record/connection_adapters/postgresql_adapter"

ActiveSupport.run_load_hooks(:active_record_connection_adapters)

eager_autoload do
autoload :AbstractAdapter
end
Expand Down
1 change: 1 addition & 0 deletions guides/source/engines.md
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,7 @@ These are the load hooks you can use in your own code. To hook into the initiali
| `ActiveModel::Model` | `active_model` |
| `ActiveRecord::Base` | `active_record` |
| `ActiveRecord::TestFixtures` | `active_record_fixtures` |
| `ActiveRecord::ConnectionAdapters` | `active_record_connection_adapters` |
| `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter` | `active_record_postgresqladapter` |
| `ActiveRecord::ConnectionAdapters::Mysql2Adapter` | `active_record_mysql2adapter` |
| `ActiveRecord::ConnectionAdapters::TrilogyAdapter` | `active_record_trilogyadapter` |
Expand Down

0 comments on commit b194c77

Please sign in to comment.