Ruby MySQL output conflicting on different servers

98 views Asked by At

I have coded a Ruby IRC bot which is on github (/ninjex/rubot) which is having some conflicting output with MySQL on a dedicated server I just purchased.

Firstly we have the connection to the database in the MySQL folder (in .gitignore) which looks similar to the following code block.

@con = Mysql.new('localhost', 'root', 'pword', 'db_name')

Then we have an actual function to query the database

def db_query
 que = get_message # Grabs query from user i.e,./db_query SELECT * FROM words
 results = @con.query(que) # Send query through the connection i.e, @con.query("SELECT * FROM WORDS")
 results.each {|x| chan_send(x)} # For each row returned, send it to the channel via
end

On my local machine, when running the command:

./db_query SELECT amount, user from words WHERE user = 'Bob' and word = 'hello'

I receive the output in IRC in an Array like fashion: ["17", "Bob"] Where 17 is amount and Bob is the user.

However, using this same function on my dedicated server results in an output like: 17Bob I have attempted many changes in the code, as well as try to parse the data into it's own variable, however it seems that 17Bob is coming out as a single variable, making it impossible to parse into something like an array, which I could then use to send the data correctly.

This seems odd to me on both my local machine and the dedicated server, as I was expecting the output to first send 17 to the IRC and then Bob like:

17
Bob

For all the functions and source you can check my github /Ninjex/rubot, however you may need to install some gems.

1

There are 1 answers

7
Matt Dressel On BEST ANSWER

A few notes:

  1. Make sure you are sanitizing query via get_message. Or you are opening yourself up to some serious security problems.
  2. Ensure you are using the same versions of the mysql gem, ruby and MySql. Differences in any of these may alter the expected output.
  3. If you are at your wits end and are unable to resolve the underlying issue, you can always send a custom delimiter and use it to split. Unfortunately, it will muck up the case that is actually working and will need to be stripped out.

Here's how I would approach debugging the issue on the dedicated machine:

def db_query
 que = get_sanitized_message
 results = @con.query(que)
 require 'pry'
 binding.pry

 results.each {|x| chan_send(x)}
end
  1. Add the pry gem to your Gemfile, or gem install pry.
  2. Update your code to use pry: see above
  3. This will open up a pry console when the binding.pry line is hit and you can interrogate almost everything in your running application.
  4. I would take a look at results and see if it's an array. Just type results in the console and it will print out the value. Also type out results.class. It's possible that query is returning some special result set object that is not an array, but that has a method to access the result array.
  5. If results is an array, then the issue is most likely in chan_send. Perhaps it needs to be using something like puts vs print to ensure there's a new line after each message. Is it possible that you have different versions of your codebase deployed? I would also add a sleep 1 within the each block to ensure that this is not related to your handling of messages arriving at the same time.