rom-rb / rom

Data mapping and persistence toolkit for Ruby
https://rom-rb.org
MIT License
2.07k stars 161 forks source link

Using Repository#transaction with non-default gateways #620

Closed MasanoriOnishi closed 3 years ago

MasanoriOnishi commented 3 years ago

Describe the bug

I use the code following this, to realize multiple database connection.

ROM.container(
  default: [:sql, 'mysql2://localhost/common_tasks'], # gateway 1
  sub: [:sql, 'mysql2://localhost/each_tasks']           # gateway 2
) do |config|
    # setup code goes here...
end

And I want to use transaction for both gateway. However, due to the following code, I can use transaction only for default gateway. https://github.com/rom-rb/rom/blob/master/lib/rom/repository.rb#L151

To Reproduce

Expected behavior

We can use transaction for each gateway in the use of multiple database connection.

https://github.com/rom-rb/rom/blob/master/lib/rom/repository.rb#L151

I think I want to fix the code in the following way.

My environment

flash-gordon commented 3 years ago

This is not a bug. The Repository class doesn't have access to gateway. I think we could use keywords for providing a gateway and change its default value in Repository::Root. I'll send a PR today but it'll be a breaking change so 6.0 only. In the meantime, you can define your own method for using transactions, nothing really depends on the default implementation.

v-kolesnikov commented 3 years ago

In the same case I just redefined #transaction method in my root repository.