Advertisement
Guest User

Untitled

a guest
Jul 20th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. def connect_to_read_db
  2. DatabaseConnector.connect_to_db('read')
  3. end
  4.  
  5. def connect_to_write_db
  6. DatabaseConnector.connect_to_db('write')
  7. end
  8.  
  9. class DatabaseConnector
  10.  
  11. class << self
  12. def connect_to_db(function)
  13. if !(Rails.env.development? || Rails.env.test?)
  14. if ActiveRecord::Base.connection_config[:function] != function
  15. Rails.logger.debug "Gathering DB details"
  16. seed_file = Rails.root.join('config', 'database.yml')
  17. loaded_yml_file = YAML::load_file(seed_file)
  18. connection_name = "#{Rails.env}_#{function}"
  19. Rails.logger.debug "attempting connection to: #{connection_name}"
  20. db_details = loaded_yml_file[connection_name]
  21. Rails.logger.debug "database details:"
  22. db_details.each do |d|
  23. Rails.logger.debug(d)
  24. end
  25. ActiveRecord::Base.establish_connection(
  26. adapter: db_details['adapter'],
  27. port: db_details['port'],
  28. pool: db_details['pool'],
  29. timeout: db_details['timeout'],
  30. encoding: db_details['encoding'],
  31. host: db_details['host'],
  32. database: db_details['database'],
  33. username: db_details['username'],
  34. password: db_details['password']
  35. )
  36. end
  37. end
  38. end
  39. end
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement