janko / sequel-activerecord_connection

Allows Sequel to reuse Active Record's database connection
MIT License
137 stars 11 forks source link

Bug with prepared statements #21

Closed ollym closed 1 year ago

ollym commented 1 year ago

For whatever reason, this is the case:

db = Sequel.postgres(extensions: [:activerecord_connection])
puts db['SELECT TRUE'].prepare(:select, :select_true).call // #=> [{:bool=>false}]

Without using the extension, it works fine:

db = Sequel.postgres
puts db['SELECT TRUE'].prepare(:select, :select_true).call // #=> [{:bool=>true}]
janko commented 1 year ago

Hmm, for me this returns the latter when reusing the connection:

require "active_record"
require "sequel"
require "pg"

ActiveRecord.version # => #<Gem::Version "7.0.4">
Sequel.version # => "5.63.0"
Gem::Specification.find_by_name("sequel-activerecord_connection").version # => #<Gem::Version "1.2.9">
PG::VERSION # => "1.4.4"

ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "rodauth_demo_development")

db = Sequel.postgres(extensions: [:activerecord_connection])
db['SELECT TRUE'].prepare(:select, :select_true).call # => [{:bool=>true}]
ollym commented 1 year ago

@janko Only difference I can see is the Rails version:

ActiveRecord.version # => #<Gem::Version "6.1.7">
Sequel.version # => "5.63.0"
Gem::Specification.find_by_name("sequel-activerecord_connection").version # => #<Gem::Version "1.2.9">
PG::VERSION # => "1.4.5"

db = Sequel.postgres(extensions: [:activerecord_connection])
db['SELECT TRUE'].prepare(:select, :select_true).call # => [{:bool=>false}]
ollym commented 1 year ago

And without using prepared statements it works:

ActiveRecord.version # => #<Gem::Version "6.1.7">
Sequel.version # => "5.63.0"
Gem::Specification.find_by_name("sequel-activerecord_connection").version # => #<Gem::Version "1.2.9">
PG::VERSION # => "1.4.5"

db = Sequel.postgres(extensions: [:activerecord_connection])
db['SELECT TRUE'].all # => [{:bool=>true}]

Let me know if you can't recreate it and i'll dig deeper

janko commented 1 year ago

I was able to reproduce it 👍🏻 The difference is the sequel_pg gem that Sequel automatically loads when available, and I have it installed locally. When I set ENV["NO_SEQUEL_PG"] = "1", I get your behavior. I'll investigate.

ollym commented 1 year ago

Adding gem 'sequel_pg', require: false fixed it, would you recommend that we use that regardless?

janko commented 1 year ago

I would generally recommend it for performance, mainly if you're doing bigger selects, and if you want to use streaming.

janko commented 1 year ago

I discovered the issue, it's related to the discrepancy in type maps between Active Record in Sequel. I already bridged this difference for normal queries, but this code wasn't getting called for prepared statements. I'll push a fix shortly.

ollym commented 1 year ago

Thanks @janko hope you're otherwise well.