Skip to content

Commit

Permalink
Fixed connection leasing for Active Record 7.2 - fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 7, 2024
1 parent 5f88029 commit 29684d8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added experimental support for MariaDB 11.6 Vector
- Added experimental support for MySQL 9
- Changed `normalize` option to use Active Record normalization
- Fixed connection leasing for Active Record 7.2
- Dropped support for Active Record < 7

## 0.4.3 (2024-09-02)
Expand Down
6 changes: 3 additions & 3 deletions lib/neighbor/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def self.neighbor_attributes

distance = distance.to_s

quoted_attribute = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(attribute_name)}"
quoted_attribute = with_connection { |c| "#{c.quote_table_name(table_name)}.#{c.quote_column_name(attribute_name)}" }

column_info = columns_hash[attribute_name.to_s]
column_type = column_info&.type
Expand All @@ -113,7 +113,7 @@ def self.neighbor_attributes
Neighbor::Utils.validate(vector, dimensions: dimensions, type: type || Utils.type(adapter, column_info&.type), adapter: adapter)
vector = Neighbor::Utils.normalize(vector, column_info: column_info) if normalize

query = connection.quote(column_attribute.serialize(vector))
query = with_connection { |c| c.quote(column_attribute.serialize(vector)) }

if !precision.nil?
if adapter != :postgresql || column_type != :vector
Expand All @@ -124,7 +124,7 @@ def self.neighbor_attributes
when "half"
cast_dimensions = dimensions || column_info&.limit
raise ArgumentError, "Unknown dimensions" unless cast_dimensions
quoted_attribute += "::halfvec(#{connection.quote(cast_dimensions.to_i)})"
quoted_attribute += "::halfvec(#{with_connection { |c| c.quote(cast_dimensions.to_i) }})"
else
raise ArgumentError, "Invalid precision"
end
Expand Down
9 changes: 9 additions & 0 deletions test/neighbor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def test_schema
assert_match %{t.sparsevec "sparse_embedding", limit: 3}, contents
end

def test_connection_pool
PostgresRecord.connection_handler.clear_active_connections!
assert_nil PostgresRecord.connection_pool.active_connection?
PostgresRecord.connection_pool.with_connection do
Item.nearest_neighbors(:embedding, [1, 1, 1], distance: "euclidean")
end
assert_nil PostgresRecord.connection_pool.active_connection?
end

def test_composite_primary_key
skip if ActiveRecord::VERSION::STRING.to_f < 7.1

Expand Down

0 comments on commit 29684d8

Please sign in to comment.