From ec110ab510318f7320a674f7c4073277570b6beb Mon Sep 17 00:00:00 2001 From: glaszig Date: Wed, 17 Jun 2020 16:58:58 +0200 Subject: [PATCH] be compatible with peek-mysql2 don't use alias_method since peek-mysql2 uses prepend on rails >= 5 related to #437 and #444 --- lib/patches/db/mysql2.rb | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/patches/db/mysql2.rb b/lib/patches/db/mysql2.rb index 576ae4bd..fd00a82f 100644 --- a/lib/patches/db/mysql2.rb +++ b/lib/patches/db/mysql2.rb @@ -1,30 +1,34 @@ # frozen_string_literal: true -# The best kind of instrumentation is in the actual db provider, however we don't want to double instrument - class Mysql2::Result - alias_method :each_without_profiling, :each - def each(*args, &blk) - return each_without_profiling(*args, &blk) unless defined?(@miniprofiler_sql_id) + module MiniProfiler + def each(*args, &blk) + return super unless defined?(@miniprofiler_sql_id) - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - result = each_without_profiling(*args, &blk) - elapsed_time = SqlPatches.elapsed_time(start) + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + result = super + elapsed_time = SqlPatches.elapsed_time(start) - @miniprofiler_sql_id.report_reader_duration(elapsed_time) - result + @miniprofiler_sql_id.report_reader_duration(elapsed_time) + result + end end + + prepend MiniProfiler end class Mysql2::Client - alias_method :query_without_profiling, :query - def query(*args, &blk) - return query_without_profiling(*args, &blk) unless SqlPatches.should_measure? + module MiniProfiler + def query(*args, &blk) + return super unless SqlPatches.should_measure? - result, record = SqlPatches.record_sql(args[0]) do - query_without_profiling(*args, &blk) + result, record = SqlPatches.record_sql(args[0]) do + super + end + result.instance_variable_set("@miniprofiler_sql_id", record) if result + result end - result.instance_variable_set("@miniprofiler_sql_id", record) if result - result end + + prepend MiniProfiler end