Skip to content

Commit

Permalink
Fixed connection leasing for Active Record 7.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 7, 2024
1 parent dc3d455 commit eead742
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.2.1 (unreleased)

- Fixed connection leasing for Active Record 7.2+

## 5.2.0 (2024-09-04)

- Improved error handling for invalid API parameters
Expand Down
22 changes: 11 additions & 11 deletions lib/ahoy/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ def where_event(name, properties = {})
def where_props(properties)
return all if properties.empty?

adapter_name = respond_to?(:connection) ? connection.adapter_name.downcase : "mongoid"
adapter_name = respond_to?(:connection_db_config) ? connection_db_config.adapter.to_s : "mongoid"
case adapter_name
when "mongoid"
where(properties.to_h { |k, v| ["properties.#{k}", v] })
when /mysql|trilogy/
when /mysql|trilogy/i
where("JSON_CONTAINS(properties, ?, '$') = 1", properties.to_json)
when /postgres|postgis/
when /postg/i
case columns_hash["properties"].type
when :hstore
properties.inject(all) do |relation, (k, v)|
Expand All @@ -31,7 +31,7 @@ def where_props(properties)
else
where("properties::jsonb @> ?", properties.to_json)
end
when /sqlite/
when /sqlite/i
properties.inject(all) do |relation, (k, v)|
if v.nil?
relation.where("JSON_EXTRACT(properties, ?) IS NULL", "$.#{k}")
Expand All @@ -50,29 +50,29 @@ def group_prop(*props)
props.flatten!

relation = all
adapter_name = respond_to?(:connection) ? connection.adapter_name.downcase : "mongoid"
adapter_name = respond_to?(:connection_db_config) ? connection_db_config.adapter.to_s : "mongoid"
case adapter_name
when "mongoid"
raise "Adapter not supported: #{adapter_name}"
when /mysql|trilogy/
when /mysql|trilogy/i
props.each do |prop|
quoted_prop = connection.quote("$.#{prop}")
quoted_prop = connection_pool.with_connection { |c| c.quote("$.#{prop}") }
relation = relation.group("JSON_UNQUOTE(JSON_EXTRACT(properties, #{quoted_prop}))")
end
when /postgres|postgis/
when /postg/i
# convert to jsonb to fix
# could not identify an equality operator for type json
# and for text columns
column_type = columns_hash["properties"].type
cast = [:jsonb, :hstore].include?(column_type) ? "" : "::jsonb"

props.each do |prop|
quoted_prop = connection.quote(prop)
quoted_prop = connection_pool.with_connection { |c| c.quote(prop) }
relation = relation.group("properties#{cast} -> #{quoted_prop}")
end
when /sqlite/
when /sqlite/i
props.each do |prop|
quoted_prop = connection.quote("$.#{prop}")
quoted_prop = connection_pool.with_connection { |c| c.quote("$.#{prop}") }
relation = relation.group("JSON_EXTRACT(properties, #{quoted_prop})")
end
else
Expand Down
18 changes: 17 additions & 1 deletion test/support/query_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ def test_scopes
end
end

def test_connection_leasing
skip if mongoid?

ActiveRecord::Base.connection_handler.clear_active_connections!
assert_nil ActiveRecord::Base.connection_pool.active_connection?
ActiveRecord::Base.connection_pool.with_connection do
count_events(value: 1)
group_events
end
assert_nil ActiveRecord::Base.connection_pool.active_connection?
end

def create_event(properties)
model.create(properties: properties)
end
Expand Down Expand Up @@ -185,7 +197,11 @@ def hstore?
self.class.name == "PostgresqlHstoreTest"
end

def mongoid?
self.class.name == "MongoidTest"
end

def group_supported?
self.class.name != "MongoidTest"
!mongoid?
end
end

0 comments on commit eead742

Please sign in to comment.