我尝试将我的应用程序从 Rails 5.2 升级到 6.0,升级后运行rspec
它会引发错误PG::ConnectionBad: connection is closed
.
该错误是由表达式引起的ActiveRecord::Migration.maintain_test_schema!
in rails_helper.rb
错误如下,
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!
在控制台中没有错误。并且还尝试从3.10.1
to 4-0-dev
但仍然遇到同样的错误。提前致谢。
最后我创建了一个新的rails 6应用程序,然后逐个文件复制以检查导致此错误的原因,事实证明,在我的spec/support
文件夹中有一个文件shared_db_conntection.rb
其中有以下内容,是从这个博客复制的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
删除这个文件后就没有这样的错误了
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)