Skip to content

Support loading SQLite3 extensions with config/database.yml#53827

Merged
eileencodes merged 1 commit intorails:mainfrom
flavorjones:flavorjones-support-sqlite-extensions
Dec 4, 2024
Merged

Support loading SQLite3 extensions with config/database.yml#53827
eileencodes merged 1 commit intorails:mainfrom
flavorjones:flavorjones-support-sqlite-extensions

Conversation

@flavorjones
Copy link
Member

@flavorjones flavorjones commented Dec 4, 2024

Motivation / Background

The sqlite3 gem v2.4.0 introduces support for loading extensions passed as a kwarg to Database.new. This PR leverages that feature to allow configuration of extensions in the database.yml file using either filesystem paths or the names of modules that respond to .to_path.

Documentation is added to both SQLite3Adapter rdoc and the "Configuring" guide. It also extends and improves the documentation around general SQLite3Adapter configuration.

See sparklemotion/sqlite3-ruby#586 for more information.

Detail

The SQLite3Adapter is designed to pass the database config to the SQLite3::Database constructor, so configuring extensions with filesystem paths will work without this PR like this:

development:
  adapter: sqlite3
  extensions:
    - .sqlpkg/nalgeon/crypto/crypto.so # a filesystem path
    - <%= SQLean::UUID.to_path %>      # or ruby code returning a path

What this PR does, though, is allow this usage:

development:
  adapter: sqlite3
  extensions:
    - SQLean::UUID

which I think is more readable and ergonomic.

Additional information

See sparklemotion/sqlite3-ruby#586 for how the underlying driver changed in v2.4.0.

For an idea of what using extensions looked like until now, check out this blog post from @fractaledmind which talks about using this Rails initializer to inject extensions.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@flavorjones
Copy link
Member Author

The CI failure seems like an unrelated flake.

Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! One comment on docs, but otherwise this looks good.

The `sqlite3` gem v2.4.0 introduces support for loading extensions
that are passed to the Database constructor. This feature leverages
that feature to allow configuration of extensions using either
filesystem paths or the names of modules that respond to `.to_path`.

This commit documents the feature in both SQLite3Adapter rdoc and the
"Configuring" guide. It also extends and improves the documentation
around general SQLite3Adapter configuration.

See sparklemotion/sqlite3-ruby#586 for more information.
@flavorjones flavorjones force-pushed the flavorjones-support-sqlite-extensions branch from 36613fe to b599d4c Compare December 4, 2024 19:54
@flavorjones
Copy link
Member Author

@eileencodes Thanks! I just force-pushed an amended commit addressing the docs comment.

@eileencodes eileencodes merged commit 91d4563 into rails:main Dec 4, 2024
@flavorjones flavorjones deleted the flavorjones-support-sqlite-extensions branch December 4, 2024 22:46
rossta added a commit to rossta/asg017-sqlite-dist that referenced this pull request Dec 12, 2024
The sqlite3-ruby recently published an improvement to make it easier and
more flexible to load extensions:

sparklemotion/sqlite3-ruby#586

Ruby modules for SQLite extensions should implement the interface:

    interface _ExtensionSpecifier
      def to_path: () → String
    end

A complementary change in Rails takes advantage of this interface to
integrate the primary configuration file with the new sqlite3-ruby
interface for extension loading:

rails/rails#53827

The gem template provided here already has a similar method:

    def loadable_path

The change proposed here is to modify the gem template to provide an
alias to loadable_path as to_path.o

    # example
    SqliteVec.to_path # => returns same result as loadable_path

As a result of this change, Ruby gems published with this tool will
conform to the new interface supported in sqlite3-ruby and Rails.
rossta added a commit to rossta/asg017-sqlite-dist that referenced this pull request Dec 12, 2024
The sqlite3-ruby recently published an improvement to make it easier and
more flexible to load extensions:

sparklemotion/sqlite3-ruby#586

Ruby modules for SQLite extensions should implement the interface:

    interface _ExtensionSpecifier
      def to_path: () → String
    end

A complementary change in Rails takes advantage of this interface to
integrate the primary configuration file with the new sqlite3-ruby
interface for extension loading:

rails/rails#53827

The gem template provided here already has a similar method:

    def loadable_path

The change proposed here is to modify the gem template to provide an
alias to loadable_path as to_path.o

    # example
    SqliteVec.to_path # => returns same result as loadable_path

As a result of this change, Ruby gems published with this tool will
conform to the new interface supported in sqlite3-ruby and Rails.
thibaudgg added a commit to csa-admin-org/csa-admin that referenced this pull request Mar 4, 2025
This patch aims to improve string ordering by properly support accent character.

SQLite extensions are required, which is supported on Rails main branch.

rails/rails#53827
https://fractaledmind.github.io/2024/12/09/sqlite-on-rails-loading-extensions/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants