Guest User

Untitled

a guest
Jul 26th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #########################################################
  2. #
  3. # This method takes a db connection url and returns rails
  4. # config YAML
  5. #
  6. ##########################################################
  7.  
  8. require "uri"
  9. require "yaml"
  10.  
  11. db_url = "mysql2://ted:password@example.net:3306/test?reconnect=false"
  12. uri = URI.parse(ENV["db_url"] || db_url)
  13.  
  14. qs = Hash[URI::decode_www_form(uri.query)]
  15. ui = uri.userinfo.split(':')
  16.  
  17.  
  18. config = {
  19. "production" => {
  20. "encoding" => qs["encoding"] || "utf-8",
  21. "adapter" => uri.scheme,
  22. "host" => uri.host,
  23. "port" => uri.port || 3306,
  24. "database" => uri.path[1..-1],
  25. "username" => ui.first,
  26. "password" => ui.last,
  27. "reconnect" => qs["reconnect"] || true,
  28. "pool" => qs["pool"] || 5,
  29. }.to_yaml
  30.  
  31. puts config
  32. # ---
  33. # production:
  34. # encoding: utf-8
  35. # adapter: mysql2
  36. # host: example.net
  37. # port: 3306
  38. # database: test
  39. # username: ted
  40. # password: password
  41. # reconnect: 'false'
  42. # pool: 5
Add Comment
Please, Sign In to add comment