From c9b25c89ae4372008be0380df6a659d8989ec181 Mon Sep 17 00:00:00 2001 From: abeidahmed Date: Wed, 4 Aug 2021 22:59:37 +0530 Subject: [PATCH] Allow pg query param syntax Allow configuring Postgres password through the socket URL. For example: ```ruby ActiveRecord::DatabaseConfigurations::UrlConfig.new( :production, :production, 'postgres:///?user=user&password=secret&dbname=app', {} ).configuration_hash ``` will now return, ```ruby { :user=>"user", :password=>"secret", :dbname=>"app", :adapter=>"postgresql" } ``` --- activerecord/CHANGELOG.md | 17 +++++++++++++++++ .../connection_url_resolver.rb | 2 +- .../database_configurations/resolver_test.rb | 12 ++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index fe66926685..fe80da8453 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -80,6 +80,23 @@ *Kevin Newton* +* Allow configuring Postgres password through the socket URL. + + For example: + ```ruby + ActiveRecord::DatabaseConfigurations::UrlConfig.new( + :production, :production, 'postgres:///?user=user&password=secret&dbname=app', {} + ).configuration_hash + ``` + + will now return, + + ```ruby + { :user=>"user", :password=>"secret", :dbname=>"app", :adapter=>"postgresql" } + ``` + + *Abeid Ahmed* + * Fix `eager_loading?` when ordering with `Symbol` `eager_loading?` is triggered correctly when using `order` with symbols. diff --git a/activerecord/lib/active_record/database_configurations/connection_url_resolver.rb b/activerecord/lib/active_record/database_configurations/connection_url_resolver.rb index 13fddb98f8..f64b97e64a 100644 --- a/activerecord/lib/active_record/database_configurations/connection_url_resolver.rb +++ b/activerecord/lib/active_record/database_configurations/connection_url_resolver.rb @@ -67,7 +67,7 @@ def raw_config database: uri.opaque ) else - query_hash.merge( + query_hash.reverse_merge( adapter: @adapter, username: uri.user, password: uri.password, diff --git a/activerecord/test/cases/database_configurations/resolver_test.rb b/activerecord/test/cases/database_configurations/resolver_test.rb index f3be8a28fa..c0f1c57b82 100644 --- a/activerecord/test/cases/database_configurations/resolver_test.rb +++ b/activerecord/test/cases/database_configurations/resolver_test.rb @@ -62,6 +62,18 @@ def test_url_sub_key_merges_correctly }, pool_config.configuration_hash) end + def test_url_sub_key_merges_correctly_when_query_param + hash = { "url" => "abstract:///?user=user&password=passwd&dbname=app" } + pool_config = resolve_db_config :production, "production" => hash + + assert_equal({ + adapter: "abstract", + user: "user", + password: "passwd", + dbname: "app" + }, pool_config.configuration_hash) + end + def test_url_host_no_db pool_config = resolve_db_config "abstract://foo?encoding=utf8" assert_equal({