Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database connection is still failing when creating a database #14

Closed
adilsoncarvalho opened this issue Nov 2, 2020 · 6 comments
Closed

Comments

@adilsoncarvalho
Copy link

adilsoncarvalho commented Nov 2, 2020

Thank you for providing a fix so quickly.

馃帀 The invocation of flash method is fixed!

Unfortunately, the database problem still happening. I discovered that it only happens when I try to create the database. I created a repo with the vanilla rails API app I used and my docker setup. I hope it helps.

The steps:

  • Try to run rake db:create -- it fails (see trace below)
  • Comment all the lines in config/initializers/sequel.rb
  • Try again to run rake db:create -- it succeeds
  • Uncomment all the lines in config/initializers/sequel.rb
  • Try running rake db:migrate -- it succeeds
  • curl localhost:3000 and it works

I have the following questions:

  • Shouldn't the rodauth routes to be present when I run rails routes? I only got the default Rails routes.
  • Do an API only app require the app/controllers/rodauth_controller.rb file?

Here is the trace for the failed rake db:create. It feels like some other component tries to reach the database before its creation, and then fails because it doesn't exist.

rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "api_development" does not exist
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb:50:in `rescue in postgresql_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb:33:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:189:in `connection'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection.rb:130:in `activerecord_connection'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection.rb:122:in `activerecord_lock'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection.rb:34:in `synchronize'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection/postgres.rb:5:in `synchronize'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/connecting.rb:278:in `test_connection'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/misc.rb:169:in `initialize'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/connecting.rb:57:in `new'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/connecting.rb:57:in `connect'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/core.rb:124:in `connect'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/core.rb:402:in `adapter_method'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/core.rb:409:in `block (2 levels) in def_adapter_method'
/api/config/initializers/sequel.rb:4:in `<main>'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:318:in `block in load'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:318:in `load'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:666:in `block in load_config_initializer'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/notifications.rb:182:in `instrument'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:665:in `load_config_initializer'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:625:in `block (2 levels) in <class:Engine>'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:624:in `each'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:624:in `block in <class:Engine>'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:32:in `instance_exec'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:32:in `run'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:61:in `block in run_initializers'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:50:in `each'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:50:in `tsort_each_child'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:60:in `run_initializers'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/application.rb:363:in `initialize!'
/api/config/environment.rb:5:in `<main>'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/usr/local/bundle/gems/zeitwerk-2.4.1/lib/zeitwerk/kernel.rb:33:in `require'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/application.rb:339:in `require_environment!'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/application.rb:523:in `block in run_tasks_blocks'

Caused by:
PG::ConnectionBad: FATAL:  database "api_development" does not exist
/usr/local/bundle/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'
/usr/local/bundle/gems/pg-1.2.3/lib/pg.rb:58:in `new'
/usr/local/bundle/gems/pg-1.2.3/lib/pg.rb:58:in `connect'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb:46:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:189:in `connection'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection.rb:130:in `activerecord_connection'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection.rb:122:in `activerecord_lock'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection.rb:34:in `synchronize'
/usr/local/bundle/gems/sequel-activerecord_connection-1.0.1/lib/sequel/extensions/activerecord_connection/postgres.rb:5:in `synchronize'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/connecting.rb:278:in `test_connection'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/misc.rb:169:in `initialize'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/connecting.rb:57:in `new'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/database/connecting.rb:57:in `connect'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/core.rb:124:in `connect'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/core.rb:402:in `adapter_method'
/usr/local/bundle/gems/sequel-5.38.0/lib/sequel/core.rb:409:in `block (2 levels) in def_adapter_method'
/api/config/initializers/sequel.rb:4:in `<main>'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:318:in `block in load'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:318:in `load'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:666:in `block in load_config_initializer'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/notifications.rb:182:in `instrument'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:665:in `load_config_initializer'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:625:in `block (2 levels) in <class:Engine>'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:624:in `each'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/engine.rb:624:in `block in <class:Engine>'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:32:in `instance_exec'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:32:in `run'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:61:in `block in run_initializers'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:50:in `each'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:50:in `tsort_each_child'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/initializable.rb:60:in `run_initializers'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/application.rb:363:in `initialize!'
/api/config/environment.rb:5:in `<main>'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.5.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/usr/local/bundle/gems/zeitwerk-2.4.1/lib/zeitwerk/kernel.rb:33:in `require'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/application.rb:339:in `require_environment!'
/usr/local/bundle/gems/railties-6.0.3.4/lib/rails/application.rb:523:in `block in run_tasks_blocks'
Tasks: TOP => db:create => db:load_config => environment
(See full trace by running task with --trace)

Originally posted by @adilsoncarvalho in #13 (comment)

@adilsoncarvalho
Copy link
Author

Oh, I see that the routes not showing is expected as mentioned at #5. It seems to see the source code of rodauth is the only way for now. 馃槥

@janko
Copy link
Owner

janko commented Nov 2, 2020

For the database connection issue, add test: false in config/initializers/sequel.rb:

DB = Sequel.postgres(extensions: :activerecord_connection, test: false)

I'll probably update rodauth-rails to include test: false as well by default, as I haven't accounted for the rails db:create task.

Do an API only app require the app/controllers/rodauth_controller.rb file?

No, it doesn't. Feel free to remove it, and remove the rails_controller { RodauthController } line in your rodauth_app.rb. I'll update rodauth-rails to do this as well.

@janko
Copy link
Owner

janko commented Nov 2, 2020

Yes, the fact that a complete list of routes isn't mentioned in Rodauth documentation is not ideal. I'll think about how this can be addressed.

@adilsoncarvalho
Copy link
Author

I am testing locally to create the database with test: false in config/initializers/sequel.rb.

@adilsoncarvalho
Copy link
Author

馃帀 Yeap. Works flawlessly. Thanks for helping me with adding rodauth in my app.

rake db:create

Created database 'api_development'
Created database 'api_test'

adilsoncarvalho referenced this issue in adilsoncarvalho/rodauth-rails-test Nov 2, 2020
Details in janko/rodauth-rails/issues#14
@janko
Copy link
Owner

janko commented Nov 2, 2020

That's good to hear!

I've addressed both of the these issues (test: false and RodauthController) in 0.4.1, so I'm closing this one.

@janko janko closed this as completed Nov 2, 2020
adilsoncarvalho added a commit to adilsoncarvalho/rodauth-demo-rails that referenced this issue Nov 3, 2020
When creating a new database this code might fail. This was addressed as
mentioned at janko/rodauth-rails#14.
janko pushed a commit to janko/rodauth-demo-rails that referenced this issue Nov 3, 2020
* Bumping rodauth-rails version to 0.4.0

* Preventing database connection errors

When creating a new database this code might fail. This was addressed as
mentioned at janko/rodauth-rails#14.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants