web-cat / code-workout

CodeWorkout: a programming practice and self-study web site
https://codeworkout.cs.vt.edu
226 stars 82 forks source link

Enviroment variables from docker-compose.yml have no effect #172

Open 1-alex98 opened 4 years ago

1-alex98 commented 4 years ago

The DB options in the docker-compose file seem to have no effect on the connection parameters of the application instead only the config/database.yml is respected.

How would this be corrected? Like this? puma.rb:

config_hash = YAML.load_file("#{app_dir}/config/database.yml")[rails_env]
if ENV["DB_USERNAME"]
      config_hash[:database] = ENV["DB_USERNAME"]
end
ActiveRecord::Base.establish_connection(
        config_hash)
s-edwards commented 4 years ago

Good catch--I agree you're correct. The code you suggest is one way to fix it. However, there are multiple settings in the app that may need to be overridden, including basically everything in the database.yml and secrets.yml files.

A better solution may be to use an erb expression in the yaml instead, so that any value can be conditionally overridden using ENV settings. For example:

development:
   adapter: mysql2
   database: <%= ENV["DB_DATABASE"] || 'codeworkout' %>
   username: <%= ENV["DB_USERNAME"] || 'codeworkout' %>
   password: <%= ENV["DB_PASSWORD"] || 'codeworkout' %>
   host: <%= ENV["DB_HOST"] || 'localhost' %>

This is supported in database.yml ... but would need to check on secrets.yml. With this change, the code itself would be unchanged and all the ENV support would go through the config file(s).

1-alex98 commented 4 years ago

So it needs to be renamed .yml.erb right?

s-edwards commented 4 years ago

No, ERB notation is supported directly in the file and the extension does not have to be changed. (From docs, but not tested :-).)