1

I need to create a new test database (postgres) and am having trouble. It seems that when I try to run any tests it is attempting to connect to the production database which is worrisome for many reasons. Here is my database.yml currently.

test:
  adapter: postgresql
  encoding: unicode
  database: xxxxxx-test
  pool: 5
  username: xxx
  host: localhost

But when I run a test I see:

An error occurred while loading ./spec/models/recipe_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!

PG::ConnectionBad:
  FATAL:  no pg_hba.conf entry for host "IP_ADDRESS", user "PRODUCTION_USER", database "PRODUCTION_DATABASE", SSL off

I'm not sure why this is happening or how I am supposed to set this up.

Here's my gemfile for test:

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15', '< 4.0'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
  gem 'rspec-rails', '~> 3.5'
  gem 'database_cleaner'
  gem 'factory_bot_rails'
end

It's worth noting that I do not have database_cleaner doing anything at the moment.

Jeremy Thomas
  • 6,240
  • 9
  • 47
  • 92

1 Answers1

3

The database configuration from database.yml is merged with ENV["DATABASE_URL"]. But ENV["DATABASE_URL"] ALWAYS takes precedence over the YML configuration. See the Rails guides on configuration.

The bad news is that you have set ENV["DATABASE_URL"] to point to your production database which could have been catastrophic.

The good news is that you seem to have a IP whitelist on the production DB that denied your local IP. Otherwise you would be clobbering your production DB!

To fix this you need to determine where ENV["DATABASE_URL"] is being set and get rid of it. Depending on your setup this can be anywhere from your ~/.profile to the docker container configuration. Then confirm that you have the correct config:

$ rails runner -e test "puts ActiveRecord::Base.connection_config"   

This should print something like:

{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"xxxxxx-test"}

At this point its safe to create the databases for dev/test with:

$ rails db:create
$ rails db:schema:load

You can use ENV["DATABASE_URL"] to let developers use their own local configuration, but you should NEVER let it point to the production DB on anything except the actual production server!

max
  • 96,212
  • 14
  • 104
  • 165
  • Luckily I had backed up my DB right before doing any of this. Is there a way to update the `ActiveRecord::Base.connection_config`? – Jeremy Thomas Jan 24 '19 at 11:22
  • `ActiveRecord::Base.connection_config` is automatically created by merging `database.yml` with `ENV["DATABASE_URL"]`. You could monkeypatch it but thats really the wrong answer to the wrong question. – max Jan 24 '19 at 11:30
  • now its showing my test db as the "database". Is there a way to set this to development as a default? – Jeremy Thomas Jan 24 '19 at 11:31
  • 1
    If you run `rails runner "puts ActiveRecord::Base.connection_config"` without the `-e` option it should default to development. The `-e` option sets `ENV["RAILS_ENV"]`. – max Jan 24 '19 at 11:34