TalentBox / sequel-rails

A gem for using Sequel with Rails 5.x, 6.x, 7.x, 8.x
http://talentbox.github.io/sequel-rails/
MIT License
326 stars 81 forks source link

Database does not exist error on rails db:create ( introduced somewhere after 0.9.17) #146

Closed mofmn closed 6 years ago

mofmn commented 6 years ago

Got the error mentioned in the title when coming back to projects after a little hiatus. Old project worked, new project refused to create database due to 'database missing', traced it back to updating from sequel-rails 0.9.17 to 1.0.0

Not really got any more info than that, except the stack for the error -

Sequel::DatabaseConnectionError: PG::ConnectionBad: FATAL: database "meespace_development" does not exist /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/adapters/postgres.rb:190:ininitialize' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/adapters/postgres.rb:190:in new' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/adapters/postgres.rb:190:inconnect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool.rb:126:in make_new' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool/threaded.rb:192:inassign_connection' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool/threaded.rb:133:in acquire' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool/threaded.rb:90:inhold' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/database/connecting.rb:264:in synchronize' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/database/connecting.rb:279:intest_connection' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/database/connecting.rb:58:in connect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/core.rb:116:inconnect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-rails-1.0.0/lib/sequel_rails/configuration.rb:60:in connect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-rails-1.0.0/lib/sequel_rails/configuration.rb:8:insetup' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-rails-1.0.0/lib/sequel_rails/railtie.rb:71:in block in <class:Railtie>' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:30:ininstance_exec' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:30:in run' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:55:inblock in run_initializers' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:54:in run_initializers' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:352:ininitialize!' /Users/meredith/meespace/config/environment.rb:5:in <top (required)>' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:328:inrequire' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:328:in require_environment!' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:448:inblock in run_tasks_blocks' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands/rake_proxy.rb:14:in block in run_rake_task' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands/rake_proxy.rb:11:inrun_rake_task' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands/commands_tasks.rb:51:in run_command!' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands.rb:18:in<top (required)>' bin/rails:4:in require' bin/rails:4:in

' PG::ConnectionBad: FATAL: database "meespace_development" does not exist /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/adapters/postgres.rb:190:in initialize' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/adapters/postgres.rb:190:innew' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/adapters/postgres.rb:190:in connect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool.rb:126:inmake_new' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool/threaded.rb:192:in assign_connection' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool/threaded.rb:133:inacquire' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/connection_pool/threaded.rb:90:in hold' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/database/connecting.rb:264:insynchronize' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/database/connecting.rb:279:in test_connection' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/database/connecting.rb:58:inconnect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-5.4.0/lib/sequel/core.rb:116:in connect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-rails-1.0.0/lib/sequel_rails/configuration.rb:60:inconnect' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-rails-1.0.0/lib/sequel_rails/configuration.rb:8:in setup' /Users/meredith/.rvm/gems/ruby-head/gems/sequel-rails-1.0.0/lib/sequel_rails/railtie.rb:71:inblock in ' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:30:in instance_exec' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:30:inrun' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:55:in block in run_initializers' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/initializable.rb:54:inrun_initializers' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:352:in initialize!' /Users/meredith/meespace/config/environment.rb:5:in<top (required)>' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:328:in require' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:328:inrequire_environment!' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/application.rb:448:in block in run_tasks_blocks' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands/rake_proxy.rb:14:inblock in run_rake_task' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands/rake_proxy.rb:11:in run_rake_task' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands/commands_tasks.rb:51:inrun_command!' /Users/meredith/.rvm/gems/ruby-head/gems/railties-5.0.4/lib/rails/commands.rb:18:in <top (required)>' bin/rails:4:inrequire' bin/rails:4:in <main>' Tasks: TOP => db:create => environment (See full trace by running task with --trace)

olivierlacan commented 6 years ago

This is definitely an issue I've encountered as well. It looks like sequel-rails is trying to initialize the connection to Postgres when it is preloaded by spring which prevents the rails db:create command from ever succeeding.

Somehow I believe that ActiveRecord avoids this issue but I haven't yet been able to establish how:

https://github.com/rails/rails/blob/d7f9adf7941574d0a40a165739938d28f278bd77/activerecord/lib/active_record/railtie.rb#L129-L150

It does rescue ActiveRecord::NoDatabaseError to print some useful feedback (which I think sequel-rails should also do) but I haven't yet caught the logic that prevents a connection from being attempted when rake db:create is called.

ms-ati commented 6 years ago

It would be amazing if this could be fixed. And then, how about in rake db:schema:load?

olivierlacan commented 6 years ago

@ms-ati Do you get an issue in schema:load? It feels like you would want a database connection for that since it shouldn't be possible to load the schema before the db is created. Right?

ms-ati commented 6 years ago

On every single schema:load, we get the warning:

SEQUEL DEPRECATION WARNING: Setting a model class dataset to an invalid dataset is deprecated and will be removed in Sequel 5.  Either use a valid dataset or set require_valid_table = false for the model class.
/bundle/gems/sequel-4.49.0/lib/sequel/model/base.rb:1008:in `rescue in check_non_connection_error'
/bundle/gems/sequel-4.49.0/lib/sequel/model/base.rb:1001:in `check_non_connection_error'
/bundle/gems/sequel-4.49.0/lib/sequel/model/base.rb:1123:in `block in get_db_schema'
/bundle/gems/sequel-4.49.0/lib/sequel/model/base.rb:1156:in `get_db_schema'
/bundle/gems/sequel-4.49.0/lib/sequel/model/base.rb:893:in `set_dataset'
/bundle/gems/sequel-4.49.0/lib/sequel/model/base.rb:704:in `inherited'
/bundle/gems/sequel-4.49.0/lib/sequel/plugins/subclasses.rb:70:in `inherited'
/app/app/models/user.rb:8:in `<top (required)>'
/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:476:in `load'
/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:476:in `block in load_file'
ms-ati commented 6 years ago

@olivierlacan I suspect this is from the Devise railtie loading the User model? But we have to see this warning on every single test run in CI, etc.

olivierlacan commented 6 years ago

@ms-ati This feels like an unrelated issue. Might be worth opening a separate issue?