database_config.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. require 'yaml'
  2. cnf = YAML.load_file(File.join(__dir__, '../../config/database/database.yml'))
  3. cnf.delete('default')
  4. database = ENV['ENFORCE_DB_SERVICE']
  5. if !database
  6. hostsfile = '/etc/hosts'
  7. database = %w[postgresql mysql].shuffle.find do |possible_database|
  8. File.foreach(hostsfile).any? { |l| l[possible_database] }
  9. end
  10. end
  11. raise "Can't find any supported database in #{hostsfile}." if database.nil?
  12. puts "NOTICE: Found/selected #{database} Database Service"
  13. db_settings_map = {
  14. 'postgresql' => {
  15. 'adapter' => 'postgresql',
  16. 'username' => 'zammad',
  17. 'password' => 'zammad',
  18. 'host' => 'postgresql', # db alias from gitlab-ci.yml
  19. },
  20. 'mysql' => {
  21. 'adapter' => 'mysql2',
  22. 'username' => 'root',
  23. 'password' => 'zammad',
  24. 'host' => 'mysql', # db alias from gitlab-ci.yml
  25. }
  26. }
  27. # fetch DB settings from settings map and fallback to postgresql
  28. db_settings = db_settings_map.fetch(database) { db_settings_map['postgresql'] }
  29. %w[development test production].each do |environment|
  30. cnf[environment].merge!(db_settings)
  31. end
  32. File.open('config/database.yml', 'w') do |file|
  33. file.write(Psych.dump(cnf))
  34. end