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

feat: Add extension for ActiveRecord transactions #18

Merged
merged 5 commits into from
Jul 31, 2024

Conversation

tiev
Copy link
Contributor

@tiev tiev commented Jul 22, 2024

Overview

The conventions of this gem have been adopted in many Rails applications using dry-monad/Do-notation. Therefore, it's useful to have an extension for ActiveRecord.

Details

This PR introduces a new extension to Dry::Operation that allows operations to be wrapped in ActiveRecord transactions. This is useful for ensuring atomicity of operations that involve multiple steps that need to be rolled back in case of failure. The extension provides a transaction method that can be used to wrap steps in a transaction. The transaction will be rolled back if any of the steps return a Dry::Monads::Result::Failure.

The extension also supports specifying a custom ActiveRecord class to initiate the transaction, which is useful when working with multiple databases.

@tiev tiev force-pushed the extension-ar branch 2 times, most recently from e57dcb2 to 6392cf3 Compare July 22, 2024 11:04
This commit introduces a new extension to Dry::Operation that allows
operations to be wrapped in ActiveRecord transactions. This is useful
for ensuring atomicity of operations that involve multiple steps that
need to be rolled back in case of failure. The extension provides a
`transaction` method that can be used to wrap steps in a transaction.
The transaction will be rolled back if any of the steps return a
`Dry::Monads::Result::Failure`.

The extension also supports specifying a custom ActiveRecord class to
initiate the transaction, which is useful when working with multiple
databases.
@tiev
Copy link
Contributor Author

tiev commented Jul 22, 2024

@waiting-for-dev Hello, I'm actively implementing operations in my apps and am eager to integrate this gem. This feature would be useful for me. Can you help review this PR?

@tiev tiev changed the title feat: Add extention for ActiveRecord transactions feat: Add extension for ActiveRecord transactions Jul 22, 2024
Copy link
Member

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

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

Thanks @tiev, this is looking great and I'm willing to add it to gem (I already had it in the roadmap). Leaving some comments/suggestions, and also pinging @timriley for his view on this one.

lib/dry/operation/extensions/active_record.rb Outdated Show resolved Hide resolved
lib/dry/operation/extensions/active_record.rb Outdated Show resolved Hide resolved
lib/dry/operation/extensions/active_record.rb Outdated Show resolved Hide resolved
spec/integration/extensions/active_record_spec.rb Outdated Show resolved Hide resolved
spec/integration/extensions/active_record_spec.rb Outdated Show resolved Hide resolved
spec/integration/extensions/active_record_spec.rb Outdated Show resolved Hide resolved
tiev added 2 commits July 26, 2024 17:24
- Fix information in doc.
- Improve tests to get rid of `let!` and more reliable way to call model
methods
- Prevent verbose messages of AR migrations. Cleanup db after tests
This commit enhances the ActiveRecord extension of the Dry::Operation
module to support additional options for the ActiveRecord transaction.
These options can be set as default for all transactions or overridden
at runtime. The changes include updates to the method signatures and
the addition of new tests to verify the functionality.
Copy link
Member

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this one, @tiev. Waiting for @timriley feedback before merging.

@timriley
Copy link
Member

@waiting-for-dev If you're happy with this, I'm happy with this 👍🏼

@timriley
Copy link
Member

Just noting the build is red; we should fix that before merging :)

The line exceeds 100 characters. Shorter param names are still good
enough.
@tiev
Copy link
Contributor Author

tiev commented Jul 31, 2024

Just noting the build is red; we should fix that before merging :)

I fixed it and ran bundle exec rake successfully on local.

@waiting-for-dev
Copy link
Member

Hey @tiev, still read :/

Move the yardoc comment block to the define_method block.
With the stand-alone `@!method` block, yard always generate warning of
unknown parameter name.
@tiev
Copy link
Contributor Author

tiev commented Jul 31, 2024

Hey @tiev, still read :/

yeah, yardoc warning concludes as failed. I've fixed it and ensured 0 exit code.

@waiting-for-dev waiting-for-dev merged commit d234c20 into dry-rb:main Jul 31, 2024
3 checks passed
@waiting-for-dev
Copy link
Member

Thanks for your brilliant work on this one, @tiev 🙌

@tiev tiev deleted the extension-ar branch July 31, 2024 08:55
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.

3 participants