Switching Branches causes database does not exist error when testing

I recently set up postgresql in a new workspace. I set it up the same way I usually do. Or at least I thought I did. But then I got this error

Failure/Error: ActiveRecord::Migration.maintain_test_schema!

ActiveRecord::NoDatabaseError:
  FATAL:  database "app_test" does not exist

It only happens when I change to a branch and run rspec. So far I've just been creating the database and running migrations each time I switch between branches as a solution.

It doesn't impact the development database. And as long as I remain on that branch I don't have to add the test database again. But if I have to leave it for any reason, I'll have to add the database again.

It seems to impact any branch that isn't the master. And this only occurs locally. I am using Cloud 9 and my app is a Rails app. Should I uninstall and reinstall postgresql, then set it up again?

Recently I've had to work on a few branches at the same time, so it's becoming a hassle. I'm tempted to just make a new workspace.

here is my database.yml(with some things hidden of course, app is a stand in for my app's name).

default: &default
  adapter: postgresql
  encoding: unicode
  host: localhost
  username: postgres
  password: secret
  pool: 5

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

Thank you in advance

Upvotes: 1

Views: 547

Answers (3)

  1. First I deleted my local branches to start clean (it may not be necessary but as most of them altered the schema I thought it'd be cleaner). My changes where all stored remotely so this was wasn't a big deal.
  2. Then I added template: template0 to my database.yml in the development and test sections. I followed this suggestion Fix for PG::Error: ERROR: new encoding (UTF8) is incompatible because I had this error:

"PG::InvalidParameterValue: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII) HINT: Use the same encoding as in the template database, or use template0 as template."

whenever I would try to use rake db:create

  1. After that I followed NM Pennypacker's guidance and did, rake db:drop, rake db:create, rake db:migrate and rake db:test:prepare. I've made some branches, pulled in the remote changes and run rspec with no problem. Thank you everyone for your advice!

Upvotes: 0

NM Pennypacker
NM Pennypacker

Reputation: 6952

You should be able to reset your databases by running:

rake db:drop db:create db:migrate db:test:prepare

That'll delete your dev database (along with any data in it), recreate it, rebuild the schema from your migration files, and rebuild the test database. If it doesn't work at that point you may be a permissions issue

Upvotes: 1

NeverBe
NeverBe

Reputation: 5038

I faced with similar issue. Just try to change this line of database.yml

host: 127.0.0.1

when i provided localhost it used default settings for connection. (i have many DB server version on different ports)

Upvotes: 0

Related Questions