Search code examples
postgresqljdbcsinatrajrubysinatra-activerecord

JRuby JDBC setup for rake db:migrate task


I had a ruby Sinatra app and gem pg to use some Java open source applications, I switched to jruby. This is the output for jruby -S rake db:migrate.

NoMethodError: undefined method `get_oid_type' for #<ActiveRecord::ConnectionAdapters::JdbcAdapter:0x294f9d50>
api/tasks/db.rake:18:in `block in (root)'

rake file

require 'active_record'
require 'jdbc/postgresql'

namespace :db do
  task(:environment) do
    ActiveRecord::Base.establish_connection(
    :adapter => 'jdbc',
    :driver => 'org.postgresql.Driver',
    :url => 'jdbc:postgresql:db_name',
    :username => 'username',
    :password => 'password'
    )
  end

  desc 'Migrate the MOT core models (options: VERSION=x, VERBOSE=false)'
  task :migrate => :environment do
    ActiveRecord::Migration.verbose = true
    ActiveRecord::Migrator.migrate "#{File.dirname(__FILE__)}/../db/migrate", ENV['VERSION'] ? ENV['VERSION'].to_i : nil
  end

  desc 'Rolls the schema back to the previous version of MOT core model(specify steps w/ STEP=n).'
  task :rollback => :environment do
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    ActiveRecord::Migrator.rollback "#{File.dirname(__FILE__)}/../db/migrate", ENV['VERSION'] ? ENV['VERSION'].to_i : step
  end
end

db.rake:#18

ActiveRecord::Migrator.migrate "#{File.dirname(FILE)}/../db/migrate", ENV['VERSION'] ? ENV['VERSION'].to_i : nil

I am running postgresql with jdbc.

UPDATE

I add the postgresql.jar to my jruby path (jruby installed by brew in my mac) and I am getting the same error. I also removed the jar file and I had driver not found error.

Thanks for your help.


Solution

  • I found the solution.

    First make sure you have the followings in your Gemfile

    gem 'activerecord-jdbc-adapter', :require => 'arjdbc'
    gem 'activerecord-jdbcpostgresql-adapter'
    

    then in your db migrate task this is the setup for environment:

    task(:environment) do
        ActiveRecord::Base.establish_connection(
        :adapter => 'jdbcpostgresql',
        :driver => 'org.postgresql.Driver',
        :url => 'jdbc:postgresql://localhost/YOUR_DATABASE_NAME'
        )
      end
    

    Also this is the database.yaml for development:

    development:
        adapter: jdbcpostgresql
        driver: org.postgresql.Driver
        encoding: unicode
        url: jdbc:postgresql://localhost/YOUR_DATABASE_NAME
        username: USER_NAME
        password: PASSWORD
    

    Enjoy your JDBC with Jruby on Sinatra application !!!!