Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG::ConnectionBad: connection is closed after upgrading rails from 4.2 to 5.2

I have upgraded my rails version of a project from 4.2 to 5.2.1 some of my tests are failed because of pg connection closed error on the full trace is is as following.

    Failure/Error: DatabaseCleaner[:active_record].clean_with(:truncation)

    ActiveRecord::StatementInvalid:
    PG::ConnectionBad: connection is closed: TRUNCATE TABLE "public"."alerts", "public"."article_attachments", "public"."article_check_specification_tolerances", "public"."article_machine_part_settings", "public"."articles", "public"."attachments", "public"."check_batches", "public"."check_groups", "public"."check_specification_machine_types", "public"."check_specification_priorities", "public"."check_specification_responsibility_areas", "public"."check_specifications", "public"."checks", "public"."comments", "public"."counters", "public"."customers", "public"."defect_groups", "public"."defect_translations", "public"."defects", "public"."delayed_jobs", "public"."furnaces", "public"."gv_area_equipments", "public"."gv_areas", "public"."gv_components", "public"."gv_entries", "public"."gv_equipment_families", "public"."gv_equipments", "public"."gv_squads", "public"."gv_stop_reasons", "public"."gv_sub_equipment_components", "public"."gv_sub_equipments", "public"."job_specifications", "public"."jobs", "public"."lab_recipe_versions", "public"."lab_recipes", "public"."lines", "public"."machine_downtimes", "public"."machine_groups", "public"."machine_part_change_reasons", "public"."machine_part_changes", "public"."machine_part_translations", "public"."machine_parts", "public"."machine_type_group_machine_types", "public"."machine_type_groups", "public"."machine_types", "public"."messages", "public"."mold_sets", "public"."packing_schemes", "public"."rails_admin_settings", "public"."reasons", "public"."rejects", "public"."responsibility_areas", "public"."roles", "public"."settings", "public"."shift_definitions", "public"."shifts", "public"."system_log_entries", "public"."task_status_changes", "public"."tasks", "public"."tresholds", "public"."user_responsibility_areas", "public"."users", "public"."users_roles", "public"."workstations", "public"."machines", "public"."systematic_rejects", "public"."systematic_reject_machines" RESTART IDENTITY CASCADE;

and my rspec configurations are

RSpec.configure do |config|
  config.around(:each) do |example|
    DatabaseCleaner[:active_record].clean_with(:truncation)
    # DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end

strange thing is I can run tests one by one directory like

rspec spec/controllers/
rspec spec/contexts/
rspec spec/models

but it does not work when I try to run all in one with just

rspec spec/featues/

my feature_helper.rb is

require 'spec_helper'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'capybara/rails'
require 'rack/handler/puma'
require 'support/shared_activerecord_connection'
require 'support/feature_macros'

Capybara.register_server :puma do |app, port, host|
  require 'rack/handler/puma'
  Capybara.asset_host = "http://#{host}:#{port}"
  Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: '0:4', Silent: true, config_files: ['-'])
end
Capybara.configure do |config|
  config.server = :puma
end

RSpec.configure do |config|
  config.before(:suite) do
    Rails.application.load_tasks
    Rake::Task['assets:precompile'].invoke
  end
end

require 'spec_helper'
require 'counter_column_fix'

RedisStore.class_eval do
  def self.new_instance
    $redis = Redis.new(Rails.configuration.redis_config)
  end
end

what should I do to remove this error?

like image 686
Asnad Atta Avatar asked Nov 14 '18 08:11

Asnad Atta


1 Answers

DatabaseCleaner isn’t generally needed with Rails 5.1+ . Remove all references to it from your project and enable transactional testing in your RSpec config

like image 89
Thomas Walpole Avatar answered Nov 04 '22 05:11

Thomas Walpole