Module: Simple::SQL::Config
Overview
module to determine database configuration
Instance Method Summary collapse
-
#determine_url(path: nil) ⇒ Object
determines the database_url from either the DATABASE_URL environment setting or a config/database.yml file.
-
#parse_url(url) ⇒ Object
parse a DATABASE_URL, return PG::Connection settings.
Instance Method Details
#determine_url(path: nil) ⇒ Object
determines the database_url from either the DATABASE_URL environment setting or a config/database.yml file.
30 31 32 33 34 35 36 37 38 |
# File 'lib/simple/sql/config.rb', line 30 def determine_url(path: nil) if path database_url_from_database_yml(path) elsif ENV["DATABASE_URL"] ENV["DATABASE_URL"] else database_url_from_database_yml("config/database.yml") end end |
#parse_url(url) ⇒ Object
parse a DATABASE_URL, return PG::Connection settings.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/simple/sql/config.rb', line 10 def parse_url(url) expect! url => /^postgres(ql)?s?:\/\// require "uri" uri = URI.parse(url) raise ArgumentError, "Invalid URL #{url}" unless uri.hostname && uri.path config = { dbname: uri.path.sub(%r{^/}, ""), host: uri.hostname } config[:port] = uri.port if uri.port config[:user] = uri.user if uri.user config[:password] = uri.password if uri.password config[:sslmode] = uri.scheme == "postgress" || uri.scheme == "postgresqls" ? "require" : "prefer" config end |