Skip to content

Commit

Permalink
Make after_commit_everywhere a manual dependency on AR 7.1 or older
Browse files Browse the repository at this point in the history
Active Record 7.2 added native model-less transaction callbacks, so
there is no need for folks using newest AR versions to have
their Gemfiles.`after_commit_everywhere` in their project.

See #32
  • Loading branch information
janko committed Nov 10, 2024
1 parent 4e2e354 commit eb41616
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 18 deletions.
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ background to share the same database connection, which is something Sequel
wasn't designed for. Reusing Active Record's connection means (dis)connecting
and sharing between threads is all handled automatically.

## Framework Agnostic

The only hard dependencies are:

* [ActiveRecord](https://github.com/rails/rails/tree/main/activerecord)
* [Sequel](https://github.com/jeremyevans/sequel)
* [after_commit_everywhere](https://github.com/Envek/after_commit_everywhere) (on Active Record 7.1 or older)

...which means you can use it with any Rack / Ruby based framework:
Rails / Roda / Sinatra etc. or even without a framework.

## Installation

Add the gem to your project:
Expand All @@ -57,6 +46,12 @@ Add the gem to your project:
$ bundle add sequel-activerecord_connection
```

If you're using Active Record 7.1 or older, you'll also need to add the [after_commit_everywhere] gem:

```sh
$ bundle add after_commit_everywhere # on Active Record 7.1 or older
```

## Usage

Assuming you've configured your ActiveRecord connection, you can initialize the
Expand All @@ -67,7 +62,7 @@ appropriate Sequel adapter and load the `activerecord_connection` extension: e.g
# e.g. Rails: config/initializers/sequel.rb

require "sequel"
DB = Sequel.postgres(extensions: :activerecord_connection) # postgres
DB = Sequel.postgres(extensions: :activerecord_connection) # for PostgreSQL
```

Now any Sequel operations that you make will internaly be done using the
Expand Down Expand Up @@ -286,3 +281,4 @@ Everyone interacting in this project's codebases, issue trackers, chat rooms and
[Oracle enhanced]: https://github.com/rsim/oracle-enhanced
[SQL Server]: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter
[sql_log_normalizer]: https://sequel.jeremyevans.net/rdoc-plugins/files/lib/sequel/extensions/sql_log_normalizer_rb.html
[after_commit_everywhere]: https://github.com/Envek/after_commit_everywhere
1 change: 1 addition & 0 deletions gemfiles/Gemfile.activerecord-5.0
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", "~> 5.0.0"
gem "after_commit_everywhere", "~> 1.1"

platform :mri do
gem "pg", "~> 1.0"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/Gemfile.activerecord-5.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", "~> 5.1.0"
gem "after_commit_everywhere", "~> 1.1"

platform :mri do
gem "pg", "~> 1.0"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/Gemfile.activerecord-5.2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", "~> 5.2.0"
gem "after_commit_everywhere", "~> 1.1"

platform :mri do
gem "pg", "~> 1.0"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/Gemfile.activerecord-6.0
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", "~> 6.0.0"
gem "after_commit_everywhere", "~> 1.1"

platform :mri do
gem "pg", "~> 1.0"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/Gemfile.activerecord-6.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", "~> 6.1.0"
gem "after_commit_everywhere", "~> 1.1"

platform :mri do
gem "pg", "~> 1.0"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/Gemfile.activerecord-7.0
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", "~> 7.0.0"
gem "after_commit_everywhere", "~> 1.1"

platform :mri do
gem "pg", "~> 1.0"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/Gemfile.activerecord-7.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", "~> 7.1.0"
gem "after_commit_everywhere", "~> 1.1"

platform :mri do
gem "pg", "~> 1.0"
Expand Down
13 changes: 8 additions & 5 deletions lib/sequel/extensions/activerecord_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ def rollback_transaction(conn, opts = OPTS)

# When Active Record holds the transaction, we cannot use Sequel hooks,
# because Sequel doesn't have knowledge of when the transaction is
# committed. So in this case we register an Active Record hook using the
# after_commit_everywhere gem.
# committed. So in this case we register the hook using Active Record.
def add_transaction_hook(conn, type, block)
if _trans(conn)[:activerecord]
activerecord_transaction_callback(type, &block)
Expand All @@ -130,8 +129,7 @@ def add_transaction_hook(conn, type, block)

# When Active Record holds the savepoint, we cannot use Sequel hooks,
# because Sequel doesn't have knowledge of when the savepoint is
# released. So in this case we register an Active Record hook using the
# after_commit_everywhere gem.
# released. So in this case we register the hook using Active Record.
def add_savepoint_hook(conn, type, block)
if _trans(conn)[:savepoints].last[:activerecord]
activerecord_transaction_callback(type, &block)
Expand All @@ -145,7 +143,12 @@ def activerecord_transaction_callback(type, &block)
activerecord_connection.current_transaction.public_send(type, &block)
end
else
require "after_commit_everywhere"
begin
gem "after_commit_everywhere", "~> 1.1"
require "after_commit_everywhere"
rescue LoadError
fail Error, %q(You need to add `gem "after_commit_everywhere", "~> 1.1"` to your Gemfile when using Active Record < 7.2)
end

def activerecord_transaction_callback(type, &block)
AfterCommitEverywhere.public_send(type, &block)
Expand Down
1 change: 0 additions & 1 deletion sequel-activerecord_connection.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Gem::Specification.new do |spec|

spec.add_dependency "sequel", "~> 5.38"
spec.add_dependency "activerecord", ">= 5.0", "< 8.1"
spec.add_dependency "after_commit_everywhere", "~> 1.1"

spec.add_development_dependency "sequel_pg" unless RUBY_ENGINE == "jruby"
spec.add_development_dependency "minitest"
Expand Down

0 comments on commit eb41616

Please sign in to comment.