Skip to content

Commit c9d2c11

Browse files
committed
Recognize sqlite:filename and amalgalite:filename connection URLs (Fixes #2334)
1 parent 1843b0b commit c9d2c11

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
=== master
2+
3+
* Recognize sqlite:filename and amalgalite:filename connection URLs (jeremyevans) (#2334)
4+
15
=== 5.97.0 (2025-10-01)
26

37
* Simplify Postgres::PGRange#inspect output (jeremyevans)

lib/sequel/adapters/amalgalite.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,26 @@ class Database < Sequel::Database
6262

6363
# Mimic the file:// uri, by having 2 preceding slashes specify a relative
6464
# path, and 3 preceding slashes specify an absolute path.
65+
# Also support no preceding slashes to specify a relative path.
6566
def self.uri_to_options(uri) # :nodoc:
66-
{ :database => (uri.host.nil? && uri.path == '/') ? nil : "#{uri.host}#{uri.path}" }
67+
database = if uri.host.nil?
68+
case uri.path
69+
when '/'
70+
nil
71+
when nil
72+
uri.opaque
73+
else
74+
uri.path
75+
end
76+
else
77+
"#{uri.host}#{uri.path}"
78+
end
79+
80+
{ :database => database }
6781
end
6882
private_class_method :uri_to_options
6983

84+
7085
# Connect to the database. Since SQLite is a file based database,
7186
# the only options available are :database (to specify the database
7287
# name), and :timeout, to specify how long to wait for the database to

lib/sequel/adapters/sqlite.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,22 @@ class Database < Sequel::Database
8989

9090
# Mimic the file:// uri, by having 2 preceding slashes specify a relative
9191
# path, and 3 preceding slashes specify an absolute path.
92+
# Also support no preceding slashes to specify a relative path.
9293
def self.uri_to_options(uri) # :nodoc:
93-
{ :database => (uri.host.nil? && uri.path == '/') ? nil : "#{uri.host}#{uri.path}" }
94+
database = if uri.host.nil?
95+
case uri.path
96+
when '/'
97+
nil
98+
when nil
99+
uri.opaque
100+
else
101+
uri.path
102+
end
103+
else
104+
"#{uri.host}#{uri.path}"
105+
end
106+
107+
{ :database => database }
94108
end
95109

96110
private_class_method :uri_to_options

spec/adapters/sqlite_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
DB.class.send(:options_from_uri, URI("scheme://app%2Fdata%2Ftest.db"))[:database].must_equal 'app/data/test.db'
1717
end if DB.adapter_scheme == :sqlite || DB.adapter_scheme == :amalgalite
1818

19+
it "should unescape escaped paths in URI for database file" do
20+
filename = "spec/sqlite-test-#{$$}.rb"
21+
begin
22+
File.file?(filename).must_equal false
23+
Sequel.connect("#{DB.adapter_scheme}:#{filename}")
24+
File.file?(filename).must_equal true
25+
ensure
26+
File.delete(filename) if File.file?(filename)
27+
end
28+
end if DB.adapter_scheme == :sqlite || DB.adapter_scheme == :amalgalite
29+
1930
it "should support casting to Date by using the date function" do
2031
@db.get(Sequel.cast('2012-10-20 11:12:13', Date)).must_equal '2012-10-20'
2132
end

0 commit comments

Comments
 (0)