Skip to content

Commit

Permalink
Raise when fast_distinct_count is used on primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Aug 20, 2023
1 parent bc26421 commit eb3ef87
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Style/SymbolArray:
Style/WordArray:
EnforcedStyle: brackets

Style/IfUnlessModifier:
Enabled: false

Layout/IndentationConsistency:
EnforcedStyle: indented_internal_methods

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master (unreleased)

- Raise when `fast_distinct_count` is used on primary key

## 0.2.0 (2023-07-24)

- Support for quickly getting an exact number of distinct values in a column
Expand Down
4 changes: 4 additions & 0 deletions lib/fast_count/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def fast_count(threshold: FastCount.threshold)
# User.fast_distinct_count(column: :company_id)
#
def fast_distinct_count(column:)
if column.to_s == primary_key
raise "Use `#fast_count` when counting primary keys."
end

adapter = Adapters.for_connection(connection)
adapter.fast_distinct_count(table_name, column)
end
Expand Down
7 changes: 7 additions & 0 deletions test/fast_count_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def test_fast_distinct_count
@connection.remove_index(:users, :company_id)
end

def test_fast_distinct_count_on_primary_key
error = assert_raises(RuntimeError) do
User.fast_distinct_count(column: :id)
end
assert_equal "Use `#fast_count` when counting primary keys.", error.message
end

def test_fast_distinct_count_counts_nulls
@connection.add_index(:users, :company_id)
@connection.execute(<<~SQL)
Expand Down

0 comments on commit eb3ef87

Please sign in to comment.