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.