rails: ActiveRecord::NoDatabaseError not raised

Hi!

I’d like to know when ActiveRecord::NoDatabaseError exception is supposed to be raised? Because it’s not always the case. Sometimes I’ve got a PG::ConnectionBad exception which is pretty unexpected. Here a basic test case with Postgres and Mysql :

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.1.6"
  gem "pg"
  gem "mysql2"
end

require "active_record"
require "minitest/autorun"
require "logger"

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

class BugTest < Minitest::Test
  def test_bad_connection_with_postgres
    assert_raises ActiveRecord::NoDatabaseError do # got PG::ConnectionBad instead
      ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "foo")
      ActiveRecord::Base.connection
    end
  end

  def test_bad_connection_with_mysql2
    assert_raises ActiveRecord::NoDatabaseError do
      ActiveRecord::Base.establish_connection(adapter: "mysql2", database: "foo")
      ActiveRecord::Base.connection
    end
  end
end

Thank you!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 25 (9 by maintainers)

Most upvoted comments

$ psql foo
psql: FATAL:  database "foo" does not exist
$ mysql foo
ERROR 1044 (42000): Access denied for user 'u'@'localhost' to database 'foo'

I am under the impression that you are a little quick to open issues. Have you read the guidelines on support resources and Reporting an Issue ?