[rspec-users] How to modify database inside a transaction in before(:all)?

Rodrigo Rosenfeld Rosas rr.rosas at gmail.com
Mon Apr 23 22:39:05 UTC 2012


I have a set of examples that should run with a specific set of records 
in the database.

Since setting those records is an expensive operation I'd like to 
perform it just once per context.

For example:

context 'sample tree' do
     before(:all) { create_tree_records }

     example ...
end

The problem with this is that while before(:each) and the examples will 
run in a transaction that will be rolled back at the end of each 
example, that won't happen to the records created/modified by the 
before(:all) block.

It seems my database vendor (PostgreSQL) supports nested transactions 
(savepoints):

http://www.postgresql.org/about/

I should also notice that I'm using Sequel and it seems that 
transactions are reentrant in Sequel:

http://cheat.errtheblog.com/s/sequel/

Database#transaction is re-entrant:

   DB.transaction do # BEGIN issued only here
     DB.transaction
       dataset << {:first_name => 'Inigo', :last_name => 'Montoya'}
     end
end # COMMIT issued only here

So, this logic wouldn't work for me. I need a save point in a 
before(:all), so that I'd restore it on a before(:each).

Is there any recommendation to make this work in my specs?

Thanks in advance,
Rodrigo.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120423/2054867f/attachment-0001.html>


More information about the rspec-users mailing list