rails-templates icon indicating copy to clipboard operation
rails-templates copied to clipboard

Add a default database adapter for production environment

Open hoangmirs opened this issue 4 years ago • 3 comments

The rails template is missing the adapter configuration for production in database.yml file Therefore, I got the ActiveRecord::AdapterNotSpecified when building Docker image on production Screen Shot 2020-07-24 at 17 30 24

hoangmirs avatar Jul 24 '20 10:07 hoangmirs

On production, as default Rails is using DATABASE_URL format which is we can set the adapter in the URL, for example: postgres://mdhxjqtgwtrcgu:xxx.compute-1.amazonaws.com:5432/yyy

andyduong1920 avatar Aug 17 '20 10:08 andyduong1920

After checking with @hoangmirs and @andyduong1920, it seems there are 2 wordarounds:

  • As Andy suggested we can add the DATABASE_URL env variable with the postgres:// adapter. But that requires changing the CI/CD scripts
  • Hoang suggested to add only the adapter: postgresin the database.yml file (inside the production section). This is working too.

IMO while handling this issue, we should try to understand what the pre-compile assets script is doing and see if we cannot remove this script or update it so that it does not need a DB connection 🤔

malparty avatar Aug 12 '22 02:08 malparty

Solution found by @hoangmirs 🥳

In the Just adding the DATABASE_URL in the dummy env variables (bin/docker-assets-precompile)!

This prevents the fix to affect another place than the root issue (legacy of this file).

if rails_env == 'production'
  env_keys = YAML.load_file('config/application.yml')['docker_build'].keys
  env_keys.each { |name| ENV[name] = 'dummy_value' }

  ENV['DATABASE_URL'] = 'postgres://postgres:postgres@postgres:5432/postgres'
end

exit system('bin/rails i18n:js:export && bin/rails assets:precompile')

malparty avatar Aug 12 '22 06:08 malparty