jruby / activerecord-jdbc-adapter

JRuby's ActiveRecord adapter using JDBC.
BSD 2-Clause "Simplified" License
462 stars 387 forks source link

PostgreSQL adapter sets invalid value "panic" for client_min_messages #1085

Open bblack opened 3 years ago

bblack commented 3 years ago

This has been a problem for us from 50.0 all the way up to 52.7. I see from the source in 60.x this may not be an issue anymore.

The postgresql docs (for pg versions that support client_min_messages, i.e. 10 through 13) specify the valid values for client_min_messages:

Valid values are DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, LOG, NOTICE, WARNING, and ERROR.

Yet, the postgres adapter here (up to 52.7 at least) tries setting it to "panic":

https://github.com/jruby/activerecord-jdbc-adapter/blob/1100221417a692663936e367e9775c7e48867859/lib/arjdbc/postgresql/adapter.rb#L206-L219

https://github.com/jruby/activerecord-jdbc-adapter/blob/1100221417a692663936e367e9775c7e48867859/lib/arjdbc/postgresql/adapter.rb#L221-L235

In our app this didn't actually present a problem until we upgraded postgres to either 11 or 12 (we're on 12 now), so perhaps "panic" actually was an acceptable value in a prior version, despite the docs. In any case, it's not an acceptable value now.

The effect is that trying to set it to "panic" raises an error, which is rescued, and @standard_conforming_strings is incorrectly set to :unsupported, which causes problems later when our app constructs properly-escaped queries.

We've worked around it by monkeypatching these functions to remove the extra bits:

module ArJdbc
  module PostgreSQL
    def standard_conforming_strings=(enable)
      value = enable ? "on" : "off"
      execute("SET standard_conforming_strings = #{value}", 'SCHEMA')
      @standard_conforming_strings = ( value == "on" )
    end

    def standard_conforming_strings?
      value = select_one('SHOW standard_conforming_strings', 'SCHEMA')['standard_conforming_strings']
      @standard_conforming_strings = ( value == "on" )
    end
  end
end

As I said, it looks like this may be fixed in 60.x. Can this be backported to 52.x (or further)?

bblack commented 3 years ago

This has been a problem for us from 50.0 all the way up to 52.7. I see from the source in 60.x this may not be an issue anymore.

Actually, this was a problem before 50.x when we were on Rails 4 too.