Skip to content

Commit

Permalink
Merge pull request #47028 from bensheldon/connection_error_postgres
Browse files Browse the repository at this point in the history
Fix inaccurate error message when connecting to `postgres` database
  • Loading branch information
rafaelfranca committed Jan 20, 2023
2 parents bcd0a8a + 6835980 commit 3551f9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Expand Up @@ -66,7 +66,9 @@ class << self
def new_client(conn_params)
PG.connect(**conn_params)
rescue ::PG::Error => error
if conn_params && conn_params[:dbname] && error.message.include?(conn_params[:dbname])
if conn_params && conn_params[:dbname] == "postgres"
raise ActiveRecord::ConnectionNotEstablished, error.message
elsif conn_params && conn_params[:dbname] && error.message.include?(conn_params[:dbname])
raise ActiveRecord::NoDatabaseError.db_error(conn_params[:dbname])
elsif conn_params && conn_params[:user] && error.message.include?(conn_params[:user])
raise ActiveRecord::DatabaseConnectionError.username_error(conn_params[:user])
Expand Down
Expand Up @@ -78,6 +78,18 @@ def test_bad_connection
end
end

def test_bad_connection_to_postgres_database
connect_raises_error = proc { |**_conn_params| raise(PG::ConnectionBad, 'FATAL: database "postgres" does not exist') }
PG.stub(:connect, connect_raises_error) do
assert_raises ActiveRecord::ConnectionNotEstablished do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
configuration = db_config.configuration_hash.merge(database: "postgres")
connection = ActiveRecord::Base.postgresql_connection(configuration)
connection.exec_query("SELECT 1")
end
end
end

def test_database_exists_returns_false_when_the_database_does_not_exist
config = { database: "non_extant_database", adapter: "postgresql" }
assert_not ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(config),
Expand Down

0 comments on commit 3551f9b

Please sign in to comment.