1

In a Rails project that uses a Postgres database, I use rspec for testing. This worked fine until I added another migration. Now rspec fails with:

Failure/Error: ActiveRecord::Migration.maintain_test_schema!

PG::ConnectionBad:
connection is closed

I have tried to prepare the test database in various ways:

rails db:reset RAILS_ENV=test
rails db:prepare RAILS_ENV=test
rails db:migrate RAILS_ENV=test

and also

rails db:test:prepare

My tests used to work until I added the migration, so clearly something has gone foul, but what?


Edited to add more detail:

While the Postgres logs were not revealing at all, I did find out the following:

The error occurs at this line in rails_helper.rb:

ActiveRecord::Migration.maintain_test_schema!

From the Rails console in the test environment, I can call the maintain_test_schema! method without problems:

root@0bf32217fb90:/home/dora/rails# bin/rails c -e test
Loading test environment (Rails 6.0.3.4)
[1] pry(main)> ActiveRecord::Migration.maintain_test_schema!
(1.2ms)  SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1  [["key", "schema_sha1"]]
(1.8ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
=> nil
[2] pry(main)>

So evidently there is a difference between calling the method from rspec and calling it from the console.

I'll keep this updated for others to say, and if someone tumbles upon this and knows the solution, I'd be grateful.

4
  • Try looking in your postgresql logs. Commented Jan 4, 2021 at 18:24
  • The logs did not contain anything revealing, just a few statements with regards to the current time zone. I have updated my question with additional information. Evidently, the error occurs only in a given context. Commented Jan 10, 2021 at 13:36
  • I met same issue after upgrading my application from Rails 5.2 to 6.0. Have you found a fix? Thanks. Commented Feb 21, 2021 at 5:47
  • @AndyWang Unfortnuately not. I commented out the block that calls ActiveRecord::Migration.maintain_test_schema! in spec_helper.rb, which lets me run the tests, but I don't consier this a fix... Commented Feb 22, 2021 at 6:25

2 Answers 2

1

I met same issue after upgrading my rails app from 5.2 to 6.0. Finally I fixed it and the details are at PG::ConnectionBad(connection is closed) when running rspec after upgrading application from Rails 5.2 to 6.0 . You should check if there are any rspec supporters overrides the creation of dB connections

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Would never have thought of old cruft in a support directory being the cause... As advised, I had to remove spec/support/shared_db_connection.rb which is not a genuine Rails file.
0

I had the same issue and after many hours pulling my hair out, I've found that we need to replace all require 'rails_helper by require 'spec_helper' at the top of each spec.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.