Skip to content

Commit

Permalink
Remove the assumption of schema in DATABASE_URL
Browse files Browse the repository at this point in the history
If you set the DATABASE_URL environment variable to `mydatabase` by accident, you end up getting a series of errors that are hard to trace. For example: 

```
warning: already initialized constant ActiveRecord::Base::OrmAdapter
```

Turns out the cascade of errors is due to the error raised by `.tr` being called on `nil`.

This commit makes sure that `scheme` is set before calling `.tr` on it. My previous iteration used `@uri.scheme.try(:tr, '-', '_')` but using the `&&` logical operator is a fair bit faster: http://stackoverflow.com/questions/26655032/try-vs-performance

With this change, the error message becomes much more understandable:

```
FATAL:  database "mydatabase" does not exist (ActiveRecord::NoDatabaseError)
```
  • Loading branch information
jhubert committed Feb 4, 2016
1 parent dfa48f2 commit 7429bc5
Showing 1 changed file with 1 addition and 1 deletion.
Expand Up @@ -33,7 +33,7 @@ class ConnectionUrlResolver # :nodoc:
def initialize(url)
raise "Database URL cannot be empty" if url.blank?
@uri = uri_parser.parse(url)
@adapter = @uri.scheme.tr('-', '_')
@adapter = @uri.scheme && @uri.scheme.tr('-', '_')
@adapter = "postgresql" if @adapter == "postgres"

if @uri.opaque
Expand Down

0 comments on commit 7429bc5

Please sign in to comment.