Skip to content

Commit

Permalink
Restore unescaping of database file paths in the sqlite and amalgalit…
Browse files Browse the repository at this point in the history
…e adapters (Fixes #2201)

The 5.83.0 release notes mentioned that uri_to_options was changed
to handle conversion of URI options, but considering that I missed
fixing this issue for the sqlite and amalgalite adapters that ship
with Sequel, expecting external adapters to handle changes seems
unreasonable.

Add an options_from_uri method that calls uri_to_options and handles
the options unescaping, and have Sequel call that in the two places
that previously called uri_to_options directly.

As this is a regression from the previously release, bump version
to 5.83.1.
  • Loading branch information
jeremyevans committed Aug 8, 2024
1 parent a0cbb6c commit a368d65
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
=== 5.83.1 (2024-08-08)

* Restore unescaping of database file paths in the sqlite and amalgalite adapters (jeremyevans) (#2201)

=== 5.83.0 (2024-08-01)

* Make optimistic_locking plugin not keep lock column in changed_columns after updating instance (jeremyevans) (#2196)
Expand Down
2 changes: 1 addition & 1 deletion doc/release_notes/5.83.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

* The Database.uri_to_options private class method now handles
conversion of URI parameters to options. Previously, this was
handled by callers of this method.
handled by callers of this method (change reverted in 5.83.1).

* The _merge_matched_sql and _merge_not_matched_sql private Dataset
methods in PostgreSQL have been replaced with
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/database/connecting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.connect(conn_string, opts = OPTS)
uri = URI.parse(conn_string)
scheme = uri.scheme
c = adapter_class(scheme)
opts = c.send(:uri_to_options, uri).merge!(opts).merge!(:orig_opts=>opts.dup, :uri=>conn_string, :adapter=>scheme)
opts = c.send(:options_from_uri, uri).merge!(opts).merge!(:orig_opts=>opts.dup, :uri=>conn_string, :adapter=>scheme)
end
when Hash
opts = conn_string.merge(opts)
Expand Down
11 changes: 8 additions & 3 deletions lib/sequel/database/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,23 @@ def self.run_after_initialize(instance)
# Converts a uri to an options hash. These options are then passed
# to a newly created database object.
def self.uri_to_options(uri)
uri_options = {
{
:user => uri.user,
:password => uri.password,
:port => uri.port,
:host => uri.hostname,
:database => (m = /\/(.*)/.match(uri.path)) && (m[1])
}
end
private_class_method :uri_to_options

def self.options_from_uri(uri)
uri_options = uri_to_options(uri)
uri.query.split('&').map{|s| s.split('=')}.each{|k,v| uri_options[k.to_sym] = v if k && !k.empty?} unless uri.query.to_s.strip.empty?
uri_options.to_a.each{|k,v| uri_options[k] = URI::DEFAULT_PARSER.unescape(v) if v.is_a?(String)}
uri_options
end
private_class_method :uri_to_options
private_class_method :options_from_uri

# The options hash for this database
attr_reader :opts
Expand Down Expand Up @@ -266,7 +271,7 @@ def inspect
keys = [:host, :database, :user]
opts = self.opts
if !keys.any?{|k| opts[k]} && opts[:uri]
opts = self.class.send(:uri_to_options, URI.parse(opts[:uri]))
opts = self.class.send(:options_from_uri, URI.parse(opts[:uri]))
end

keys.each do |key|
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Sequel

# The tiny version of Sequel. Usually 0, only bumped for bugfix
# releases that fix regressions from previous versions.
TINY = 0
TINY = 1

# The version of Sequel you are using, as a string (e.g. "2.11.0")
VERSION = [MAJOR, MINOR, TINY].join('.').freeze
Expand Down
4 changes: 4 additions & 0 deletions spec/adapters/sqlite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
Sequel.datetime_class = Time
end

it "should unescape escaped paths in URI for database file" do
DB.class.send(:options_from_uri, URI("scheme://app%2Fdata%2Ftest.db"))[:database].must_equal 'app/data/test.db'
end if DB.adapter_scheme == :sqlite || DB.adapter_scheme == :amalgalite

it "should support casting to Date by using the date function" do
@db.get(Sequel.cast('2012-10-20 11:12:13', Date)).must_equal '2012-10-20'
end
Expand Down

0 comments on commit a368d65

Please sign in to comment.