Skip to content
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

Add extension for Sequel transactions #21

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

waiting-for-dev
Copy link
Member

Overview

We add a Dry::Operation::Extensions::Sequel module that, when included, gives access to a #transaction method. This method wraps the yielded steps in a Sequel transaction, rolling back in case one of them returns a failure.

The extension expects the including class to define a #db method giving access to the Sequel database definition:

class MyOperation < Dry::Operation
  include Dry::Operation::Extensions::Sequel

  attr_reader :db

  def initialize(db:)
    @db = db
  end

  def call(input)
    attrs = step validate(input)
    user = transaction do
      new_user = step persist(attrs)
      step assign_initial_role(new_user)
      new_user
    end
    step notify(user)
    user
  end

  # ...
end

Default options for the #transaction options (which delegates to Sequel transaction method) can be given both at include time with include Dry::Operation::Extensions::Sequel[isolation: :serializable], and at runtime with #transaction(isolation: :serializable).

Screenshots/Screencasts

Details

@waiting-for-dev
Copy link
Member Author

Waiting for #22

@waiting-for-dev waiting-for-dev marked this pull request as draft September 13, 2024 14:14
We add a `Dry::Operation::Extensions::Sequel` module that, when included, gives access to a `#transaction` method. This method wraps the yielded steps in a [Sequel](https://sequel.jeremyevans.net/) transaction, rolling back in case one of them returns a failure.

The extension expects the including class to define a `#db` method giving access to the Sequel database definition:

```ruby
class MyOperation < Dry::Operation
  include Dry::Operation::Extensions::Sequel

  attr_reader :db

  def initialize(db:)
    @db = db
  end

  def call(input)
    attrs = step validate(input)
    user = transaction do
      new_user = step persist(attrs)
      step assign_initial_role(new_user)
      new_user
    end
    step notify(user)
    user
  end

  # ...
end
```

Default options for the `#transaction` options (which delegates to Sequel [transaction method](https://sequel.jeremyevans.net/rdoc/files/doc/transactions_rdoc.html)) can be given both at include time with `include Dry::Operation::Extensions::Sequel[isolation: :serializable]`, and at runtime with `#transaction(isolation: :serializable)`.
@waiting-for-dev waiting-for-dev marked this pull request as ready for review September 13, 2024 15:07
@waiting-for-dev waiting-for-dev marked this pull request as draft September 17, 2024 15:51
@waiting-for-dev
Copy link
Member Author

Setting back to draft as we'll need to apply changes as in #24

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.

1 participant