Skip to content

feat: enable load_extensions config for connections #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2023

Conversation

bkono
Copy link
Contributor

@bkono bkono commented Mar 25, 2023

Opening this to start a discussion. This is an attempt to implement this comment from issue #136, piggybacking on the hard work done in #160.

I needed this to be able to actively use extensions with Ecto inside a Phoenix app. There's a few scenarios where it comes in handy, but the primary driver for me is an app doing vector queries via sqlite-vss which needs both vector0 and vss0 libs loaded on the connection.

With this change, adding the below in a phoenix app makes everything Just Work™:

config :exqlite,
  load_extensions: [
    "./priv/sqlite/darwin-arm64/lines0",
    "./priv/sqlite/darwin-arm64/vector0",
    "./priv/sqlite/darwin-arm64/vss0"
  ]

If there's interest in this, I'm happy to clean up the PR (coughIO.puts...cough) and update with documentation as well, but didn't want to invest too much time without confirming it's a change you want to take upstream.

@warmwaffles
Copy link
Member

This does introduce loading extensions globally for anyone using multiple ecto sqlite databases. While I think 90+% of use cases just have one DB, I think there will be others out there that need different extensions loaded for different repos.

This is contrived but I was thinking something like:

config :my_app, MyApp.FooRepo,
  database: "foo.db",
  load_extensions: [
    "./priv/sqlite/darwin-arm64/lines0"
  ]

config :my_app, MyApp.BarRepo,
  database: "bar.db",
  load_extensions: [
    "./priv/sqlite/darwin-arm64/vector0",
    "./priv/sqlite/darwin-arm64/vss0"
  ]

Having it at the global level for exqlite is good to have, but allowing it to be loaded on a per repo basis would be another configuration we could support.

@bkono
Copy link
Contributor Author

bkono commented Mar 27, 2023

This does introduce loading extensions globally for anyone using multiple ecto sqlite databases. While I think 90+% of use cases just have one DB, I think there will be others out there that need different extensions loaded for different repos.
....
Having it at the global level for exqlite is good to have, but allowing it to be loaded on a per repo basis would be another configuration we could support.

Great suggestion. I've gone ahead and updated to support a combined global and per-connection options loading. Let me know if you have any codebase style preferences like extracting the case into separate do_load_extensions funs disregard, credo was unhappy with that extra nest so I extracted it

@bkono bkono force-pushed the feat/load-extensions-config branch from 96b45d2 to 278bb02 Compare March 27, 2023 16:43
@warmwaffles
Copy link
Member

Perfect!

@warmwaffles
Copy link
Member

@bkono would you mind adding some documentation to the Exqlite.Connection.connect/1 function? Once that's in place we should be ready to merge and release.

@bkono bkono force-pushed the feat/load-extensions-config branch from 278bb02 to 60837f8 Compare March 27, 2023 18:50
@bkono
Copy link
Contributor Author

bkono commented Mar 27, 2023

@bkono would you mind adding some documentation to the Exqlite.Connection.connect/1 function? Once that's in place we should be ready to merge and release.

Of course.

image

In my apps I've been using a little helper for architecture resolution (since I'm on macOS & linux for dev & deploy). Wasn't sure if there's a good place to suggest something like this. I'll probably just create a light sample repo to point people at, but adding it here in case you have an alternative:

arch_dir =
  System.cmd("uname", ["-sm"])
  |> elem(0)
  |> String.trim()
  |> String.replace(" ", "-")
  |> String.downcase()

config :myapp, arch_dir: arch_dir

config :myapp, Myapp.Repo,
  load_extensions: [
    "./priv/sqlite/#{arch_dir}/vector0",
    "./priv/sqlite/#{arch_dir}/vss0"
  ]

@warmwaffles
Copy link
Member

In my apps I've been using a little helper for architecture resolution (since I'm on macOS & linux for dev & deploy). Wasn't sure if there's a good place to suggest something like this. I'll probably just create a light sample repo to point people at, but adding it here in case you have an alternative:

I think that is a totally reasonable example to provide. Others may do their resolutions differently.

@bkono bkono force-pushed the feat/load-extensions-config branch from 60837f8 to c7b40a3 Compare March 28, 2023 04:44
@bkono
Copy link
Contributor Author

bkono commented Mar 28, 2023

In my apps I've been using a little helper for architecture resolution (since I'm on macOS & linux for dev & deploy). Wasn't sure if there's a good place to suggest something like this. I'll probably just create a light sample repo to point people at, but adding it here in case you have an alternative:

I think that is a totally reasonable example to provide. Others may do their resolutions differently.

Done and updated. Let me know if there's anything else missing.

@warmwaffles warmwaffles merged commit 8aa1675 into elixir-sqlite:main Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants