Skip to content

Commit ab4ad5f

Browse files
authored
Allow for more granular SQL statement matching for loads (#25)
* CI: Remove ruby 2.5.x and 2.6.x tests in favor of 2.7.x * minor: Update publish script tag version -> ruby-version * refactor: Update MODEL_SQL_PATTERN to allow for more granular matches; remove MODEL_LOAD_PATTERN * rubocop: Fix or ignore * chore: Remove dead code * bump * revert: Roll-back query matcher changes * refactor: Ensure MODEL_SQL_PATTERN captures first-most table
1 parent c8341a1 commit ab4ad5f

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

.github/workflows/push_gem.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Set up Ruby 2.6
1616
uses: actions/setup-ruby@v1
1717
with:
18-
version: 2.6.x
18+
ruby-version: 2.6.x
1919

2020
- name: Publish to RubyGems
2121
run: |

.github/workflows/run_tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
ruby: [2.5.x, 2.6.x]
11-
appraisal: ["rails-4", "rails-5", "rails-6"]
10+
ruby: [2.7.x]
11+
appraisal: ["rails-5", "rails-6"]
1212
steps:
1313
- uses: actions/checkout@v1
1414
- name: Setup System

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.5.2] - 2021-05-26
10+
### Changed
11+
- Update MODEL_SQL_PATTERN to allow for more accurate matches against SQL strings
12+
913
## [0.5.1] - 2020-11-19
1014
### Changed
1115
- Removes zero count expectations from hash before comparing

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.1
1+
0.5.2

lib/ar_query_matchers/queries/load_counter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LoadQueryFilter < Queries::QueryFilter
2121

2222
# Matches unnamed SQL operations like the following:
2323
# "SELECT COUNT(*) FROM `users` ..."
24-
MODEL_SQL_PATTERN = /SELECT .* FROM [`"](?<table_name>[^`"]+)[`"]/.freeze
24+
MODEL_SQL_PATTERN = /SELECT (?:(?!SELECT).)* FROM [`"](?<table_name>[^`"]+)[`"]/.freeze
2525

2626
def filter_map(name, sql)
2727
# First check for a `SELECT * FROM` query that ActiveRecord has

spec/ar_query_matchers/queries/load_counter_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,15 @@
7777

7878
expect(stats.query_counts).to eq('MockUser' => 1, 'MockPost' => 1)
7979
end
80+
81+
it 'returns the correct result when there are subqueries' do
82+
user = MockUser.create!(name: 'Daniel')
83+
MockPost.create!(mock_user: user)
84+
85+
stats = described_class.instrument do
86+
MockPost.where(mock_user: MockUser.where(name: 'Daniel')).count
87+
end
88+
89+
expect(stats.query_counts).to eq('MockPost' => 1)
90+
end
8091
end

0 commit comments

Comments
 (0)