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

**URL:** https://meta.discourse.org/t/bizarre-import-issue-mysql-query-returning-pg-result/126852
**Category:** Development
**Created:** [August 26, 2019, 5:51pm UTC](https://meta.discourse.org/t/bizarre-import-issue-mysql-query-returning-pg-result/126852 "2019-08-26T17:51:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 26, 2019, 5:51pm UTC](https://meta.discourse.org/t/bizarre-import-issue-mysql-query-returning-pg-result/126852/1 "2019-08-26T17:51:21Z")

</div>

This has me stumped. Here is `mysql_query`:

```ruby
  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:

```ruby
      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.

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [August 28, 2019, 9:17pm UTC](https://meta.discourse.org/t/bizarre-import-issue-mysql-query-returning-pg-result/126852/2 "2019-08-28T21:17:24Z")

</div>

> [@pfaffman](#):
>
> `ActiveRecord::Base.connection.reconnect!`

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. 😉

I guess what you are really looking for is the `reconnect` [connection option](https://github.com/brianmario/mysql2#connection-options) of the mysql2 client.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 28, 2019, 9:36pm UTC](https://meta.discourse.org/t/bizarre-import-issue-mysql-query-returning-pg-result/126852/3 "2019-08-28T21:36:27Z")

</div>

> [@gerhard](#):
>
> That call reconnects to Postgres and returns the `PG::Result` object you are seeing.

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!
