rake db:migrate 任务的 JRuby JDBC 设置

JRuby JDBC setup for rake db:migrate task

我有一个 ruby Sinatra 应用程序和 gem pg 使用一些 Java 开源应用程序,我切换到 jruby。这是 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 文件

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 : 零

我是运行 postgresql jdbc。

更新

我将 postgresql.jar 添加到我的 jruby 路径(jruby 由 brew 在我的 mac 中安装),我得到了同样的错误。我还删除了 jar 文件,但出现驱动程序未找到错误。

感谢您的帮助。

我找到了解决方案。

首先确保您的 Gemfile

中包含以下内容
gem 'activerecord-jdbc-adapter', :require => 'arjdbc'
gem 'activerecord-jdbcpostgresql-adapter'

然后在您的 db 迁移任务中 这是环境设置:

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

这也是 database.yaml 用于开发:

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

享受你的 JDBC 与 Jruby on Sinatra 应用程序!!!!

只需在您的配置中使用 适配器:postgresql,它应该可以正常工作。