Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG::ConnectionBad(connection is closed) when running rspec after upgrading application from Rails 5.2 to 6.0

I tried to upgrade my application from Rails 5.2 to 6.0, after upgrading when run rspec it raises error PG::ConnectionBad: connection is closed.

The error is caused by the expression ActiveRecord::Migration.maintain_test_schema! in rails_helper.rb and the error is as follows,

root@00de976cbbd4:/app# rspec

An error occurred while loading rails_helper.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!

PG::ConnectionBad:
  connection is closed
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/postgresql/quoting.rb:21:in `escape_string'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/postgresql/quoting.rb:21:in `quote_string'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/abstract/quoting.rb:220:in `_quote'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/postgresql/quoting.rb:144:in `_quote'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/abstract/quoting.rb:18:in `quote'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/postgresql/schema_statements.rb:750:in `quoted_scope'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/postgresql/schema_statements.rb:727:in `data_source_sql'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:62:in `table_exists?'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/internal_metadata.rb:32:in `table_exists?'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/tasks/database_tasks.rb:347:in `schema_up_to_date?'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/migration.rb:594:in `block in load_schema_if_pending!'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/migration.rb:593:in `all?'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/migration.rb:593:in `load_schema_if_pending!'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/migration.rb:614:in `block in maintain_test_schema!'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/migration.rb:867:in `suppress_messages'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/migration.rb:619:in `method_missing'
# /usr/local/bundle/gems/activerecord-6.0.3.5/lib/active_record/migration.rb:614:in `maintain_test_schema!'
# ./spec/rails_helper.rb:30:in `<top (required)>'
No examples found.
No examples found.

I can launch RAILS_ENV=test rails c and run ActiveRecord::Migration.maintain_test_schema! in console without errors. And also tried to upgrade rspec from 3.10.1 to 4-0-dev but still got the same error. Thanks in advance.

like image 465
Andy Wang Avatar asked Sep 10 '25 05:09

Andy Wang


1 Answers

Finally I created a new rails 6 app and then copy file by file to check what caused this error, it turns out that in my spec/support folder there is a file shared_db_conntection.rb which has following content, which is copied from this blog http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

After remove this file there is no such error

like image 64
Andy Wang Avatar answered Sep 13 '25 04:09

Andy Wang