Search code examples
rubysinatrasequel

Use DATABASE_URL by default with Sinatra/Sequel extension


The README for sinatra/sequel states that the set :database statement can be omitted and it will use the DATABASE_URL environment variable by default. Looking at the source, this appears to be true. However, omitting this line causes the following error when it tries to load my model definitions:

.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sequel-4.25.0/lib/sequel/model/base.rb:226:
in `db': No database associated with Sequel::Model: have you called Sequel.connect 
or Sequel::Model.db= ? (Sequel::Error)

I realize I'm being a bit pedantic by wanting to omit this line (and not have to write :set database, ENV['DATABASE_URL']) but for the sake of learning is there a way to invoke the default behavior of this module? I asked the question over here as well. Thanks in advance.


Solution

  • As I answered in the linked GitHub issue, the best way to do it is to simply call database from the configure block, and this will invoke the default logic.

    configure do
      abort 'Unable to establish database connection' unless database
    end