-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ActiveRecordProxyAdapters::LogSubscriber (#8)
* Add ActiveRecordProxyAdapters::LogSubscriber * Require log subscriber in unit tests
- Loading branch information
1 parent
6470ef5
commit 68b8c1f
Showing
5 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecordProxyAdapters | ||
class LogSubscriber < ActiveRecord::LogSubscriber # rubocop:disable Style/Documentation | ||
attach_to :active_record | ||
|
||
IGNORE_PAYLOAD_NAMES = %w[SCHEMA EXPLAIN].freeze | ||
|
||
def sql(event) | ||
payload = event.payload | ||
name = payload[:name] | ||
unless IGNORE_PAYLOAD_NAMES.include?(name) | ||
name = [database_instance_prefix_for(event), name].compact.join(" ") | ||
payload[:name] = name | ||
end | ||
super | ||
end | ||
|
||
protected | ||
|
||
def database_instance_prefix_for(event) | ||
connection = event.payload[:connection] | ||
config = connection.instance_variable_get(:@config) | ||
prefix = if config[:replica] || config["replica"] | ||
log_subscriber_replica_prefix | ||
else | ||
log_subscriber_primary_prefix | ||
end | ||
|
||
"[#{prefix.call(event)}]" | ||
end | ||
|
||
private | ||
|
||
delegate :log_subscriber_primary_prefix, :log_subscriber_replica_prefix, to: :config | ||
|
||
def config | ||
ActiveRecordProxyAdapters.config | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters