lib/rom/plugins/relation/sql/instrumentation.rb in rom-sql-3.6.4 vs lib/rom/plugins/relation/sql/instrumentation.rb in rom-sql-4.0.0.alpha1
- old
+ new
@@ -1,7 +1,9 @@
# frozen_string_literal: true
+require "dry/effects"
+
module ROM
module Plugins
module Relation
module SQL
# Instrumentation for relations and commands
@@ -24,24 +26,18 @@
# c.notifications = MyNotifications.new
# end
#
# @api public
module Instrumentation
- extend Notifications::Listener
+ extend Dry::Effects.Reader(:registry)
- subscribe('configuration.relations.registry.created') do |event|
- registry = event[:registry]
+ def self.apply(target, notifications:, **)
+ db = registry.gateways[target.config.component.gateway].connection
- relations = registry.select { |_, r| r.adapter == :sql && r.respond_to?(:notifications) }.to_h
- db_notifications = relations.values.map { |r| [r.dataset.db, r.notifications] }.uniq.to_h
+ return if db.respond_to?(:rom_instrumentation?)
- db_notifications.each do |db, notifications|
- next if db.respond_to?(:rom_instrumentation?)
-
- instrumenter = Instrumenter.new(db.database_type, notifications)
- db.extend(instrumenter)
- end
+ db.extend(Instrumenter.new(db.database_type, notifications))
end
# This stateful module is used to extend database connection objects
# and monkey-patches `log_connection_yield` method, which unfortunately
# is the only way to provide instrumentation on the sequel side.
@@ -57,11 +53,10 @@
attr_reader :notifications
# @api private
def initialize(name, notifications)
super()
-
@name = name
@notifications = notifications
define_log_connection_yield
end
@@ -94,9 +89,9 @@
end
end
end
ROM.plugins do
- adapter :sql do
+ adapter(:sql) do
register :instrumentation, ROM::Plugins::Relation::SQL::Instrumentation, type: :relation
end
end