DEV Community

Janko Marohnić
Janko Marohnić

Posted on

sequel-activerecord_connection now fully supports Sequel's transaction API

A while ago I created the sequel-activerecord_connection gem, which allows you to use Sequel alongside ActiveRecord and reuse the same database connection. This makes it easier to try Sequel out or use libraries that depend on Sequel (e.g. Rodauth) in ActiveRecord-based projects.

Originally the transaction support was partially implemented, where the most common Sequel transaction options were emulated with the ActiveRecord API. In the latest version I've rewritten the transaction code to fully support Sequel's transaction API, including transaction/savepoint hooks.

require "sequel"

DB = Sequel.postgres(test: false) # avoid creating a database connection
DB.extension :activerecord_connection # use ActiveRecord's database connection

DB.transaction(isolation: :serializable) do # handles isolation levels
  DB.after_commit { ... } # runs after transaction commits
  DB.transaction(savepoint: true) do # creates a savepoint
    DB.after_rollback { ... } # runs after savepoint rolls back
  end
  DB.in_transaction? #=> true
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)