Skip to content

Commit

Permalink
Merge pull request #50405 from skipkayhil/hm-filter-dbconfig-inspect
Browse files Browse the repository at this point in the history
Add condensed #inspect for Pool, Adapter, Config
  • Loading branch information
rafaelfranca committed Jun 21, 2024
2 parents 5cfa136 + 5c81725 commit 5bec50b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Add condensed `#inspect` for `ConnectionPool`, `AbstractAdapter`, and
`DatabaseConfig`.

*Hartley McGuire*

* Add `.shard_keys`, `.sharded?`, & `.connected_to_all_shards` methods.

```ruby
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ def initialize(pool_config)
@reaper.run
end

def inspect # :nodoc:
name_field = " name=#{db_config.name.inspect}" unless db_config.name == "primary"
shard_field = " shard=#{@shard.inspect}" unless @shard == :default

"#<#{self.class.name} env_name=#{db_config.env_name.inspect}#{name_field} role=#{role.inspect}#{shard_field}>"
end

def schema_cache
@schema_cache ||= BoundSchemaReflection.new(schema_reflection, self)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ def initialize(config_or_deprecated_connection, deprecated_logger = nil, depreca
@verified = false
end

def inspect # :nodoc:
name_field = " name=#{pool.db_config.name.inspect}" unless pool.db_config.name == "primary"
shard_field = " shard=#{shard.inspect}" unless shard == :default

"#<#{self.class.name}:#{'%#016x' % (object_id << 1)} env_name=#{pool.db_config.env_name.inspect}#{name_field} role=#{role.inspect}#{shard_field}>"
end

def lock_thread=(lock_thread) # :nodoc:
@lock =
case lock_thread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def adapter_class
@adapter_class ||= ActiveRecord::ConnectionAdapters.resolve(adapter)
end

def inspect # :nodoc:
"#<#{self.class.name} env_name=#{@env_name} name=#{@name} adapter_class=#{adapter_class}>"
end

def new_connection
adapter_class.new(configuration_hash)
end
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ def test_select_methods_passing_a_relation
test "type_to_sql returns a String for unmapped types" do
assert_equal "special_db_type", @connection.type_to_sql(:special_db_type)
end

test "inspect does not show secrets" do
output = @connection.inspect

assert_match(/ActiveRecord::ConnectionAdapters::\w+:0x[\da-f]+ env_name="\w+" role=:writing>/, output)
end
end

class AdapterForeignKeyTest < ActiveRecord::TestCase
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/connection_pool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,16 @@ def test_pin_connection_nesting_lock_inverse
assert_equal ActiveSupport::Concurrency::NullLock, @pool.lease_connection.lock
end

def test_inspect_does_not_show_secrets
assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:writing>/, @pool.inspect)

db_config = ActiveRecord::Base.connection_pool.db_config
pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, db_config, :reading, :shard_one)
pool = ConnectionPool.new(pool_config)

assert_match(/#<ActiveRecord::ConnectionAdapters::ConnectionPool env_name="\w+" role=:reading shard=:shard_one>/, pool.inspect)
end

private
def active_connections(pool)
pool.connections.find_all(&:in_use?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def test_validate_checks_the_adapter_exists
config.validate!
end
end

def test_inspect_does_not_show_secrets
config = HashConfig.new("default_env", "primary", { adapter: "abstract", password: "hunter2" })
assert_equal "#<ActiveRecord::DatabaseConfigurations::HashConfig env_name=default_env name=primary adapter_class=ActiveRecord::ConnectionAdapters::AbstractAdapter>", config.inspect
end
end
end
end

0 comments on commit 5bec50b

Please sign in to comment.