diff --git a/lib/active_record/connection_adapters/makara_abstract_adapter.rb b/lib/active_record/connection_adapters/makara_abstract_adapter.rb index f160a305..4be4ecee 100644 --- a/lib/active_record/connection_adapters/makara_abstract_adapter.rb +++ b/lib/active_record/connection_adapters/makara_abstract_adapter.rb @@ -102,7 +102,7 @@ def custom_error_message?(connection, message) SQL_MASTER_MATCHERS = [/^\s*select.+for update$/i, /select.+lock in share mode$/i].map(&:freeze).freeze SQL_SLAVE_MATCHERS = [/^\s*select\s/i].map(&:freeze).freeze - SQL_ALL_MATCHERS = [/^\s*set\s/i].map(&:freeze).freeze + SQL_ALL_MATCHERS = [/\A\s*set\s/i].map(&:freeze).freeze SQL_SKIP_STICKINESS_MATCHERS = [/^\s*show\s([\w]+\s)?(field|table|database|schema|view|index)(es|s)?/i, /^\s*(set|describe|explain|pragma)\s/i].map(&:freeze).freeze diff --git a/lib/makara/logging/subscriber.rb b/lib/makara/logging/subscriber.rb index 7a0e003a..3574cae7 100644 --- a/lib/makara/logging/subscriber.rb +++ b/lib/makara/logging/subscriber.rb @@ -2,11 +2,14 @@ module Makara module Logging module Subscriber + IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"] def sql(event) name = event.payload[:name] - name = [current_wrapper_name(event), name].compact.join(' ') - event.payload[:name] = name + unless IGNORE_PAYLOAD_NAMES.include?(name) + name = [current_wrapper_name(event), name].compact.join(' ') + event.payload[:name] = name + end super(event) end diff --git a/spec/active_record/connection_adapters/makara_abstract_adapter_spec.rb b/spec/active_record/connection_adapters/makara_abstract_adapter_spec.rb index 3a436518..98daf265 100644 --- a/spec/active_record/connection_adapters/makara_abstract_adapter_spec.rb +++ b/spec/active_record/connection_adapters/makara_abstract_adapter_spec.rb @@ -19,6 +19,7 @@ 'describe table' => true, 'show index' => true, 'set @@things' => true, + 'SET @@things' => true, 'commit' => true, 'select * from felines' => false, ' select * from felines' => false, @@ -29,7 +30,7 @@ 'select * from users where name = "lock in share mode"' => false }.each do |sql, should_go_to_master| - it "determines if \"#{sql}\" #{should_go_to_master ? 'requires' : 'does not require'} master" do + it "determines that \"#{sql}\" #{should_go_to_master ? 'requires' : 'does not require'} master" do proxy = klass.new(config(1,1)) expect(proxy.master_for?(sql)).to eq(should_go_to_master) end @@ -37,15 +38,39 @@ end + { + "SET @@things" => true, + "INSERT INTO wisdom ('The truth will set you free.')" => false, + "INSERT INTO wisdom ('The truth will\nset you free.')" => false, + "UPDATE dogs SET max_treats = 10 WHERE max_treats IS NULL" => false, + %Q{ + UPDATE + dogs + SET + max_treats = 10 + WHERE + max_treats IS NULL + } => false + }.each do |sql, should_send_to_all_connections| + + it "determines that \"#{sql}\" #{should_send_to_all_connections ? 'should' : 'should not'} be sent to all underlying connections" do + proxy = klass.new(config(1,1)) + proxy.master_pool.connections.each{|con| expect(con).to receive(:execute).with(sql).once} + proxy.slave_pool.connections.each do |con| + if should_send_to_all_connections + expect(con).to receive(:execute).with(sql).once + else + expect(con).to receive(:execute).with(sql).never + end + end - it 'should send SET operations to all underlying connections' do - proxy = klass.new(config(1,1)) - proxy.master_pool.connections.each{|con| expect(con).to receive(:execute).with('SET @@things').once } - proxy.slave_pool.connections.each{|con| expect(con).to receive(:execute).with('SET @@things').once } + proxy.execute(sql) - proxy.execute("SET @@things") + if should_send_to_all_connections + expect(proxy.master_context).to be_nil + end + end - expect(proxy.master_context).to be_nil end {