rails icon indicating copy to clipboard operation
rails copied to clipboard

Rails 7 weird `ActiveRecord::NoDatabaseError` during development

Open masterkain opened this issue 1 year ago • 0 comments

I have no idea how to reproduce this but I have a pretty basic Rails 7.0.3.1 app, connected to a postgres.

occasionally during normal development the database cannot be found, one or two browser refresh and the request goes through. it's the first time I've seen this. log doesn't say what it is issuing to the database to make it fail. database has nothing going on.

edit: I just noticed it can also happen at rails boot.

using puma in multithreaded mode, relevant config:

max_threads_count = ENV.fetch('RAILS_MAX_THREADS') { 15 }
min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count }
threads min_threads_count, max_threads_count
preload_app!
workers ENV.fetch('WEB_CONCURRENCY') { 5 }
before_fork { ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord) }
on_worker_boot { ActiveRecord::Base.establish_connection if defined?(ActiveRecord) }

attached three requests in succession, first two fails, third goes through.

Started GET "/school_adoptions?q%5Bscholastic_year_eq%5D=2020" for 192.168.144.1 at 2022-08-10 16:25:33 +0000
Processing by SchoolAdoptionsController#index as HTML
  Parameters: {"q"=>{"scholastic_year_eq"=>"2020"}}
Completed 500 Internal Server Error in 23ms (Allocations: 2564)

ActiveRecord::NoDatabaseError (We could not find your database: opendata_development. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create
):

app/models/school_adoption.rb:73:in `block in <class:SchoolAdoption>'
app/controllers/application_controller.rb:6:in `load_menu_data'
Finished "/cable/" [WebSocket] for 192.168.144.1 at 2022-08-10 16:25:34 +0000
Turbo::StreamsChannel stopped streaming from hotwire-livereload
Finished "/cable/" [WebSocket] for 192.168.144.1 at 2022-08-10 16:25:34 +0000
Hotwire::Livereload::ReloadChannel stopped streaming from hotwire-reload
Started GET "/school_adoptions?q%5Bscholastic_year_eq%5D=2020" for 192.168.144.1 at 2022-08-10 16:29:22 +0000
Processing by SchoolAdoptionsController#index as HTML
  Parameters: {"q"=>{"scholastic_year_eq"=>"2020"}}
Completed 500 Internal Server Error in 15ms (Allocations: 2560)

ActiveRecord::NoDatabaseError (We could not find your database: opendata_development. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create
):

app/controllers/application_controller.rb:6:in `load_menu_data'
Started GET "/school_adoptions?q%5Bscholastic_year_eq%5D=2020" for 192.168.144.1 at 2022-08-10 16:29:24 +0000
Processing by SchoolAdoptionsController#index as HTML
  Parameters: {"q"=>{"scholastic_year_eq"=>"2020"}}
  SchoolAdoption Count (5.5ms)  SELECT COUNT(*) FROM "school_adoptions" WHERE "school_adoptions"."scholastic_year" = 2019

masterkain avatar Aug 10 '22 23:08 masterkain

Hey @masterkain is it possible this has been happening for some time but the error was different? The error changed in this PR https://github.com/rails/rails/pull/39723.

I also found this issue https://github.com/rails/rails/issues/41606 - it sounds like it could be a similar problem and that you don't have a connection established but are getting an incorrect error. I didn't find any other issues that are potentially related and without a reproduction and a full backtrace it's hard to say where it's coming from. We definitely need more information to help.

eileencodes avatar Aug 11 '22 00:08 eileencodes

Testing will require some time since it appears to be random but after commenting this option in config/development.rb:

config.action_view.annotate_rendered_view_with_filenames = true

I was not able to reproduce the database error, at least for now. If I recall correctly ths option is commented by default in a new rails 7 app and I'm unsure how it can even be related to a database issue.

I'll reopen if I have more info or a reproducible test case.

masterkain avatar Aug 11 '22 03:08 masterkain

100% an issue with local docker

masterkain avatar Aug 11 '22 07:08 masterkain