Skip to content
Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Rails/ActiveRecord-style config/database.yml hashes.
Ruby
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec Fix deprecation warning for mysql2 Dec 10, 2015
.gitignore bundle gem database_url --test Sep 19, 2013
.rspec bundle gem database_url --test Sep 19, 2013
.travis.yml bundle gem database_url --test Sep 19, 2013
CHANGELOG v0.1.2 - fix deprec on mysql2 Jun 12, 2017
Gemfile bundle gem database_url --test Sep 19, 2013
LICENSE.txt bundle gem database_url --test Sep 19, 2013
README.md DatabaseUrl.{to_active_record_hash,to_active_record_url,to_sequel_has… Jul 12, 2014
Rakefile bundle gem database_url --test Sep 19, 2013
database_url.gemspec correct gem description Jul 12, 2014

README.md

DatabaseUrl

Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Rails/ActiveRecord-style config/database.yml config hashes.

Usage

Methods

DatabaseUrl.to_active_record_hash(url = nil)
DatabaseUrl.to_active_record_url(hash)
DatabaseUrl.to_sequel_hash(url = nil)
DatabaseUrl.to_sequel_url(hash)

Get DATABASE_URL from config hash

You pass it a hash (string or symbol keys, doesn't matter) and it gives you a URL:

>> c = YAML.load_file('/path/to/config/database.yml')
=> {"development"=>{"adapter"=>"mysql2", "database"=>"myapp_development", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "test"=>{"adapter"=>"mysql2", "database"=>"myapp_test", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "production"=>{"adapter"=>"mysql2", "database"=>"myapp", "username"=>"myapp", "password"=>"XXX", "encoding"=>"utf8"}, "cucumber"=>{"adapter"=>"mysql2", "database"=>"myapp_cucumber", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}}
>> DatabaseUrl.to_active_record_url c['development']
=> "mysql2://root:password@127.0.0.1/myapp_development?encoding=utf8"

Get config hash from DATABASE_URL

>> DatabaseUrl.to_active_record_hash('postgres://uuu:xxx@127.0.0.1:1234/abc')
=> {:adapter=>"postgres", :host=>"127.0.0.1", :port=>1234, :database=>"abc", :user=>"uuu", :password=>"xxx"}

If you omit the argument, it will try ENV['DATABASE_URL']

Query parameters supported

  • encoding
  • pool

TODO

  • all query params
  • automatically pull from config/database.yml
  • automatically pull from ActiveRecord::Base.connection_config

Copyright

Copyright 2014 Seamus Abshere

You can’t perform that action at this time.