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

Merged
merged 2 commits into from
Oct 27, 2024
Merged

Conversation

waiting-for-dev
Copy link
Member

@waiting-for-dev waiting-for-dev commented Sep 13, 2024

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).

As with the ActiveRecord extension, savepoints on nested transactions are not supported yet

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
@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

@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/sequel branch 2 times, most recently from 4eb7232 to 305c248 Compare October 27, 2024 04:28
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)`.

As with the ActiveRecord extension, savepoints on nested transactions
are not supported yet
@waiting-for-dev
Copy link
Member Author

Applying changes as in #25

@waiting-for-dev waiting-for-dev marked this pull request as ready for review October 27, 2024 04:30
Copy link
Member

@timriley timriley left a comment

Choose a reason for hiding this comment

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

Couple of minor things to check, but otherwise looks good!

@@ -156,7 +156,7 @@ def initialize(model)
def call
transaction do
step create_record
transaction(requires_new: true) do
Copy link
Member

@timriley timriley Oct 27, 2024

Choose a reason for hiding this comment

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

Was this change intended to be part of this PR? It's a test for AR, not Sequel.

Copy link
Member Author

Choose a reason for hiding this comment

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

My bad, not sure how but I confused AR and Sequel options in the tests

instance.()
end

xit "works with `requires_new` for nested transactions" do
Copy link
Member

Choose a reason for hiding this comment

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

Is requires_new also a Sequel thing? I recall seeing this skipped test from the AR extension... is it a copy/paste mistake?

Copy link
Member Author

Choose a reason for hiding this comment

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

Same... Sequel uses savepoint. Fixed. Thanks for spotting it.

@waiting-for-dev waiting-for-dev merged commit e57f4c8 into main Oct 27, 2024
6 checks passed
@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/sequel branch October 27, 2024 09:07
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