将应用程序从 Rails 5.2 升级到 6.0 后运行 rspec 时出现 PG::ConnectionBad(连接已关闭) [英] PG::ConnectionBad(connection is closed) when running rspec after upgrading application from Rails 5.2 to 6.0
问题描述
我尝试将我的应用程序从 Rails 5.2 升级到 6.0,升级后运行 rspec
它引发错误 PG::ConnectionBad: connection is closed
.
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
.
错误是由rails_helper.rb
中的表达式ActiveRecord::Migration.maintain_test_schema!
引起的,错误如下,
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.
我可以在控制台中启动 RAILS_ENV=test rails c
并运行 ActiveRecord::Migration.maintain_test_schema!
而不会出错.并且还尝试将 rspec 从 3.10.1
升级到 4-0-dev
但仍然出现相同的错误.提前致谢.
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.
推荐答案
最后我创建了一个新的 rails 6 应用程序,然后逐个文件复制以检查导致此错误的原因,结果是在我的 spec/support
文件夹中有一个文件 shared_db_conntection.rb
有以下内容,这是从这个博客复制的 http://blog.plataformatec.com.br/2011/12/three-tips-to-提高测试套件的性能/
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
这篇关于将应用程序从 Rails 5.2 升级到 6.0 后运行 rspec 时出现 PG::ConnectionBad(连接已关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!