On Monday, October 5, 2015 at 8:36:35 AM UTC-7, Ofer Nave wrote: > > I was reading this section of the docs: > > http://sequel.jeremyevans.net/rdoc/files/doc/testing_rdoc.html#label-Transactional+testing+with+savepoints > Which shows this example: > > class Minitest::HooksSpec > def around > Sequel::Model.db.transaction(:rollback=>:always, :savepoint=>true, > :auto_savepoint=>true){super} > end > > def around_all > Sequel::Model.db.transaction(:rollback=>:always){super} > end > end > > > So I jumped over to the RDoc for Sequel::Database#transaction to learn > about those options: > > > :auto_savepoint > > Automatically use a savepoint for Database#transaction calls inside this > transaction block. > > > :savepoint > > Whether to create a new savepoint for this transaction, only respected if > the database/adapter supports savepoints. By default Sequel will reuse > an existing transaction, so if you want to use a savepoint you must use > this option. If the surrounding transaction uses :auto_savepoint, you > can set this to false to not use a savepoint. > > > Based on my understand from the docs, shouldn't the Minitest example be: > > class Minitest::HooksSpec > def around > Sequel::Model.db.transaction(:rollback=>:always){super} > end > > def around_all > Sequel::Model.db.transaction(:rollback=>:always, > :auto_savepoint=>true){super} > end > end > > > The :auto_savepoint option in the around_all hook (which I believe wraps > the whole test suite) would ensure that the per-test transaction (in the > around hook) will use a savepoint, eliminating the need for > savepoint-related options there, right? >
In general, you want to use :auto_savepoint in around, so that transactions inside your code work correctly. Otherwise if you use DB.transaction in your code without the savepoint=>true option, it won't create a new transaction, it will reuse the existing transaction, which will complicate/break things if you try to rollback or handle errors in your code. You could use :auto_savepoint in both around_all and around, and not use the :savepoint option in around, that would be functionally equivalent. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
