I want to implement a h2 database in my jruby (9.2.20.0) and rails project (Rails 6.0.4.1 but also with 6.1.4.1) on my Ubuntu system. Therefore, I created a simple h2_demo
rails new h2_demo --webpack -T -d jdbc
I found a very old setting (https://gist.github.com/sixman9/847658), but unfortunately it is not compatible.
activerecord-jdbch2-adapter
needs activerecord-jdbc-adapter (~> 1.3, >= 1.3.20)
but of course rails needs gem 'activerecord-jdbc-adapter', '~> 60.4'
So I thought jruby still supports h2 via jdbc driver. I use the generated Gemfile and didn't change anything there.
I only changed my database.yml to:
default: &default
adapter: jdbc
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
driver: org.h2.Driver
development:
<<: *default
username: sa
password:
url: jdbc:h2:db/development
test:
<<: *default
username: sa
password:
url: jdbc:h2:db/test
production:
url: jdbc:h2:db/pod
username: sa
password:
Optional I used following url string url: jdbc:h2:db/development;AUTO_SERVER=TRUE;
but without differences.
If I try rails db:create
nothing happens. No error message, but also no databases are created.
I also created a model with a migration
rails g model user name:string
But with `rails db:migrate' I get an error message:
$ rails db:migrate
rails aborted!
ArgumentError: wrong number of arguments (given 4, expected 1..3)
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-jdbc-adapter-60.4-java/lib/arjdbc/abstract/core.rb:12:in `initialize'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-jdbc-adapter-60.4-java/lib/arjdbc/jdbc/callbacks.rb:14:in `new'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-jdbc-adapter-60.4-java/lib/arjdbc/jdbc/connection_methods.rb:10:in `jdbc_connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1125:in `retrieve_connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/connection_handling.rb:189:in `connection'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/tasks/database_tasks.rb:238:in `migrate'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/railties/databases.rake:86:in `block in <main>'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/activerecord-6.0.4.1/lib/active_record/railties/databases.rake:84:in `block in <main>'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/railties-6.0.4.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/railties-6.0.4.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/railties-6.0.4.1/lib/rails/command.rb:48:in `invoke'
/home/user/.rvm/gems/jruby-9.2.20.0/gems/railties-6.0.4.1/lib/rails/commands.rb:18:in `<main>'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Just in case rails is not able to create a h2 database, I created a h2 by my own and placed it in the db folder. The error message is still the same.
So my two questions are:
- Any idea how to archive a working h2 connection in this project?
- Is there also a good way to implement h2 in a ruby project? I haven't found any working solutions.
The recommended solution (How to use H2 as embedded database in Postgres-compat mode, from jruby/rails) using the postgres mode isn't working Maybe due unclear setup options in this thread???
Thanks a lot.