SamSaffron/MiniProfiler

Does not work on Rails 3.0 when setting DB options

Opened this issue · 2 comments

Hi,

This may not be a high priority anymore, but miniprofiler doesn't work with Rails 3.0. There's a conflict between sql_patches and rack-mount near the #instance_variable_set:

  class Mysql2::Client
    alias_method :query_without_profiling, :query
    def query(*args,&blk)
      current = ::Rack::MiniProfiler.current
      return query_without_profiling(*args,&blk) unless current

      start = Time.now
      result = query_without_profiling(*args,&blk)
      elapsed_time = ((Time.now - start).to_f * 1000).round(1)
      result.instance_variable_set("@miniprofiler_sql_id", ::Rack::MiniProfiler.record_sql(args[0], elapsed_time))

      result

    end
  end

The Mysql2 adapter configures each connection by sending a SET statement to the DB. The code for that is here:

        def configure_connection
          @connection.query_options.merge!(:as => :array)

          # By default, MySQL 'where id is null' selects the last inserted id.
          # Turn this off. http://dev.rubyonrails.org/ticket/6778
          variable_assignments = ['SQL_AUTO_IS_NULL=0']
          encoding = @config[:encoding]

          # make sure we set the encoding
          variable_assignments << "NAMES '#{encoding}'" if encoding

          # increase timeout so mysql server doesn't disconnect us
          wait_timeout = @config[:wait_timeout]
          wait_timeout = 2147483 unless wait_timeout.is_a?(Fixnum)
          variable_assignments << "@@wait_timeout = #{wait_timeout}"

          execute("SET #{variable_assignments.join(', ')}", :skip_logging)
        end

This returns nil as the result (not an empty array). You normally would be able to get away with defining instance variables on the nil singleton object, but unfortunately, rack-mount (for Rails 3.0) has also decided to have its way with nil.

    def initialize(app, prefix = nil)
      @app, @prefix = app, prefix.freeze
      freeze
    end

So you get the inevitable cannot modify a frozen object error. Seems this pull-request https://github.com/josh/rack-mount/pull/30 fixes it on the Rack side.

This is FYI. I will attempt a Rails 3.1 upgrade now.

do you think this is worth fixing?

No. Rails 3.0 is not supported anymore. Thanks.