[LRUG] Handling opening and closing of transactions manually in ActiveRecord

Murray Steele murray.steele at gmail.com
Fri Oct 8 04:20:23 PDT 2010


As penance, I dug a little.  It looks like you'd need to spin up a
block-less version of
active_record/connection_adapters/abstract/database_statements.rb
#transaction (rails 2.3.x).  Could be painful because that method 'aint
simple.

However.  I wonder.  If you're on 1.9 could something cunning be done with
Fibers being passed into the block form of transaction ?

However(again) might it be easier to just:

Before(:all) do
  # create whatever
end

After(:all) do
  # destroy whatever
  # or
  # use database_cleaner to truncate the DB
end

?
On 8 October 2010 12:07, Murray Steele <murray.steele at gmail.com> wrote:

> Ecch.  Actually reading the linked-to article reveals what you're trying to
> do.  My comment is pointless.  Ignore me.  I wasn't here.
>
>
> On 8 October 2010 12:03, Murray Steele <murray.steele at gmail.com> wrote:
>
>> I thought that RSpec (via rspec-rails) respected the
>> use_transactional_fixtures (e.g. wrap all the specs in a transaction) stuff
>> in ActiveSupport::TestCase?  Am I wrong?
>>
>>
>> On 8 October 2010 11:40, Matthew Willson <matthew at playlouder.com> wrote:
>>
>>> Can't speak for datamapper, or for RSpec infact, but here's a little test
>>> mixin we use with Sequel and test/spec in case a similar approach works.
>>>
>>> module TransactionalTestCase
>>>  def run(*args, &block)
>>>    database.transaction do
>>>      super
>>>      raise Sequel::Error::Rollback
>>>    end
>>>  end
>>> end
>>>
>>> On 8 Oct 2010, at 11:32, Tim Cowlishaw wrote:
>>>
>>> > Hey all,
>>> >
>>> > I stumbled across Xavier Shay's article on setting up RSpec to use
>>> > transactional before(:all) blocks with datamapper the other day
>>> > (
>>> http://rhnh.net/2010/10/06/transactional-before-all-with-rspec-and-datamapper
>>> ),
>>> > which is convenient as it's something I've been meaning to do for ages
>>> > on one of our codebases that has a rather large test suite with lots
>>> > of setup in before(:each) blocks. Therefore, I thought I'd spend a bit
>>> > of time trying to get his solution working with ActiveRecord, rather
>>> > than Datamapper. However, because ActiveRecord's transaction interface
>>> > requires you to pass a block to the transaction method, containing the
>>> > work to be done within the transaction, rather than offering
>>> > transaction.begin and transaction.rollback methods a la datamapper,
>>> > this is a little trickier than expected. There is
>>> > ActiveRecord::Base.connection.begin_db_transaction, and
>>> > ActiveRecord::Base.rollback_db_transaction, but these don't handle
>>> > emulating nesting transactions with savepoints as
>>> > ActiveRecord::Base.transaction does, meaning I'd have to reimplement
>>> > that seperately - not an insurmountable task, but a sufficiently
>>> > complex bit of work to make me nervous. Therefore, has anyone run into
>>> > a similar need (to make before(:all) blocks transactional in rspec)
>>> > before? if so, how did you manage it?
>>> >
>>> > Cheers,
>>> >
>>> > Tim
>>> > _______________________________________________
>>> > Chat mailing list
>>> > Chat at lists.lrug.org
>>> > http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>
>>> --
>>> Matthew Willson
>>> Development Lead, MSP / Playlouder
>>>
>>> _______________________________________________
>>> Chat mailing list
>>> Chat at lists.lrug.org
>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20101008/93118387/attachment.html>


More information about the Chat mailing list