Skip to content

Commit 17769da

Browse files
committed
Removed outdated explain subscriber code.
1 parent 01e6954 commit 17769da

File tree

2 files changed

+19
-69
lines changed

2 files changed

+19
-69
lines changed
Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1 @@
1-
module ActiveRecord
2-
module ConnectionAdapters
3-
module Sqlserver
4-
module CoreExt
5-
class ExplainSubscriber
6-
def call(*args)
7-
if queries = Thread.current[:available_queries_for_explain]
8-
payload = args.last
9-
queries << payload.values_at(:sql, :binds) unless ignore_sqlserver_payload?(payload)
10-
end
11-
end
12-
13-
IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE)
14-
SQLSERVER_EXPLAINED_SQLS = /(select|update|delete|insert)/i
15-
16-
# TODO Need to modify the regex for the TSQL generated by this adapter so we can explain the proper sql statements
17-
def ignore_sqlserver_payload?(payload)
18-
payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ SQLSERVER_EXPLAINED_SQLS
19-
end
20-
21-
ActiveSupport::Notifications.subscribe("sql.active_record", new)
22-
end
23-
end
24-
end
25-
end
26-
end
1+
ActiveRecord::ExplainSubscriber::EXPLAINED_SQLS = /(select|update|delete|insert)/i

test/cases/showplan_test_sqlserver.rb

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
require 'models/car'
33

44
class ShowplanTestSqlserver < ActiveRecord::TestCase
5-
5+
66
fixtures :cars
7-
7+
88
context 'Unprepare previously prepared SQL' do
9-
9+
1010
should 'from simple statement' do
11-
plan = Car.where(id: 1).explain
11+
plan = Car.where(id: 1).explain
1212
assert plan.starts_with?("EXPLAIN for: SELECT [cars].* FROM [cars] WHERE [cars].[id] = 1")
1313
assert plan.include?("Clustered Index Seek"), 'make sure we do not showplan the sp_executesql'
1414
end
@@ -18,81 +18,56 @@ class ShowplanTestSqlserver < ActiveRecord::TestCase
1818
assert plan.starts_with?("EXPLAIN for: SELECT [cars].* FROM [cars] WHERE (\n id = 1 \n)")
1919
assert plan.include?("Clustered Index Seek"), 'make sure we do not showplan the sp_executesql'
2020
end
21-
21+
2222
should 'from prepared statement ...' do
23-
plan = capture_logger do
24-
Car.where(:name => ',').limit(1).explain
25-
end
23+
plan = Car.where(:name => ',').limit(1).explain
2624
assert plan.include?("SELECT TOP (1) [cars].* FROM [cars] WHERE [cars].[name] = N','")
2725
assert plan.include?("TOP EXPRESSION"), 'make sure we do not showplan the sp_executesql'
2826
assert plan.include?("Clustered Index Scan"), 'make sure we do not showplan the sp_executesql'
2927
end
30-
28+
3129
end
32-
30+
3331
context 'With SHOWPLAN_TEXT option' do
34-
32+
3533
should 'use simple table printer' do
3634
with_showplan_option('SHOWPLAN_TEXT') do
3735
plan = Car.where(:id => 1).explain
38-
assert plan.starts_with?("EXPLAIN for: SELECT [cars].* FROM [cars] WHERE [cars].[id] = 1")
36+
assert plan.starts_with?("EXPLAIN for: SELECT [cars].* FROM [cars] WHERE [cars].[id] = 1")
3937
assert plan.include?("Clustered Index Seek"), 'make sure we do not showplan the sp_executesql'
4038
end
4139
end
42-
40+
4341
end
44-
42+
4543
context 'With SHOWPLAN_XML option' do
46-
44+
4745
should 'show formatted xml' do
4846
with_showplan_option('SHOWPLAN_XML') do
4947
plan = Car.where(:id => 1).explain
5048
assert plan.include?('ShowPlanXML')
5149
end
5250
end
53-
51+
5452
end
55-
56-
53+
54+
5755
private
58-
56+
5957
def base
6058
ActiveRecord::Base
6159
end
6260

6361
def connection
6462
base.connection
6563
end
66-
64+
6765
def with_showplan_option(option)
6866
old_option = ActiveRecord::ConnectionAdapters::SQLServerAdapter.showplan_option
6967
ActiveRecord::ConnectionAdapters::SQLServerAdapter.showplan_option = option
7068
yield
7169
ensure
7270
ActiveRecord::ConnectionAdapters::SQLServerAdapter.showplan_option = old_option
7371
end
74-
75-
76-
def capture_logger
77-
original_logger = base.logger
78-
log = StringIO.new
79-
base.logger = Logger.new(log)
80-
base.logger.level = Logger::WARN
81-
yield
82-
log.string
83-
ensure
84-
base.logger = original_logger
85-
end
8672

87-
def capture_queries
88-
queries = Thread.current[:available_queries_for_explain] = []
89-
with_threshold(0) do
90-
yield
91-
end
92-
queries
93-
ensure
94-
Thread.current[:available_queries_for_explain] = nil
95-
end
96-
97-
9873
end

0 commit comments

Comments
 (0)