rom-rb / rom-rb.org

The official rom-rb website
https://rom-rb.org/
45 stars 108 forks source link

Feedback on ROM - Transactions #354

Open morgan121 opened 1 year ago

morgan121 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.