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

Fix db:create rake tasks #145

Closed rolftimmermans closed 6 years ago

rolftimmermans commented 7 years ago

When running rake db:create with a default sequel-rails installation (testing this with Rails 5.1.4), the following exception occurs:

rake aborted!
Sequel::DatabaseConnectionError: PG::ConnectionBad: FATAL:  database "my_rails_app_development" does not exist

This happens because by default a connection is made to the configured database. This patch checks to see if we are running inside a Rake tasks and does not automatically connect if we do.

The Rake tasks themselves then call db_for_current_env if they need a connection to the database for the current environment. This only sets up a database connection in Rake tasks as needed.

This solves the exception on db:create for us and others remain working.

jdanielian commented 6 years ago

I'm also running into this issue when trying to upgrade to the latest 1.0 version of the gem. I can't issue a rake sequel:drop , rake sequel:create and have it just work. Now it is stuck with an error saying that the database needs to exist.

I was able to get around these issues by adding test: false as a property to the database.yml. This reverts the connection behavior to sequel 4.x which will NOT attempt to connect to the database by default.

olivierlacan commented 6 years ago

@JonathanTron Could you prioritize merging this as an emergency patch? 1.0 of the gem effectively makes it impossible to use a new Rails app (no schema) at this time because of the attempt to connect prior to db rake tasks.

I've helped document this issue along with @mofmn in this issue.

olivierlacan commented 6 years ago

@rolftimmermans You have duplicated commits in this branch. Could you rebase and merge them so this PR can be merge more easily?

JonathanTron commented 6 years ago

@rolftimmermans @olivierlacan thanks!

olivierlacan commented 6 years ago

@rolftimmermans @JonathanTron I've grabbed the gem from master and somehow this didn't fix the issue for me. I'll try to step through with pry to see what's happening in the railtie.

olivierlacan commented 6 years ago

Figured it out. @rolftimmermans's patch assumes rake command is invoked when Rails 5 starts to proxy rails db:create to rake db:create and makes direct calls to rake unnecessary.

See Rails Guides: image

rolftimmermans commented 6 years ago

Correct, I ran into the same thing.

I now have the equivalent of this code:

if !defined?(Rails::Server) && (Rails.env.development? || Rails.env.test?)
  config.sequel.skip_connect = true
end

That seems to work fine.

So presumably we can use something like this instead of this PR:

if defined?(Rails::Server) || (!Rails.env.development? && !Rails.env.test?)
  ::SequelRails.setup ::Rails.env unless app.config.sequel[:skip_connect]
end
olivierlacan commented 6 years ago

@rolftimmermans See https://github.com/TalentBox/sequel-rails/pull/148. I think we can simply set skip_connect when appropriate instead of wrapping a complex condition around the SequelRails.setup command.

I couldn't think of a reason why we'd have to avoid a connection attempt to the database than when we're trying to create it, and maybe drop it. I could clearly be forgetting something, but for me #148 fixes my db:create issues.