diff options
author | Eileen Uchitelle <eileencodes@gmail.com> | 2018-06-12 15:36:54 -0400 |
---|---|---|
committer | Eileen Uchitelle <eileencodes@gmail.com> | 2018-06-12 15:37:23 -0400 |
commit | e0874b6f88509270d7603184ecd158d55df7204f (patch) | |
tree | 3fce037f95b5e951c63014437cbdbdb512f54f08 /activerecord/test/cases | |
parent | 39cb84e8bdd4b9a35d68b33222a174d2350aee89 (diff) | |
download | rails-e0874b6f88509270d7603184ecd158d55df7204f.tar.gz rails-e0874b6f88509270d7603184ecd158d55df7204f.tar.bz2 rails-e0874b6f88509270d7603184ecd158d55df7204f.zip |
Add ability to configure cache notifications info
This may seem like an unnecessary refactoring but some apps want / need
to configure the information passed to the query cache logger. In order
to do that we can add a method here that can be easily overridden by the
app itself, rather than hacking the query cache logger to include that
information.
To override apps can call
```
def cache_notifications_info
super.merge(connected_host: "hostname")
end
```
This will take what's already in the query cache logger and add
`@something="yea"` to the object.
At GitHub we use this to log the number of queries that are cached, the
connection host and the connection url.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/query_cache_test.rb | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index 0f990bac9d..1c05571f1b 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -13,12 +13,13 @@ class QueryCacheTest < ActiveRecord::TestCase fixtures :tasks, :topics, :categories, :posts, :categories_posts class ShouldNotHaveExceptionsLogger < ActiveRecord::LogSubscriber - attr_reader :logger + attr_reader :logger, :events def initialize super @logger = ::Logger.new File::NULL @exception = false + @events = [] end def exception? @@ -26,6 +27,7 @@ class QueryCacheTest < ActiveRecord::TestCase end def sql(event) + @events << event super rescue @exception = true @@ -265,6 +267,26 @@ class QueryCacheTest < ActiveRecord::TestCase end end + def test_cache_notifications_can_be_overridden + logger = ShouldNotHaveExceptionsLogger.new + subscriber = ActiveSupport::Notifications.subscribe "sql.active_record", logger + + connection = ActiveRecord::Base.connection.dup + + def connection.cache_notification_info(sql, name, binds) + super.merge(neat: true) + end + + connection.cache do + connection.select_all "select 1" + connection.select_all "select 1" + end + + assert_equal true, logger.events.last.payload[:neat] + ensure + ActiveSupport::Notifications.unsubscribe subscriber + end + def test_cache_does_not_raise_exceptions logger = ShouldNotHaveExceptionsLogger.new subscriber = ActiveSupport::Notifications.subscribe "sql.active_record", logger |