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.
ActiveRecord::Migration.maintain_test_schema!inspec_helper.rb, which lets me run the tests, but I don't consier this a fix...