diff --git a/CHANGELOG.md b/CHANGELOG.md index 50d0193e..789996f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ahoy/query_methods.rb b/lib/ahoy/query_methods.rb index a17f4a06..e777cde9 100644 --- a/lib/ahoy/query_methods.rb +++ b/lib/ahoy/query_methods.rb @@ -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)| @@ -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}") @@ -50,16 +50,16 @@ 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 @@ -67,12 +67,12 @@ def group_prop(*props) 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 diff --git a/test/support/query_methods_test.rb b/test/support/query_methods_test.rb index 251ccf74..6d9d1abe 100644 --- a/test/support/query_methods_test.rb +++ b/test/support/query_methods_test.rb @@ -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 @@ -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