Skip to content

Commit 50153ab

Browse files
committed
[Rails5] New SQL counter hacks and helper.
1 parent d07ea03 commit 50153ab

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

test/cases/coerced_tests.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def test_should_return_decimal_average_of_integer_field_coerced
129129

130130
coerce_tests! :test_limit_is_kept
131131
def test_limit_is_kept_coerced
132-
queries = assert_sql { Account.limit(1).count }
132+
queries = capture_sql_ss { Account.limit(1).count }
133133
assert_equal 1, queries.length
134134
queries.first.must_match %r{ORDER BY \[accounts\]\.\[id\] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1}
135135
end

test/cases/helper_sqlserver.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class TestCase < ActiveSupport::TestCase
1717

1818
include ARTest::SQLServer::CoerceableTest,
1919
ARTest::SQLServer::ConnectionReflection,
20+
ARTest::SQLServer::SqlCounterSqlserver,
2021
ActiveSupport::Testing::Stream
2122

2223
let(:logger) { ActiveRecord::Base.logger }
Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
module ARTest
22
module SQLServer
33

4-
extend self
4+
module SqlCounterSqlserver
55

6-
attr_accessor :sql_counter_listenter
6+
# Only return the log vs. log_all
7+
def capture_sql_ss
8+
ActiveRecord::SQLCounter.clear_log
9+
yield
10+
ActiveRecord::SQLCounter.log.dup
11+
end
712

8-
def ignored_sql
9-
[ /SELECT SCOPE_IDENTITY/,
10-
/INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS)/,
11-
/SELECT @@version/,
12-
/SELECT @@TRANCOUNT/,
13-
/(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION/,
14-
/SELECT CAST\(.* AS .*\) AS value/ ]
1513
end
1614

17-
def sql_counter_listenters
18-
ActiveSupport::Notifications.notifier.listeners_for('sql.active_record').select do |listener|
19-
listener.inspect =~ /ActiveRecord::SQLCounter/
20-
end
21-
end
15+
ignored_sql = [
16+
/INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS)/im,
17+
/SELECT @@version/,
18+
/SELECT @@TRANCOUNT/,
19+
/(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION/,
20+
/SELECT CAST\(.* AS .*\) AS value/
21+
]
2222

23-
def sql_counter_listenters_unsubscribe
24-
sql_counter_listenters.each { |listener| ActiveSupport::Notifications.unsubscribe(listener) }
25-
end
23+
sqlcounter = ObjectSpace.each_object(ActiveRecord::SQLCounter).to_a.first
24+
sqlcounter.instance_variable_set :@ignore, Regexp.union(ignored_sql.push(sqlcounter.ignore))
2625

2726
end
2827
end
29-
30-
ActiveRecord::SQLCounter.ignored_sql.concat ARTest::SQLServer.ignored_sql
31-
ARTest::SQLServer.sql_counter_listenters_unsubscribe
32-
ARTest::SQLServer.sql_counter_listenter = ActiveSupport::Notifications.subscribe 'sql.active_record', ActiveRecord::SQLCounter.new

0 commit comments

Comments
 (0)