Hard exception when database connection opened in routes

What does this MR do and why?

Now that we have addressed all warnings, turn it to a hard exception

This helps prevents database connections not dropped because connections are held open

See also https://github.com/rails/rails/issues/44875

Related issue: #359913 (closed)

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

  1. Add something like Project.first within config/routes.rb
  2. You should see the following error when running bundle exec rails console:
	24: from (eval):3:in `draw_route'
	23: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/querying.rb:22:in `first'
	22: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/relation/finder_methods.rb:122:in `first'
	21: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/relation/finder_methods.rb:538:in `find_nth'
	20: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/relation/finder_methods.rb:545:in `find_nth_with_limit'
	19: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/relation/finder_methods.rb:579:in `ordered_relation'
	18: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/relation/delegation.rb:93:in `primary_key'
	17: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/attribute_methods/primary_key.rb:71:in `primary_key'
	16: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/attribute_methods/primary_key.rb:83:in `reset_primary_key'
	15: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/attribute_methods/primary_key.rb:95:in `get_primary_key'
	14: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/model_schema.rb:380:in `table_exists?'
	13: from /Users/tkuah/code/gdk-ee/gitlab/lib/gitlab/database/load_balancing/connection_proxy.rb:89:in `method_missing'
	12: from /Users/tkuah/code/gdk-ee/gitlab/lib/gitlab/database/load_balancing/connection_proxy.rb:119:in `write_using_load_balancer'
	11: from /Users/tkuah/code/gdk-ee/gitlab/lib/gitlab/database/load_balancing/load_balancer.rb:111:in `read_write'
	10: from /Users/tkuah/code/gdk-ee/gitlab/lib/gitlab/database/load_balancing/load_balancer.rb:184:in `retry_with_backoff'
	 9: from /Users/tkuah/code/gdk-ee/gitlab/lib/gitlab/database/load_balancing/load_balancer.rb:112:in `block in read_write'
	 8: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:428:in `connection'
	 7: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:588:in `checkout'
	 6: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:866:in `acquire_connection'
	 5: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:905:in `try_to_checkout_new_connection'
	 4: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:926:in `checkout_new_connection'
	 3: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `new_connection'
	 2: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `public_send'
	 1: from /Users/tkuah/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
/Users/tkuah/code/gdk-ee/gitlab/config/initializers/00_connection_logger.rb:15:in `new_client': Database connection should not be called during initializers. Read more at https://docs.gitlab.com/ee/development/rails_initializers.html#database-connections-in-initializers (RuntimeError)

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Thong Kuah

Merge request reports

Loading