Skip to content

Conversation

@ryopeko
Copy link
Contributor

@ryopeko ryopeko commented Jun 23, 2014

This feature supports transaction using multi models.
Using multi models in a transaction...

class Book1 < ActiveRecord::Base
  use_switch_point :db1
end

class Book2 < ActiveRecord::Base
  use_switch_point :db1
end

transaction do
  Book1.with_writable do
    #insert, update, delete, etc...
  end

  Book2.with_writable do
    #insert, update, delete, etc...
  end
end

It is not "DRY".

In this case, Book1 and Book2 has a same db connection.
Therefore, we do not need that write twice a "with_writable".

When we write code like this, we must think that switch "readable, writable".

And, transaction must need a same connection.

Now, using this patch...

Book1.transaction_with(Book2) do
  #insert, update, delete, etc...
end

It is "DRY" and easier to understand the intention.

Also, user does not run of unintended query to DB.
(ex. User did not know Book2 using other DB.)

It will raise a exception in that case.

class Book3 < ActiveRecord::Base
  use_switch_point :db2
end

Book1.transaction_with(Book2, Book3) do
  #insert, update, delete, etc...
end

#=> raise RuntimeError

But, this method is declarative.
You can switch a connection in a block of transaction_with.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.12%) when pulling c2283be on ryopeko:feature/transaction_with into 68876d8 on eagletmt:master.

@eagletmt
Copy link
Owner

Looks good. Thank you 👍

eagletmt added a commit that referenced this pull request Jun 24, 2014
Add transaction_with for transaction multiple models related
@eagletmt eagletmt merged commit 634553c into eagletmt:master Jun 24, 2014
@eagletmt
Copy link
Owner

Released as v0.4.3 💎
https://rubygems.org/gems/switch_point/versions/0.4.3

@ryopeko
Copy link
Contributor Author

ryopeko commented Jun 24, 2014

🎊

@ryopeko ryopeko deleted the feature/transaction_with branch June 24, 2014 14:19
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