Rework with_disabled_database_connections to work with Rails 7.0
What does this MR do and why?
Note: the original motivation is from #359913 (closed)
Note 2: Due to changes, the method is now renamed to raise_if_new_database_connection
In Rails 7.0, attribute
definitions in models now run at class
definition time, and calls model.connection_db_config
. This means
swapping the connection_handler now breaks because there's no valid
connection_pool within with_disabled_database_connections block. See
https://github.com/rails/rails/pull/41166, and https://github.com/rails/rails/pull/41302
On investigation, .connection_pool
, and .connection_db_config
does
not create a database connection (model.connection does).
In order to have this keep working in Rails 7.0, we switch to counting the number of connections before, and after instead.
Also to prevent
re-use of connections, we also disconnect all active connections first (already implemented prior, see clear_active_connections_again
)
Related issue: #389879 (closed)
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
- Add
Project.first
, orCi::Pipeline.first
inside thewith_disabled_database_connections
block inconfig/routes.rb
- You should see an error:
Database connection should not be called during initializers
Also make sure that Rails boots in Rails 7 :
- Check out !90907 (merged)
- Cherry pick commits from this branch
- Reverse the temporary change to
lib/initializer_connections.rb
(!90907 (diffs)) - Test that
bundle exec rails console
still works
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.