Fixing “uninitialized constant MysqlCompat::MysqlRes”

This is an old problem that affects an old Ruby gem: mysql (2.8.1). Apparently, it has a little bug somewhere that makes it look for a file in the wrong folder. I get this error always after installing a new app. The last time has been some minutes ago, after installing TracksApp 2.1, which is based on the old Rails 2.3.

The MySQL page about Ruby adapters speaks about two of them. One called MySQL/Ruby, and the other (after a huge name creation effort)… Ruby/MySQL. They are respectively provided by the gems mysql (2.8.1) and ruby-mysql (2.9.10). As you may have noticed, the first gem is the old version of the second. That is also confirmed by the dates they got updated, which is: 2.8.1, on 2009-08-21 and 2.9.10, on 2012-07-12.

Both adapters are authored by Tomita Masahiro. I do appreciate the effort he did, and his generosity in making them available to me, no doubt about that. But in my opinion, the name change was a mistake. I suppose he did it to tell the world they were two very different adapters (the old one C-based and the new one Ruby-based). Nonetheless, they really were two subsequent versions of the same thing.

The problem with TracksApp is exactly that. If Tomita Masahiro had stuck to the mysql name, then gem “mysql” in TracksApps would have referenced the latest version (what today is instead ruby-mysql) and all would have worked fine. But now, even with ruby-mysql installed, that gem requirement in TracksApp makes my system install the old mysql gem. That in turn makes Rails prefer mysql over ruby-mysql (why? lexical order?) and due to the known bug all my apps break.

My solution is to manually uninstall mysql each time it gets installed by some app. In fact I don’t know how to make my global Rails environment ignore mysql (2.8.1) even if some app requires it. I’ve tried with “bundle install –without mysql”, but mysql is a gem, not a group, so it does not work… My fix works because ruby-mysql perfectly replaces mysql and it’s just a drop-in, sharing the same adapter name (mysql).

Clearly, TracksApp is a new version of an old software, even if it is still based on Rails 2.3. If I was developing it, I’d have updated the Gemfile…

 

How to remotely debug Rails in Apache + Passenger + RubyMine

I’m now a Ruby on Rails programmer and the past week I’ve been frantically learning how to develop on my MacBook Air.

My configuration

  1. Mac OS X Mountain Lion
  2. Xcode 4.4
  3. Apache 2.2.22
  4. MySQL 5.5.27
  5. Phusion Passenger 3.0.15
  6. Ruby 1.8.7
  7. Rails 2.3.12

My IDE

I wanted an IDE like Zend Studio, whose remote debugging support for PHP I simply love.

After some research I bought a license for RubyMine 4.5, because I was impressed by its integration with Ruby and Rails and its excellent debugger.

Unfortunately, RubyMine’s offers remote debugging configurations that (at the moment) don’t allow to hook into a running instance. That means that you can’t remotely debug on your own application stack, unless your Rails server is run by a command line script.

Well… luckily enough, I’ve just found another way that allows me to remotely debug using my own stack.
In fact, RubyMine’s local debugger does support Passenger Standalone as a Rails server, through a command line like this:

/usr/bin/ruby /usr/bin/passenger start -a 0.0.0.0 -p 3000 --max-pool-size 1 --spawn-method conservative -e development

in conjunction with the following file (config/initializers/rubymine_passenger_debug.rb)

# generated by RubyMine
debug_port = ENV['RUBYMINE_DEBUG_PORT']

if debug_port
  puts "Preparing to launch debugger for #{$$}"
  $:.push(*ENV['RUBYLIB'].split(":"))
  require 'ruby-debug-ide'

  Debugger.cli_debug = ("true" == ENV['RUBYMINE_DEBUG_VERBOSE'])
  Debugger.start_server('127.0.0.1', debug_port.to_i)
end

And it works like a charm.

My method

This example is based on the Notebook app created in the RubyMine tutorial Discover RubyMine in a Few Simple Steps.

(1/6) Edit /etc/hosts

I make an alias in my Mac such that notebook.dev corresponds to 127.0.0.1 (localhost).

#...
127.0.0.1    notebook.dev

(2/6) Edit …/apache2/extra/httpd-vhosts.conf

I make a virtual host for my “remote” development site. Note that the notebook folder is under Sites. On the other hand, my “local” development site will be under RubymineProjects. To synch them you need to deploy, i.e. delete the notebook folder in Sites and copy again there the one in RubymineProjects.

#...
NameVirtualHost *:80

<VirtualHost *:80>
    # this is the default entry !!!
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    ServerName notebook.dev
    ServerAlias www.notebook.dev
    
    ServerAdmin cappuccino.e.cornetto@gmail.com
    ErrorLog "/private/var/log/apache2/notebook.dev-error_log"
    CustomLog "/private/var/log/apache2/notebook.dev-access_log" common

    DocumentRoot "/Users/andrea/Sites/notebook/public"
    <Directory "/Users/andrea/Sites/notebook/public">
        # This relaxes Apache security settings.
        AllowOverride all
        # MultiViews must be turned off.
        Options -MultiViews
    </Directory>
</VirtualHost>

(3/6) Edit …/apache2/httpd.conf

This is where Passenger is configured into Apache. I configure my instance like RubyMine does when it starts Passenger Standalone for local debugging. This is a minor drawback… I think I can live with it + remote debugging.

#...
LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-3.0.15/ext/apache2/mod_passenger.so
   PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-3.0.15
   PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
   
   # when RubyMine executes Passenger Standalone for debugging, these are the settings...
   PassengerMaxPoolSize 1
   PassengerSpawnMethod conservative
   SetEnv RAILS_ENV development

#...

(4/6) Create …/notebook/lib/tasks/rdebug.rake

I create a simple rake task to start the server with debugging enabled.

task :rdebug do
  system("touch tmp/restart.txt")
  system("touch tmp/debug.txt")
end

(5/6) Add RubyMine – Run/Debug Configurations – Ruby remote debug

I add a new Ruby remote debug configuration. Remote host is the IP of my “remote” development site. The remote port can be any that is not already used. The local port is decided by the dialog box, so you’ll have a different number each time you add a new configuration. Remote and local root folders must match the same app tree such that the mapping works when debugging.

(6/6) Edit …/notebook/config/environments/development.rb

I add this block at the end of my development configuration. It is the keystone. Note that remote_host and remote_port must match the corresponding values in the Ruby remote debug configuration.

#...
remote_host = '127.0.0.1'
remote_port = 7000

debug_flag = File.join(RAILS_ROOT, 'tmp', 'debug.txt')
if File.exists?(debug_flag)
  
  require 'ruby-debug-ide'
  Debugger.start_server(remote_host, remote_port)

  File.delete(debug_flag)
end

Debug sequence

This is the sequence I use for remote debugging. Apart from the first step, the rest is the same used for local debugging.

  1. From a Terminal window: rake rdebug
  2. From RubyMine – (notebook) Debug
  3. From the browser – Reload a page and hit a breakpoint
  4. RubyMine gets the focus and waits for you to debug

And it works like a charm.

How to neatly deploy with Git on OSX and Linux

First and foremost, let me apologize if you got here by a misleading title. I always strive to find a good title for my posts but this time I’m not satisfied. This post is about my specific needs in the described scenario, and I decided to publish it mainly for my own record. Unfortunately, a more precise title would have had an unfeasible length.

Lately I’ve been making changes to a web project I had downloaded to my Mac from a Linux server provided by 1and1. My first mistake was to use FTP on the root tree. Even if I have a 50 Mb connection at home, it was a never ending task. After hours waiting for the download to complete, I found out that both FileZilla and Cyberduck had problems with some filenames.

Then I discovered that 1and1 has a file manager in the admin interface that allows you to make a zip. My second mistake was to think that the feature would work as advertised. That was not the case. Apparently you can select many files and directories at once an compress them to a zip file by means of a button. The button pops up a second window from where you can choose the filename. When you accept, it starts working, and working, and working, up to when it exhausts the time allowed for running the task (15 minutes, I think).

I made two wrong assumptions here really: first that selecting multiple files had an effect on the subsequent operation and second that the button would do it. Luckily for me, I discovered that if I selected just one file or directory and used the compress option from its contextual menu, it worked fine. It took some time to compress about 500 MB but the resulting file took merely a bunch of seconds to download by FTP.

I started making my changes to the code and testing them on localhost. Meanwhile, also the remote web tree was changing because users were uploading to and deleting files from the web. (If I make a web, code and data are clearly set apart, such that a trunk cannot branch without control. But this is not a web I made, and the code is so weird and highly duplicated that I understand it with some difficulty.)

It was at that point in time when I discovered that 1and1 had Git installed and ssh was available. So I realized Git could help me deploy changes from my development environment back to the production environment with minimum downtime. I wanted to be able to move changes the other way around too, for example to get new data files. My third mistake was to think that I could do all of it with a production repo cloned to a development repo. Unfortunately, Git (by default) don’t allow to push to a standard (non-bare) repo. Neither could I easily pull from the development repo to the production repo, because of my dynamic IP at home.

I understood that I needed three repos: a development repo, a production repo, and a proxy, internet-reachable, bare repo. Not only I had to find a place for the proxy repo but it also needed to exist before the other two, so that I could clone them from it. The problem I faced was that the website was already fully deployed right into the htdocs directory and 1and1 didn’t allow me to write outside. Thank Git, a clone is equal to each other (mostly) and .gitignore is very effective, so I solved it like this:

  1. init a production repo into htdocs
  2. add /bare.git/ to htdocs/.gitignore
  3. commit anything (to the production repo)
  4. bare-clone the production repo (htdocs/.git) to the proxy repo (htdocs/bare.git)
  5. reverse the clone relationship between both repos

I know it’s weird but works like a charm.

$ cd /<path>/htdocs
$ git init
$ cat > .gitignore

/bare.git/<CTRL+D>

$ git commit -a -m "initial files"
$ git clone --bare -l ./ ./bare.git
$ cd bare.git
$ git remote rm origin
$ cd ..
$ git remote add origin /<path>/htdocs/bare.git
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master

After cloning the proxy repo to the development repo I could push and pull without any issue, but my fourth mistake was to think that all was nearly done. Instead UTF-8 filenames were my next headache because git status detected all those files as untracked. I tried hard to solve that issue, but in the end I decided just to work around it by committing those untracked files again.

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   ".../1... Foz d'Iguac314247u 2010.doc"
$ git add ".../1... Foz d'Iguac314247u 2010.doc"
fatal: pathspec '.../1... Foz d'Iguac314247u 2010.doc' did not match any files
$ git add ".../1... Foz d'Iguaçu 2010.doc"
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   ".../1... Foz d'Iguac314247u 2010.doc"

One of the problems is that Git/OSX is showing escaped filenames in the output of git status, but it wants them added unescaped. After adding, committing and pushing those files I got this from Cyberduck (I only clipped the same confidential data from all the lines):

Mirrored copies of the same files, also with the same filenames! Oh, that’s weird! Of course they have different encodings.

Git/Linux and Git/OSX work perfectly well inside their respective operating systems with UTF-8 filenames but as soon as you cross the border of a system, issues arise. Linux and OSX support UTF-8 filenames differently. I think Git is to blame because it should offer a state of the art layer for supporting a distributed VCS. I read something about it being fixed in a new version, but I cannot install anything on the remote Linux machine to test it.

Even after all of the above, at some point (I do not remember how it happened) git status kept showing again the pathspec error. So I completely removed all the data files directories from Git and added them to .gitignore. That solved all the issues… hopefully.