DatabaseCleaner / database_cleaner-active_record

Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing.
MIT License
64 stars 63 forks source link

Problem with restarting a transaction when using transaction strategy #28

Open kenips opened 11 years ago

kenips commented 11 years ago

I have a method like this:

def make_foo(bar, params)
  foo = nil 
  self.transaction do
    foo = self.create!(name: bar) rescue nil
  end
  unless foo
    self.transaction do
      foo = Something.find_by(name: bar).update(params) 
    end
  end
  foo
end

This method relies on a unique index on name to raise PG:Error, in which it will update the object instead. This is all fine until I use transaction in database_cleaner and it complains about

I tried using truncation for the test, and it somewhat works, but I'm just wondering if this is something that can be supported (sounds like nested transaction?)? Essentially I'd like to get the transaction to restart within the test, if that's possible.

zhenyuchen commented 11 years ago

+1