rails-templates
rails-templates copied to clipboard
Add a default database adapter for production environment
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
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
After checking with @hoangmirs and @andyduong1920, it seems there are 2 wordarounds:
- As Andy suggested we can add the
DATABASE_URL
env variable with thepostgres://
adapter. But that requires changing the CI/CD scripts - Hoang suggested to add only the
adapter: postgres
in thedatabase.yml
file (inside theproduction
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 🤔
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')