Skip to content

Commit 858cc7e

Browse files
committed
Add Sqlpkg.path_for method to get file path to installed extension
1 parent 278de92 commit 858cc7e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ bundle exec sqlpkg help
7878
└────────────────────────────────────────────────┘
7979
```
8080

81+
You can get the path to an installed extension using the `Sqlpkg.path_for` method, e.g.:
82+
83+
```ruby
84+
Sqlpkg.path_for("nalgeon/uuid")
85+
# => "./.sqlpkg/nalgeon/uuid/uuid.dylib"
86+
```
87+
88+
You can also use the shorter `.[]` alias:
89+
90+
```ruby
91+
Sqlpkg["nalgeon/uuid"]
92+
# => "./.sqlpkg/nalgeon/uuid/uuid.dylib"
93+
```
94+
95+
If you try to access an extension that hasn't been installed, a `Sqlpkg::ExtensionNotInstalledError` will be raised:
96+
97+
```ruby
98+
Sqlpkg["nalgeon/ulid"]
99+
# raises Sqlpkg::ExtensionNotInstalledError
100+
```
101+
102+
This feature is particulary useful for the [new Rails feature](https://github.com/rails/rails/pull/53827) and [`sqlite3-ruby` feature](https://github.com/sparklemotion/sqlite3-ruby/pull/586) that allows automatically loading extensions via the `extensions` keyword defined in the Rails `config/database.yml` or the `SQLite3::Database.new` method call. You can now use either:
103+
104+
```yaml
105+
development:
106+
adapter: sqlite3
107+
extensions:
108+
- <%= Sqlpkg.path_for("asg017/ulid") %>
109+
```
110+
111+
or if you are using `SQLite3::Database` directly:
112+
113+
```ruby
114+
db = SQLite3::Database.new(":memory:", extensions: [Sqlpkg.path_for("asg017/ulid")])
115+
```
116+
81117
## Development
82118

83119
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/sqlpkg.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
# frozen_string_literal: true
22

33
module Sqlpkg
4+
DIR = ".sqlpkg"
5+
FILE_PATTERN = "*.{dylib,so,dll}"
6+
7+
Error = Class.new(StandardError)
8+
ExtensionNotInstalledError = Class.new(Error)
9+
10+
class << self
11+
# File path for identified extension
12+
# => "./.sqlpkg/nalgeon/uuid/uuid.dylib"
13+
def path_for(identifier)
14+
path_glob = File.join(file_dir, identifier, FILE_PATTERN)
15+
path = Dir.glob(path_glob).first
16+
17+
path || raise(ExtensionNotInstalledError, "No extension found for identifier: #{identifier}")
18+
end
19+
alias_method :[], :path_for
20+
21+
# The directory where `sqlpkg` stores installed extensions
22+
# => "./.sqlpkg"
23+
def file_dir
24+
File.join(__dir__, DIR)
25+
end
26+
27+
# List of file paths for all installed extensions
28+
# => ["./.sqlpkg/asg017/ulid/ulid0.dylib", "./.sqlpkg/nalgeon/uuid/uuid.dylib"]
29+
def installed_extension_paths
30+
Dir.glob File.join(file_dir, "**", FILE_PATTERN)
31+
end
32+
end
433
end
534

635
require_relative "sqlpkg/version"

test/test_sqlpkg.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,30 @@ class TestSqlpkg < Minitest::Test
66
def test_that_it_has_a_version_number
77
refute_nil ::Sqlpkg::VERSION
88
end
9+
10+
def test_path_for_when_glob_returns_paths
11+
path = "./.sqlpkg/nalgeon/uuid/uuid.dylib"
12+
Dir.stub :glob, [path] do
13+
assert_equal path, Sqlpkg.path_for("nalgeon/uuid")
14+
end
15+
end
16+
17+
def test_path_for_when_glob_returns_empty_array
18+
assert_raises Sqlpkg::ExtensionNotInstalledError do
19+
Sqlpkg.path_for("nalgeon/uuid")
20+
end
21+
end
22+
23+
def test_accessor_when_glob_returns_paths
24+
path = "./.sqlpkg/nalgeon/uuid/uuid.dylib"
25+
Dir.stub :glob, [path] do
26+
assert_equal path, Sqlpkg["nalgeon/uuid"]
27+
end
28+
end
29+
30+
def test_accessor_when_glob_returns_empty_array
31+
assert_raises Sqlpkg::ExtensionNotInstalledError do
32+
Sqlpkg["nalgeon/uuid"]
33+
end
34+
end
935
end

0 commit comments

Comments
 (0)