bmabey/database_cleaner — Issue #80: New strategy idea
Author: joevandyk
Created: 2011-11-08 20:52:30+00:00
State: open

I'd like to be able to use transactions inside my tests, so that rules out the transaction strategy.

My general pattern when running tests:

1. Remove all the data from the database
1. Load some initial data using factories
1. Run tests, reverting back to the initial data after each test run

I would use the deletion or truncate pattern (fwiw, deletion is much faster than truncate for me with postgresql), but they don't revert the data to the initial state -- they erase everything. Which means I need to re-run the factories each time, which is really slow.

I'm not sure what the name of this pattern should be, but here's how it would work:

1. Remove all the data from the database
1. Load some initial data using factories
1. Save a dump of the database to a temp file (probably just the data, not the schema)
1. After each test run:
* Remove all the data from the database using the deletion strategy
* Using raw sql, load the data dump back into the database

This would let me use transactions in tests and quickly revert to a good initial state. It probably wouldn't be as fast as the transaction pattern, but I think that would be impossible to achieve, until postgresql lets us roll back to an earlier transaction.