Error to connect to database
Hello everyone,
I came across with a very weird behavior, when I setup the database in database.yml it can't connect, it just shows the error could not obtain a database connection within 5.000 seconds (waited 5.000 seconds) (ActiveRecord::ConnectionTimeoutError)
When I type ActiveRecord::Base.configurations on the console, it returns an empty hash ({}) it's like it could not load the database.yml file.
I work around this declaring a DATABASE_URL environment variable
I tried to create a brand new project with stealth new <NAME> and run the command with the default configurations and sqlite adapter, but I had the same problem.
Versions:
Ruby 2.4.7
Stealth 1.1.6 and I tried 1.1.5 too
ActiveRecord 5.2.4.3
database.yml
default: &default
adapter: postgresql
host: <%= ENV['POSTGRES_HOST'] %>
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
database: <%= ENV['POSTGRES_DB'] || 'zion_bot_dev' %>
port: <%= ENV.fetch('POSTGRES_PORT', 5432) %>
encoding: unicode
pool: <%= ENV.fetch("STEALTH_MAX_THREADS", 5).to_i %>
development:
<<: *default
staging:
<<: *default
pool: 5
test:
<<: *default
database: zion_bot_test
production:
<<: *default
pool: 10
Looks like some ERB parsing is missing in https://github.com/hellostealth/stealth/blob/af5c4705157c979c5ea3df9b91de9abe5428877c/lib/stealth/base.rb#L126-L132
It's loading the config file as is and considers the <%= ENV.fetch... as text instead of parsing it to the appropriate value...
I suppose something like this would do:
if ENV['DATABASE_URL'].present?
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
else
database_config = File.read(File.join(Stealth.root, 'config', 'database.yml'))
ActiveRecord::Base.establish_connection(
YAML.load(ERB.new(database_config).result)[Stealth.env]
)
end