Bizarre import issue -- Mysql query returning PG::Result?

This has me stumped. Here is mysql_query:

  def mysql_query(sql)
    begin
      #puts "Query: \n#{sql}"
      @client.query(sql, cache_rows: true)
    rescue Exception => ex
      begin
        ActiveRecord::Base.connection.reconnect!
      rescue
        puts "Database connection failed, retrying in 10 seconds"
        sleep 10
        puts "Time to retry. . . "
        @client.query(sql, cache_rows: true)
      end
    end
  end

The first ~5M posts imported OK, but now, a fair amount of the time,

        posts = mysql_query(query).to_a

returns, not the mysql result, but instead:

 [#<PG::Result:0x0000563f3ee6d8d0 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>]

I’ve resorted to this:

      while !posts[-1].is_a?(Hash)
        puts "Posts: #{posts}"
        ActiveRecord::Base.connection.reconnect!
        @mysql_client = Mysql2::Client.new(
          host: DB_HOST,
          username: DB_USER,
          password: DB_PW,
          database: DB_NAME
        )
        posts = mysql_query(query).to_a
      end

And that is catching the issue and resolving it, but it’s still failing on nearly every query (sometimes as many as 3 5000-record queries succeed). I wrote it as a loop, but I think that it’s never run more than once. I briefly thought that perhaps something else was using @client and sticking a PG query in it, so I switched to @mysql_client, but that didn’t fix it.

I’ve googled a bit but came up with nothing.

My hack is working, so I guess it’s “fixed,” but I’m still curious just what could be causing this.

1 Like

Why?! That call reconnects to Postgres and returns the PG::Result object you are seeing. Your hack is just hiding the fact that you are doing something stupid you shouldn’t do. :wink:

I guess what you are really looking for is the reconnect connection option of the mysql2 client.

4 Likes

Aha! So maybe my code that’s supposed to be catching mysql errors and retrying is to blame. I thought that code had worked previously. And I still seem to forget that a function returns the value of the last line that executes.

Thanks a million, @gerhard. It’ll be at least 15 minutes before I see what’s going to happen next, but at least I now understand where the PG return was coming from!

1 Like