Is a DB connection via an options hash supported?


#1

I’m not sure where I’m supposed to be asking this. If this is the wrong place, can you please redirect me to the correct spot and I can re-post there.

I only discovered rom rb a couple of months ago. I’m also a bit of a newbie when it comes to ruby. I’m trying to connect to the database using an options hash rather than a connection string, like so:

@rom = ROM.container(:sql, adapter: 'mysql2', user: 'someuser', password: 'somepassword', database: 'somedb', host: '127.0.0.1', port: 6603) do |config| ... end

I’m not using a framework or anything. I’m just doing everything in a standalone directory, trying to learn how this all works. I’ve used the sequel gem before and was able to connect using an options hash and assumed that we could do the same thing via rom but I get the following error:

URI::InvalidURIError: bad URI(is not URI?): {:adapter=>"mysql2", :user=>"someuser", :password=>"somepassword", :database=>"somedb", :host=>"127.0.0.1", :port=>6603}

In trying to learn how the connection is supposed to work, I traced it down to the file rom-sql-1.2.2/lib/rom/sql/gateway.rb

def connect(uri, *args) case uri when ::Sequel::Database uri else ::Sequel.connect(uri.to_s, *args) end end

Is the ::Sequel.connect line supposed to convert the uri.to_s? If I remove the to_s and let it go into into the sequel connect as a hash, things just seem to work.

Am I misusing rom rb if I want to be able to connect via an options hash?
The documentation only provides using a connection string as an example so I’m wondering whether I’m doing something incorrectly.

Any help would be much appreciated.


#2

At the moment URIs are required, and it’s going to stay like that. Hashes are more complex to handle and in most cases people use ENV to store connection URIs anyway.


#3

ROM can accept a Sequel connection instance for such cases.


#4

For an illustrative example, look at https://github.com/rom-rb/rom-rails/blob/master/lib/rom/rails/active_record/configuration.rb

This is the code that ROM::Rails uses to extract and use the hash-configuration it extracts from activerecord, in a case where we’ve got a mixed adapter setup in a Rails project.


#5

Thank you for the help and code examples. It is much appreciated and a good learning experience for me. I’ve since found out that the reason I was getting a bad URI was that the password contained an invalid character so it failed the URI parse.