Search code examples
ruby-on-railspostgresqlrvmruby-on-rails-2ruby-1.9.3

require 'pg' returns true but fails to define PGconn and PG


In my TestLogger class, require 'pg' (version 0.16.0) returns true, but PGconn is not registered.

So far, only the server has this problem, and only in the TestLogger. Other ruby code that does require 'pg' has no problem getting PG and PGconn. And everything works on my developer machine (Mac).

So the problem only happens on the server (Debian), and only in TestLogger.

The failing environment is built like this: I have a folder containing autorun.rb, Gemfile (+fresh Gemfile.lock generated by running bundle install), and testlogger.rb. The autorun.rb script is run sometimes manually, sometimes from cron, but always doing cd $some_folder; cd autorun_folder before starting thus ensuring that the proper environment (rvm and Gemfile) gets loaded.

TestLogger looks like this:

# encoding: UTF-8
class TestLogger
  def initialize(opt = {})
    require 'pg'
    @@pg = PGconn.open :dbname => 'statuslog'
  end
end

autorun.rb begins like this:

require_relative './testlogger'
$runlog = TestLogger.new
...

and the code dies with "PGconn missing", so it seems that require 'pg', while returning true, actually didn't really load PG and PGconn as it usually does. The strange thing is that tasks that are started by autorun does also use postgres (require 'pg' and PGconn.open ...) and there it works. It uses the same table 'statuslog' and is run by the same user.

I've also checked $PATH and $: and my .rvm folders, looking for any pg.rb that could be found by require instead of the one want. I found activerecord-jdbc-adapter-1.2.9/lib/pg.rb (which in turn loads PG) and heroku-2.35.0/lib/heroku/command/pg.rb -- could this be the issue and how could I check?


Solution

  • (copy & paste as per Simon B.'s answer for attribution - thanks Simon)

    In testlogger.rb:

    before = $LOADED_FEATURES.dup
    require 'pg'
    p $LOADED_FEATURES-before
    

    This prints which files are actually loaded by require 'pg' Since it was activerecord-jdbc-adapter, and since jdbc is for java/jruby, and since Simon uses MRI, the solution was to uninstall the jdbc adapter.

    Uninstalling with

    gem uninstall activerecord-jdbc-adapter
    

    Worked