Skip to content

Commit 5bec50b

Browse files
authored
Merge pull request rails#50405 from skipkayhil/hm-filter-dbconfig-inspect
Add condensed #inspect for Pool, Adapter, Config
2 parents 5cfa136 + 5c81725 commit 5bec50b

File tree

7 files changed

+44
-0
lines changed

7 files changed

+44
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add condensed `#inspect` for `ConnectionPool`, `AbstractAdapter`, and
2+
`DatabaseConfig`.
3+
4+
*Hartley McGuire*
5+
16
* Add `.shard_keys`, `.sharded?`, & `.connected_to_all_shards` methods.
27

38
```ruby

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ def initialize(pool_config)
263263
@reaper.run
264264
end
265265

266+
def inspect # :nodoc:
267+
name_field = " name=#{db_config.name.inspect}" unless db_config.name == "primary"
268+
shard_field = " shard=#{@shard.inspect}" unless @shard == :default
269+
270+
"#<#{self.class.name} env_name=#{db_config.env_name.inspect}#{name_field} role=#{role.inspect}#{shard_field}>"
271+
end
272+
266273
def schema_cache
267274
@schema_cache ||= BoundSchemaReflection.new(schema_reflection, self)
268275
end

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ def initialize(config_or_deprecated_connection, deprecated_logger = nil, depreca
172172
@verified = false
173173
end
174174

175+
def inspect # :nodoc:
176+
name_field = " name=#{pool.db_config.name.inspect}" unless pool.db_config.name == "primary"
177+
shard_field = " shard=#{shard.inspect}" unless shard == :default
178+
179+
"#<#{self.class.name}:#{'%#016x' % (object_id << 1)} env_name=#{pool.db_config.env_name.inspect}#{name_field} role=#{role.inspect}#{shard_field}>"
180+
end
181+
175182
def lock_thread=(lock_thread) # :nodoc:
176183
@lock =
177184
case lock_thread

activerecord/lib/active_record/database_configurations/database_config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def adapter_class
1818
@adapter_class ||= ActiveRecord::ConnectionAdapters.resolve(adapter)
1919
end
2020

21+
def inspect # :nodoc:
22+
"#<#{self.class.name} env_name=#{@env_name} name=#{@name} adapter_class=#{adapter_class}>"
23+
end
24+
2125
def new_connection
2226
adapter_class.new(configuration_hash)
2327
end

activerecord/test/cases/adapter_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ def test_select_methods_passing_a_relation
323323
test "type_to_sql returns a String for unmapped types" do
324324
assert_equal "special_db_type", @connection.type_to_sql(:special_db_type)
325325
end
326+
327+
test "inspect does not show secrets" do
328+
output = @connection.inspect
329+
330+
assert_match(/ActiveRecord::ConnectionAdapters::\w+:0x[\da-f]+ env_name="\w+" role=:writing>/, output)
331+
end
326332
end
327333

328334
class AdapterForeignKeyTest < ActiveRecord::TestCase

activerecord/test/cases/connection_pool_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,16 @@ def test_pin_connection_nesting_lock_inverse
984984
assert_equal ActiveSupport::Concurrency::NullLock, @pool.lease_connection.lock
985985
end
986986

987+
def test_inspect_does_not_show_secrets
988+
assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:writing>/, @pool.inspect)
989+
990+
db_config = ActiveRecord::Base.connection_pool.db_config
991+
pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, db_config, :reading, :shard_one)
992+
pool = ConnectionPool.new(pool_config)
993+
994+
assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:reading shard=:shard_one>/, pool.inspect)
995+
end
996+
987997
private
988998
def active_connections(pool)
989999
pool.connections.find_all(&:in_use?)

activerecord/test/cases/database_configurations/hash_config_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ def test_validate_checks_the_adapter_exists
174174
config.validate!
175175
end
176176
end
177+
178+
def test_inspect_does_not_show_secrets
179+
config = HashConfig.new("default_env", "primary", { adapter: "abstract", password: "hunter2" })
180+
assert_equal "#<ActiveRecord::DatabaseConfigurations::HashConfig env_name=default_env name=primary adapter_class=ActiveRecord::ConnectionAdapters::AbstractAdapter>", config.inspect
181+
end
177182
end
178183
end
179184
end

0 commit comments

Comments
 (0)