Last active
August 15, 2024 15:15
-
-
Save pricees/9630464 to your computer and use it in GitHub Desktop.
Rails database connection, url to yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################### | |
# | |
# This method takes a db connection url and returns rails | |
# config YAML | |
# | |
########################################################## | |
require "uri" | |
require "yaml" | |
db_url = "mysql2://ted:[email protected]:3306/test?reconnect=false" | |
uri = URI.parse(ENV["db_url"] || db_url) | |
qs = Hash[URI::decode_www_form(uri.query)] | |
ui = uri.userinfo.split(':') | |
config = { | |
"production" => { | |
"encoding" => qs["encoding"] || "utf-8", | |
"adapter" => uri.scheme, | |
"host" => uri.host, | |
"port" => uri.port || 3306, | |
"database" => uri.path[1..-1], | |
"username" => ui.first, | |
"password" => ui.last, | |
"reconnect" => qs["reconnect"] || true, | |
"pool" => qs["pool"] || 5, | |
}.to_yaml | |
puts config | |
# --- | |
# production: | |
# encoding: utf-8 | |
# adapter: mysql2 | |
# host: example.net | |
# port: 3306 | |
# database: test | |
# username: ted | |
# password: password | |
# reconnect: 'false' | |
# pool: 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment