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

Feedback on ROM - Transactions #354

Open
morgan121 opened this issue Jun 22, 2023 · 3 comments
Open

Feedback on ROM - Transactions #354

morgan121 opened this issue Jun 22, 2023 · 3 comments

Comments

@morgan121
Copy link

Could you expand on this example? E.g. if a second table is modified within the user transaction, will that also get rolled back if there is an error with the user.create? what happens if there is an error with the other table operation, will that also fail the whole transaction?

For example, something like below, where we only want to create the user role if the user is successfully created:

user.transaction do
  user.create(...)
  user_role.create(...)
end
@morgan121
Copy link
Author

Also, does the transaction have to be for a single table? in active record there was an ActiveRecord::Base.transaction that could be used instead of a specific table. Is there an equivalent concept here?

@morgan121
Copy link
Author

Turns out you can just use transaction not for a specific table - so that solves most of my problems.

Would be nice to have in the docs though

@flash-gordon
Copy link
Member

Transactions is a database concept here, transaction do is just an API for invoking BEGIN + COMMIT/ROLLBACK + a guarantee the same connection is used for all operations within the block. There's no way to distinguish between tables within a transaction in SQL. It works the same way in active-record, rom, sequel, whathaveyou. Yes, you can call User.transaction do or user_repo.transaction do but it'll end up with the same set of instructions for the database. The only caveat will be if there's more than one database in your app, only then should you worry.

I think, we could have a reference to a guide on transactions in general in the docs but I don't think we need to expand on the details of what transactions are there.

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

No branches or pull requests

2 participants